Skip to content

Commit

Permalink
Bump ecdsa crate to v0.15.0-pre (#674)
Browse files Browse the repository at this point in the history
This (unpublished) prerelease of the `ecdsa` crate impl's the
prospective `signature` v2 API from: RustCrypto/traits#1141
  • Loading branch information
tarcieri committed Nov 6, 2022
1 parent 8a7c9f7 commit ec35dc7
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 126 deletions.
16 changes: 7 additions & 9 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 @@ -12,3 +12,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
2 changes: 1 addition & 1 deletion bp256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = "1.57"
elliptic-curve = { version = "0.12", default-features = false, features = ["hazmat", "sec1"] }

# optional dependencies
ecdsa = { version = "0.14", optional = true, default-features = false, features = ["der"] }
ecdsa = { version = "=0.15.0-pre", optional = true, default-features = false, features = ["der"] }
sha2 = { version = "0.10", optional = true, default-features = false }

[features]
Expand Down
2 changes: 1 addition & 1 deletion bp384/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = "1.57"
elliptic-curve = { version = "0.12", default-features = false, features = ["hazmat", "sec1"] }

# optional dependencies
ecdsa = { version = "0.14", optional = true, default-features = false, features = ["der"] }
ecdsa = { version = "=0.15.0-pre", optional = true, default-features = false, features = ["der"] }
sha2 = { version = "0.10", optional = true, default-features = false }

[features]
Expand Down
6 changes: 3 additions & 3 deletions k256/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "k256"
version = "0.11.6"
version = "0.12.0-pre"
description = """
secp256k1 elliptic curve library written in pure Rust with support for ECDSA
signing/verification (including Ethereum-style signatures with public-key
Expand All @@ -22,7 +22,7 @@ cfg-if = "1.0"
elliptic-curve = { version = "0.12.3", default-features = false, features = ["hazmat", "sec1"] }

# optional dependencies
ecdsa-core = { version = "0.14.6", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
ecdsa-core = { version = "=0.15.0-pre", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
hex-literal = { version = "0.3", optional = true }
serdect = { version = "0.1", optional = true, default-features = false }
sha2 = { version = "0.10", optional = true, default-features = false }
Expand All @@ -31,7 +31,7 @@ sha3 = { version = "0.10", optional = true, default-features = false }
[dev-dependencies]
blobby = "0.3"
criterion = "0.4"
ecdsa-core = { version = "0.14", package = "ecdsa", default-features = false, features = ["dev"] }
ecdsa-core = { version = "=0.15.0-pre", package = "ecdsa", default-features = false, features = ["dev"] }
hex-literal = "0.3"
num-bigint = "0.4"
num-traits = "0.2"
Expand Down
10 changes: 5 additions & 5 deletions k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use elliptic_curve::{
generic_array::arr,
group::ff::{Field, PrimeField},
ops::{Reduce, ReduceNonZero},
rand_core::{CryptoRng, RngCore},
rand_core::{CryptoRngCore, RngCore},
subtle::{
Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess,
CtOption,
Expand Down Expand Up @@ -184,7 +184,7 @@ impl Scalar {
}

/// Returns a (nearly) uniformly-random scalar, generated in constant time.
pub fn generate_biased(mut rng: impl CryptoRng + RngCore) -> Self {
pub fn generate_biased(rng: &mut impl CryptoRngCore) -> Self {
// We reduce a random 512-bit value into a 256-bit field, which results in a
// negligible bias from the uniform distribution, but the process is constant-time.
let mut buf = [0u8; 64];
Expand All @@ -194,7 +194,7 @@ impl Scalar {

/// Returns a uniformly-random scalar, generated using rejection sampling.
// TODO(tarcieri): make this a `CryptoRng` when `ff` allows it
pub fn generate_vartime(mut rng: impl RngCore) -> Self {
pub fn generate_vartime(rng: &mut impl RngCore) -> Self {
let mut bytes = FieldBytes::default();

// TODO: pre-generate several scalars to bring the probability of non-constant-timeness down?
Expand Down Expand Up @@ -223,7 +223,7 @@ impl Scalar {
}

impl Field for Scalar {
fn random(rng: impl RngCore) -> Self {
fn random(mut rng: impl RngCore) -> Self {
// Uses rejection sampling as the default random generation method,
// which produces a uniformly random distribution of scalars.
//
Expand All @@ -233,7 +233,7 @@ impl Field for Scalar {
//
// With an unbiased RNG, the probability of failing to complete after 4
// iterations is vanishingly small.
Self::generate_vartime(rng)
Self::generate_vartime(&mut rng)
}

fn zero() -> Self {
Expand Down
13 changes: 6 additions & 7 deletions k256/src/ecdsa/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
#[cfg(all(test, feature = "ecdsa"))]
mod tests {
use crate::ecdsa::Signature;
use ecdsa_core::signature::Signature as _;

// Test vectors generated using rust-secp256k1
#[test]
#[rustfmt::skip]
fn normalize_s_high() {
let sig_hi = Signature::from_bytes(&[
let sig_hi = Signature::try_from([
0x20, 0xc0, 0x1a, 0x91, 0x0e, 0xbb, 0x26, 0x10,
0xaf, 0x2d, 0x76, 0x3f, 0xa0, 0x9b, 0x3b, 0x30,
0x92, 0x3c, 0x8e, 0x40, 0x8b, 0x11, 0xdf, 0x2c,
Expand All @@ -20,9 +19,9 @@ mod tests {
0x61, 0x7d, 0x13, 0x57, 0xf4, 0xd5, 0x56, 0x41,
0x09, 0x0a, 0x48, 0xf2, 0x01, 0xe9, 0xb9, 0x59,
0xc4, 0x8f, 0x6f, 0x6b, 0xec, 0x6f, 0x93, 0x8f,
]).unwrap();
].as_slice()).unwrap();

let sig_lo = Signature::from_bytes(&[
let sig_lo = Signature::try_from([
0x20, 0xc0, 0x1a, 0x91, 0x0e, 0xbb, 0x26, 0x10,
0xaf, 0x2d, 0x76, 0x3f, 0xa0, 0x9b, 0x3b, 0x30,
0x92, 0x3c, 0x8e, 0x40, 0x8b, 0x11, 0xdf, 0x2c,
Expand All @@ -31,7 +30,7 @@ mod tests {
0x9e, 0x82, 0xec, 0xa8, 0x0b, 0x2a, 0xa9, 0xbd,
0xb1, 0xa4, 0x93, 0xf4, 0xad, 0x5e, 0xe6, 0xe1,
0xfb, 0x42, 0xef, 0x20, 0xe3, 0xc6, 0xad, 0xb2,
]).unwrap();
].as_slice()).unwrap();

let sig_normalized = sig_hi.normalize_s().unwrap();
assert_eq!(sig_lo, sig_normalized);
Expand All @@ -40,12 +39,12 @@ mod tests {
#[test]
fn normalize_s_low() {
#[rustfmt::skip]
let sig = Signature::from_bytes(&[
let sig = Signature::try_from([
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]).unwrap();
].as_slice()).unwrap();

assert_eq!(sig.normalize_s(), None);
}
Expand Down
20 changes: 14 additions & 6 deletions k256/src/ecdsa/recoverable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! ```

use core::fmt::{self, Debug};
use ecdsa_core::{signature::Signature as _, Error, Result};
use ecdsa_core::{Error, Result};
use elliptic_curve::subtle::Choice;

#[cfg(feature = "ecdsa")]
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Signature {
/// is valid for this signature.
pub fn new(signature: &super::Signature, recovery_id: Id) -> Result<Self> {
let mut bytes = [0u8; SIZE];
bytes[..64].copy_from_slice(signature.as_ref());
bytes[..64].copy_from_slice(&signature.to_bytes());
bytes[64] = recovery_id.0;
Ok(Self { bytes })
}
Expand Down Expand Up @@ -223,9 +223,11 @@ impl Signature {
}
}

impl ecdsa_core::signature::Signature for Signature {
fn from_bytes(bytes: &[u8]) -> Result<Self> {
bytes.try_into()
impl ecdsa_core::signature::SignatureEncoding for Signature {
type Repr = [u8; SIZE];

fn to_bytes(&self) -> Self::Repr {
self.bytes
}
}

Expand All @@ -241,6 +243,12 @@ impl Debug for Signature {
}
}

impl From<Signature> for [u8; SIZE] {
fn from(signature: Signature) -> [u8; SIZE] {
signature.bytes
}
}

impl TryFrom<&[u8]> for Signature {
type Error = Error;

Expand All @@ -257,7 +265,7 @@ impl TryFrom<&[u8]> for Signature {

impl From<Signature> for super::Signature {
fn from(sig: Signature) -> Self {
Self::from_bytes(&sig.bytes[..64]).unwrap()
Self::try_from(&sig.bytes[..64]).unwrap()
}
}

Expand Down
20 changes: 7 additions & 13 deletions k256/src/ecdsa/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use ecdsa_core::{
signature::{
digest::{Digest, FixedOutput},
hazmat::PrehashSigner,
DigestSigner, Keypair, RandomizedDigestSigner,
DigestSigner, KeypairRef, RandomizedDigestSigner,
},
};
use elliptic_curve::{
bigint::U256,
consts::U32,
ops::{Invert, Reduce},
rand_core::{CryptoRng, RngCore},
rand_core::CryptoRngCore,
subtle::{Choice, ConstantTimeEq, CtOption},
zeroize::{Zeroize, ZeroizeOnDrop},
IsHigh,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct SigningKey {

impl SigningKey {
/// Generate a cryptographically random [`SigningKey`].
pub fn random(rng: impl CryptoRng + RngCore) -> Self {
pub fn random(rng: &mut impl CryptoRngCore) -> Self {
NonZeroScalar::random(rng).into()
}

Expand Down Expand Up @@ -92,7 +92,7 @@ where
S: PrehashSignature,
Self: RandomizedDigestSigner<S::Digest, S>,
{
fn try_sign_with_rng(&self, rng: impl CryptoRng + RngCore, msg: &[u8]) -> signature::Result<S> {
fn try_sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> signature::Result<S> {
self.try_sign_digest_with_rng(rng, S::Digest::new_with_prefix(msg))
}
}
Expand All @@ -117,13 +117,7 @@ where

#[cfg(feature = "sha256")]
#[cfg_attr(docsrs, doc(cfg(feature = "sha256")))]
impl Keypair<Signature> for SigningKey {
type VerifyingKey = VerifyingKey;
}

#[cfg(feature = "keccak256")]
#[cfg_attr(docsrs, doc(cfg(feature = "keccak256")))]
impl Keypair<recoverable::Signature> for SigningKey {
impl KeypairRef for SigningKey {
type VerifyingKey = VerifyingKey;
}

Expand Down Expand Up @@ -159,7 +153,7 @@ where
{
fn try_sign_digest_with_rng(
&self,
rng: impl CryptoRng + RngCore,
rng: &mut impl CryptoRngCore,
digest: D,
) -> Result<Signature, Error> {
RandomizedDigestSigner::<D, recoverable::Signature>::try_sign_digest_with_rng(
Expand All @@ -175,7 +169,7 @@ where
{
fn try_sign_digest_with_rng(
&self,
mut rng: impl CryptoRng + RngCore,
rng: &mut impl CryptoRngCore,
msg_digest: D,
) -> Result<recoverable::Signature, Error> {
let mut ad = FieldBytes::default();
Expand Down
Loading

0 comments on commit ec35dc7

Please sign in to comment.