diff --git a/bellman/src/groth16/mod.rs b/bellman/src/groth16/mod.rs index 8f5d7d4ee..42b96d90e 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, EncodedPoint}; +use group::CurveAffine; use pairing::{Engine, PairingCurveAffine}; 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::empty(); + 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::empty(); + let mut g2_repr = ::Compressed::default(); reader.read_exact(g2_repr.as_mut())?; let affine = E::G2Affine::from_compressed(&g2_repr); @@ -158,7 +158,7 @@ impl VerifyingKey { pub fn read(mut reader: R) -> io::Result { let read_g1 = |reader: &mut R| -> io::Result { - let mut g1_repr = ::Uncompressed::empty(); + let mut g1_repr = ::Uncompressed::default(); reader.read_exact(g1_repr.as_mut())?; let affine = E::G1Affine::from_uncompressed(&g1_repr); @@ -170,7 +170,7 @@ impl VerifyingKey { }; let read_g2 = |reader: &mut R| -> io::Result { - let mut g2_repr = ::Uncompressed::empty(); + let mut g2_repr = ::Uncompressed::default(); reader.read_exact(g2_repr.as_mut())?; let affine = E::G2Affine::from_uncompressed(&g2_repr); @@ -289,7 +289,7 @@ impl Parameters { pub fn read(mut reader: R, checked: bool) -> io::Result { let read_g1 = |reader: &mut R| -> io::Result { - let mut repr = ::Uncompressed::empty(); + let mut repr = ::Uncompressed::default(); reader.read_exact(repr.as_mut())?; let affine = if checked { @@ -317,7 +317,7 @@ impl Parameters { }; let read_g2 = |reader: &mut R| -> io::Result { - let mut repr = ::Uncompressed::empty(); + let mut repr = ::Uncompressed::default(); reader.read_exact(repr.as_mut())?; let affine = if checked { diff --git a/bellman/src/groth16/tests/dummy_engine.rs b/bellman/src/groth16/tests/dummy_engine.rs index b85c65f70..c00d42a9a 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, ScalarEngine}; -use group::{CurveAffine, CurveProjective, EncodedPoint, Group, PrimeGroup}; +use group::{CurveAffine, CurveProjective, Group, PrimeGroup}; use pairing::{Engine, PairingCurveAffine}; use rand_core::RngCore; @@ -417,7 +417,7 @@ impl CurveProjective for Fr { } } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Default)] pub struct FakePoint; impl AsMut<[u8]> for FakePoint { @@ -432,22 +432,6 @@ impl AsRef<[u8]> for FakePoint { } } -impl EncodedPoint for FakePoint { - type Affine = Fr; - - fn empty() -> Self { - unimplemented!() - } - - fn size() -> usize { - unimplemented!() - } - - fn from_affine(_: Self::Affine) -> Self { - unimplemented!() - } -} - impl CurveAffine for Fr { type Compressed = FakePoint; type Uncompressed = FakePoint; @@ -479,6 +463,10 @@ impl CurveAffine for Fr { unimplemented!() } + fn into_compressed(&self) -> Self::Compressed { + unimplemented!() + } + fn from_uncompressed(_bytes: &Self::Uncompressed) -> CtOption { unimplemented!() } @@ -486,6 +474,10 @@ impl CurveAffine for Fr { fn from_uncompressed_unchecked(_bytes: &Self::Uncompressed) -> CtOption { unimplemented!() } + + fn into_uncompressed(&self) -> Self::Uncompressed { + unimplemented!() + } } impl PairingCurveAffine for Fr { diff --git a/group/src/lib.rs b/group/src/lib.rs index dac6f474e..13a17c8e3 100644 --- a/group/src/lib.rs +++ b/group/src/lib.rs @@ -138,8 +138,8 @@ pub trait CurveAffine: type Scalar: PrimeField; type Base: Field; type Projective: CurveProjective; - type Uncompressed: EncodedPoint; - type Compressed: EncodedPoint; + type Uncompressed: Default + AsRef<[u8]> + AsMut<[u8]>; + type Compressed: Default + AsRef<[u8]> + AsMut<[u8]>; /// Returns the additive identity. fn identity() -> Self; @@ -167,9 +167,7 @@ pub trait CurveAffine: /// Converts this element into its compressed encoding, so long as it's not /// the point at infinity. - fn into_compressed(&self) -> Self::Compressed { - ::from_affine(*self) - } + fn into_compressed(&self) -> Self::Compressed; /// Attempts to deserialize an element from its uncompressed encoding. fn from_uncompressed(bytes: &Self::Uncompressed) -> CtOption; @@ -184,24 +182,5 @@ pub trait CurveAffine: /// Converts this element into its uncompressed encoding, so long as it's not /// the point at infinity. - fn into_uncompressed(&self) -> Self::Uncompressed { - ::from_affine(*self) - } -} - -/// An encoded elliptic curve point, which should essentially wrap a `[u8; N]`. -pub trait EncodedPoint: - Sized + Send + Sync + AsRef<[u8]> + AsMut<[u8]> + Clone + Copy + 'static -{ - type Affine: CurveAffine; - - /// Creates an empty representation. - fn empty() -> Self; - - /// Returns the number of bytes consumed by this representation. - fn size() -> usize; - - /// Creates an `EncodedPoint` from an affine point, as long as the - /// point is not the point at infinity. - fn from_affine(affine: Self::Affine) -> Self; + fn into_uncompressed(&self) -> Self::Uncompressed; } diff --git a/pairing/src/bls12_381/ec.rs b/pairing/src/bls12_381/ec.rs index 82b080e40..5225535d7 100644 --- a/pairing/src/bls12_381/ec.rs +++ b/pairing/src/bls12_381/ec.rs @@ -247,6 +247,10 @@ macro_rules! curve_impl { } } + fn into_compressed(&self) -> Self::Compressed { + $compressed::from_affine(*self) + } + fn from_uncompressed(bytes: &Self::Uncompressed) -> CtOption { Self::from_uncompressed_unchecked(bytes).and_then(|affine| { CtOption::new( @@ -271,6 +275,10 @@ macro_rules! curve_impl { CtOption::new(Self::identity(), Choice::from(0)) } } + + fn into_uncompressed(&self) -> Self::Uncompressed { + $uncompressed::from_affine(*self) + } } impl PairingCurveAffine for $affine { @@ -901,7 +909,7 @@ pub mod g1 { use super::{g2::G2Affine, GroupDecodingError}; use crate::{Engine, PairingCurveAffine}; use ff::{BitIterator, Field, PrimeField}; - use group::{CurveAffine, CurveProjective, EncodedPoint, Group, PrimeGroup}; + use group::{CurveAffine, CurveProjective, Group, PrimeGroup}; use rand_core::RngCore; use std::fmt; use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; @@ -922,6 +930,12 @@ pub mod g1 { #[derive(Copy, Clone)] pub struct G1Uncompressed([u8; 96]); + impl Default for G1Uncompressed { + fn default() -> Self { + G1Uncompressed([0; 96]) + } + } + impl AsRef<[u8]> for G1Uncompressed { fn as_ref(&self) -> &[u8] { &self.0 @@ -941,6 +955,10 @@ pub mod g1 { } impl G1Uncompressed { + #[cfg(test)] + pub(crate) fn size() -> usize { + 96 + } fn into_affine_unchecked(&self) -> Result { // Create a copy of this representation. let mut copy = self.0; @@ -991,19 +1009,8 @@ pub mod g1 { }) } } - } - - impl EncodedPoint for G1Uncompressed { - type Affine = G1Affine; - - fn empty() -> Self { - G1Uncompressed([0; 96]) - } - fn size() -> usize { - 96 - } fn from_affine(affine: G1Affine) -> Self { - let mut res = Self::empty(); + let mut res = Self::default(); if affine.is_identity().into() { // Set the second-most significant bit to indicate this point @@ -1021,6 +1028,12 @@ pub mod g1 { #[derive(Copy, Clone)] pub struct G1Compressed([u8; 48]); + impl Default for G1Compressed { + fn default() -> Self { + G1Compressed([0; 48]) + } + } + impl AsRef<[u8]> for G1Compressed { fn as_ref(&self) -> &[u8] { &self.0 @@ -1040,6 +1053,10 @@ pub mod g1 { } impl G1Compressed { + #[cfg(test)] + pub(crate) fn size() -> usize { + 48 + } fn into_affine_unchecked(&self) -> Result { // Create a copy of this representation. let mut copy = self.0; @@ -1080,19 +1097,8 @@ pub mod g1 { } } } - } - - impl EncodedPoint for G1Compressed { - type Affine = G1Affine; - - fn empty() -> Self { - G1Compressed([0; 48]) - } - fn size() -> usize { - 48 - } fn from_affine(affine: G1Affine) -> Self { - let mut res = Self::empty(); + let mut res = Self::default(); if affine.is_identity().into() { // Set the second-most significant bit to indicate this point @@ -1493,7 +1499,7 @@ pub mod g2 { use super::{g1::G1Affine, GroupDecodingError}; use crate::{Engine, PairingCurveAffine}; use ff::{BitIterator, Field, PrimeField}; - use group::{CurveAffine, CurveProjective, EncodedPoint, Group, PrimeGroup}; + use group::{CurveAffine, CurveProjective, Group, PrimeGroup}; use rand_core::RngCore; use std::fmt; use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; @@ -1514,6 +1520,12 @@ pub mod g2 { #[derive(Copy, Clone)] pub struct G2Uncompressed([u8; 192]); + impl Default for G2Uncompressed { + fn default() -> Self { + G2Uncompressed([0; 192]) + } + } + impl AsRef<[u8]> for G2Uncompressed { fn as_ref(&self) -> &[u8] { &self.0 @@ -1533,6 +1545,10 @@ pub mod g2 { } impl G2Uncompressed { + #[cfg(test)] + pub(crate) fn size() -> usize { + 192 + } fn into_affine_unchecked(&self) -> Result { // Create a copy of this representation. let mut copy = self.0; @@ -1595,19 +1611,8 @@ pub mod g2 { }) } } - } - - impl EncodedPoint for G2Uncompressed { - type Affine = G2Affine; - - fn empty() -> Self { - G2Uncompressed([0; 192]) - } - fn size() -> usize { - 192 - } fn from_affine(affine: G2Affine) -> Self { - let mut res = Self::empty(); + let mut res = Self::default(); if affine.is_identity().into() { // Set the second-most significant bit to indicate this point @@ -1627,6 +1632,12 @@ pub mod g2 { #[derive(Copy, Clone)] pub struct G2Compressed([u8; 96]); + impl Default for G2Compressed { + fn default() -> Self { + G2Compressed([0; 96]) + } + } + impl AsRef<[u8]> for G2Compressed { fn as_ref(&self) -> &[u8] { &self.0 @@ -1646,6 +1657,10 @@ pub mod g2 { } impl G2Compressed { + #[cfg(test)] + pub(crate) fn size() -> usize { + 96 + } fn into_affine_unchecked(&self) -> Result { // Create a copy of this representation. let mut copy = self.0; @@ -1701,19 +1716,8 @@ pub mod g2 { } } } - } - - impl EncodedPoint for G2Compressed { - type Affine = G2Affine; - - fn empty() -> Self { - G2Compressed([0; 96]) - } - fn size() -> usize { - 96 - } fn from_affine(affine: G2Affine) -> Self { - let mut res = Self::empty(); + let mut res = Self::default(); if affine.is_identity().into() { // Set the second-most significant bit to indicate this point diff --git a/pairing/src/bls12_381/tests/mod.rs b/pairing/src/bls12_381/tests/mod.rs index 815c2bb4f..45dcb7e02 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, EncodedPoint, Group}; +use group::{CurveAffine, CurveProjective, Group}; use super::*; use crate::*; @@ -57,20 +57,21 @@ fn test_pairing_result_against_relic() { fn uncompressed_test_vectors(expected: &[u8]) { let mut e = G::identity(); + let encoded_len = ::Uncompressed::default() + .as_ref() + .len(); let mut v = vec![]; { let mut expected = expected; for _ in 0..1000 { let e_affine = e.into_affine(); - let encoded = ::Uncompressed::from_affine(e_affine); + let encoded = e_affine.into_uncompressed(); v.extend_from_slice(encoded.as_ref()); - let mut decoded = ::Uncompressed::empty(); - decoded - .as_mut() - .copy_from_slice(&expected[0..::Uncompressed::size()]); - expected = &expected[::Uncompressed::size()..]; + let mut decoded = ::Uncompressed::default(); + decoded.as_mut().copy_from_slice(&expected[0..encoded_len]); + expected = &expected[encoded_len..]; let decoded = G::Affine::from_uncompressed(&decoded).unwrap(); assert_eq!(e_affine, decoded); @@ -83,20 +84,21 @@ fn uncompressed_test_vectors(expected: &[u8]) { fn compressed_test_vectors(expected: &[u8]) { let mut e = G::identity(); + let encoded_len = ::Compressed::default() + .as_ref() + .len(); let mut v = vec![]; { let mut expected = expected; for _ in 0..1000 { let e_affine = e.into_affine(); - let encoded = ::Compressed::from_affine(e_affine); + let encoded = e_affine.into_compressed(); v.extend_from_slice(encoded.as_ref()); - let mut decoded = ::Compressed::empty(); - decoded - .as_mut() - .copy_from_slice(&expected[0..::Compressed::size()]); - expected = &expected[::Compressed::size()..]; + 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(); assert_eq!(e_affine, decoded);