Skip to content

Commit a518cb3

Browse files
committed
[WIP] Bump crypto-bigint
This PR includes #113 along with breaking changes to `crypto-bigint`
1 parent dae16a7 commit a518cb3

File tree

8 files changed

+65
-59
lines changed

8 files changed

+65
-59
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories = ["cryptography", "no-std"]
1010
rust-version = "1.85"
1111

1212
[dependencies]
13-
crypto-bigint = { version = "0.7.0-rc.22", default-features = false, features = ["rand_core"] }
13+
crypto-bigint = { version = "0.7.0-rc.26", default-features = false, features = ["rand_core"] }
1414
libm = { version = "0.2.13", default-features = false, features = ["arch"] }
1515
rand_core = { version = "0.10", default-features = false }
1616
rayon = { version = "1", optional = true, default-features = false }
@@ -23,7 +23,7 @@ glass_pumpkin = { version = "1", optional = true }
2323
[dev-dependencies]
2424
rand = { version = "0.10", features = ["chacha"] }
2525
# need `crypto-bigint` with `alloc` to test `BoxedUint`
26-
crypto-bigint = { version = "0.7.0-pre.22", default-features = false, features = ["alloc"] }
26+
crypto-bigint = { version = "0.7.0-pre.26", default-features = false, features = ["alloc"] }
2727
criterion = { version = "0.5", features = ["html_reports"] }
2828
num-modular = { version = "0.5", features = ["num-bigint"] }
2929
num-bigint = "0.4"
@@ -61,3 +61,6 @@ harness = false
6161
[[bench]]
6262
name = "cctv"
6363
harness = false
64+
65+
[patch.crates-io.crypto-bigint]
66+
git = "https://github.com/RustCrypto/crypto-bigint"

