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

Basic serde support for Scalar, G1 and G2 (Fix #57) #61

Open
wants to merge 6 commits 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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ optional = true
[dependencies.ff]
version = "0.12"
default-features = false
features = ["derive"]


[dependencies.group]
version = "0.12"
Expand All @@ -54,7 +56,7 @@ version = "0.6"
default-features = false

[dependencies.subtle]
version = "2.2.1"
version = "2.4.1"
default-features = false

[dependencies.zeroize]
Expand Down
115 changes: 115 additions & 0 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,89 @@ impl UncompressedEncoding for G1Affine {
}
}

#[cfg(feature = "serde")]
mod serde_support {
use super::{fmt, G1Affine, G1Projective};

use serde::de::Visitor;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};

impl Serialize for G1Affine {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeTuple;
let mut tup = serializer.serialize_tuple(48)?;
for byte in self.to_compressed().iter() {
tup.serialize_element(byte)?;
}
tup.end()
}
}

impl<'de> Deserialize<'de> for G1Affine {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct G1AffineVisitor;

impl<'de> Visitor<'de> for G1AffineVisitor {
type Value = G1Affine;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 48-byte compressed canonical bls12_381 G1 point")
}

fn visit_seq<A>(self, mut seq: A) -> Result<G1Affine, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = [0u8; 48];
for i in 0..48 {
bytes[i] = seq.next_element()?.ok_or_else(|| {
serde::de::Error::invalid_length(i, &"expected 48 bytes")
})?;
}

let res = G1Affine::from_compressed(&bytes);
if res.is_some().into() {
Ok(res.unwrap())
} else {
Err(serde::de::Error::custom(
&"G1 point was not canonically encoded",
))
}
}
}

deserializer.deserialize_tuple(48, G1AffineVisitor)
}
}

impl Serialize for G1Projective {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
G1Affine::from(*self).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for G1Projective {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
G1Affine::deserialize(deserializer).map(G1Projective::from)
}
}
}

#[cfg(feature = "serde")]
pub use self::serde_support::*;

#[test]
fn test_beta() {
assert_eq!(
Expand Down Expand Up @@ -1706,6 +1789,38 @@ fn test_batch_normalize() {
}
}

#[test]
#[cfg(feature = "serde")]
fn test_affine_serde_serialization() {
use serde_test::{assert_tokens, Token};

let g = G1Affine::generator();
let raw_bytes = g.to_compressed();

let expected_tokens = std::iter::once(Token::Tuple { len: 48 })
.chain(raw_bytes.iter().map(|&b| Token::U8(b)))
.chain(std::iter::once(Token::TupleEnd))
.collect::<alloc::vec::Vec<_>>();

assert_tokens(&g, &expected_tokens);
}

#[test]
#[cfg(feature = "serde")]
fn test_projective_serde_serialization() {
use serde_test::{assert_tokens, Token};

let g = G1Projective::generator();
let raw_bytes = G1Affine::from(g).to_compressed();

let expected_tokens = std::iter::once(Token::Tuple { len: 48 })
.chain(raw_bytes.iter().map(|&b| Token::U8(b)))
.chain(std::iter::once(Token::TupleEnd))
.collect::<alloc::vec::Vec<_>>();

assert_tokens(&g, &expected_tokens);
}

#[cfg(feature = "zeroize")]
#[test]
fn test_zeroize() {
Expand Down
116 changes: 116 additions & 0 deletions src/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,90 @@ impl UncompressedEncoding for G2Affine {
}
}

#[cfg(feature = "serde")]
mod serde_support {
use super::{fmt, G2Affine, G2Projective};

use serde::de::Visitor;

use serde::{self, Deserialize, Deserializer, Serialize, Serializer};

impl Serialize for G2Affine {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeTuple;
let mut tup = serializer.serialize_tuple(96)?;
for byte in self.to_compressed().iter() {
tup.serialize_element(byte)?;
}
tup.end()
}
}

impl<'de> Deserialize<'de> for G2Affine {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct G2AffineVisitor;

impl<'de> Visitor<'de> for G2AffineVisitor {
type Value = G2Affine;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 96-byte compressed canonical bls12_381 G2 point")
}

fn visit_seq<A>(self, mut seq: A) -> Result<G2Affine, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = [0u8; 96];
for i in 0..96 {
bytes[i] = seq.next_element()?.ok_or_else(|| {
serde::de::Error::invalid_length(i, &"expected 96 bytes")
})?;
}

let res = G2Affine::from_compressed(&bytes);
if res.is_some().into() {
Ok(res.unwrap())
} else {
Err(serde::de::Error::custom(
&"G2 point was not canonically encoded",
))
}
}
}

