Skip to content

Commit

Permalink
Fix macro to divide unused serde derive features correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
akakou committed Jul 3, 2021
1 parent 4916f7c commit 86c2215
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 161 deletions.
122 changes: 64 additions & 58 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,82 +1013,88 @@ impl UncompressedEncoding for G1Affine {
}

#[cfg(feature = "serde")]
use serde::de::Visitor;
#[cfg(feature = "serde")]
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
mod serde_support {
use super::{fmt, G1Affine, G1Projective};

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)?;
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()
}
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;
impl<'de> Deserialize<'de> for G1Affine {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct G1AffineVisitor;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 48-byte compressed canonical bls12_381 G1 point")
}
impl<'de> Visitor<'de> for G1AffineVisitor {
type Value = G1Affine;

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"))?;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 48-byte compressed canonical bls12_381 G1 point")
}

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",
))
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)
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 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)
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_is_on_curve() {
assert!(bool::from(G1Affine::identity().is_on_curve()));
Expand Down
123 changes: 65 additions & 58 deletions src/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,82 +1200,89 @@ impl UncompressedEncoding for G2Affine {
}

#[cfg(feature = "serde")]
use serde::de::Visitor;
#[cfg(feature = "serde")]
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
mod serde_support {
use super::{fmt, G2Affine, G2Projective};

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)?;
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()
}
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;
impl<'de> Deserialize<'de> for G2Affine {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct G2AffineVisitor;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 96-byte compressed canonical bls12_381 G2 point")
}
impl<'de> Visitor<'de> for G2AffineVisitor {
type Value = G2Affine;

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"))?;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a 96-byte compressed canonical bls12_381 G2 point")
}

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",
))
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)
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 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)
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
Loading

0 comments on commit 86c2215

Please sign in to comment.