-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic serde support for Scalar, G1, G2 with human readable encoding
- Loading branch information
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use group::Curve; | ||
use serde::de::Error; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::g1::{G1Affine, G1Projective}; | ||
use crate::g2::{G2Affine, G2Projective}; | ||
use crate::scalar::Scalar; | ||
|
||
impl Serialize for Scalar { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serdect::array::serialize_hex_lower_or_bin(&self.to_bytes(), s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for Scalar { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'d>, | ||
{ | ||
let mut byte_array = [0; 32]; | ||
|
||
serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; | ||
|
||
Option::from(Scalar::from_bytes(&byte_array)) | ||
.ok_or(D::Error::custom("Could not decode scalar")) | ||
} | ||
} | ||
|
||
impl Serialize for G1Affine { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serdect::array::serialize_hex_lower_or_bin(&self.to_compressed(), s) | ||
|
||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G1Affine { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'d>, | ||
{ | ||
let mut byte_array = [0; 48]; | ||
|
||
serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; | ||
|
||
Option::from(G1Affine::from_compressed(&byte_array)).ok_or(D::Error::custom( | ||
"Could not decode compressed group element", | ||
)) | ||
} | ||
} | ||
|
||
impl Serialize for G2Affine { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serdect::array::serialize_hex_lower_or_bin(&self.to_compressed(), s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G2Affine { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'d>, | ||
{ | ||
let mut byte_array = [0; 96]; | ||
|
||
serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; | ||
|
||
Option::from(G2Affine::from_compressed(&byte_array)).ok_or(D::Error::custom( | ||
"Could not decode compressed group element", | ||
)) | ||
} | ||
} | ||
|
||
impl Serialize for G1Projective { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
self.to_affine().serialize(s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G1Projective { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'d>, | ||
{ | ||
Ok(G1Affine::deserialize(d)?.into()) | ||
} | ||
} | ||
|
||
impl Serialize for G2Projective { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
self.to_affine().serialize(s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G2Projective { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'d>, | ||
{ | ||
Ok(G2Affine::deserialize(d)?.into()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn serde_json_scalar_roundtrip() { | ||
let serialized = serde_json::to_string(&Scalar::zero()).unwrap(); | ||
|
||
assert_eq!( | ||
serialized, | ||
"\"0000000000000000000000000000000000000000000000000000000000000000\"" | ||
); | ||
|
||
let deserialized: Scalar = serde_json::from_str(&serialized).unwrap(); | ||
|
||
assert_eq!(deserialized, Scalar::zero()); | ||
} | ||
|
||
#[test] | ||
fn serde_json_g1_roundtrip() { | ||
let serialized = serde_json::to_string(&G1Affine::generator()).unwrap(); | ||
|
||
assert_eq!( | ||
serialized, | ||
"\"97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb\"" | ||
); | ||
|
||
let deserialized: G1Affine = serde_json::from_str(&serialized).unwrap(); | ||
|
||
assert_eq!(deserialized, G1Affine::generator()); | ||
} | ||
|
||
#[test] | ||
fn serde_json_g2_roundtrip() { | ||
let serialized = serde_json::to_string(&G2Affine::generator()).unwrap(); | ||
|
||
assert_eq!( | ||
serialized, | ||
"\"93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8\"" | ||
); | ||
|
||
let deserialized: G2Affine = serde_json::from_str(&serialized).unwrap(); | ||
|
||
assert_eq!(deserialized, G2Affine::generator()); | ||
} |