deserializer.deserialize_tuple(96, G2AffineVisitor)
}
}

impl Serialize for G2Projective {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
G2Affine::from(*self).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for G2Projective {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
G2Affine::deserialize(deserializer).map(G2Projective::from)
}
}
}

#[cfg(feature = "serde")]
pub use self::serde_support::*;

#[test]
fn test_is_on_curve() {
assert!(bool::from(G2Affine::identity().is_on_curve()));
Expand Down Expand Up @@ -2109,6 +2193,38 @@ fn test_batch_normalize() {
}
}

#[test]
#[cfg(feature = "serde")]
fn test_affine_serde_serialization() {
use serde_test::{assert_tokens, Token};

let g = G2Affine::generator();
let raw_compressed_bytes = g.to_compressed();

let expected_tokens = std::iter::once(Token::Tuple { len: 96 })
.chain(raw_compressed_bytes.iter().map(|&b| Token::U8(b)))
.chain(std::iter::once(Token::TupleEnd))
.collect::<alloc::vec::Vec<_>>();

assert_tokens(&g, &expected_tokens);
}

#[test]
#[cfg(feature = "serde")]
fn test_projective_serde_serialization() {
use serde_test::{assert_tokens, Token};

let g = G2Projective::generator();
let raw_compressed_bytes = G2Affine::from(g).to_compressed();

let expected_tokens = std::iter::once(Token::Tuple { len: 96 })
.chain(raw_compressed_bytes.iter().map(|&b| Token::U8(b)))
.chain(std::iter::once(Token::TupleEnd))
.collect::<alloc::vec::Vec<_>>();

assert_tokens(&g, &expected_tokens);
}

#[cfg(feature = "zeroize")]
#[test]
fn test_zeroize() {
Expand Down
80 changes: 80 additions & 0 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,70 @@ where
}
}

#[cfg(feature = "serde")]
mod serde_support {
use super::{fmt, Scalar};

use serde::de::Visitor;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
impl Serialize for Scalar {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeTuple;
let mut tup = serializer.serialize_tuple(32)?;
for byte in self.to_bytes().iter() {
tup.serialize_element(byte)?;
}
tup.end()
}
}

impl<'de> Deserialize<'de> for Scalar {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ScalarVisitor;

impl<'de> Visitor<'de> for ScalarVisitor {
type Value = Scalar;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 32-byte canonical bls12_381 scalar")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Scalar, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = [0u8; 32];
for i in 0..32 {
bytes[i] = seq.next_element()?.ok_or_else(|| {
serde::de::Error::invalid_length(i, &"expected 32 bytes")
})?;
}

let res = Scalar::from_bytes(&bytes);
if res.is_some().into() {
Ok(res.unwrap())
} else {
Err(serde::de::Error::custom(
&"scalar was not canonically encoded",
))
}
}
}

deserializer.deserialize_tuple(32, ScalarVisitor)
}
}
}

#[cfg(feature = "serde")]
pub use self::serde_support::*;

#[test]
fn test_inv() {
// Compute -(q^{-1} mod 2^64) mod 2^64 by exponentiating
Expand Down Expand Up @@ -1231,6 +1295,22 @@ fn test_double() {
assert_eq!(a.double(), a + a);
}

#[test]
#[cfg(feature = "serde")]
fn test_serde_serialization() {
use serde_test::{assert_tokens, Token};

let s = R;
let raw_bytes = s.to_bytes();

let expected_tokens = std::iter::once(Token::Tuple { len: 32 })
.chain(raw_bytes.iter().map(|&b| Token::U8(b)))
.chain(std::iter::once(Token::TupleEnd))
.collect::<alloc::vec::Vec<_>>();

assert_tokens(&s, &expected_tokens)
}

#[cfg(feature = "zeroize")]
#[test]
fn test_zeroize() {
Expand Down