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.
In addition: - Added `ConstantTimeEq`, `PartialEq`, and `dusk_bytes::Serializable` impls for `AffineNielsPoint` and `ExtendedNielsPoint`. Added `From<ExtendedPoint>` impl for `SubgroupPoint` - Added serde feature tests to CI - Added tests for `Serializable` impls of `ExtendedNielsPoint` and `AffineNielsPoint`
- Loading branch information
Showing
6 changed files
with
371 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,167 @@ | ||
extern crate alloc; | ||
|
||
use alloc::format; | ||
use alloc::string::String; | ||
|
||
use dusk_bytes::Serializable; | ||
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::{ | ||
AffineNielsPoint, AffinePoint, ExtendedNielsPoint, ExtendedPoint, Fr, | ||
SubgroupPoint, | ||
}; | ||
|
||
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)?; | ||
match hex::decode(s) { | ||
Ok(bytes) => { | ||
let bytes: [u8; 32] = bytes.try_into().map_err(|_| Error::custom(format!("Failed to deserialize AffinePoint: invalid byte length")))?; | ||
AffinePoint::from_bytes(bytes).into_option().ok_or( | ||
Error::custom(format!( | ||
"Failed to deserialize AffinePoint: invalid AffinePoint" | ||
)), | ||
) | ||
} | ||
Err(e) => Err(Error::custom(format!( | ||
"Failed to deserialize AffinePoint with error: {e}" | ||
))), | ||
} | ||
} | ||
} | ||
|
||
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(|point| point.into()) | ||
} | ||
} | ||
|
||
impl Serialize for Fr { | ||
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 Fr { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
match hex::decode(s) { | ||
Ok(bytes) => { | ||
let bytes: [u8; 32] = bytes.try_into().map_err(|_| { | ||
Error::custom(format!( | ||
"Failed to deserialize Fr: invalid byte length" | ||
)) | ||
})?; | ||
Fr::from_bytes(&bytes).into_option().ok_or(Error::custom( | ||
format!("Failed to deserialize Fr: invalid Fr"), | ||
)) | ||
} | ||
Err(e) => Err(Error::custom(format!( | ||
"Failed to deserialize Fr with error: {e}" | ||
))), | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for AffineNielsPoint { | ||
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 AffineNielsPoint { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
match hex::decode(s) { | ||
Ok(bytes) => { | ||
let bytes: [u8; AffineNielsPoint::SIZE] = bytes.try_into().map_err(|_| Error::custom(format!("Failed to deserialize AffineNielsPoint: invalid byte length")))?; | ||
AffineNielsPoint::from_bytes(&bytes) | ||
.map_err(|_| Error::custom(format!("Failed to deserialize AffineNielsPoint: invalid AffineNielsPoint"))) | ||
} | ||
Err(e) => Err(Error::custom(format!( | ||
"Failed to deserialize AffineNielsPoint with error: {e}" | ||
))), | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for ExtendedNielsPoint { | ||
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 ExtendedNielsPoint { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
match hex::decode(s) { | ||
Ok(bytes) => { | ||
let bytes: [u8; 128] = bytes.try_into().map_err(|_| Error::custom(format!("Failed to deserialize ExtendedNielsPoint: invalid byte length")))?; | ||
ExtendedNielsPoint::from_bytes(&bytes) | ||
.map_err(|_| Error::custom(format!("Failed to deserialize ExtendedNielsPoint: invalid ExtendedNielsPoint"))) | ||
} | ||
Err(e) => Err(Error::custom(format!( | ||
"Failed to deserialize ExtendedNielsPoint with error: {e}" | ||
))), | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for SubgroupPoint { | ||
fn serialize<S: Serializer>( | ||
&self, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
self.0.serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for SubgroupPoint { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
ExtendedPoint::deserialize(deserializer) | ||
.map(|point| SubgroupPoint::from(point)) | ||
} | ||
} |
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,20 @@ | ||
use dusk_bytes::Serializable; | ||
use dusk_jubjub::{AffineNielsPoint, ExtendedNielsPoint}; | ||
|
||
#[test] | ||
fn affine_niels_point_encoding() { | ||
let point = AffineNielsPoint::identity(); | ||
assert_eq!( | ||
point, | ||
AffineNielsPoint::from_bytes(&point.to_bytes()).unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
fn extended_niels_point_encoding() { | ||
let point = ExtendedNielsPoint::identity(); | ||
assert_eq!( | ||
point, | ||
ExtendedNielsPoint::from_bytes(&point.to_bytes()).unwrap() | ||
); | ||
} |
Oops, something went wrong.