Skip to content

Commit a7c3282

Browse files
committed
Bump elliptic-curve to v0.11.0-pre
Sourced from git. This includes transitive updates to ff and group v0.11. A major notable change of these updates is switching several APIs to use `subtle::Choice` and `subtle::CtOption` instead of `bool`/`Option`. Fortunately, the crates in this repo are already written with constant-time implementations internally, so this was an easy change.
1 parent eda5854 commit a7c3282

File tree

16 files changed

+73
-120
lines changed

16 files changed

+73
-120
lines changed

Cargo.lock

Lines changed: 36 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ members = [
77
"p256",
88
"p384",
99
]
10+
11+
[patch.crates-io]
12+
ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
13+
elliptic-curve = { git = "https://github.com/RustCrypto/traits.git" }

bp256/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories = ["cryptography", "no-std"]
1212
keywords = ["brainpool", "crypto", "ecc"]
1313

1414
[dependencies]
15-
elliptic-curve = { version = "0.10", default-features = false, features = ["hazmat"] }
15+
elliptic-curve = { version = "=0.11.0-pre", default-features = false, features = ["hazmat"] }
1616

1717
# optional dependencies
1818
ecdsa = { version = "0.12", optional = true, default-features = false, features = ["der"] }

bp384/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories = ["cryptography", "no-std"]
1212
keywords = ["brainpool", "crypto", "ecc"]
1313

1414
[dependencies]
15-
elliptic-curve = { version = "0.10", default-features = false, features = ["hazmat"] }
15+
elliptic-curve = { version = "=0.11.0-pre", default-features = false, features = ["hazmat"] }
1616

1717
# optional dependencies
1818
ecdsa = { version = "0.12", optional = true, default-features = false, features = ["der"] }

k256/Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,18 @@ keywords = ["bitcoin", "crypto", "ecc", "ethereum", "secp256k1"]
1818

1919
[dependencies]
2020
cfg-if = "1.0"
21-
elliptic-curve = { version = "0.10.6", default-features = false, features = ["hazmat"] }
21+
elliptic-curve = { version = "=0.11.0-pre", default-features = false, features = ["hazmat"] }
2222

