Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Felt serde #883

Merged
merged 10 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ jobs:
cd ensure-no_std
cargo build

- name: Run cargo build for all workspace
run: |
cargo build --workspace

- name: Run cargo build ensure-no_std crate for wasm
run: |
cd ensure-no_std
Expand Down
2 changes: 1 addition & 1 deletion examples/merkle-tree-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ path = "src/main.rs"

[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
lambdaworks-crypto = { workspace = true }
lambdaworks-crypto = { workspace = true, features = ["serde"]}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came up when building all other examples. The crate relies on serde and it fails to compile if it's not added.

lambdaworks-math = { workspace = true, features = ["lambdaworks-serde-string"] }
serde = { version = "1.0" }
serde_json = "1"
Expand Down
9 changes: 5 additions & 4 deletions math/src/field/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl<F: IsPrimeField> Serialize for FieldElement<F> {
where
S: Serializer,
{
use crate::alloc::string::ToString;
let mut state = serializer.serialize_struct("FieldElement", 1)?;
state.serialize_field("value", &F::representative(self.value()).to_string())?;
state.end()
Expand Down Expand Up @@ -653,7 +654,7 @@ impl<'de, F: IsPrimeField> Deserialize<'de> for FieldElement<F> {
where
M: MapAccess<'de>,
{
let mut value = None;
let mut value: Option<&str> = None;
while let Some(key) = map.next_key()? {
match key {
Field::Value => {
Expand All @@ -665,22 +666,22 @@ impl<'de, F: IsPrimeField> Deserialize<'de> for FieldElement<F> {
}
}
let value = value.ok_or_else(|| de::Error::missing_field("value"))?;
Ok(FieldElement::from_hex(&value).unwrap())
FieldElement::from_hex(&value).map_err(|_| de::Error::custom("invalid hex"))
}

fn visit_seq<S>(self, mut seq: S) -> Result<FieldElement<F>, S::Error>
where
S: SeqAccess<'de>,
{
let mut value = None;
let mut value: Option<&str> = None;
while let Some(val) = seq.next_element()? {
if value.is_some() {
return Err(de::Error::duplicate_field("value"));
}
value = Some(val);
}
let value = value.ok_or_else(|| de::Error::missing_field("value"))?;
Ok(FieldElement::from_hex(&value).unwrap())
FieldElement::from_hex(&value).map_err(|_| de::Error::custom("invalid hex"))
}
}

Expand Down
Loading