Skip to content

Commit 7d34b9e

Browse files
authored
k256+p256: use revised LinearCombination trait (#478)
See RustCrypto/traits#835
1 parent ad7bbc5 commit 7d34b9e

File tree

9 files changed

+30
-37
lines changed

9 files changed

+30
-37
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

k256/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rust-version = "1.56"
1919

2020
[dependencies]
2121
cfg-if = "1.0"
22-
elliptic-curve = { version = "0.11.4", default-features = false, features = ["hazmat", "sec1"] }
22+
elliptic-curve = { version = "0.11.5", default-features = false, features = ["hazmat", "sec1"] }
2323
sec1 = { version = "0.2", default-features = false }
2424

2525
# optional dependencies

k256/bench/scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use criterion::{
66
use hex_literal::hex;
77
use k256::{
88
elliptic_curve::{generic_array::arr, group::ff::PrimeField, ops::LinearCombination},
9-
ProjectivePoint, Scalar, Secp256k1,
9+
ProjectivePoint, Scalar,
1010
};
1111

1212
fn test_scalar_x() -> Scalar {
@@ -40,7 +40,7 @@ fn bench_point_lincomb<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
4040
let s = Scalar::from_repr(m.into()).unwrap();
4141
group.bench_function("lincomb via mul+add", |b| b.iter(|| &p * &s + &p * &s));
4242
group.bench_function("lincomb()", |b| {
43-
b.iter(|| Secp256k1::lincomb(&p, &s, &p, &s))
43+
b.iter(|| ProjectivePoint::lincomb(&p, &s, &p, &s))
4444
});
4545
}
4646

k256/src/arithmetic/mul.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,9 @@
6565
//! In experiments, I was not able to detect any case where they would go outside the 128 bit bound,
6666
//! but I cannot be sure that it cannot happen.
6767
68-
use crate::{
69-
arithmetic::{
70-
scalar::{Scalar, WideScalar},
71-
ProjectivePoint,
72-
},
73-
Secp256k1,
68+
use crate::arithmetic::{
69+
scalar::{Scalar, WideScalar},
70+
ProjectivePoint,
7471
};
7572
use core::ops::{Mul, MulAssign};
7673
use elliptic_curve::{
@@ -305,7 +302,7 @@ fn mul(x: &ProjectivePoint, k: &Scalar) -> ProjectivePoint {
305302
lincomb_generic(&[*x], &[*k])
306303
}
307304

308-
impl LinearCombination for Secp256k1 {
305+
impl LinearCombination for ProjectivePoint {
309306
fn lincomb(
310307
x: &ProjectivePoint,
311308
k: &Scalar,
@@ -354,10 +351,7 @@ impl MulAssign<&Scalar> for ProjectivePoint {
354351

355352
#[cfg(test)]
356353
mod tests {
357-
use crate::{
358-
arithmetic::{ProjectivePoint, Scalar},
359-
Secp256k1,
360-
};
354+
use crate::arithmetic::{ProjectivePoint, Scalar};
361355
use elliptic_curve::{ops::LinearCombination, rand_core::OsRng, Field, Group};
362356

363357
#[test]
@@ -368,7 +362,7 @@ mod tests {
368362
let l = Scalar::random(&mut OsRng);
369363

370364
let reference = &x * &k + &y * &l;
371-
let test = Secp256k1::lincomb(&x, &k, &y, &l);
365+
let test = ProjectivePoint::lincomb(&x, &k, &y, &l);
372366
assert_eq!(reference, test);
373367
}
374368
}

k256/src/ecdsa/recoverable.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::{
5151
ops::{Invert, LinearCombination, Reduce},
5252
DecompressPoint,
5353
},
54-
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar, Secp256k1,
54+
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar,
5555
};
5656

5757
#[cfg(feature = "keccak256")]
@@ -176,18 +176,18 @@ impl Signature {
176176
let z = <Scalar as Reduce<U256>>::from_be_bytes_reduced(*digest_bytes);
177177
let R = AffinePoint::decompress(&r.to_bytes(), self.recovery_id().is_y_odd());
178178

179-
if R.is_some().into() {
180-
let R = ProjectivePoint::from(R.unwrap());
181-
let r_inv = r.invert().unwrap();
182-
let u1 = -(r_inv * z);
183-
let u2 = r_inv * *s;
184-
let pk = Secp256k1::lincomb(&ProjectivePoint::generator(), &u1, &R, &u2).to_affine();
185-
186-
// TODO(tarcieri): ensure the signature verifies?
187-
Ok(VerifyingKey::from(&pk))
188-
} else {
189-
Err(Error::new())
179+
if R.is_none().into() {
180+
return Err(Error::new());
190181
}
182+
183+
let R = ProjectivePoint::from(R.unwrap());
184+
let r_inv = r.invert().unwrap();
185+
let u1 = -(r_inv * z);
186+
let u2 = r_inv * *s;
187+
let pk = ProjectivePoint::lincomb(&ProjectivePoint::generator(), &u1, &R, &u2).to_affine();
188+
189+
// TODO(tarcieri): ensure the signature verifies?
190+
Ok(VerifyingKey::from(&pk))
191191
}
192192

193193
/// Parse the `r` component of this signature to a [`NonZeroScalar`]

k256/src/ecdsa/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl VerifyPrimitive<Secp256k1> for AffinePoint {
110110
let u1 = z * s_inv;
111111
let u2 = *r * s_inv;
112112

113-
let x = Secp256k1::lincomb(
113+
let x = ProjectivePoint::lincomb(
114114
&ProjectivePoint::generator(),
115115
&u1,
116116
&ProjectivePoint::from(*self),

p256/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2021"
1717
rust-version = "1.56"
1818

1919
[dependencies]
20-
elliptic-curve = { version = "0.11", default-features = false, features = ["hazmat", "sec1"] }
20+
elliptic-curve = { version = "0.11.5", default-features = false, features = ["hazmat", "sec1"] }
2121
sec1 = { version = "0.2", default-features = false }
2222

2323
# optional dependencies

p256/src/arithmetic.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ pub(crate) mod projective;
66
pub(crate) mod scalar;
77
pub(crate) mod util;
88

9-
use crate::NistP256;
109
use affine::AffinePoint;
11-
use elliptic_curve::ops::LinearCombination;
1210
use field::{FieldElement, MODULUS};
1311
use projective::ProjectivePoint;
1412
use scalar::Scalar;
@@ -27,8 +25,6 @@ const CURVE_EQUATION_B: FieldElement = FieldElement([
2725
0xdc30_061d_0487_4834,
2826
]);
2927

30-
impl LinearCombination for NistP256 {}
31-
3228
#[cfg(test)]
3329
mod tests {
3430
use super::{CURVE_EQUATION_A, CURVE_EQUATION_B};

p256/src/arithmetic/projective.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use elliptic_curve::{
1313
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
1414
Curve, Group, GroupEncoding,
1515
},
16+
ops::LinearCombination,
1617
rand_core::RngCore,
1718
sec1::{FromEncodedPoint, ToEncodedPoint},
1819
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
@@ -93,6 +94,8 @@ impl PrimeCurve for ProjectivePoint {
9394
type Affine = AffinePoint;
9495
}
9596

97+
impl LinearCombination for ProjectivePoint {}
98+
9699
impl From<AffinePoint> for ProjectivePoint {
97100
fn from(p: AffinePoint) -> Self {
98101
let projective = ProjectivePoint {

0 commit comments

Comments
 (0)