benches/bench.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn bench_sieve(c: &mut Criterion) {
6565
// 5 is the average number of pre-sieved samples we need to take before we encounter a prime
6666
group.bench_function("(U128) average sieve samples for a prime (5)", |b| {
6767
b.iter_batched(
68-
|| make_sieve::<{ nlimbs!(128) }, _>(&mut rng),
68+
|| make_sieve::<{ nlimbs(128) }, _>(&mut rng),
6969
|sieve| sieve.take(5).for_each(drop),
7070
BatchSize::SmallInput,
7171
)
@@ -86,7 +86,7 @@ fn bench_sieve(c: &mut Criterion) {
8686
// 42 is the average number of pre-sieved samples we need to take before we encounter a prime
8787
group.bench_function("(U1024) average sieve samples for a prime (42)", |b| {
8888
b.iter_batched(
89-
|| make_sieve::<{ nlimbs!(1024) }, _>(&mut rng),
89+
|| make_sieve::<{ nlimbs(1024) }, _>(&mut rng),
9090
|sieve| sieve.take(42).for_each(drop),
9191
BatchSize::SmallInput,
9292
)
@@ -96,7 +96,7 @@ fn bench_sieve(c: &mut Criterion) {
9696
// before we encounter a safe prime
9797
group.bench_function("(U1024) average sieve samples for a safe prime (42^2)", |b| {
9898
b.iter_batched(
99-
|| make_sieve::<{ nlimbs!(1024) }, _>(&mut rng),
99+
|| make_sieve::<{ nlimbs(1024) }, _>(&mut rng),
100100
|sieve| sieve.take(42 * 42).for_each(drop),
101101
BatchSize::SmallInput,
102102
)
@@ -119,7 +119,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
119119

120120
group.bench_function("(U128) random base test (pre-sieved)", |b| {
121121
b.iter_batched(
122-
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(128) }, _>(&mut rng.clone())),
122+
|| MillerRabin::new(make_presieved_num::<{ nlimbs(128) }, _>(&mut rng.clone())),
123123
|mr| mr.test_random_base(&mut rng.clone()),
124124
BatchSize::SmallInput,
125125
)
@@ -135,7 +135,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
135135

136136
group.bench_function("(U1024) random base test (pre-sieved)", |b| {
137137
b.iter_batched(
138-
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(1024) }, _>(&mut rng.clone())),
138+
|| MillerRabin::new(make_presieved_num::<{ nlimbs(1024) }, _>(&mut rng.clone())),
139139
|mr| mr.test_random_base(&mut rng.clone()),
140140
BatchSize::SmallInput,
141141
)
@@ -148,7 +148,7 @@ fn bench_lucas(c: &mut Criterion) {
148148
let mut rng = make_rng();
149149
group.bench_function("(U128) Selfridge base, strong check (pre-sieved)", |b| {
150150
b.iter_batched(
151-
|| make_presieved_num::<{ nlimbs!(128) }, _>(&mut rng),
151+
|| make_presieved_num::<{ nlimbs(128) }, _>(&mut rng),
152152
|n| lucas_test(n, SelfridgeBase, LucasCheck::Strong),
153153
BatchSize::SmallInput,
154154
)
@@ -157,7 +157,7 @@ fn bench_lucas(c: &mut Criterion) {
157157
let mut rng = make_rng();
158158
group.bench_function("(U1024) Selfridge base, strong check (pre-sieved)", |b| {
159159
b.iter_batched(
160-
|| make_presieved_num::<{ nlimbs!(1024) }, _>(&mut rng),
160+
|| make_presieved_num::<{ nlimbs(1024) }, _>(&mut rng),
161161
|n| lucas_test(n, SelfridgeBase, LucasCheck::Strong),
162162
BatchSize::SmallInput,
163163
)
@@ -166,7 +166,7 @@ fn bench_lucas(c: &mut Criterion) {
166166
let mut rng = make_rng();
167167
group.bench_function("(U1024) A* base, Lucas-V check (pre-sieved)", |b| {
168168
b.iter_batched(
169-
|| make_presieved_num::<{ nlimbs!(1024) }, _>(&mut rng),
169+
|| make_presieved_num::<{ nlimbs(1024) }, _>(&mut rng),
170170
|n| lucas_test(n, AStarBase, LucasCheck::LucasV),
171171
BatchSize::SmallInput,
172172
)
@@ -175,7 +175,7 @@ fn bench_lucas(c: &mut Criterion) {
175175
let mut rng = make_rng();
176176
group.bench_function("(U1024) brute force base, almost extra strong (pre-sieved)", |b| {
177177
b.iter_batched(
178-
|| make_presieved_num::<{ nlimbs!(1024) }, _>(&mut rng),
178+
|| make_presieved_num::<{ nlimbs(1024) }, _>(&mut rng),
179179
|n| lucas_test(n, BruteForceBase, LucasCheck::AlmostExtraStrong),
180180
BatchSize::SmallInput,
181181
)
@@ -184,7 +184,7 @@ fn bench_lucas(c: &mut Criterion) {
184184
let mut rng = make_rng();
185185
group.bench_function("(U1024) brute force base, extra strong (pre-sieved)", |b| {
186186
b.iter_batched(
187-
|| make_presieved_num::<{ nlimbs!(1024) }, _>(&mut rng),
187+
|| make_presieved_num::<{ nlimbs(1024) }, _>(&mut rng),
188188
|n| lucas_test(n, BruteForceBase, LucasCheck::ExtraStrong),
189189
BatchSize::SmallInput,
190190
)
@@ -405,15 +405,15 @@ fn bench_gmp(c: &mut Criterion) {
405405

406406
group.bench_function("(U128) Random prime", |b| {
407407
b.iter_batched(
408-
|| random::<{ nlimbs!(128) }, _>(&mut rng),
408+
|| random::<{ nlimbs(128) }, _>(&mut rng),
409409
|n| n.next_prime(),
410410
BatchSize::SmallInput,
411411
)
412412
});
413413

414414
group.bench_function("(U1024) Random prime", |b| {
415415
b.iter_batched(
416-
|| random::<{ nlimbs!(1024) }, _>(&mut rng),
416+
|| random::<{ nlimbs(1024) }, _>(&mut rng),
417417
|n| n.next_prime(),
418418
BatchSize::SmallInput,
419419
)

src/fips.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
44
5-
use crypto_bigint::{RandomMod, UnsignedMontyForm};
5+
use crypto_bigint::{RandomMod, UnsignedWithMontyForm};
66
use rand_core::CryptoRng;
77

88
use crate::{
@@ -84,7 +84,7 @@ impl FipsOptions {
8484
/// [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
8585
pub fn is_prime<T>(rng: &mut (impl CryptoRng + ?Sized), flavor: Flavor, candidate: &T, options: FipsOptions) -> bool
8686
where
87-
T: UnsignedMontyForm + RandomMod,
87+
T: UnsignedWithMontyForm + RandomMod,
8888
{
8989
match flavor {
9090
Flavor::Any => {}
@@ -128,7 +128,7 @@ where
128128
/// [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
129129
fn is_safe_prime<T>(rng: &mut (impl CryptoRng + ?Sized), candidate: &T, options: FipsOptions) -> bool
130130
where
131-
T: UnsignedMontyForm + RandomMod,
131+
T: UnsignedWithMontyForm + RandomMod,
132132
{
133133
// Since, by the definition of safe prime, `(candidate - 1) / 2` must also be prime,
134134
// and therefore odd, `candidate` has to be equal to 3 modulo 4.

src/hazmat/float.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub(crate) const fn floor_sqrt(x: u32) -> u32 {
114114

115115
// Calculate the natural logarithm of a big integer using the relation ln(x) = log₂(x) / log₂(e).
116116
// Uses fixed-point arithmetic for large values of x (> 2^53).
117+
#[allow(trivial_numeric_casts)]
117118
pub(crate) fn ln<const LIMBS: usize>(x: &Uint<LIMBS>) -> f64 {
118119
if x <= &Uint::ONE {
119120
return 0.0;

src/hazmat/lucas.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Lucas primality test.
22
use core::num::NonZero;
3-
use crypto_bigint::{Limb, MontyForm, MontyMultiplier, Odd, Square, UnsignedMontyForm, Word};
3+
use crypto_bigint::{Limb, MontyForm, MontyMultiplier, Odd, Square, UnsignedWithMontyForm, Word};
44

55
use super::{
66
Primality,
@@ -28,7 +28,7 @@ pub trait LucasBase {
2828
/// Given an odd integer, returns `Ok((P, abs(Q), is_negative(Q)))` on success,
2929
/// or `Err(Primality)` if the primality for the given integer was discovered
3030
/// during the search for a base.
31-
fn generate<T: UnsignedMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality>;
31+
fn generate<T: UnsignedWithMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality>;
3232
}
3333

3434
/// "Method A" for selecting the base given in Baillie & Wagstaff[^Baillie1980],
@@ -45,7 +45,7 @@ pub trait LucasBase {
4545
pub struct SelfridgeBase;
4646

4747
impl LucasBase for SelfridgeBase {
48-
fn generate<T: UnsignedMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
48+
fn generate<T: UnsignedWithMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
4949
let mut abs_d = 5;
5050
let mut d_is_negative = false;
5151
let n_is_small = n.bits_vartime() < Word::BITS; // if true, `n` fits into one `Word`
@@ -109,7 +109,7 @@ impl LucasBase for SelfridgeBase {
109109
pub struct AStarBase;
110110

111111
impl LucasBase for AStarBase {
112-
fn generate<T: UnsignedMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
112+
fn generate<T: UnsignedWithMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
113113
SelfridgeBase.generate(n).map(|(p, abs_q, q_is_negative)| {
114114
if abs_q == 1 && q_is_negative {
115115
(5, 5, false)
@@ -131,7 +131,7 @@ impl LucasBase for AStarBase {
131131
pub struct BruteForceBase;
132132

133133
impl LucasBase for BruteForceBase {
134-
fn generate<T: UnsignedMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
134+
fn generate<T: UnsignedWithMontyForm>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
135135
let mut p = 3;
136136
let mut attempts = 0;
137137

@@ -178,7 +178,7 @@ impl LucasBase for BruteForceBase {
178178
/// For the given odd `n`, finds `s` and odd `d` such that `n + 1 == 2^s * d`.
179179
fn decompose<T>(n: &Odd<T>) -> (u32, Odd<T>)
180180
where
181-
T: UnsignedMontyForm,
181+
T: UnsignedWithMontyForm,
182182
{
183183
// Need to be careful here since `n + 1` can overflow.
184184
// Instead of adding 1 and counting trailing 0s, we count trailing ones on the original `n`.
@@ -320,7 +320,7 @@ pub enum LucasCheck {
320320
/// [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
321321
pub fn lucas_test<T>(candidate: Odd<T>, base: impl LucasBase, check: LucasCheck) -> Primality
322322
where
323-
T: UnsignedMontyForm,
323+
T: UnsignedWithMontyForm,
324324
{
325325
// The comments in this function use references in `LucasCheck`, plus this one:
326326
//
@@ -375,10 +375,10 @@ where
375375
let (s, d) = decompose(&candidate);
376376

377377
// Some constants in Montgomery form
378-
let params = <T as UnsignedMontyForm>::MontyForm::new_params_vartime(candidate.clone());
378+
let params = <T as UnsignedWithMontyForm>::MontyForm::new_params_vartime(candidate.clone());
379379

380-
let zero = <T as UnsignedMontyForm>::MontyForm::zero(&params);
381-
let one = <T as UnsignedMontyForm>::MontyForm::one(&params);
380+
let zero = <T as UnsignedWithMontyForm>::MontyForm::zero(&params);
381+
let one = <T as UnsignedWithMontyForm>::MontyForm::one(&params);
382382
let two = one.clone() + &one;
383383
let minus_two = -two.clone();
384384

@@ -387,7 +387,7 @@ where
387387
let q = if q_is_one {
388388
one.clone()
389389
} else {
390-
let abs_q = <T as UnsignedMontyForm>::MontyForm::new(to_integer(abs_q), &params);
390+
let abs_q = <T as UnsignedWithMontyForm>::MontyForm::new(to_integer(abs_q), &params);
391391
if q_is_negative { -abs_q } else { abs_q }
392392
};
393393

@@ -396,7 +396,7 @@ where
396396
let p = if p_is_one {
397397
one.clone()
398398
} else {
399-
<T as UnsignedMontyForm>::MontyForm::new(to_integer(p), &params)
399+
<T as UnsignedWithMontyForm>::MontyForm::new(to_integer(p), &params)
400400
};
401401

402402
// Compute d-th element of Lucas sequence (U_d(P, Q), V_d(P, Q)), where:
@@ -415,15 +415,15 @@ where
415415

416416
// Starting with k = 0
417417
let mut vk = two.clone(); // keeps V_k
418-
let mut uk = <T as UnsignedMontyForm>::MontyForm::zero(&params); // keeps U_k
418+
let mut uk = <T as UnsignedWithMontyForm>::MontyForm::zero(&params); // keeps U_k
419419
let mut qk = one.clone(); // keeps Q^k
420420

421-
let mut temp = <T as UnsignedMontyForm>::MontyForm::zero(&params);
421+
let mut temp = <T as UnsignedWithMontyForm>::MontyForm::zero(&params);
422422

423-
let mut mm = <<T as UnsignedMontyForm>::MontyForm as MontyForm>::Multiplier::from(&params);
423+
let mut mm = <<T as UnsignedWithMontyForm>::MontyForm as MontyForm>::Multiplier::from(&params);
424424

425425
// D in Montgomery representation - note that it can be negative.
426-
let abs_d = <T as UnsignedMontyForm>::MontyForm::new(to_integer(abs_d), &params);
426+
let abs_d = <T as UnsignedWithMontyForm>::MontyForm::new(to_integer(abs_d), &params);
427427
let d_m = if d_is_negative { -abs_d } else { abs_d };
428428

429429
for i in (0..d.bits_vartime()).rev() {
@@ -606,7 +606,7 @@ mod tests {
606606

607607
use alloc::format;
608608

609-
use crypto_bigint::{Odd, U64, U128, Uint, UnsignedMontyForm, Word};
609+
use crypto_bigint::{Odd, U64, U128, Uint, UnsignedWithMontyForm, Word};
610610

611611
#[cfg(feature = "tests-exhaustive")]
612612
use num_prime::nt_funcs::is_prime64;
@@ -657,7 +657,7 @@ mod tests {
657657
struct TestBase;
658658

659659
impl LucasBase for TestBase {
660-
fn generate<T: UnsignedMontyForm>(&self, _n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
660+
fn generate<T: UnsignedWithMontyForm>(&self, _n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
661661
Ok((5, 5, false))
662662
}
663663
}

src/hazmat/miller_rabin.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Miller-Rabin primality test.
22
3-
use crypto_bigint::{Limb, MontyForm, NonZero as CTNonZero, Odd, PowBoundedExp, RandomMod, Square, UnsignedMontyForm};
3+
use crypto_bigint::{
4+
Limb, MontyForm, NonZero as CTNonZero, Odd, PowBoundedExp, RandomMod, Square, UnsignedWithMontyForm,
5+
};
46
use rand_core::CryptoRng;
57

68
use super::{
@@ -21,18 +23,18 @@ use super::{
2123
///
2224
/// [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
2325
#[derive(Clone, Debug, PartialEq, Eq)]
24-
pub struct MillerRabin<T: UnsignedMontyForm> {
26+
pub struct MillerRabin<T: UnsignedWithMontyForm> {
2527
// The odd number that may or may not be a prime.
2628
candidate: T,
2729
/// The number of bits necessary to represent the candidate. Note: this is not the number of
2830
/// bits used by a `T` in memory.
2931
bits: u32,
3032
/// Pre-computed parameters for the Montgomery form of `T`.
31-
montgomery_params: <<T as UnsignedMontyForm>::MontyForm as MontyForm>::Params,
33+
montgomery_params: <<T as UnsignedWithMontyForm>::MontyForm as MontyForm>::Params,
3234
/// The number 1 in Montgomery form.
33-
one: <T as UnsignedMontyForm>::MontyForm,
35+
one: <T as UnsignedWithMontyForm>::MontyForm,
3436
/// The number -1 in Montgomery form.
35-
minus_one: <T as UnsignedMontyForm>::MontyForm,
37+
minus_one: <T as UnsignedWithMontyForm>::MontyForm,
3638
/// The `s` exponent in the Miller-Rabin test, that finds `s` and `d` odd s.t. `candidate - 1 ==
3739
/// 2^s * d` (the pair `s` and `d` is unique).
3840
s: u32,
@@ -41,11 +43,11 @@ pub struct MillerRabin<T: UnsignedMontyForm> {
4143
d: T,
4244
}
4345

44-
impl<T: UnsignedMontyForm + RandomMod> MillerRabin<T> {
46+
impl<T: UnsignedWithMontyForm + RandomMod> MillerRabin<T> {
4547
/// Initializes a Miller-Rabin test for `candidate`.
4648
pub fn new(candidate: Odd<T>) -> Self {
47-
let params = <T as UnsignedMontyForm>::MontyForm::new_params_vartime(candidate.clone());
48-
let m_one = <T as UnsignedMontyForm>::MontyForm::one(&params);
49+
let params = <T as UnsignedWithMontyForm>::MontyForm::new_params_vartime(candidate.clone());
50+
let m_one = <T as UnsignedWithMontyForm>::MontyForm::one(&params);
4951
let m_minus_one = -m_one.clone();
5052

5153
let one = T::one_like(candidate.as_ref());
@@ -79,7 +81,7 @@ impl<T: UnsignedMontyForm + RandomMod> MillerRabin<T> {
7981
// One could check here if `gcd(base, candidate) == 1` and return `Composite` otherwise.
8082
// In practice it doesn't make any performance difference in normal operation.
8183

82-
let base = <T as UnsignedMontyForm>::MontyForm::new(base.clone(), &self.montgomery_params);
84+
let base = <T as UnsignedWithMontyForm>::MontyForm::new(base.clone(), &self.montgomery_params);
8385

8486
// Implementation detail: bounded exp gets faster every time we decrease the bound
8587
// by the window length it uses, which is currently 4 bits.
@@ -233,7 +235,7 @@ mod tests {
233235
use alloc::format;
234236
use core::num::NonZero;
235237

236-
use crypto_bigint::{Odd, RandomMod, U64, U128, U1024, U1536, Uint, UnsignedMontyForm};
238+
use crypto_bigint::{Odd, RandomMod, U64, U128, U1024, U1536, Uint, UnsignedWithMontyForm};
237239
use rand::rngs::ChaCha8Rng;
238240
use rand_core::{CryptoRng, SeedableRng};
239241

@@ -265,7 +267,7 @@ mod tests {
265267
pseudoprimes::STRONG_BASE_2.contains(&num)
266268
}
267269

268-
fn random_checks<T: UnsignedMontyForm + RandomMod, R: CryptoRng + ?Sized>(
270+
fn random_checks<T: UnsignedWithMontyForm + RandomMod, R: CryptoRng + ?Sized>(
269271
rng: &mut R,
270272
mr: &MillerRabin<T>,
271273
count: usize,

0 commit comments

Comments
 (0)