Skip to content

Commit

Permalink
Fix scalar deserialization issue (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
survived authored Sep 5, 2022
1 parent d733f37 commit 665cc11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ features = ["ecdsa", "ecdsa-core"]

[dev-dependencies]
serde_test = "1.0"
serde_json = "1"
paste = "1.0.2"
proptest = "0.10"
proptest-derive = "0.2"
Expand Down
25 changes: 22 additions & 3 deletions src/elliptic/curves/wrappers/serde_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,15 @@ impl<'de, E: Curve> Deserialize<'de> for ScalarFromBytes<E> {
}
}

deserializer
.deserialize_bytes(ScalarBytesVisitor(PhantomData))
.map(ScalarFromBytes)
if !deserializer.is_human_readable() {
deserializer
.deserialize_bytes(ScalarBytesVisitor(PhantomData))
.map(ScalarFromBytes)
} else {
deserializer
.deserialize_str(ScalarBytesVisitor(PhantomData))
.map(ScalarFromBytes)
}
}
}

Expand Down Expand Up @@ -640,4 +646,17 @@ mod serde_tests {
),
)
}

test_for_all_curves!(supports_serde_json);
fn supports_serde_json<E: Curve>() {
let random_scalar = Scalar::<E>::random();
let scalar_json = serde_json::to_string(&random_scalar).unwrap();
let deserialized_scalar = serde_json::from_str(&scalar_json).unwrap();
assert_eq!(random_scalar, deserialized_scalar);

let random_point = Point::generator() * random_scalar;
let point_json = serde_json::to_string(&random_point).unwrap();
let deserialized_point: Point<E> = serde_json::from_str(&point_json).unwrap();
assert_eq!(random_point, deserialized_point);
}
}

0 comments on commit 665cc11

Please sign in to comment.