Skip to content

Commit

Permalink
fix: Felt serde (#883)
Browse files Browse the repository at this point in the history
* fix: Element serde

* chore: Revert cargo toml changes

* fix: Use &str

* feat: Build all examples on compile job

* Fix job

* Fix job

* Change job to build worksapce

---------

Co-authored-by: Diego K <[email protected]>
  • Loading branch information
igamigo and diegokingston authored Aug 7, 2024
1 parent b2931c3 commit e09040f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
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"]}
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

0 comments on commit e09040f

Please sign in to comment.