Skip to content

Commit 7f9b341

Browse files
authored
Use new Reduce trait (#1359)
Uses the `Reduce` trait added in RustCrypto/traits#1949
1 parent ea1e763 commit 7f9b341

File tree

45 files changed

+214
-205
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+214
-205
lines changed

Cargo.lock

Lines changed: 6 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ members = [
2121
opt-level = 2
2222

2323
[patch.crates-io]
24-
crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint" }
24+
ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
2525
elliptic-curve = { git = "https://github.com/RustCrypto/traits.git" }
2626

2727
hash2curve = { path = "hash2curve" }

bign256/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ edition = "2024"
1818
rust-version = "1.85"
1919

2020
[dependencies]
21-
elliptic-curve = { version = "0.14.0-rc.10", features = ["sec1"] }
21+
elliptic-curve = { version = "0.14.0-rc.11", features = ["sec1"] }
2222

2323
# optional dependencies
2424
belt-hash = { version = "0.2.0-rc.0", optional = true, default-features = false }

bign256/src/arithmetic/scalar.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,18 @@ impl PrimeFieldBits for Scalar {
168168
}
169169

170170
impl Reduce<U256> for Scalar {
171-
type Bytes = FieldBytes;
172-
173-
fn reduce(w: U256) -> Self {
171+
fn reduce(w: &U256) -> Self {
174172
let (r, underflow) = w.borrowing_sub(&BignP256::ORDER, Limb::ZERO);
175173
let underflow = Choice::from((underflow.0 >> (Limb::BITS - 1)) as u8);
176-
Self::from_uint_unchecked(U256::conditional_select(&w, &r, !underflow))
174+
Self::from_uint_unchecked(U256::conditional_select(w, &r, !underflow))
177175
}
176+
}
178177

178+
impl Reduce<FieldBytes> for Scalar {
179179
#[inline]
180-
fn reduce_bytes(bytes: &FieldBytes) -> Self {
180+
fn reduce(bytes: &FieldBytes) -> Self {
181181
let w = <U256 as FieldBytesEncoding<BignP256>>::decode_field_bytes(bytes);
182-
Self::reduce(w)
182+
Self::reduce(&w)
183183
}
184184
}
185185

bign256/src/ecdsa/signing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl PrehashSigner<Signature> for SigningKey {
109109
let mut h_word: Array<u8, U32> = Array::clone_from_slice(prehash);
110110
h_word.reverse();
111111

112-
let h = Scalar::reduce_bytes(&h_word);
112+
let h = Scalar::reduce(&h_word);
113113

114114
//2. Generate 𝑘 ← rand(1,..,𝑞-1)
115115
let k = Scalar::from_repr(rfc6979::generate_k::<BeltHash, _>(

bign256/src/ecdsa/verifying.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl PrehashVerifier<Signature> for VerifyingKey {
121121
let mut hash: Array<u8, U32> = Array::clone_from_slice(prehash);
122122
hash.reverse();
123123

124-
let hw = Scalar::reduce_bytes(FieldBytes::from_slice(&hash));
124+
let hw = Scalar::reduce(FieldBytes::from_slice(&hash));
125125
let left = s1.add(&hw);
126126

127127
let right = s0.add(&Scalar::from_u64(2).pow([128, 0, 0, 0]));

bign256/tests/dsa.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hex_literal::hex;
77
use proptest::prelude::*;
88

99
use bign256::{
10-
NonZeroScalar, Scalar, U256,
10+
FieldBytes, NonZeroScalar, Scalar,
1111
ecdsa::{
1212
Signature, SigningKey, VerifyingKey,
1313
signature::{Signer, Verifier},
@@ -35,7 +35,7 @@ fn verify_test_vector() {
3535
prop_compose! {
3636
fn signing_key()(bytes in any::<[u8; 32]>()) -> SigningKey {
3737
loop {
38-
let scalar = <Scalar as Reduce<U256>>::reduce_bytes(&bytes.into());
38+
let scalar = <Scalar as Reduce<FieldBytes>>::reduce(&bytes.into());
3939
if let Some(scalar) = Option::from(NonZeroScalar::new(scalar)) {
4040
return SigningKey::from_nonzero_scalar(scalar).unwrap();
4141
}

bign256/tests/scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
33
#![cfg(feature = "arithmetic")]
44

5-
use bign256::{Scalar, U256};
5+
use bign256::{FieldBytes, Scalar};
66
use elliptic_curve::ops::{Invert, Reduce};
77
use proptest::prelude::*;
88

99
prop_compose! {
1010
fn scalar()(bytes in any::<[u8; 32]>()) -> Scalar {
11-
<Scalar as Reduce<U256>>::reduce_bytes(&bytes.into())
11+
<Scalar as Reduce<FieldBytes>>::reduce(&bytes.into())
1212
}
1313
}
1414

bp256/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ edition = "2024"
1414
rust-version = "1.85"
1515

1616
[dependencies]
17-
elliptic-curve = { version = "0.14.0-rc.10", default-features = false, features = ["sec1"] }
17+
elliptic-curve = { version = "0.14.0-rc.11", default-features = false, features = ["sec1"] }
1818

1919
# optional dependencies
2020
ecdsa = { version = "0.17.0-rc.4", optional = true, default-features = false, features = ["der"] }

bp256/src/arithmetic/scalar.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,17 @@ impl PrimeField for Scalar {
132132
}
133133

134134
impl Reduce<U256> for Scalar {
135-
type Bytes = FieldBytes;
136-
137-
fn reduce(w: U256) -> Self {
135+
fn reduce(w: &U256) -> Self {
138136
let (r, underflow) = w.borrowing_sub(&ORDER, Limb::ZERO);
139137
let underflow = Choice::from((underflow.0 >> (Limb::BITS - 1)) as u8);
140-
Self::from_uint_unchecked(U256::conditional_select(&w, &r, !underflow))
138+
Self::from_uint_unchecked(U256::conditional_select(w, &r, !underflow))
141139
}
140+
}
142141

142+
impl Reduce<FieldBytes> for Scalar {
143143
#[inline]
144-
fn reduce_bytes(bytes: &FieldBytes) -> Self {
145-
Self::reduce(U256::from_be_byte_array(*bytes))
144+
fn reduce(bytes: &FieldBytes) -> Self {
145+
Self::reduce(&U256::from_be_byte_array(*bytes))
146146
}
147147
}
148148

0 commit comments

Comments
 (0)