From 5397d73e0bb6bf4d9f8df32832f50abf10393206 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 14 May 2020 20:03:29 +1200 Subject: [PATCH] group: Define group operations on Group trait The GroupOps trait represents the group operation (addition), and the combination of the group operation with group inversion (subtraction). Group inversion (negation) is constrained directly on the Group trait. --- group/src/lib.rs | 50 +++++++++++++++++++++++++++------------------- pairing/src/lib.rs | 10 +++++----- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/group/src/lib.rs b/group/src/lib.rs index 977f48654..fc51af2ca 100644 --- a/group/src/lib.rs +++ b/group/src/lib.rs @@ -12,9 +12,35 @@ pub mod tests; mod wnaf; pub use self::wnaf::Wnaf; +/// A helper trait for types with a group operation. +pub trait GroupOps: + Add + Sub + AddAssign + SubAssign +{ +} + +impl GroupOps for T where + T: Add + Sub + AddAssign + SubAssign +{ +} + +/// A helper trait for references with a group operation. +pub trait GroupOpsOwned: for<'r> GroupOps<&'r Rhs, Output> {} +impl GroupOpsOwned for T where T: for<'r> GroupOps<&'r Rhs, Output> {} + /// This trait represents an element of a cryptographic group. pub trait Group: - Clone + Copy + fmt::Debug + fmt::Display + Eq + Sized + Send + Sync + 'static + Clone + + Copy + + fmt::Debug + + fmt::Display + + Eq + + Sized + + Send + + Sync + + 'static + + Neg + + GroupOps + + GroupOpsOwned { /// Returns an element chosen uniformly at random using a user-provided RNG. fn random(rng: &mut R) -> Self; @@ -32,30 +58,12 @@ pub trait Group: /// This trait represents an element of a prime-order cryptographic group. pub trait PrimeGroup: Group {} -/// A helper trait for types implementing group addition. -pub trait CurveOps: - Add + Sub + AddAssign + SubAssign -{ -} - -impl CurveOps for T where - T: Add + Sub + AddAssign + SubAssign -{ -} - -/// A helper trait for references implementing group addition. -pub trait CurveOpsOwned: for<'r> CurveOps<&'r Rhs, Output> {} -impl CurveOpsOwned for T where T: for<'r> CurveOps<&'r Rhs, Output> {} - /// Projective representation of an elliptic curve point guaranteed to be /// in the correct prime order subgroup. pub trait CurveProjective: Group - + Neg - + CurveOps - + CurveOpsOwned - + CurveOps<::Affine> - + CurveOpsOwned<::Affine> + + GroupOps<::Affine> + + GroupOpsOwned<::Affine> { type Scalar: PrimeField; type Base: Field; diff --git a/pairing/src/lib.rs b/pairing/src/lib.rs index e08eaacc8..341b0d0b7 100644 --- a/pairing/src/lib.rs +++ b/pairing/src/lib.rs @@ -21,7 +21,7 @@ pub mod tests; pub mod bls12_381; use ff::{Field, PrimeField, ScalarEngine}; -use group::{CurveAffine, CurveOps, CurveOpsOwned, CurveProjective}; +use group::{CurveAffine, CurveProjective, GroupOps, GroupOpsOwned}; use subtle::CtOption; /// An "engine" is a collection of types (fields, elliptic curve groups, etc.) @@ -31,8 +31,8 @@ pub trait Engine: ScalarEngine { /// The projective representation of an element in G1. type G1: CurveProjective + From - + CurveOps - + CurveOpsOwned; + + GroupOps + + GroupOpsOwned; /// The affine representation of an element in G1. type G1Affine: PairingCurveAffine< @@ -46,8 +46,8 @@ pub trait Engine: ScalarEngine { /// The projective representation of an element in G2. type G2: CurveProjective + From - + CurveOps - + CurveOpsOwned; + + GroupOps + + GroupOpsOwned; /// The affine representation of an element in G2. type G2Affine: PairingCurveAffine<