Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: (De)Serialization of points using GroupEncoding #88

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ criterion = { version = "0.3", features = ["html_reports"] }
rand_xorshift = "0.3"
ark-std = { version = "0.3" }
bincode = "1.3.3"
serde_json = "1.0.105"

[dependencies]
subtle = "2.4"
Expand All @@ -30,14 +31,15 @@ num-traits = "0.2"
paste = "1.0.11"
serde = { version = "1.0", default-features = false, optional = true }
serde_arrays = { version = "0.1.0", optional = true }
hex = { version = "0.4", optional = true, default-features = false, features = ["alloc", "serde"] }
blake2b_simd = "1"

[features]
default = ["reexport", "bits"]
asm = []
bits = ["ff/bits"]
bn256-table = []
derive_serde = ["serde/derive", "serde_arrays"]
derive_serde = ["serde/derive", "serde_arrays", "hex"]
prefetch = []
print-trace = ["ark-std/print-trace"]
reexport = []
Expand Down
70 changes: 67 additions & 3 deletions src/derive/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,86 @@ macro_rules! new_curve_impl {

}

/// A macro to help define point serialization using the [`group::GroupEncoding`] trait
/// This assumes both point types ($name, $nameaffine) implement [`group::GroupEncoding`].
#[cfg(feature = "derive_serde")]
macro_rules! serialize_deserialize_to_from_bytes {
() => {
impl ::serde::Serialize for $name {
fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let bytes = &self.to_bytes();
if serializer.is_human_readable() {
::hex::serde::serialize(&bytes.0, serializer)
} else {
::serde_arrays::serialize(&bytes.0, serializer)
}
}
}

paste::paste! {
use ::serde::de::Error as _;
impl<'de> ::serde::Deserialize<'de> for $name {
fn deserialize<D: ::serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let bytes = if deserializer.is_human_readable() {
::hex::serde::deserialize(deserializer)?
} else {
::serde_arrays::deserialize::<_, u8, [< $name _COMPRESSED_SIZE >]>(deserializer)?
};
Option::from(Self::from_bytes(&[< $name Compressed >](bytes))).ok_or_else(|| {
D::Error::custom("deserialized bytes don't encode a valid field element")
})
}
}
}

impl ::serde::Serialize for $name_affine {
fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let bytes = &self.to_bytes();
if serializer.is_human_readable() {
::hex::serde::serialize(&bytes.0, serializer)
} else {
::serde_arrays::serialize(&bytes.0, serializer)
}
}
}

paste::paste! {
use ::serde::de::Error as _;
impl<'de> ::serde::Deserialize<'de> for $name_affine {
fn deserialize<D: ::serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let bytes = if deserializer.is_human_readable() {
::hex::serde::deserialize(deserializer)?
} else {
::serde_arrays::deserialize::<_, u8, [< $name _COMPRESSED_SIZE >]>(deserializer)?
};
Option::from(Self::from_bytes(&[< $name Compressed >](bytes))).ok_or_else(|| {
D::Error::custom("deserialized bytes don't encode a valid field element")
})
}
}
}
};
}

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "derive_serde", derive(Serialize, Deserialize))]
$($privacy)* struct $name {
pub x: $base,
pub y: $base,
pub z: $base,
}

#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "derive_serde", derive(Serialize, Deserialize))]
$($privacy)* struct $name_affine {
pub x: $base,
pub y: $base,
}


#[cfg(feature = "derive_serde")]
serialize_deserialize_to_from_bytes!();

impl_compressed!();
impl_uncompressed!();
Expand Down
12 changes: 12 additions & 0 deletions src/tests/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ where
assert_eq!(projective_point.to_affine(), affine_point_rec);
assert_eq!(affine_point, affine_point_rec);
}
{
let affine_json = serde_json::to_string(&affine_point).unwrap();
let reader = std::io::Cursor::new(affine_json);
let affine_point_rec: G::AffineExt = serde_json::from_reader(reader).unwrap();
assert_eq!(affine_point, affine_point_rec);
}
{
let projective_bytes = bincode::serialize(&projective_point).unwrap();
let reader = std::io::Cursor::new(projective_bytes);
let projective_point_rec: G = bincode::deserialize_from(reader).unwrap();
assert_eq!(projective_point, projective_point_rec);
}
{
let projective_json = serde_json::to_string(&projective_point).unwrap();
let reader = std::io::Cursor::new(projective_json);
let projective_point_rec: G = serde_json::from_reader(reader).unwrap();
assert_eq!(projective_point, projective_point_rec);
}
}
}

Expand Down
Loading