Skip to content

Commit ba1024f

Browse files
authored
Bump crypto-bigint to v0.7.0-rc.22 (#110)
Also bumps: `rand_core` v0.10.0-rc-6 This release brought a few breaking changes: - For this crate's purposes: `Unsigned` => `UnsignedMontyForm`. I moved the associated type here, which has now been renamed `MontyForm`, because... - `Monty` => `MontyForm`: the stack-allocated type previously known as `MontyForm` is now known as `FixedMontyForm`, and the `Monty` trait is now `MontyForm`. - `Monty(Form)` methods that take a `params` argument now borrow: this gets rid of the params cloning that was happening before. It now clones (an `Arc`) for you, or copies, as need be. Word of warning: as of this `rand_core` release, `(Try)RngCore` is now `(Try)Rng`. This crate wasn't impacted, however, since it uses `CryptoRng` which didn't change.
1 parent 189a0e8 commit ba1024f

File tree

6 files changed

+48
-44
lines changed

6 files changed

+48
-44
lines changed

Cargo.toml

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

1212
[dependencies]
13-
crypto-bigint = { version = "0.7.0-rc.21", default-features = false, features = ["rand_core"] }
13+
crypto-bigint = { version = "0.7.0-rc.22", default-features = false, features = ["rand_core"] }
1414
libm = { version = "0.2.13", default-features = false, features = ["arch"] }
15-
rand_core = { version = "0.10.0-rc-5", default-features = false }
15+
rand_core = { version = "0.10.0-rc-6", default-features = false }
1616
rayon = { version = "1", optional = true, default-features = false }
1717

1818
# Optional dependencies used in tests and benchmarks
@@ -23,7 +23,7 @@ glass_pumpkin = { version = "1", optional = true }
2323
[dev-dependencies]
2424
rand = { version = "0.10.0-rc.7", features = ["chacha"] }
2525
# need `crypto-bigint` with `alloc` to test `BoxedUint`
26-
crypto-bigint = { version = "0.7.0-pre.21", default-features = false, features = ["alloc"] }
26+
crypto-bigint = { version = "0.7.0-pre.22", 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,7 @@ harness = false
6161
[[bench]]
6262
name = "cctv"
6363
harness = false
64+
65+
[patch.crates-io.rand]
66+
git = "https://github.com/rust-random/rand"
67+
branch = "rand_core/v0.10.0-rc-6"

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, Unsigned};
5+
use crypto_bigint::{RandomMod, UnsignedMontyForm};
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: Unsigned + RandomMod,
87+
T: UnsignedMontyForm + 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: Unsigned + RandomMod,
131+
T: UnsignedMontyForm + 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/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, Monty, MontyMultiplier, Odd, Square, Unsigned, Word};
3+
use crypto_bigint::{Limb, MontyForm, MontyMultiplier, Odd, Square, UnsignedMontyForm, 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: Unsigned>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality>;
31+
fn generate<T: UnsignedMontyForm>(&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: Unsigned>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
48+
fn generate<T: UnsignedMontyForm>(&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: Unsigned>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
112+
fn generate<T: UnsignedMontyForm>(&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: Unsigned>(&self, n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
134+
fn generate<T: UnsignedMontyForm>(&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: Unsigned,
181+
T: UnsignedMontyForm,
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: Unsigned,
323+
T: UnsignedMontyForm,
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 Unsigned>::Monty::new_params_vartime(candidate.clone());
378+
let params = <T as UnsignedMontyForm>::MontyForm::new_params_vartime(candidate.clone());
379379

380-
let zero = <T as Unsigned>::Monty::zero(params.clone());
381-
let one = <T as Unsigned>::Monty::one(params.clone());
380+
let zero = <T as UnsignedMontyForm>::MontyForm::zero(&params);
381+
let one = <T as UnsignedMontyForm>::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 Unsigned>::Monty::new(to_integer(abs_q), &params);
390+
let abs_q = <T as UnsignedMontyForm>::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 Unsigned>::Monty::new(to_integer(p), &params)
399+
<T as UnsignedMontyForm>::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 Unsigned>::Monty::zero(params.clone()); // keeps U_k
418+
let mut uk = <T as UnsignedMontyForm>::MontyForm::zero(&params); // keeps U_k
419419
let mut qk = one.clone(); // keeps Q^k
420420

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

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

425425
// D in Montgomery representation - note that it can be negative.
426-
let abs_d = <T as Unsigned>::Monty::new(to_integer(abs_d), &params);
426+
let abs_d = <T as UnsignedMontyForm>::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, Unsigned, Word};
609+
use crypto_bigint::{Odd, U64, U128, Uint, UnsignedMontyForm, 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: Unsigned>(&self, _n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
660+
fn generate<T: UnsignedMontyForm>(&self, _n: &Odd<T>) -> Result<(Word, Word, bool), Primality> {
661661
Ok((5, 5, false))
662662
}
663663
}

src/hazmat/miller_rabin.rs

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

66
use super::{
@@ -21,18 +21,18 @@ use super::{
2121
///
2222
/// [^FIPS]: FIPS-186.5 standard, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf>
2323
#[derive(Clone, Debug, PartialEq, Eq)]
24-
pub struct MillerRabin<T: Unsigned> {
24+
pub struct MillerRabin<T: UnsignedMontyForm> {
2525
// The odd number that may or may not be a prime.
2626
candidate: T,
2727
/// The number of bits necessary to represent the candidate. Note: this is not the number of
2828
/// bits used by a `T` in memory.
2929
bits: u32,
3030
/// Pre-computed parameters for the Montgomery form of `T`.
31-
montgomery_params: <<T as Unsigned>::Monty as Monty>::Params,
31+
montgomery_params: <<T as UnsignedMontyForm>::MontyForm as MontyForm>::Params,
3232
/// The number 1 in Montgomery form.
33-
one: <T as Unsigned>::Monty,
33+
one: <T as UnsignedMontyForm>::MontyForm,
3434
/// The number -1 in Montgomery form.
35-
minus_one: <T as Unsigned>::Monty,
35+
minus_one: <T as UnsignedMontyForm>::MontyForm,
3636
/// The `s` exponent in the Miller-Rabin test, that finds `s` and `d` odd s.t. `candidate - 1 ==
3737
/// 2^s * d` (the pair `s` and `d` is unique).
3838
s: u32,
@@ -41,11 +41,11 @@ pub struct MillerRabin<T: Unsigned> {
4141
d: T,
4242
}
4343

44-
impl<T: Unsigned + RandomMod> MillerRabin<T> {
44+
impl<T: UnsignedMontyForm + RandomMod> MillerRabin<T> {
4545
/// Initializes a Miller-Rabin test for `candidate`.
4646
pub fn new(candidate: Odd<T>) -> Self {
47-
let params = <T as Unsigned>::Monty::new_params_vartime(candidate.clone());
48-
let m_one = <T as Unsigned>::Monty::one(params.clone());
47+
let params = <T as UnsignedMontyForm>::MontyForm::new_params_vartime(candidate.clone());
48+
let m_one = <T as UnsignedMontyForm>::MontyForm::one(&params);
4949
let m_minus_one = -m_one.clone();
5050

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

82-
let base = <T as Unsigned>::Monty::new(base.clone(), &self.montgomery_params);
82+
let base = <T as UnsignedMontyForm>::MontyForm::new(base.clone(), &self.montgomery_params);
8383

8484
// Implementation detail: bounded exp gets faster every time we decrease the bound
8585
// by the window length it uses, which is currently 4 bits.
@@ -233,7 +233,7 @@ mod tests {
233233
use alloc::format;
234234
use core::num::NonZero;
235235

236-
use crypto_bigint::{Odd, RandomMod, U64, U128, U1024, U1536, Uint, Unsigned};
236+
use crypto_bigint::{Odd, RandomMod, U64, U128, U1024, U1536, Uint, UnsignedMontyForm};
237237
use rand::rngs::ChaCha8Rng;
238238
use rand_core::{CryptoRng, SeedableRng};
239239

@@ -265,7 +265,7 @@ mod tests {
265265
pseudoprimes::STRONG_BASE_2.contains(&num)
266266
}
267267

268-
fn random_checks<T: Unsigned + RandomMod, R: CryptoRng + ?Sized>(
268+
fn random_checks<T: UnsignedMontyForm + RandomMod, R: CryptoRng + ?Sized>(
269269
rng: &mut R,
270270
mr: &MillerRabin<T>,
271271
count: usize,

src/multicore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Prime-finding functions that can parallelize across multiple cores.
22
3-
use crypto_bigint::{RandomBits, RandomMod, Unsigned};
3+
use crypto_bigint::{RandomBits, RandomMod, UnsignedMontyForm};
44
use rand_core::{CryptoRng, SeedableRng};
55
use rayon::iter::{ParallelBridge, ParallelIterator};
66

@@ -105,7 +105,7 @@ where
105105
/// Panics if the platform is unable to spawn threads.
106106
pub fn random_prime<T, R>(rng: &mut R, flavor: Flavor, bit_length: u32, threadcount: usize) -> T
107107
where
108-
T: Unsigned + RandomBits + RandomMod,
108+
T: UnsignedMontyForm + RandomBits + RandomMod,
109109
R: CryptoRng + Send + Sync + SeedableRng,
110110
{
111111
let factory = SmallFactorsSieveFactory::new(flavor, bit_length, SetBits::Msb)

src/presets.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crypto_bigint::{RandomBits, RandomMod, Unsigned};
1+
use crypto_bigint::{RandomBits, RandomMod, UnsignedMontyForm};
22
use rand_core::CryptoRng;
33

44
use crate::{
@@ -27,7 +27,7 @@ pub enum Flavor {
2727
/// See [`is_prime`] for details about the performed checks.
2828
pub fn random_prime<T, R>(rng: &mut R, flavor: Flavor, bit_length: u32) -> T
2929
where
30-
T: Unsigned + RandomBits + RandomMod,
30+
T: UnsignedMontyForm + RandomBits + RandomMod,
3131
R: CryptoRng + ?Sized,
3232
{
3333
let factory = SmallFactorsSieveFactory::new(flavor, bit_length, SetBits::Msb)
@@ -63,7 +63,7 @@ where
6363
/// DOI: [10.1090/mcom/3616](https://doi.org/10.1090/mcom/3616)
6464
pub fn is_prime<T>(flavor: Flavor, candidate: &T) -> bool
6565
where
66-
T: Unsigned + RandomMod,
66+
T: UnsignedMontyForm + RandomMod,
6767
{
6868
match flavor {
6969
Flavor::Any => {}
@@ -94,7 +94,7 @@ where
9494
/// See [`is_prime`] for details about the performed checks.
9595
fn is_safe_prime<T>(candidate: &T) -> bool
9696
where
97-
T: Unsigned + RandomMod,
97+
T: UnsignedMontyForm + RandomMod,
9898
{
9999
// Since, by the definition of safe prime, `(candidate - 1) / 2` must also be prime,
100100
// and therefore odd, `candidate` has to be equal to 3 modulo 4.
@@ -115,7 +115,7 @@ where
115115

116116
#[cfg(test)]
117117
mod tests {
118-
use crypto_bigint::{BoxedUint, CheckedAdd, RandomMod, U64, U128, Uint, Unsigned, Word, nlimbs};
118+
use crypto_bigint::{BoxedUint, CheckedAdd, RandomMod, U64, U128, Uint, UnsignedMontyForm, Word, nlimbs};
119119
use num_prime::nt_funcs::is_prime64;
120120

121121
use super::{Flavor, is_prime, random_prime};
@@ -124,7 +124,7 @@ mod tests {
124124
hazmat::{primes, pseudoprimes},
125125
};
126126

127-
fn fips_is_prime<T: Unsigned + RandomMod>(flavor: Flavor, num: &T) -> bool {
127+
fn fips_is_prime<T: UnsignedMontyForm + RandomMod>(flavor: Flavor, num: &T) -> bool {
128128
let mut rng = rand::rng();
129129
fips::is_prime(
130130
&mut rng,
@@ -134,7 +134,7 @@ mod tests {
134134
)
135135
}
136136

137-
fn fips_is_prime_trial_division<T: Unsigned + RandomMod>(flavor: Flavor, num: &T) -> bool {
137+
fn fips_is_prime_trial_division<T: UnsignedMontyForm + RandomMod>(flavor: Flavor, num: &T) -> bool {
138138
let mut rng = rand::rng();
139139
fips::is_prime(
140140
&mut rng,

0 commit comments

Comments
 (0)