From 71586914d471f798250d45c674a54e0fc785e63d Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 29 May 2020 20:22:53 +1200 Subject: [PATCH] group: Extract compressed encodings from CurveAffine trait --- bellman/src/groth16/mod.rs | 6 +++--- bellman/src/groth16/tests/dummy_engine.rs | 7 +++++-- group/src/lib.rs | 7 ++++++- group/src/tests/mod.rs | 2 +- pairing/src/bls12_381/ec.rs | 13 ++++++++++--- pairing/src/bls12_381/tests/mod.rs | 6 +++--- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/bellman/src/groth16/mod.rs b/bellman/src/groth16/mod.rs index e4285ce77..0d85c0ffb 100644 --- a/bellman/src/groth16/mod.rs +++ b/bellman/src/groth16/mod.rs @@ -2,7 +2,7 @@ //! //! [Groth16]: https://eprint.iacr.org/2016/260 -use group::{CurveAffine, UncompressedEncoding}; +use group::{CurveAffine, GroupEncoding, UncompressedEncoding}; use pairing::{Engine, MultiMillerLoop}; use crate::SynthesisError; @@ -47,7 +47,7 @@ impl Proof { pub fn read(mut reader: R) -> io::Result { let read_g1 = |reader: &mut R| -> io::Result { - let mut g1_repr = ::Compressed::default(); + let mut g1_repr = ::Compressed::default(); reader.read_exact(g1_repr.as_mut())?; let affine = E::G1Affine::from_compressed(&g1_repr); @@ -70,7 +70,7 @@ impl Proof { }; let read_g2 = |reader: &mut R| -> io::Result { - let mut g2_repr = ::Compressed::default(); + let mut g2_repr = ::Compressed::default(); reader.read_exact(g2_repr.as_mut())?; let affine = E::G2Affine::from_compressed(&g2_repr); diff --git a/bellman/src/groth16/tests/dummy_engine.rs b/bellman/src/groth16/tests/dummy_engine.rs index 22361d6b5..5fb661bf4 100644 --- a/bellman/src/groth16/tests/dummy_engine.rs +++ b/bellman/src/groth16/tests/dummy_engine.rs @@ -1,5 +1,5 @@ use ff::{Field, PrimeField}; -use group::{CurveAffine, CurveProjective, Group, PrimeGroup, UncompressedEncoding}; +use group::{CurveAffine, CurveProjective, Group, GroupEncoding, PrimeGroup, UncompressedEncoding}; use pairing::{Engine, MillerLoopResult, MultiMillerLoop, PairingCurveAffine}; use rand_core::RngCore; @@ -425,7 +425,6 @@ impl AsRef<[u8]> for FakePoint { } impl CurveAffine for Fr { - type Compressed = FakePoint; type Projective = Fr; type Scalar = Fr; @@ -444,6 +443,10 @@ impl CurveAffine for Fr { fn to_projective(&self) -> Self::Projective { *self } +} + +impl GroupEncoding for Fr { + type Compressed = FakePoint; fn from_compressed(_bytes: &Self::Compressed) -> CtOption { unimplemented!() diff --git a/group/src/lib.rs b/group/src/lib.rs index 8837345f2..59e25aa76 100644 --- a/group/src/lib.rs +++ b/group/src/lib.rs @@ -132,13 +132,13 @@ pub trait CurveAffine: + PartialEq + Eq + 'static + + GroupEncoding + Neg + Mul<::Scalar, Output = ::Projective> + for<'r> Mul<::Scalar, Output = ::Projective> { type Scalar: PrimeField; type Projective: CurveProjective; - type Compressed: Default + AsRef<[u8]> + AsMut<[u8]>; /// Returns the additive identity. fn identity() -> Self; @@ -152,6 +152,11 @@ pub trait CurveAffine: /// Converts this element into its affine representation. fn to_projective(&self) -> Self::Projective; +} + +pub trait GroupEncoding: Sized { + /// The encoding of group elements. + type Compressed: Default + AsRef<[u8]> + AsMut<[u8]>; /// Attempts to deserialize an element from its compressed encoding. fn from_compressed(bytes: &Self::Compressed) -> CtOption; diff --git a/group/src/tests/mod.rs b/group/src/tests/mod.rs index 1862df8fa..cb1f12f8a 100644 --- a/group/src/tests/mod.rs +++ b/group/src/tests/mod.rs @@ -3,7 +3,7 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{Mul, Neg}; -use crate::{CurveAffine, CurveProjective, UncompressedEncoding}; +use crate::{CurveAffine, CurveProjective, GroupEncoding, UncompressedEncoding}; pub fn curve_tests() { let mut rng = XorShiftRng::from_seed([ diff --git a/pairing/src/bls12_381/ec.rs b/pairing/src/bls12_381/ec.rs index e0d59d183..29c591cf2 100644 --- a/pairing/src/bls12_381/ec.rs +++ b/pairing/src/bls12_381/ec.rs @@ -200,7 +200,6 @@ macro_rules! curve_impl { impl CurveAffine for $affine { type Scalar = $scalarfield; type Projective = $projective; - type Compressed = $compressed; fn identity() -> Self { $affine { @@ -221,6 +220,10 @@ macro_rules! curve_impl { fn to_projective(&self) -> $projective { (*self).into() } + } + + impl GroupEncoding for $affine { + type Compressed = $compressed; fn from_compressed(bytes: &Self::Compressed) -> CtOption { Self::from_compressed_unchecked(bytes).and_then(|affine| { @@ -904,7 +907,9 @@ pub mod g1 { use super::{g2::G2Affine, GroupDecodingError}; use crate::{Engine, PairingCurveAffine}; use ff::{BitIterator, Field, PrimeField}; - use group::{CurveAffine, CurveProjective, Group, PrimeGroup, UncompressedEncoding}; + use group::{ + CurveAffine, CurveProjective, Group, GroupEncoding, PrimeGroup, UncompressedEncoding, + }; use rand_core::RngCore; use std::fmt; use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; @@ -1481,7 +1486,9 @@ pub mod g2 { use super::{g1::G1Affine, GroupDecodingError}; use crate::{Engine, PairingCurveAffine}; use ff::{BitIterator, Field, PrimeField}; - use group::{CurveAffine, CurveProjective, Group, PrimeGroup, UncompressedEncoding}; + use group::{ + CurveAffine, CurveProjective, Group, GroupEncoding, PrimeGroup, UncompressedEncoding, + }; use rand_core::RngCore; use std::fmt; use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; diff --git a/pairing/src/bls12_381/tests/mod.rs b/pairing/src/bls12_381/tests/mod.rs index 30be7edbb..cac563caf 100644 --- a/pairing/src/bls12_381/tests/mod.rs +++ b/pairing/src/bls12_381/tests/mod.rs @@ -1,5 +1,5 @@ use ff::PrimeField; -use group::{CurveAffine, CurveProjective, UncompressedEncoding}; +use group::{CurveAffine, CurveProjective, GroupEncoding, UncompressedEncoding}; use super::*; use crate::*; @@ -87,7 +87,7 @@ where fn compressed_test_vectors(expected: &[u8]) { let mut e = G::identity(); - let encoded_len = ::Compressed::default() + let encoded_len = ::Compressed::default() .as_ref() .len(); @@ -99,7 +99,7 @@ fn compressed_test_vectors(expected: &[u8]) { let encoded = e_affine.to_compressed(); v.extend_from_slice(encoded.as_ref()); - let mut decoded = ::Compressed::default(); + let mut decoded = ::Compressed::default(); decoded.as_mut().copy_from_slice(&expected[0..encoded_len]); expected = &expected[encoded_len..]; let decoded = G::Affine::from_compressed(&decoded).unwrap();