group: Extract compressed encodings from CurveAffine trait
This commit is contained in:
parent
3759fc8aab
commit
71586914d4
|
@ -2,7 +2,7 @@
|
||||||
//!
|
//!
|
||||||
//! [Groth16]: https://eprint.iacr.org/2016/260
|
//! [Groth16]: https://eprint.iacr.org/2016/260
|
||||||
|
|
||||||
use group::{CurveAffine, UncompressedEncoding};
|
use group::{CurveAffine, GroupEncoding, UncompressedEncoding};
|
||||||
use pairing::{Engine, MultiMillerLoop};
|
use pairing::{Engine, MultiMillerLoop};
|
||||||
|
|
||||||
use crate::SynthesisError;
|
use crate::SynthesisError;
|
||||||
|
@ -47,7 +47,7 @@ impl<E: Engine> Proof<E> {
|
||||||
|
|
||||||
pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
|
pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
|
||||||
let read_g1 = |reader: &mut R| -> io::Result<E::G1Affine> {
|
let read_g1 = |reader: &mut R| -> io::Result<E::G1Affine> {
|
||||||
let mut g1_repr = <E::G1Affine as CurveAffine>::Compressed::default();
|
let mut g1_repr = <E::G1Affine as GroupEncoding>::Compressed::default();
|
||||||
reader.read_exact(g1_repr.as_mut())?;
|
reader.read_exact(g1_repr.as_mut())?;
|
||||||
|
|
||||||
let affine = E::G1Affine::from_compressed(&g1_repr);
|
let affine = E::G1Affine::from_compressed(&g1_repr);
|
||||||
|
@ -70,7 +70,7 @@ impl<E: Engine> Proof<E> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let read_g2 = |reader: &mut R| -> io::Result<E::G2Affine> {
|
let read_g2 = |reader: &mut R| -> io::Result<E::G2Affine> {
|
||||||
let mut g2_repr = <E::G2Affine as CurveAffine>::Compressed::default();
|
let mut g2_repr = <E::G2Affine as GroupEncoding>::Compressed::default();
|
||||||
reader.read_exact(g2_repr.as_mut())?;
|
reader.read_exact(g2_repr.as_mut())?;
|
||||||
|
|
||||||
let affine = E::G2Affine::from_compressed(&g2_repr);
|
let affine = E::G2Affine::from_compressed(&g2_repr);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use ff::{Field, PrimeField};
|
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 pairing::{Engine, MillerLoopResult, MultiMillerLoop, PairingCurveAffine};
|
||||||
|
|
||||||
use rand_core::RngCore;
|
use rand_core::RngCore;
|
||||||
|
@ -425,7 +425,6 @@ impl AsRef<[u8]> for FakePoint {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CurveAffine for Fr {
|
impl CurveAffine for Fr {
|
||||||
type Compressed = FakePoint;
|
|
||||||
type Projective = Fr;
|
type Projective = Fr;
|
||||||
type Scalar = Fr;
|
type Scalar = Fr;
|
||||||
|
|
||||||
|
@ -444,6 +443,10 @@ impl CurveAffine for Fr {
|
||||||
fn to_projective(&self) -> Self::Projective {
|
fn to_projective(&self) -> Self::Projective {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GroupEncoding for Fr {
|
||||||
|
type Compressed = FakePoint;
|
||||||
|
|
||||||
fn from_compressed(_bytes: &Self::Compressed) -> CtOption<Self> {
|
fn from_compressed(_bytes: &Self::Compressed) -> CtOption<Self> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
|
|
|
@ -132,13 +132,13 @@ pub trait CurveAffine:
|
||||||
+ PartialEq
|
+ PartialEq
|
||||||
+ Eq
|
+ Eq
|
||||||
+ 'static
|
+ 'static
|
||||||
|
+ GroupEncoding
|
||||||
+ Neg<Output = Self>
|
+ Neg<Output = Self>
|
||||||
+ Mul<<Self as CurveAffine>::Scalar, Output = <Self as CurveAffine>::Projective>
|
+ Mul<<Self as CurveAffine>::Scalar, Output = <Self as CurveAffine>::Projective>
|
||||||
+ for<'r> Mul<<Self as CurveAffine>::Scalar, Output = <Self as CurveAffine>::Projective>
|
+ for<'r> Mul<<Self as CurveAffine>::Scalar, Output = <Self as CurveAffine>::Projective>
|
||||||
{
|
{
|
||||||
type Scalar: PrimeField;
|
type Scalar: PrimeField;
|
||||||
type Projective: CurveProjective<Affine = Self, Scalar = Self::Scalar>;
|
type Projective: CurveProjective<Affine = Self, Scalar = Self::Scalar>;
|
||||||
type Compressed: Default + AsRef<[u8]> + AsMut<[u8]>;
|
|
||||||
|
|
||||||
/// Returns the additive identity.
|
/// Returns the additive identity.
|
||||||
fn identity() -> Self;
|
fn identity() -> Self;
|
||||||
|
@ -152,6 +152,11 @@ pub trait CurveAffine:
|
||||||
|
|
||||||
/// Converts this element into its affine representation.
|
/// Converts this element into its affine representation.
|
||||||
fn to_projective(&self) -> Self::Projective;
|
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.
|
/// Attempts to deserialize an element from its compressed encoding.
|
||||||
fn from_compressed(bytes: &Self::Compressed) -> CtOption<Self>;
|
fn from_compressed(bytes: &Self::Compressed) -> CtOption<Self>;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rand::SeedableRng;
|
||||||
use rand_xorshift::XorShiftRng;
|
use rand_xorshift::XorShiftRng;
|
||||||
use std::ops::{Mul, Neg};
|
use std::ops::{Mul, Neg};
|
||||||
|
|
||||||
use crate::{CurveAffine, CurveProjective, UncompressedEncoding};
|
use crate::{CurveAffine, CurveProjective, GroupEncoding, UncompressedEncoding};
|
||||||
|
|
||||||
pub fn curve_tests<G: CurveProjective>() {
|
pub fn curve_tests<G: CurveProjective>() {
|
||||||
let mut rng = XorShiftRng::from_seed([
|
let mut rng = XorShiftRng::from_seed([
|
||||||
|
|
|
@ -200,7 +200,6 @@ macro_rules! curve_impl {
|
||||||
impl CurveAffine for $affine {
|
impl CurveAffine for $affine {
|
||||||
type Scalar = $scalarfield;
|
type Scalar = $scalarfield;
|
||||||
type Projective = $projective;
|
type Projective = $projective;
|
||||||
type Compressed = $compressed;
|
|
||||||
|
|
||||||
fn identity() -> Self {
|
fn identity() -> Self {
|
||||||
$affine {
|
$affine {
|
||||||
|
@ -221,6 +220,10 @@ macro_rules! curve_impl {
|
||||||
fn to_projective(&self) -> $projective {
|
fn to_projective(&self) -> $projective {
|
||||||
(*self).into()
|
(*self).into()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GroupEncoding for $affine {
|
||||||
|
type Compressed = $compressed;
|
||||||
|
|
||||||
fn from_compressed(bytes: &Self::Compressed) -> CtOption<Self> {
|
fn from_compressed(bytes: &Self::Compressed) -> CtOption<Self> {
|
||||||
Self::from_compressed_unchecked(bytes).and_then(|affine| {
|
Self::from_compressed_unchecked(bytes).and_then(|affine| {
|
||||||
|
@ -904,7 +907,9 @@ pub mod g1 {
|
||||||
use super::{g2::G2Affine, GroupDecodingError};
|
use super::{g2::G2Affine, GroupDecodingError};
|
||||||
use crate::{Engine, PairingCurveAffine};
|
use crate::{Engine, PairingCurveAffine};
|
||||||
use ff::{BitIterator, Field, PrimeField};
|
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 rand_core::RngCore;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||||
|
@ -1481,7 +1486,9 @@ pub mod g2 {
|
||||||
use super::{g1::G1Affine, GroupDecodingError};
|
use super::{g1::G1Affine, GroupDecodingError};
|
||||||
use crate::{Engine, PairingCurveAffine};
|
use crate::{Engine, PairingCurveAffine};
|
||||||
use ff::{BitIterator, Field, PrimeField};
|
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 rand_core::RngCore;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use ff::PrimeField;
|
use ff::PrimeField;
|
||||||
use group::{CurveAffine, CurveProjective, UncompressedEncoding};
|
use group::{CurveAffine, CurveProjective, GroupEncoding, UncompressedEncoding};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
@ -87,7 +87,7 @@ where
|
||||||
|
|
||||||
fn compressed_test_vectors<G: CurveProjective>(expected: &[u8]) {
|
fn compressed_test_vectors<G: CurveProjective>(expected: &[u8]) {
|
||||||
let mut e = G::identity();
|
let mut e = G::identity();
|
||||||
let encoded_len = <G::Affine as CurveAffine>::Compressed::default()
|
let encoded_len = <G::Affine as GroupEncoding>::Compressed::default()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.len();
|
.len();
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ fn compressed_test_vectors<G: CurveProjective>(expected: &[u8]) {
|
||||||
let encoded = e_affine.to_compressed();
|
let encoded = e_affine.to_compressed();
|
||||||
v.extend_from_slice(encoded.as_ref());
|
v.extend_from_slice(encoded.as_ref());
|
||||||
|
|
||||||
let mut decoded = <G::Affine as CurveAffine>::Compressed::default();
|
let mut decoded = <G::Affine as GroupEncoding>::Compressed::default();
|
||||||
decoded.as_mut().copy_from_slice(&expected[0..encoded_len]);
|
decoded.as_mut().copy_from_slice(&expected[0..encoded_len]);
|
||||||
expected = &expected[encoded_len..];
|
expected = &expected[encoded_len..];
|
||||||
let decoded = G::Affine::from_compressed(&decoded).unwrap();
|
let decoded = G::Affine::from_compressed(&decoded).unwrap();
|
||||||
|
|
Loading…
Reference in New Issue