forked from zkcrypto/jubjub
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 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
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,89 @@ | ||
extern crate alloc; | ||
|
||
use alloc::string::String; | ||
|
||
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::{AffinePoint, ExtendedPoint}; | ||
|
||
impl Serialize for AffinePoint { | ||
fn serialize<S: Serializer>( | ||
&self, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
let s = hex::encode(self.to_bytes()); | ||
serializer.serialize_str(&s) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for AffinePoint { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let bytes: [u8; 32] = hex::decode(s) | ||
.map_err(Error::custom)? | ||
.try_into() | ||
.map_err(|_| { | ||
Error::custom( | ||
"Failed to deserialize AffinePoint: invalid byte length", | ||
) | ||
})?; | ||
AffinePoint::from_bytes(bytes) | ||
.into_option() | ||
.ok_or(Error::custom( | ||
"Failed to deserialize AffinePoint: invalid AffinePoint", | ||
)) | ||
} | ||
} | ||
|
||
impl Serialize for ExtendedPoint { | ||
fn serialize<S: Serializer>( | ||
&self, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
AffinePoint::from(self).serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for ExtendedPoint { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
AffinePoint::deserialize(deserializer).map(Into::into) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ff::Field; | ||
use group::Group; | ||
|
||
use crate::{AffinePoint, ExtendedPoint, Fr}; | ||
|
||
#[test] | ||
fn affine_point() { | ||
let point = AffinePoint::identity(); | ||
let ser = serde_json::to_string(&point).unwrap(); | ||
let deser = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(point, deser); | ||
} | ||
|
||
#[test] | ||
fn extended_point() { | ||
let mut rng = rand_core::OsRng; | ||
let point = ExtendedPoint::random(&mut rng); | ||
let ser = serde_json::to_string(&point).unwrap(); | ||
let deser = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(point, deser); | ||
} | ||
|
||
#[test] | ||
fn fr() { | ||
let mut rng = rand_core::OsRng; | ||
let fr = Fr::random(&mut rng); | ||
let ser = serde_json::to_string(&fr).unwrap(); | ||
let deser = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(fr, deser); | ||
} | ||
} |
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