Skip to content

Commit 9de9d49

Browse files
committed
ecdsa: bump elliptic-curve to v0.13.0-pre.1
Bumps the `elliptic-curve` crate to the latest prerelease on crates.io. Notably this includes changes to how `FieldBytes` is encoded made with the goal of supporting elliptic curves with unusual moduli such as P-224 and P-521: RustCrypto/traits#1220
1 parent bd7dfd1 commit 9de9d49

9 files changed

Lines changed: 107 additions & 96 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ members = [
99

1010
[profile.dev]
1111
opt-level = 2
12-
13-
[patch.crates-io.elliptic-curve]
14-
git = "https://github.com/RustCrypto/traits.git"

ecdsa/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ edition = "2021"
1616
rust-version = "1.61"
1717

1818
[dependencies]
19-
elliptic-curve = { version = "=0.13.0-pre", default-features = false, features = ["digest", "sec1"] }
19+
elliptic-curve = { version = "=0.13.0-pre.1", default-features = false, features = ["digest", "sec1"] }
2020
signature = { version = "2.0, <2.1", default-features = false, features = ["rand_core"] }
2121

2222
# optional dependencies
@@ -25,7 +25,7 @@ rfc6979 = { version = "=0.4.0-pre", optional = true, path = "../rfc6979" }
2525
serdect = { version = "0.1", optional = true, default-features = false, features = ["alloc"] }
2626

2727
[dev-dependencies]
28-
elliptic-curve = { version = "=0.13.0-pre", default-features = false, features = ["dev"] }
28+
elliptic-curve = { version = "=0.13.0-pre.1", default-features = false, features = ["dev"] }
2929
hex-literal = "0.3"
3030
sha2 = { version = "0.10", default-features = false }
3131

ecdsa/src/der.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use elliptic_curve::{
1010
bigint::Integer,
1111
consts::U9,
1212
generic_array::{ArrayLength, GenericArray},
13-
FieldSize, PrimeCurve,
13+
FieldBytesSize, PrimeCurve,
1414
};
1515

1616
#[cfg(feature = "alloc")]
@@ -39,7 +39,7 @@ use serdect::serde::{de, ser, Deserialize, Serialize};
3939
pub type MaxOverhead = U9;
4040

4141
/// Maximum size of an ASN.1 DER encoded signature for the given elliptic curve.
42-
pub type MaxSize<C> = <<FieldSize<C> as Add>::Output as Add<MaxOverhead>>::Output;
42+
pub type MaxSize<C> = <<FieldBytesSize<C> as Add>::Output as Add<MaxOverhead>>::Output;
4343

4444
/// Byte array containing a serialized ASN.1 signature
4545
type SignatureBytes<C> = GenericArray<u8, MaxSize<C>>;
@@ -51,7 +51,7 @@ pub struct Signature<C>
5151
where
5252
C: PrimeCurve,
5353
MaxSize<C>: ArrayLength<u8>,
54-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
54+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
5555
{
5656
/// ASN.1 DER-encoded signature data
5757
bytes: SignatureBytes<C>,
@@ -68,7 +68,7 @@ impl<C> Signature<C>
6868
where
6969
C: PrimeCurve,
7070
MaxSize<C>: ArrayLength<u8>,
71-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
71+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
7272
{
7373
/// Get the length of the signature in bytes
7474
pub fn len(&self) -> usize {
@@ -120,7 +120,7 @@ impl<C> AsRef<[u8]> for Signature<C>
120120
where
121121
C: PrimeCurve,
122122
MaxSize<C>: ArrayLength<u8>,
123-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
123+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
124124
{
125125
fn as_ref(&self) -> &[u8] {
126126
self.as_bytes()
@@ -131,7 +131,7 @@ impl<C> Clone for Signature<C>
131131
where
132132
C: PrimeCurve,
133133
MaxSize<C>: ArrayLength<u8>,
134-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
134+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
135135
{
136136
fn clone(&self) -> Self {
137137
Self {
@@ -146,7 +146,7 @@ impl<C> Debug for Signature<C>
146146
where
147147
C: PrimeCurve,
148148
MaxSize<C>: ArrayLength<u8>,
149-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
149+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
150150
{
151151
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152152
write!(f, "ecdsa::der::Signature<{:?}>(", C::default())?;
@@ -163,7 +163,7 @@ impl<C> From<crate::Signature<C>> for Signature<C>
163163
where
164164
C: PrimeCurve,
165165
MaxSize<C>: ArrayLength<u8>,
166-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
166+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
167167
{
168168
fn from(sig: crate::Signature<C>) -> Signature<C> {
169169
sig.to_der()
@@ -174,7 +174,7 @@ impl<C> TryFrom<&[u8]> for Signature<C>
174174
where
175175
C: PrimeCurve,
176176
MaxSize<C>: ArrayLength<u8>,
177-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
177+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
178178
{
179179
type Error = Error;
180180

@@ -207,7 +207,7 @@ impl<C> TryFrom<Signature<C>> for crate::Signature<C>
207207
where
208208
C: PrimeCurve,
209209
MaxSize<C>: ArrayLength<u8>,
210-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
210+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
211211
{
212212
type Error = Error;
213213

@@ -226,7 +226,7 @@ impl<C> From<Signature<C>> for Box<[u8]>
226226
where
227227
C: PrimeCurve,
228228
MaxSize<C>: ArrayLength<u8>,
229-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
229+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
230230
{
231231
fn from(signature: Signature<C>) -> Box<[u8]> {
232232
signature.to_vec().into_boxed_slice()
@@ -238,7 +238,7 @@ impl<C> SignatureEncoding for Signature<C>
238238
where
239239
C: PrimeCurve,
240240
MaxSize<C>: ArrayLength<u8>,
241-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
241+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
242242
{
243243
type Repr = Box<[u8]>;
244244

@@ -252,7 +252,7 @@ impl<C> Serialize for Signature<C>
252252
where
253253
C: PrimeCurve,
254254
MaxSize<C>: ArrayLength<u8>,
255-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
255+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
256256
{
257257
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
258258
where
@@ -267,7 +267,7 @@ impl<'de, C> Deserialize<'de> for Signature<C>
267267
where
268268
C: PrimeCurve,
269269
MaxSize<C>: ArrayLength<u8>,
270-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
270+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
271271
{
272272
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
273273
where
@@ -310,7 +310,7 @@ impl<C> signature::PrehashSignature for Signature<C>
310310
where
311311
C: PrimeCurve + crate::hazmat::DigestPrimitive,
312312
MaxSize<C>: ArrayLength<u8>,
313-
<FieldSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
313+
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
314314
{
315315
type Digest = C::Digest;
316316
}

ecdsa/src/hazmat.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ use {
2121
ff::PrimeField,
2222
group::{Curve as _, Group},
2323
ops::{Invert, LinearCombination, MulByGenerator, Reduce},
24+
point::{AffineXCoordinate, AffineYIsOdd},
25+
scalar::IsHigh,
2426
subtle::CtOption,
25-
AffineXCoordinate, AffineYIsOdd, CurveArithmetic, IsHigh, ProjectivePoint, Scalar,
27+
CurveArithmetic, ProjectivePoint, Scalar,
2628
},
2729
};
2830

2931
#[cfg(feature = "digest")]
3032
use {
31-
elliptic_curve::FieldSize,
33+
elliptic_curve::FieldBytesSize,
3234
signature::{
3335
digest::{core_api::BlockSizeUser, Digest, FixedOutput, FixedOutputReset},
3436
PrehashSignature,
@@ -39,7 +41,7 @@ use {
3941
use crate::{elliptic_curve::generic_array::ArrayLength, Signature};
4042

4143
#[cfg(feature = "rfc6979")]
42-
use elliptic_curve::{bigint::ArrayEncoding, ScalarPrimitive};
44+
use elliptic_curve::ScalarPrimitive;
4345

4446
/// Try to sign the given prehashed message using ECDSA.
4547
///
@@ -73,7 +75,7 @@ where
7375
fn try_sign_prehashed<K>(
7476
&self,
7577
k: K,
76-
z: FieldBytes<C>,
78+
z: &FieldBytes<C>,
7779
) -> Result<(Signature<C>, Option<RecoveryId>)>
7880
where
7981
K: AsRef<Self> + Invert<Output = CtOption<Self>>,
@@ -82,7 +84,7 @@ where
8284
return Err(Error::new());
8385
}
8486

85-
let z = Self::from_be_bytes_reduced(z);
87+
let z = <Self as Reduce<C::Uint>>::reduce(C::decode_field_bytes(z));
8688

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

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

9799
// Compute 𝒔 as a signature over 𝒓 and 𝒛.
98100
let s = k_inv * (z + (r * self));
@@ -128,16 +130,16 @@ where
128130
) -> Result<(Signature<C>, Option<RecoveryId>)>
129131
where
130132
Self: From<ScalarPrimitive<C>>,
131-
D: Digest + BlockSizeUser + FixedOutput<OutputSize = FieldSize<C>> + FixedOutputReset,
133+
D: Digest + BlockSizeUser + FixedOutput<OutputSize = FieldBytesSize<C>> + FixedOutputReset,
132134
{
133-
let k = C::Uint::from_be_byte_array(rfc6979::generate_k::<D, FieldSize<C>>(
135+
let k = rfc6979::generate_k::<D, FieldBytesSize<C>>(
134136
&self.to_repr(),
135-
&C::ORDER.to_be_byte_array(),
137+
&C::encode_field_bytes(&C::ORDER),
136138
&z,
137139
ad,
138-
));
139-
let k = Self::from(ScalarPrimitive::<C>::new(k).unwrap());
140-
self.try_sign_prehashed(k, z)
140+
);
141+
let k = ScalarPrimitive::<C>::new(C::decode_field_bytes(&k)).unwrap();
142+
self.try_sign_prehashed::<Self>(k.into(), &z)
141143
}
142144
}
143145

@@ -161,7 +163,7 @@ where
161163
/// CRYPTOGRAPHICALLY SECURE DIGEST ALGORITHM!!!
162164
/// - `sig`: signature to be verified against the key and message
163165
fn verify_prehashed(&self, z: FieldBytes<C>, sig: &Signature<C>) -> Result<()> {
164-
let z = Scalar::<C>::from_be_bytes_reduced(z);
166+
let z = Scalar::<C>::reduce(C::decode_field_bytes(&z));
165167
let (r, s) = sig.split_scalars();
166168
let s_inv = *s.invert();
167169
let u1 = z * s_inv;
@@ -175,7 +177,7 @@ where
175177
.to_affine()
176178
.x();
177179

178-
if Scalar::<C>::from_be_bytes_reduced(x) == *r {
180+
if *r == Scalar::<C>::reduce(C::decode_field_bytes(&x)) {
179181
Ok(())
180182
} else {
181183
Err(Error::new())
@@ -186,7 +188,7 @@ where
186188
#[cfg(feature = "digest")]
187189
fn verify_digest<D>(&self, msg_digest: D, sig: &Signature<C>) -> Result<()>
188190
where
189-
D: FixedOutput<OutputSize = FieldSize<C>>,
191+
D: FixedOutput<OutputSize = FieldBytesSize<C>>,
190192
{
191193
self.verify_prehashed(msg_digest.finalize_fixed(), sig)
192194
}
@@ -208,15 +210,15 @@ pub trait DigestPrimitive: PrimeCurve {
208210
/// elliptic curve. This is typically a member of the SHA-2 family.
209211
type Digest: BlockSizeUser
210212
+ Digest
211-
+ FixedOutput<OutputSize = FieldSize<Self>>
213+
+ FixedOutput<OutputSize = FieldBytesSize<Self>>
212214
+ FixedOutputReset;
213215
}
214216

215217
#[cfg(feature = "digest")]
216218
impl<C> PrehashSignature for Signature<C>
217219
where
218220
C: DigestPrimitive,
219-
<FieldSize<C> as core::ops::Add>::Output: ArrayLength<u8>,
221+
<FieldBytesSize<C> as core::ops::Add>::Output: ArrayLength<u8>,
220222
{
221223
type Digest = C::Digest;
222224
}

ecdsa/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use core::{
9090
use elliptic_curve::{
9191
bigint::Integer,
9292
generic_array::{sequence::Concat, ArrayLength, GenericArray},
93-
FieldBytes, FieldSize, ScalarPrimitive,
93+
FieldBytes, FieldBytesSize, ScalarPrimitive,
9494
};
9595

9696
#[cfg(feature = "alloc")]
@@ -99,14 +99,14 @@ use alloc::vec::Vec;
9999
#[cfg(feature = "arithmetic")]
100100
use {
101101
core::str,
102-
elliptic_curve::{CurveArithmetic, IsHigh, NonZeroScalar},
102+
elliptic_curve::{scalar::IsHigh, CurveArithmetic, NonZeroScalar},
103103
};
104104

105105
#[cfg(feature = "serde")]
106106
use serdect::serde::{de, ser, Deserialize, Serialize};
107107

108108
/// Size of a fixed sized signature for the given elliptic curve.
109-
pub type SignatureSize<C> = <FieldSize<C> as Add>::Output;
109+
pub type SignatureSize<C> = <FieldBytesSize<C> as Add>::Output;
110110

111111
/// Fixed-size byte array containing an ECDSA signature
112112
pub type SignatureBytes<C> = GenericArray<u8, SignatureSize<C>>;
@@ -149,7 +149,7 @@ where
149149
pub fn from_der(bytes: &[u8]) -> Result<Self>
150150
where
151151
der::MaxSize<C>: ArrayLength<u8>,
152-
<FieldSize<C> as Add>::Output: Add<der::MaxOverhead> + ArrayLength<u8>,
152+
<FieldBytesSize<C> as Add>::Output: Add<der::MaxOverhead> + ArrayLength<u8>,
153153
{
154154
der::Signature::<C>::try_from(bytes).and_then(Self::try_from)
155155
}
@@ -162,15 +162,15 @@ where
162162

163163
/// Split the signature into its `r` and `s` components, represented as bytes.
164164
pub fn split_bytes(&self) -> (FieldBytes<C>, FieldBytes<C>) {
165-
(self.r.to_be_bytes(), self.s.to_be_bytes())
165+
(self.r.to_bytes(), self.s.to_bytes())
166166
}
167167

168168
/// Serialize this signature as bytes.
169169
pub fn to_bytes(&self) -> SignatureBytes<C> {
170170
let mut bytes = SignatureBytes::<C>::default();
171171
let (r_bytes, s_bytes) = bytes.split_at_mut(C::Uint::BYTES);
172-
r_bytes.copy_from_slice(&self.r.to_be_bytes());
173-
s_bytes.copy_from_slice(&self.s.to_be_bytes());
172+
r_bytes.copy_from_slice(&self.r.to_bytes());
173+
s_bytes.copy_from_slice(&self.s.to_bytes());
174174
bytes
175175
}
176176

@@ -179,7 +179,7 @@ where
179179
pub fn to_der(&self) -> der::Signature<C>
180180
where
181181
der::MaxSize<C>: ArrayLength<u8>,
182-
<FieldSize<C> as Add>::Output: Add<der::MaxOverhead> + ArrayLength<u8>,
182+
<FieldBytesSize<C> as Add>::Output: Add<der::MaxOverhead> + ArrayLength<u8>,
183183
{
184184
let (r, s) = self.split_bytes();
185185
der::Signature::from_scalar_bytes(&r, &s).expect("DER encoding error")
@@ -285,8 +285,8 @@ where
285285
}
286286

287287
let (r_bytes, s_bytes) = bytes.split_at(C::Uint::BYTES);
288-
let r = ScalarPrimitive::from_be_slice(r_bytes).map_err(|_| Error::new())?;
289-
let s = ScalarPrimitive::from_be_slice(s_bytes).map_err(|_| Error::new())?;
288+
let r = ScalarPrimitive::from_slice(r_bytes).map_err(|_| Error::new())?;
289+
let s = ScalarPrimitive::from_slice(s_bytes).map_err(|_| Error::new())?;
290290

291291
if r.is_zero().into() || s.is_zero().into() {
292292
return Err(Error::new());

0 commit comments

Comments
 (0)