group: Remove EncodedPoint::from_affine

The EncodedPoint trait is replaced by explicit bounds on the
CurveAffine::{Uncompressed, Compressed} associated types.
This commit is contained in:
Jack Grigg 2020-05-19 18:30:31 +12:00
parent b77f8dddda
commit ceecd32ac4
5 changed files with 90 additions and 113 deletions

View File

@ -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<E: Engine> Proof<E> {
pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
let read_g1 = |reader: &mut R| -> io::Result<E::G1Affine> {
let mut g1_repr = <E::G1Affine as CurveAffine>::Compressed::empty();
let mut g1_repr = <E::G1Affine as CurveAffine>::Compressed::default();
reader.read_exact(g1_repr.as_mut())?;
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 mut g2_repr = <E::G2Affine as CurveAffine>::Compressed::empty();
let mut g2_repr = <E::G2Affine as CurveAffine>::Compressed::default();
reader.read_exact(g2_repr.as_mut())?;
let affine = E::G2Affine::from_compressed(&g2_repr);
@ -158,7 +158,7 @@ impl<E: Engine> VerifyingKey<E> {
pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
let read_g1 = |reader: &mut R| -> io::Result<E::G1Affine> {
let mut g1_repr = <E::G1Affine as CurveAffine>::Uncompressed::empty();
let mut g1_repr = <E::G1Affine as CurveAffine>::Uncompressed::default();
reader.read_exact(g1_repr.as_mut())?;
let affine = E::G1Affine::from_uncompressed(&g1_repr);
@ -170,7 +170,7 @@ impl<E: Engine> VerifyingKey<E> {
};
let read_g2 = |reader: &mut R| -> io::Result<E::G2Affine> {
let mut g2_repr = <E::G2Affine as CurveAffine>::Uncompressed::empty();
let mut g2_repr = <E::G2Affine as CurveAffine>::Uncompressed::default();
reader.read_exact(g2_repr.as_mut())?;
let affine = E::G2Affine::from_uncompressed(&g2_repr);
@ -289,7 +289,7 @@ impl<E: Engine> Parameters<E> {
pub fn read<R: Read>(mut reader: R, checked: bool) -> io::Result<Self> {
let read_g1 = |reader: &mut R| -> io::Result<E::G1Affine> {
let mut repr = <E::G1Affine as CurveAffine>::Uncompressed::empty();
let mut repr = <E::G1Affine as CurveAffine>::Uncompressed::default();
reader.read_exact(repr.as_mut())?;
let affine = if checked {
@ -317,7 +317,7 @@ impl<E: Engine> Parameters<E> {
};
let read_g2 = |reader: &mut R| -> io::Result<E::G2Affine> {
let mut repr = <E::G2Affine as CurveAffine>::Uncompressed::empty();
let mut repr = <E::G2Affine as CurveAffine>::Uncompressed::default();
reader.read_exact(repr.as_mut())?;
let affine = if checked {

View File

@ -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<Self> {
unimplemented!()
}
@ -486,6 +474,10 @@ impl CurveAffine for Fr {
fn from_uncompressed_unchecked(_bytes: &Self::Uncompressed) -> CtOption<Self> {
unimplemented!()
}
fn into_uncompressed(&self) -> Self::Uncompressed {
unimplemented!()
}
}
impl PairingCurveAffine for Fr {

View File

@ -138,8 +138,8 @@ pub trait CurveAffine:
type Scalar: PrimeField;
type Base: Field;
type Projective: CurveProjective<Affine = Self, Scalar = Self::Scalar>;
type Uncompressed: EncodedPoint<Affine = Self>;
type Compressed: EncodedPoint<Affine = Self>;
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 {
<Self::Compressed as EncodedPoint>::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<Self>;
@ -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 {
<Self::Uncompressed as EncodedPoint>::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;
}

View File

@ -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> {
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<G1Affine, GroupDecodingError> {
// 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<G1Affine, GroupDecodingError> {
// 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<G2Affine, GroupDecodingError> {
// 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<G2Affine, GroupDecodingError> {
// 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

View File

@ -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<G: CurveProjective>(expected: &[u8]) {
let mut e = G::identity();
let encoded_len = <G::Affine as CurveAffine>::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 = <G::Affine as CurveAffine>::Uncompressed::from_affine(e_affine);
let encoded = e_affine.into_uncompressed();
v.extend_from_slice(encoded.as_ref());
let mut decoded = <G::Affine as CurveAffine>::Uncompressed::empty();
decoded
.as_mut()
.copy_from_slice(&expected[0..<G::Affine as CurveAffine>::Uncompressed::size()]);
expected = &expected[<G::Affine as CurveAffine>::Uncompressed::size()..];
let mut decoded = <G::Affine as CurveAffine>::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<G: CurveProjective>(expected: &[u8]) {
fn compressed_test_vectors<G: CurveProjective>(expected: &[u8]) {
let mut e = G::identity();
let encoded_len = <G::Affine as CurveAffine>::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 = <G::Affine as CurveAffine>::Compressed::from_affine(e_affine);
let encoded = e_affine.into_compressed();
v.extend_from_slice(encoded.as_ref());
let mut decoded = <G::Affine as CurveAffine>::Compressed::empty();
decoded
.as_mut()
.copy_from_slice(&expected[0..<G::Affine as CurveAffine>::Compressed::size()]);
expected = &expected[<G::Affine as CurveAffine>::Compressed::size()..];
let mut decoded = <G::Affine as CurveAffine>::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);