From d822e34e63c76d1d40257dce9b972679045e7dd4 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 14 Dec 2019 17:15:16 +0000 Subject: [PATCH] Extract curve operations into default impl traits This makes it possible to implement mixed addition using operator-backed traits without running into type annotation problems. --- group/src/lib.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/group/src/lib.rs b/group/src/lib.rs index b2dc410ea..abda0a6eb 100644 --- a/group/src/lib.rs +++ b/group/src/lib.rs @@ -12,6 +12,21 @@ pub mod tests; mod wnaf; pub use self::wnaf::Wnaf; +/// 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: @@ -25,15 +40,9 @@ pub trait CurveProjective: + fmt::Debug + fmt::Display + 'static - + Add - + Sub + Neg - + for<'a> Add<&'a Self, Output = Self> - + for<'a> Sub<&'a Self, Output = Self> - + AddAssign - + SubAssign - + for<'a> AddAssign<&'a Self> - + for<'a> SubAssign<&'a Self> + + CurveOps + + CurveOpsOwned { type Engine: ScalarEngine; type Scalar: PrimeField + SqrtField;