Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io.elliptic-curve]
git = "https://github.com/RustCrypto/traits.git"
20 changes: 10 additions & 10 deletions ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub trait SignPrimitive<C>:
+ Into<FieldBytes<C>>
+ IsHigh
+ PrimeField<Repr = FieldBytes<C>>
+ Reduce<C::Uint>
+ Reduce<C::Uint, Bytes = FieldBytes<C>>
+ Sized
where
C: PrimeCurve + CurveArithmetic + CurveArithmetic<Scalar = Self>,
Expand Down Expand Up @@ -84,7 +84,7 @@ where
return Err(Error::new());
}

let z = <Self as Reduce<C::Uint>>::reduce(C::decode_field_bytes(z));
let z = <Self as Reduce<C::Uint>>::reduce_bytes(z);

// Compute scalar inversion of 𝑘
let k_inv = Option::<Scalar<C>>::from(k.invert()).ok_or_else(Error::new)?;
Expand All @@ -94,7 +94,7 @@ where

// Lift x-coordinate of 𝑹 (element of base field) into a serialized big
// integer, then reduce it into an element of the scalar field
let r = Self::reduce(C::decode_field_bytes(&R.x()));
let r = Self::reduce_bytes(&R.x());

// Compute 𝒔 as a signature over 𝒓 and 𝒛.
let s = k_inv * (z + (r * self));
Expand Down Expand Up @@ -132,14 +132,15 @@ where
Self: From<ScalarPrimitive<C>>,
D: Digest + BlockSizeUser + FixedOutput<OutputSize = FieldBytesSize<C>> + FixedOutputReset,
{
let k = rfc6979::generate_k::<D, FieldBytesSize<C>>(
let k = Scalar::<C>::from_repr(rfc6979::generate_k::<D, _>(
&self.to_repr(),
&C::encode_field_bytes(&C::ORDER),
z,
ad,
);
let k = ScalarPrimitive::<C>::new(C::decode_field_bytes(&k)).unwrap();
self.try_sign_prehashed::<Self>(k.into(), z)
))
.unwrap();

self.try_sign_prehashed::<Self>(k, z)
}
}

