Skip to content

Commit 9556832

Browse files
authored
ecdsa: refactor field bytes encoding (#1388)
Companion PR to RustCrypto/traits#2457
1 parent 0bb957d commit 9556832

4 files changed

Lines changed: 23 additions & 18 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ ml-dsa = { path = "./ml-dsa" }
7171
rfc6979 = { path = "./rfc6979" }
7272
slh-dsa = { path = "./slh-dsa" }
7373
xmss = { path = "./xmss" }
74+
75+
crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint" }
76+
elliptic-curve = { git = "https://github.com/RustCrypto/traits" }

ecdsa/src/hazmat.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Low-level ECDSA primitives.
22
//!
3-
//! # ⚠️ Warning: Hazmat!
3+
//! <div class="warning">
4+
//! <b>Security️ Warning: Hazardous Materials!</b>
45
//!
56
//! YOU PROBABLY DON'T WANT TO USE THESE!
67
//!
@@ -9,20 +10,20 @@
910
//! If you are an end user / non-expert in cryptography, do not use these!
1011
//! Failure to use them correctly can lead to catastrophic failures including
1112
//! FULL PRIVATE KEY RECOVERY!
13+
//! </div>
1214
1315
use crate::{EcdsaCurve, Error, Result};
1416
use core::cmp;
1517
use elliptic_curve::{FieldBytes, array::typenum::Unsigned};
1618

1719
#[cfg(feature = "algorithm")]
1820
use {
19-
crate::{
20-
RecoveryId, Signature, SignatureSize,
21-
elliptic_curve::{FieldBytesEncoding, array::ArraySize},
22-
},
21+
crate::{RecoveryId, Signature, SignatureSize},
2322
elliptic_curve::{
2423
CurveArithmetic, NonZeroScalar, ProjectivePoint, Scalar,
24+
array::ArraySize,
2525
ff::PrimeField,
26+
field,
2627
group::{Curve as _, Group},
2728
ops::{Invert, MulByGeneratorVartime, Reduce},
2829
point::AffineCoordinates,
@@ -179,7 +180,7 @@ where
179180

180181
let k = NonZeroScalar::<C>::from_repr(rfc6979::generate_k::<D, _>(
181182
&d.to_repr(),
182-
&C::ORDER.encode_field_bytes(),
183+
&field::uint_to_bytes::<C>(&C::ORDER),
183184
&z2.to_repr(),
184185
ad,
185186
))

ecdsa/src/recovery.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ use {
1010
},
1111
digest::{Digest, FixedOutputReset, Update},
1212
elliptic_curve::{
13-
AffinePoint, FieldBytesEncoding, FieldBytesSize, Group, PrimeField, ProjectivePoint,
13+
AffinePoint, CurveArithmetic, FieldBytes, FieldBytesSize, Group, PrimeField,
14+
ProjectivePoint, Scalar,
15+
array::ArraySize,
1416
bigint::CheckedAdd,
17+
field,
18+
ops::Invert,
1519
ops::{LinearCombination, Reduce},
1620
point::DecompressPoint,
1721
sec1::{self, FromSec1Point, ToSec1Point},
18-
},
19-
elliptic_curve::{
20-
CurveArithmetic, FieldBytes, Scalar, array::ArraySize, ops::Invert, subtle::CtOption,
22+
subtle::CtOption,
2123
},
2224
signature::{
2325
DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer,
@@ -363,11 +365,12 @@ where
363365
let z = Scalar::<C>::reduce(&bits2field::<C>(prehash)?);
364366

365367
let r_bytes = if recovery_id.is_x_reduced() {
366-
C::Uint::decode_field_bytes(&r.to_repr())
368+
let uint = field::bytes_to_uint::<C>(&r.to_repr())
367369
.checked_add(&C::ORDER)
368370
.into_option()
369-
.ok_or_else(Error::new)?
370-
.encode_field_bytes()
371+
.ok_or_else(Error::new)?;
372+
373+
field::uint_to_bytes::<C>(&uint)
371374
} else {
372375
r.to_repr()
373376
};

0 commit comments

Comments
 (0)