From 1434ad7b28105562fea8cbff7756237d30790559 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Sun, 7 May 2017 18:15:33 -0600 Subject: [PATCH] Introduce `Group` for generic group operations. --- src/curves/bls381/ec.rs | 12 ++++++++++++ src/curves/bls381/mod.rs | 13 +++++++++++++ src/curves/mod.rs | 12 ++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/curves/bls381/ec.rs b/src/curves/bls381/ec.rs index cdb8831..dcbaee0 100644 --- a/src/curves/bls381/ec.rs +++ b/src/curves/bls381/ec.rs @@ -43,6 +43,18 @@ macro_rules! curve_impl { } } + impl Group<$engine> for $name { + fn group_mul_assign(&mut self, e: &$engine, scalar: &$scalarfield) { + self.mul_assign(e, scalar); + } + fn group_add_assign(&mut self, e: &$engine, other: &Self) { + self.add_assign(e, other); + } + fn group_sub_assign(&mut self, e: &$engine, other: &Self) { + self.sub_assign(e, other); + } + } + impl CurveAffine<$engine, $name> for $name_affine { type Uncompressed = $name_uncompressed; diff --git a/src/curves/bls381/mod.rs b/src/curves/bls381/mod.rs index fe74790..8dd956a 100644 --- a/src/curves/bls381/mod.rs +++ b/src/curves/bls381/mod.rs @@ -5,6 +5,7 @@ use std::borrow::Borrow; use super::{ WindowTable, Engine, + Group, Curve, CurveAffine, CurveRepresentation, @@ -95,6 +96,18 @@ fp_impl!( inv = 0xfffffffeffffffff ); +impl Group for Fr { + fn group_mul_assign(&mut self, e: &Bls381, scalar: &Fr) { + self.mul_assign(e, scalar); + } + fn group_add_assign(&mut self, e: &Bls381, other: &Self) { + self.add_assign(e, other); + } + fn group_sub_assign(&mut self, e: &Bls381, other: &Self) { + self.sub_assign(e, other); + } +} + curve_impl!(Bls381, G1, G1Affine, G1Affine, G1Uncompressed, G1Params, g1params, Fq, Fr); curve_impl!(Bls381, G2, G2Affine, G2Prepared, G2Uncompressed, G2Params, g2params, Fq2, Fr); diff --git a/src/curves/mod.rs b/src/curves/mod.rs index dc39e6a..25882f9 100644 --- a/src/curves/mod.rs +++ b/src/curves/mod.rs @@ -46,13 +46,21 @@ pub trait Engine: Sized + Clone fn batch_baseexp, S: AsRef<[Self::Fr]>>(&self, table: &WindowTable>, scalars: S) -> Vec; } +pub trait Group +{ + fn group_mul_assign(&mut self, &E, scalar: &E::Fr); + fn group_add_assign(&mut self, &E, other: &Self); + fn group_sub_assign(&mut self, &E, other: &Self); +} + pub trait Curve: Sized + Copy + Clone + Send + Sync + fmt::Debug + - 'static + 'static + + Group { type Affine: CurveAffine; type Prepared: Clone + Send + Sync + 'static; @@ -193,7 +201,7 @@ pub trait PrimeField: SqrtField + Convert<[u64], E> fn capacity(&E) -> usize; } -pub trait SnarkField: PrimeField +pub trait SnarkField: PrimeField + Group { fn s(&E) -> u64; fn multiplicative_generator(&E) -> Self;