Expand All @@ -152,7 +153,6 @@ where
pub trait VerifyPrimitive<C>: AffineXCoordinate<FieldRepr = FieldBytes<C>> + Copy + Sized
where
C: PrimeCurve + CurveArithmetic<AffinePoint = Self> + CurveArithmetic,
Scalar<C>: Reduce<C::Uint>,
SignatureSize<C>: ArrayLength<u8>,
{
/// Verify the prehashed message against the provided signature
Expand All @@ -163,7 +163,7 @@ where
/// CRYPTOGRAPHICALLY SECURE DIGEST ALGORITHM!!!
/// - `sig`: signature to be verified against the key and message
fn verify_prehashed(&self, z: &FieldBytes<C>, sig: &Signature<C>) -> Result<()> {
let z = Scalar::<C>::reduce(C::decode_field_bytes(z));
let z = Scalar::<C>::reduce_bytes(z);
let (r, s) = sig.split_scalars();
let s_inv = *s.invert();
let u1 = z * s_inv;
Expand All @@ -177,7 +177,7 @@ where
.to_affine()
.x();

if *r == Scalar::<C>::reduce(C::decode_field_bytes(&x)) {
if *r == Scalar::<C>::reduce_bytes(&x) {
Ok(())
} else {
Err(Error::new())
Expand Down
22 changes: 7 additions & 15 deletions ecdsa/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use {
use {
crate::{hazmat::VerifyPrimitive, VerifyingKey},
elliptic_curve::{
ops::LinearCombination,
ops::{LinearCombination, Reduce},
point::DecompressPoint,
sec1::{self, FromEncodedPoint, ToEncodedPoint},
AffinePoint, FieldBytesSize, Group, PrimeField, ProjectivePoint,
Expand All @@ -28,9 +28,7 @@ use {
Signature, SignatureSize,
},
elliptic_curve::{
generic_array::ArrayLength,
ops::{Invert, Reduce},
CurveArithmetic, PrimeCurve, Scalar,
generic_array::ArrayLength, ops::Invert, CurveArithmetic, PrimeCurve, Scalar,
},
signature::digest::Digest,
};
Expand Down Expand Up @@ -103,7 +101,6 @@ impl RecoveryId {
AffinePoint<C>:
DecompressPoint<C> + FromEncodedPoint<C> + ToEncodedPoint<C> + VerifyPrimitive<C>,
FieldBytesSize<C>: sec1::ModulusSize,
Scalar<C>: Reduce<C::Uint>,
SignatureSize<C>: ArrayLength<u8>,
{
Self::trial_recovery_from_digest(verifying_key, C::Digest::new_with_prefix(msg), signature)
Expand All @@ -123,7 +120,6 @@ impl RecoveryId {
AffinePoint<C>:
DecompressPoint<C> + FromEncodedPoint<C> + ToEncodedPoint<C> + VerifyPrimitive<C>,
FieldBytesSize<C>: sec1::ModulusSize,
Scalar<C>: Reduce<C::Uint>,
SignatureSize<C>: ArrayLength<u8>,
{
Self::trial_recovery_from_prehash(verifying_key, &digest.finalize(), signature)
Expand All @@ -142,7 +138,6 @@ impl RecoveryId {
AffinePoint<C>:
DecompressPoint<C> + FromEncodedPoint<C> + ToEncodedPoint<C> + VerifyPrimitive<C>,
FieldBytesSize<C>: sec1::ModulusSize,
Scalar<C>: Reduce<C::Uint>,
SignatureSize<C>: ArrayLength<u8>,
{
for id in 0..=Self::MAX {
Expand Down Expand Up @@ -177,7 +172,7 @@ impl From<RecoveryId> for u8 {
impl<C> SigningKey<C>
where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + Reduce<C::Uint> + SignPrimitive<C>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
/// Sign the given message prehash, returning a signature and recovery ID.
Expand Down Expand Up @@ -210,7 +205,7 @@ impl<C, D> DigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
D: Digest,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + Reduce<C::Uint> + SignPrimitive<C>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign_digest(&self, msg_digest: D) -> Result<(Signature<C>, RecoveryId)> {
Expand All @@ -222,7 +217,7 @@ where
impl<C> PrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + Reduce<C::Uint> + SignPrimitive<C>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn sign_prehash(&self, prehash: &[u8]) -> Result<(Signature<C>, RecoveryId)> {
Expand All @@ -234,7 +229,7 @@ where
impl<C> Signer<(Signature<C>, RecoveryId)> for SigningKey<C>
where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + Reduce<C::Uint> + SignPrimitive<C>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign(&self, msg: &[u8]) -> Result<(Signature<C>, RecoveryId)> {
Expand All @@ -249,7 +244,6 @@ where
AffinePoint<C>:
DecompressPoint<C> + FromEncodedPoint<C> + ToEncodedPoint<C> + VerifyPrimitive<C>,
FieldBytesSize<C>: sec1::ModulusSize,
Scalar<C>: Reduce<C::Uint>,
SignatureSize<C>: ArrayLength<u8>,
{
/// Recover a [`VerifyingKey`] from the given message, signature, and
Expand Down Expand Up @@ -290,9 +284,7 @@ where
recovery_id: RecoveryId,
) -> Result<Self> {
let (r, s) = signature.split_scalars();
let z = <Scalar<C> as Reduce<C::Uint>>::reduce(C::decode_field_bytes(&bits2field::<C>(
prehash,
)?));
let z = <Scalar<C> as Reduce<C::Uint>>::reduce_bytes(&bits2field::<C>(prehash)?);
let R = AffinePoint::<C>::decompress(&r.to_repr(), u8::from(recovery_id.is_y_odd()).into());

if R.is_none().into() {
Expand Down
Loading