2323
# optional dependencies
24+
ecdsa-core = { version = "=0.13.0-pre", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
2425
hex-literal = { version = "0.3", optional = true }
2526
sha2 = { version = "0.9", optional = true, default-features = false }
2627
sha3 = { version = "0.9", optional = true, default-features = false }
2728

28-
[dependencies.ecdsa-core]
29-
version = "0.12.1"
30-
package = "ecdsa"
31-
optional = true
32-
default-features = false
33-
features = ["der"]
34-
3529
[dev-dependencies]
3630
blobby = "0.3"
3731
criterion = "0.3"
38-
ecdsa-core = { version = "0.12.1", package = "ecdsa", default-features = false, features = ["dev"] }
32+
ecdsa-core = { version = "=0.13.0-pre", package = "ecdsa", default-features = false, features = ["dev"] }
3933
hex-literal = "0.3"
4034
num-bigint = "0.4"
4135
num-traits = "0.2"

k256/src/arithmetic/scalar.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ impl Field for Scalar {
9595
Scalar::one()
9696
}
9797

98-
fn is_zero(&self) -> bool {
99-
self.0.is_zero().into()
100-
}
101-
10298
#[must_use]
10399
fn square(&self) -> Self {
104100
Scalar::square(self)
@@ -493,16 +489,16 @@ impl PrimeField for Scalar {
493489
///
494490
/// Returns None if the byte array does not contain a big-endian integer in the range
495491
/// [0, p).
496-
fn from_repr(bytes: FieldBytes) -> Option<Self> {
497-
ScalarImpl::from_bytes(bytes.as_ref()).map(Self).into()
492+
fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
493+
ScalarImpl::from_bytes(bytes.as_ref()).map(Self)
498494
}
499495

500496
fn to_repr(&self) -> FieldBytes {
501497
self.to_bytes()
502498
}
503499

504-
fn is_odd(&self) -> bool {
505-
self.0.is_odd().into()
500+
fn is_odd(&self) -> Choice {
501+
self.0.is_odd()
506502
}
507503

508504
fn multiplicative_generator() -> Self {
@@ -707,7 +703,7 @@ impl Scalar {
707703
// TODO: pre-generate several scalars to bring the probability of non-constant-timeness down?
708704
loop {
709705
rng.fill_bytes(&mut bytes);
710-
if let Some(scalar) = Scalar::from_repr(bytes) {
706+
if let Some(scalar) = Scalar::from_repr(bytes).into() {
711707
return scalar;
712708
}
713709
}

k256/src/ecdsa.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ pub use self::{sign::SigningKey, verify::VerifyingKey};
8383

8484
use crate::Secp256k1;
8585

86-
#[cfg(feature = "ecdsa")]
87-
use crate::NonZeroScalar;
88-
#[cfg(feature = "ecdsa")]
89-
use elliptic_curve::generic_array::GenericArray;
90-
9186
/// ECDSA/secp256k1 signature (fixed-size)
9287
pub type Signature = ecdsa_core::Signature<Secp256k1>;
9388

@@ -100,20 +95,6 @@ impl ecdsa_core::hazmat::DigestPrimitive for Secp256k1 {
10095
type Digest = sha2::Sha256;
10196
}
10297

103-
/// Validate that the scalars of an ECDSA signature are modulo the order
104-
#[cfg(feature = "ecdsa")]
105-
fn check_scalars(signature: &Signature) -> Result<(), Error> {
106-
let (r_bytes, s_bytes) = signature.as_ref().split_at(32);
107-
let r_valid = NonZeroScalar::from_repr(GenericArray::clone_from_slice(r_bytes)).is_some();
108-
let s_valid = NonZeroScalar::from_repr(GenericArray::clone_from_slice(s_bytes)).is_some();
109-
110-
if r_valid && s_valid {
111-
Ok(())
112-
} else {
113-
Err(Error::new())
114-
}
115-
}
116-
11798
#[cfg(all(test, feature = "ecdsa", feature = "arithmetic"))]
11899
mod tests {
119100
mod wycheproof {
@@ -165,7 +146,7 @@ mod tests {
165146
Err(_) => return Some("failed to parse signature ASN.1"),
166147
};
167148

168-
sig.normalize_s().unwrap();
149+
sig.normalize_s();
169150

170151
match verifying_key.verify(msg, &sig) {
171152
Ok(_) if pass => None,

k256/src/ecdsa/normalize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod tests {
4747
]).unwrap();
4848

4949
let mut sig_normalized = sig_hi;
50-
assert!(sig_normalized.normalize_s().unwrap());
50+
assert!(sig_normalized.normalize_s());
5151
assert_eq!(sig_lo, sig_normalized);
5252
}
5353

@@ -62,7 +62,7 @@ mod tests {
6262
]).unwrap();
6363

6464
let mut sig_normalized = sig;
65-
assert!(!sig_normalized.normalize_s().unwrap());
65+
assert!(!sig_normalized.normalize_s());
6666
assert_eq!(sig, sig_normalized);
6767
}
6868
}

k256/src/ecdsa/recoverable.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ use crate::{
4747
signature::{digest::Digest, DigestVerifier},
4848
VerifyingKey,
4949
},
50-
elliptic_curve::{
51-
consts::U32, generic_array::GenericArray, ops::Invert, subtle::Choice,
52-
weierstrass::DecompressPoint,
53-
},
50+
elliptic_curve::{consts::U32, ops::Invert, subtle::Choice, weierstrass::DecompressPoint},
5451
lincomb, AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar,
5552
};
5653

@@ -81,9 +78,6 @@ impl Signature {
8178
/// This is an "unchecked" conversion and assumes the provided [`Id`]
8279
/// is valid for this signature.
8380
pub fn new(signature: &super::Signature, recovery_id: Id) -> Result<Self, Error> {
84-
#[cfg(feature = "ecdsa")]
85-
super::check_scalars(signature)?;
86-
8781
let mut bytes = [0u8; SIZE];
8882
bytes[..64].copy_from_slice(signature.as_ref());
8983
bytes[64] = recovery_id.0;
@@ -127,7 +121,7 @@ impl Signature {
127121
D: Clone + Digest<OutputSize = U32>,
128122
{
129123
let mut signature = *signature;
130-
signature.normalize_s()?;
124+
signature.normalize_s();
131125

132126
for recovery_id in 0..=1 {
133127
if let Ok(recoverable_signature) = Signature::new(&signature, Id(recovery_id)) {
@@ -198,16 +192,16 @@ impl Signature {
198192
#[cfg(feature = "ecdsa")]
199193
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
200194
pub fn r(&self) -> NonZeroScalar {
201-
NonZeroScalar::from_repr(GenericArray::clone_from_slice(&self.bytes[..32]))
202-
.unwrap_or_else(|| unreachable!("r-component ensured valid in constructor"))
195+
NonZeroScalar::try_from(&self.bytes[..32])
196+
.expect("r-component ensured valid in constructor")
203197
}
204198

205199
/// Parse the `s` component of this signature to a [`NonZeroScalar`]
206200
#[cfg(feature = "ecdsa")]
207201
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
208202
pub fn s(&self) -> NonZeroScalar {
209-
NonZeroScalar::from_repr(GenericArray::clone_from_slice(&self.bytes[32..64]))
210-
.unwrap_or_else(|| unreachable!("s-component ensured valid in constructor"))
203+
NonZeroScalar::try_from(&self.bytes[32..64])
204+
.expect("s-component ensured valid in constructor")
211205
}
212206
}
213207

k256/src/ecdsa/sign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl RecoverableSignPrimitive<Secp256k1> for Scalar {
192192

193193
let mut signature = Signature::from_scalars(r, s)?;
194194
let is_r_odd = bool::from(R.y.normalize().is_odd());
195-
let is_s_high = signature.normalize_s()?;
195+
let is_s_high = signature.normalize_s();
196196
Ok((signature, is_r_odd ^ is_s_high))
197197
}
198198
}

0 commit comments

Comments
 (0)