Skip to content

Commit 784e0d2

Browse files
authored
elliptic-curve: LinearCombinationExt => LinearCombination (#1501)
Replaces the old, significantly less flexible `LinearCombination` trait with the newer `LinearCombinationExt` trait, removing the old trait completely and renaming the new one. The bounds for `CurveArithmetic::ProjectivePoint` have also been updated accordingly.
1 parent 70b43f3 commit 784e0d2

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
lines changed

elliptic-curve/src/arithmetic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ pub trait CurveArithmetic: Curve {
4444
+ DefaultIsZeroes
4545
+ From<Self::AffinePoint>
4646
+ Into<Self::AffinePoint>
47-
+ LinearCombination
47+
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar)]>
48+
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar); 2]>
4849
+ MulByGenerator
4950
+ group::Curve<AffineRepr = Self::AffinePoint>
5051
+ group::Group<Scalar = Self::Scalar>;

elliptic-curve/src/dev.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ impl group::Curve for ProjectivePoint {
661661
}
662662
}
663663

664-
impl LinearCombination for ProjectivePoint {}
664+
impl LinearCombination<[(ProjectivePoint, Scalar)]> for ProjectivePoint {}
665+
impl<const N: usize> LinearCombination<[(ProjectivePoint, Scalar); N]> for ProjectivePoint {}
665666

666667
impl Add<ProjectivePoint> for ProjectivePoint {
667668
type Output = ProjectivePoint;

elliptic-curve/src/ops.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,16 @@ fn invert_batch_internal<
149149

150150
/// Linear combination.
151151
///
152-
/// This trait enables crates to provide an optimized implementation of
153-
/// linear combinations (e.g. Shamir's Trick), or otherwise provides a default
154-
/// non-optimized implementation.
155-
// TODO(tarcieri): replace this with a trait from the `group` crate? (see zkcrypto/group#25)
156-
pub trait LinearCombination: Group {
157-
/// Calculates `x * k + y * l`.
158-
fn lincomb(x: &Self, k: &Self::Scalar, y: &Self, l: &Self::Scalar) -> Self {
159-
(*x * k) + (*y * l)
160-
}
161-
}
162-
163-
/// Linear combination (extended version).
152+
/// This trait enables optimized implementations of linear combinations (e.g. Shamir's Trick).
164153
///
165-
/// This trait enables providing an optimized implementation of
166-
/// linear combinations (e.g. Shamir's Trick).
167-
// TODO(tarcieri): replace the current `LinearCombination` with this in the next release
168-
pub trait LinearCombinationExt<PointsAndScalars>: group::Curve
154+
/// It's generic around `PointsAndScalars` to allow overlapping impls. For example, const generic
155+
/// impls can use the input size to determine the size needed to store temporary variables.
156+
pub trait LinearCombination<PointsAndScalars>: group::Curve
169157
where
170158
PointsAndScalars: AsRef<[(Self, Self::Scalar)]> + ?Sized,
171159
{
172160
/// Calculates `x1 * k1 + ... + xn * kn`.
173-
fn lincomb_ext(points_and_scalars: &PointsAndScalars) -> Self {
161+
fn lincomb(points_and_scalars: &PointsAndScalars) -> Self {
174162
points_and_scalars
175163
.as_ref()
176164
.iter()
@@ -180,14 +168,6 @@ where
180168
}
181169
}
182170

183-
/// Blanket impl of the legacy [`LinearCombination`] trait for types which impl the new
184-
/// [`LinearCombinationExt`] trait for 2-element arrays.
185-
impl<P: LinearCombinationExt<[(P, Self::Scalar); 2]>> LinearCombination for P {
186-
fn lincomb(x: &Self, k: &Self::Scalar, y: &Self, l: &Self::Scalar) -> Self {
187-
Self::lincomb_ext(&[(*x, *k), (*y, *l)])
188-
}
189-
}
190-
191171
/// Multiplication by the generator.
192172
///
193173
/// May use optimizations (e.g. precomputed tables) when available.

0 commit comments

Comments
 (0)