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

add basic serde support for Scalar, G1, G2 with human readable encoding #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hex = "0.4"
rand_xorshift = "0.3"
sha2 = "0.9"
sha3 = "0.9"
serde_json = "1.0.114"

[[bench]]
name = "groups"
Expand Down Expand Up @@ -63,6 +64,15 @@ version = "1.4"
default-features = false
optional = true

[dependencies.serde]
version = "1.0.197"
default-features = false
optional = true

[dependencies.serdect]
version = "0.2.0"
Comment on lines +72 to +73
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One important caveat on serdect is we revised the format in the v0.3.0-pre.0 prereleases, using serialize_bytes/deserialize_bytes which unfortunately adds a length header in the binary encodings even when using fixed-sized arrays but provides more compact serialization on formats like MessagePack which are more likely to be constant-time-ish, which is an unfortunate tradeoff where we opted for broader constant time assuredness over a minimally compact encoding.

Barring any complaints about the length header, we just need to fix a bug/regression on v0.3.0 in the slice encoder and we can ship a final release. I would probably suggest using that (or convincing us to roll back) to avoid future breakages in the encoding format.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would you expect to ship v0.3.0?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regression needs to be fixed first: RustCrypto/formats#1322

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this regression has already been fixed (and relevant serdect release published), is there a chance to get this PR moving again?

optional = true

[features]
default = ["groups", "pairings", "alloc", "bits"]
bits = ["ff/bits"]
Expand All @@ -71,3 +81,4 @@ pairings = ["groups", "pairing"]
alloc = ["group/alloc"]
experimental = ["digest"]
nightly = ["subtle/nightly"]
serde = ["dep:serde", "serdect"]
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ pub(crate) use digest::generic_array;

#[cfg(feature = "experimental")]
pub mod hash_to_curve;

#[cfg(feature = "serde")]
mod serde;
124 changes: 124 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
serdect::array::serialize_hex_lower_or_bin(&self.to_bytes(), s)
}
}

impl<'d> Deserialize<'d> for Scalar {
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
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_else(|| D::Error::custom("Could not decode scalar"))
}
}

impl Serialize for G1Affine {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
serdect::array::serialize_hex_lower_or_bin(&self.to_compressed(), s)
}
}

impl<'d> Deserialize<'d> for G1Affine {
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
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_else(|| D::Error::custom("Could not decode compressed group element"))
}
}

impl Serialize for G2Affine {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
serdect::array::serialize_hex_lower_or_bin(&self.to_compressed(), s)
}
}

impl<'d> Deserialize<'d> for G2Affine {
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
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_else(|| D::Error::custom("Could not decode compressed group element"))
}
}

impl Serialize for G1Projective {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
self.to_affine().serialize(s)
}
}

impl<'d> Deserialize<'d> for G1Projective {
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
Ok(G1Affine::deserialize(d)?.into())
}
}

impl Serialize for G2Projective {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
self.to_affine().serialize(s)
}
}

impl<'d> Deserialize<'d> for G2Projective {
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
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());
}