From 8963704ae413aca54012debe0c24243c579b8b6e Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 4 Dec 2021 10:27:37 -0700 Subject: [PATCH] ecdsa: use `LinearCombination` trait (#417) Use the trait introduced in RustCrypto/traits#833 to implement ECDSA verification. --- Cargo.lock | 6 +++--- ecdsa/Cargo.toml | 2 +- ecdsa/src/hazmat.rs | 16 ++++++++++------ ecdsa/src/verify.rs | 6 +++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 243da467..54b6b4ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,7 +161,7 @@ name = "ecdsa" version = "0.13.1" dependencies = [ "der 0.5.1", - "elliptic-curve 0.11.3", + "elliptic-curve 0.11.4", "hex-literal", "rfc6979", "sha2", @@ -221,9 +221,9 @@ dependencies = [ [[package]] name = "elliptic-curve" -version = "0.11.3" +version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c4c31bb557a73d165c838b614521f888112f9d4fcff7421d35646376dd17caf" +checksum = "d9be7b065e66163fd97787a4cadc56625f948e22e914a0deab1d22b1f48fde25" dependencies = [ "crypto-bigint 0.3.2", "der 0.5.1", diff --git a/ecdsa/Cargo.toml b/ecdsa/Cargo.toml index ba0eb78b..2e40988c 100644 --- a/ecdsa/Cargo.toml +++ b/ecdsa/Cargo.toml @@ -15,7 +15,7 @@ edition = "2021" rust-version = "1.56" [dependencies] -elliptic-curve = { version = "0.11.1", default-features = false, features = ["sec1"] } +elliptic-curve = { version = "0.11.4", default-features = false, features = ["sec1"] } signature = { version = ">= 1.3.1, <1.5", default-features = false, features = ["rand-preview"] } # optional dependencies diff --git a/ecdsa/src/hazmat.rs b/ecdsa/src/hazmat.rs index 95801bd7..2db9b5a7 100644 --- a/ecdsa/src/hazmat.rs +++ b/ecdsa/src/hazmat.rs @@ -16,7 +16,7 @@ use { core::borrow::Borrow, elliptic_curve::{ group::Curve as _, - ops::{Invert, Reduce}, + ops::{Invert, LinearCombination, Reduce}, AffineArithmetic, AffineXCoordinate, Field, FieldBytes, Group, ProjectiveArithmetic, Scalar, ScalarArithmetic, }, @@ -112,7 +112,7 @@ where #[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))] pub trait VerifyPrimitive: AffineXCoordinate + Copy + Sized where - C: PrimeCurve + AffineArithmetic + ProjectiveArithmetic, + C: PrimeCurve + AffineArithmetic + LinearCombination + ProjectiveArithmetic, Scalar: Reduce, SignatureSize: ArrayLength, { @@ -127,10 +127,14 @@ where let s_inv = Option::>::from(s.invert()).ok_or_else(Error::new)?; let u1 = z * s_inv; let u2 = *r * s_inv; - - let x = ((C::ProjectivePoint::generator() * u1) + (C::ProjectivePoint::from(*self) * u2)) - .to_affine() - .x(); + let x = C::lincomb( + &C::ProjectivePoint::generator(), + &u1, + &C::ProjectivePoint::from(*self), + &u2, + ) + .to_affine() + .x(); if Scalar::::from_be_bytes_reduced(x) == *r { Ok(()) diff --git a/ecdsa/src/verify.rs b/ecdsa/src/verify.rs index fd05b9ef..16c67915 100644 --- a/ecdsa/src/verify.rs +++ b/ecdsa/src/verify.rs @@ -7,7 +7,7 @@ use crate::{ use core::{cmp::Ordering, fmt::Debug}; use elliptic_curve::{ generic_array::ArrayLength, - ops::Reduce, + ops::{LinearCombination, Reduce}, sec1::{self, EncodedPoint, FromEncodedPoint, ToEncodedPoint}, AffinePoint, FieldSize, PointCompression, PrimeCurve, ProjectiveArithmetic, PublicKey, Scalar, }; @@ -79,7 +79,7 @@ impl Copy for VerifyingKey where C: PrimeCurve + ProjectiveArithmetic {} impl DigestVerifier> for VerifyingKey where - C: PrimeCurve + ProjectiveArithmetic, + C: PrimeCurve + ProjectiveArithmetic + LinearCombination, D: Digest>, AffinePoint: VerifyPrimitive, Scalar: Reduce, @@ -93,7 +93,7 @@ where impl Verifier> for VerifyingKey where - C: PrimeCurve + ProjectiveArithmetic + DigestPrimitive, + C: PrimeCurve + ProjectiveArithmetic + DigestPrimitive + LinearCombination, C::Digest: Digest>, AffinePoint: VerifyPrimitive, Scalar: Reduce,