Skip to content

Commit

Permalink
Replace lazy_static with once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Dec 25, 2023
1 parent 6f73f0a commit 3c0e6f1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 102 deletions.
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ features = [ "alloc" ]
[dependencies.libm]
version = "0.2.1"

[dependencies.lazy_static]
version = "1.2.0"
default-features = false
# no_std feature is an anti-pattern. Why, lazy_static, why?
# See https://github.com/rust-lang-nursery/lazy-static.rs/issues/150
features = ["spin_no_std"]
[dependencies.once_cell]
version = "1"

[dev-dependencies]
rand_chacha = "0.3"
Expand Down
14 changes: 7 additions & 7 deletions src/bigrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use num_iter::range_step;
use num_traits::Zero;
#[cfg(feature = "prime")]
use num_traits::{FromPrimitive, ToPrimitive};
#[cfg(feature = "prime")]
use once_cell::sync::Lazy;

#[cfg(feature = "prime")]
use crate::prime::probably_prime;
Expand Down Expand Up @@ -294,14 +296,12 @@ pub trait RandPrime {
#[cfg(feature = "prime")]
const SMALL_PRIMES: [u8; 15] = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53];

/// The product of the values in SMALL_PRIMES and allows us
/// to reduce a candidate prime by this number and then determine whether it's
/// coprime to all the elements of SMALL_PRIMES without further BigUint
/// operations.
#[cfg(feature = "prime")]
lazy_static! {
/// The product of the values in SMALL_PRIMES and allows us
/// to reduce a candidate prime by this number and then determine whether it's
/// coprime to all the elements of SMALL_PRIMES without further BigUint
/// operations.
static ref SMALL_PRIMES_PRODUCT: BigUint = BigUint::from_u64(16_294_579_238_595_022_365).unwrap();
}
const SMALL_PRIMES_PRODUCT: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(16_294_579_238_595_022_365).unwrap());

#[cfg(feature = "prime")]
impl<R: Rng + ?Sized> RandPrime for R {
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ extern crate std;
extern crate smallvec;

#[cfg(feature = "prime")]
#[macro_use]
extern crate lazy_static;
extern crate once_cell;

extern crate num_integer as integer;

Expand Down
170 changes: 83 additions & 87 deletions src/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use integer::Integer;
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
use once_cell::sync::Lazy;
use rand::rngs::StdRng;
use rand::SeedableRng;

Expand All @@ -12,12 +13,9 @@ use crate::bigrand::RandBigInt;
use crate::Sign::Plus;
use crate::{BigInt, BigUint, IntoBigUint};

lazy_static! {
pub(crate) static ref BIG_1: BigUint = BigUint::one();
pub(crate) static ref BIG_2: BigUint = BigUint::from_u64(2).unwrap();
pub(crate) static ref BIG_3: BigUint = BigUint::from_u64(3).unwrap();
pub(crate) static ref BIG_64: BigUint = BigUint::from_u64(64).unwrap();
}
pub(crate) const BIG_1: Lazy<BigUint> = Lazy::new(|| BigUint::one());
pub(crate) const BIG_2: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(2).unwrap());
pub(crate) const BIG_64: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(64).unwrap());

const PRIMES_A: u64 = 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 37;
const PRIMES_B: u64 = 29 * 31 * 41 * 43 * 47 * 53;
Expand Down Expand Up @@ -439,8 +437,7 @@ mod tests {

use crate::biguint::ToBigUint;

lazy_static! {
static ref PRIMES: Vec<&'static str> = vec![
const PRIMES: &'static [&'static str] = &[
"2",
"3",
"5",
Expand Down Expand Up @@ -469,85 +466,84 @@ mod tests {
"9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576599", // E-382: 2^382-105
"42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472367", // Curve41417: 2^414-17
"6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", // E-521: 2^521-1
];

static ref COMPOSITES: Vec<&'static str> = vec![
"0",
"1",

"21284175091214687912771199898307297748211672914763848041968395774954376176754",
"6084766654921918907427900243509372380954290099172559290432744450051395395951",
"84594350493221918389213352992032324280367711247940675652888030554255915464401",
"82793403787388584738507275144194252681",

// Arnault, "Rabin-Miller Primality Test: Composite Numbers Which Pass It",
// Mathematics of Computation, 64(209) (January 1995), pp. 335-361.
"1195068768795265792518361315725116351898245581", // strong pseudoprime to prime bases 2 through 29
// strong pseudoprime to all prime bases up to 200
"8038374574536394912570796143419421081388376882875581458374889175222974273765333652186502336163960045457915042023603208766569966760987284043965408232928738791850869166857328267761771029389697739470167082304286871099974399765441448453411558724506334092790222752962294149842306881685404326457534018329786111298960644845216191652872597534901",

// Extra-strong Lucas pseudoprimes. https://oeis.org/A217719
"989",
"3239",
"5777",
"10877",
"27971",
"29681",
"30739",
"31631",
"39059",
"72389",
"73919",
"75077",
"100127",
"113573",
"125249",
"137549",
"137801",
"153931",
"155819",
"161027",
"162133",
"189419",
"218321",
"231703",
"249331",
"370229",
"429479",
"430127",
"459191",
"473891",
"480689",
"600059",
"621781",
"632249",
"635627",

"3673744903",
"3281593591",
"2385076987",
"2738053141",
"2009621503",
"1502682721",
"255866131",
"117987841",
"587861",

"6368689",
"8725753",
"80579735209",
"105919633",
];

// Test Cases from #51
static ref ISSUE_51: Vec<&'static str> = vec![
"1579751",
"1884791",
"3818929",
"4080359",
"4145951",
];
}
];

const COMPOSITES: &'static [&'static str] = &[
"0",
"1",

"21284175091214687912771199898307297748211672914763848041968395774954376176754",
"6084766654921918907427900243509372380954290099172559290432744450051395395951",
"84594350493221918389213352992032324280367711247940675652888030554255915464401",
"82793403787388584738507275144194252681",

// Arnault, "Rabin-Miller Primality Test: Composite Numbers Which Pass It",
// Mathematics of Computation, 64(209) (January 1995), pp. 335-361.
"1195068768795265792518361315725116351898245581", // strong pseudoprime to prime bases 2 through 29
// strong pseudoprime to all prime bases up to 200
"8038374574536394912570796143419421081388376882875581458374889175222974273765333652186502336163960045457915042023603208766569966760987284043965408232928738791850869166857328267761771029389697739470167082304286871099974399765441448453411558724506334092790222752962294149842306881685404326457534018329786111298960644845216191652872597534901",

// Extra-strong Lucas pseudoprimes. https://oeis.org/A217719
"989",
"3239",
"5777",
"10877",
"27971",
"29681",
"30739",
"31631",
"39059",
"72389",
"73919",
"75077",
"100127",
"113573",
"125249",
"137549",
"137801",
"153931",
"155819",
"161027",
"162133",
"189419",
"218321",
"231703",
"249331",
"370229",
"429479",
"430127",
"459191",
"473891",
"480689",
"600059",
"621781",
"632249",
"635627",

"3673744903",
"3281593591",
"2385076987",
"2738053141",
"2009621503",
"1502682721",
"255866131",
"117987841",
"587861",

"6368689",
"8725753",
"80579735209",
"105919633",
];

// Test Cases from #51
const ISSUE_51: &'static [&'static str] = &[
"1579751",
"1884791",
"3818929",
"4080359",
"4145951",
];

#[test]
fn test_primes() {
Expand Down

0 comments on commit 3c0e6f1

Please sign in to comment.