Skip to content

Commit

Permalink
ecdsa: use LinearCombination trait (#417)
Browse files Browse the repository at this point in the history
Use the trait introduced in RustCrypto/traits#833 to implement ECDSA
verification.
  • Loading branch information
tarcieri committed Dec 4, 2021
1 parent bfb896f commit 8963704
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 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 ecdsa/Cargo.toml
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions ecdsa/src/hazmat.rs
Expand Up @@ -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,
},
Expand Down Expand Up @@ -112,7 +112,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait VerifyPrimitive<C>: AffineXCoordinate<C> + Copy + Sized
where
C: PrimeCurve + AffineArithmetic<AffinePoint = Self> + ProjectiveArithmetic,
C: PrimeCurve + AffineArithmetic<AffinePoint = Self> + LinearCombination + ProjectiveArithmetic,
Scalar<C>: Reduce<C::UInt>,
SignatureSize<C>: ArrayLength<u8>,
{
Expand All @@ -127,10 +127,14 @@ where
let s_inv = Option::<Scalar<C>>::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::<C>::from_be_bytes_reduced(x) == *r {
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions ecdsa/src/verify.rs
Expand Up @@ -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,
};
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<C> Copy for VerifyingKey<C> where C: PrimeCurve + ProjectiveArithmetic {}

impl<C, D> DigestVerifier<D, Signature<C>> for VerifyingKey<C>
where
C: PrimeCurve + ProjectiveArithmetic,
C: PrimeCurve + ProjectiveArithmetic + LinearCombination,
D: Digest<OutputSize = FieldSize<C>>,
AffinePoint<C>: VerifyPrimitive<C>,
Scalar<C>: Reduce<C::UInt>,
Expand All @@ -93,7 +93,7 @@ where

impl<C> Verifier<Signature<C>> for VerifyingKey<C>
where
C: PrimeCurve + ProjectiveArithmetic + DigestPrimitive,
C: PrimeCurve + ProjectiveArithmetic + DigestPrimitive + LinearCombination,
C::Digest: Digest<OutputSize = FieldSize<C>>,
AffinePoint<C>: VerifyPrimitive<C>,
Scalar<C>: Reduce<C::UInt>,
Expand Down

0 comments on commit 8963704

Please sign in to comment.