Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k256+p256: use revised LinearCombination trait #478

Merged
merged 1 commit into from Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion k256/Cargo.toml
Expand Up @@ -19,7 +19,7 @@ rust-version = "1.56"

[dependencies]
cfg-if = "1.0"
elliptic-curve = { version = "0.11.4", default-features = false, features = ["hazmat", "sec1"] }
elliptic-curve = { version = "0.11.5", default-features = false, features = ["hazmat", "sec1"] }
sec1 = { version = "0.2", default-features = false }

# optional dependencies
Expand Down
4 changes: 2 additions & 2 deletions k256/bench/scalar.rs
Expand Up @@ -6,7 +6,7 @@ use criterion::{
use hex_literal::hex;
use k256::{
elliptic_curve::{generic_array::arr, group::ff::PrimeField, ops::LinearCombination},
ProjectivePoint, Scalar, Secp256k1,
ProjectivePoint, Scalar,
};

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

Expand Down
18 changes: 6 additions & 12 deletions k256/src/arithmetic/mul.rs
Expand Up @@ -65,12 +65,9 @@
//! In experiments, I was not able to detect any case where they would go outside the 128 bit bound,
//! but I cannot be sure that it cannot happen.

use crate::{
arithmetic::{
scalar::{Scalar, WideScalar},
ProjectivePoint,
},
Secp256k1,
use crate::arithmetic::{
scalar::{Scalar, WideScalar},
ProjectivePoint,
};
use core::ops::{Mul, MulAssign};
use elliptic_curve::{
Expand Down Expand Up @@ -305,7 +302,7 @@ fn mul(x: &ProjectivePoint, k: &Scalar) -> ProjectivePoint {
lincomb_generic(&[*x], &[*k])
}

impl LinearCombination for Secp256k1 {
impl LinearCombination for ProjectivePoint {
fn lincomb(
x: &ProjectivePoint,
k: &Scalar,
Expand Down Expand Up @@ -354,10 +351,7 @@ impl MulAssign<&Scalar> for ProjectivePoint {

#[cfg(test)]
mod tests {
use crate::{
arithmetic::{ProjectivePoint, Scalar},
Secp256k1,
};
use crate::arithmetic::{ProjectivePoint, Scalar};
use elliptic_curve::{ops::LinearCombination, rand_core::OsRng, Field, Group};

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

let reference = &x * &k + &y * &l;
let test = Secp256k1::lincomb(&x, &k, &y, &l);
let test = ProjectivePoint::lincomb(&x, &k, &y, &l);
assert_eq!(reference, test);
}
}
24 changes: 12 additions & 12 deletions k256/src/ecdsa/recoverable.rs
Expand Up @@ -51,7 +51,7 @@ use crate::{
ops::{Invert, LinearCombination, Reduce},
DecompressPoint,
},
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar, Secp256k1,
AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar,
};

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

if R.is_some().into() {
let R = ProjectivePoint::from(R.unwrap());
let r_inv = r.invert().unwrap();
let u1 = -(r_inv * z);
let u2 = r_inv * *s;
let pk = Secp256k1::lincomb(&ProjectivePoint::generator(), &u1, &R, &u2).to_affine();

// TODO(tarcieri): ensure the signature verifies?
Ok(VerifyingKey::from(&pk))
} else {
Err(Error::new())
if R.is_none().into() {
return Err(Error::new());
}

let R = ProjectivePoint::from(R.unwrap());
let r_inv = r.invert().unwrap();
let u1 = -(r_inv * z);
let u2 = r_inv * *s;
let pk = ProjectivePoint::lincomb(&ProjectivePoint::generator(), &u1, &R, &u2).to_affine();

// TODO(tarcieri): ensure the signature verifies?
Ok(VerifyingKey::from(&pk))
}

/// Parse the `r` component of this signature to a [`NonZeroScalar`]
Expand Down
2 changes: 1 addition & 1 deletion k256/src/ecdsa/verify.rs
Expand Up @@ -110,7 +110,7 @@ impl VerifyPrimitive<Secp256k1> for AffinePoint {
let u1 = z * s_inv;
let u2 = *r * s_inv;

let x = Secp256k1::lincomb(
let x = ProjectivePoint::lincomb(
&ProjectivePoint::generator(),
&u1,
&ProjectivePoint::from(*self),
Expand Down
2 changes: 1 addition & 1 deletion p256/Cargo.toml
Expand Up @@ -17,7 +17,7 @@ edition = "2021"
rust-version = "1.56"

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

# optional dependencies
Expand Down
4 changes: 0 additions & 4 deletions p256/src/arithmetic.rs
Expand Up @@ -6,9 +6,7 @@ pub(crate) mod projective;
pub(crate) mod scalar;
pub(crate) mod util;

use crate::NistP256;
use affine::AffinePoint;
use elliptic_curve::ops::LinearCombination;
use field::{FieldElement, MODULUS};
use projective::ProjectivePoint;
use scalar::Scalar;
Expand All @@ -27,8 +25,6 @@ const CURVE_EQUATION_B: FieldElement = FieldElement([
0xdc30_061d_0487_4834,
]);

impl LinearCombination for NistP256 {}

#[cfg(test)]
mod tests {
use super::{CURVE_EQUATION_A, CURVE_EQUATION_B};
Expand Down
3 changes: 3 additions & 0 deletions p256/src/arithmetic/projective.rs
Expand Up @@ -13,6 +13,7 @@ use elliptic_curve::{
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
Curve, Group, GroupEncoding,
},
ops::LinearCombination,
rand_core::RngCore,
sec1::{FromEncodedPoint, ToEncodedPoint},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
Expand Down Expand Up @@ -93,6 +94,8 @@ impl PrimeCurve for ProjectivePoint {
type Affine = AffinePoint;
}

impl LinearCombination for ProjectivePoint {}

impl From<AffinePoint> for ProjectivePoint {
fn from(p: AffinePoint) -> Self {
let projective = ProjectivePoint {
Expand Down