Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static with once_cell #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ name = "num-bigint-dig"
repository = "https://github.com/dignifiedquire/num-bigint"
version = "0.8.4"
edition = "2021"
rust-version = "1.56"
rust-version = "1.60"
readme = "README.md"
build = "build.rs"
autobenches = false
Expand Down 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![crate](https://img.shields.io/crates/v/num-bigint-dig.svg)](https://crates.io/crates/num-bigint-dig)
[![documentation](https://docs.rs/num-bigint-dig/badge.svg)](https://docs.rs/num-bigint-dig)
![minimum rustc 1.56](https://img.shields.io/badge/rustc-1.56+-red.svg)
![minimum rustc 1.60](https://img.shields.io/badge/rustc-1.60+-red.svg)
[![Travis status](https://travis-ci.org/dignifiedquire/num-bigint.svg?branch=master)](https://travis-ci.org/dignifiedquire/num-bigint)

Big integer types for Rust, `BigInt` and `BigUint`.
Expand Down Expand Up @@ -50,7 +50,7 @@ Release notes are available in [RELEASES.md](RELEASES.md).

## Compatibility

The `num-bigint` crate is tested for rustc 1.56 and greater.
The `num-bigint` crate is tested for rustc 1.60 and greater.

## Alternatives

Expand All @@ -60,7 +60,7 @@ table offers a brief comparison to a few alternatives.

| Crate | License | Min rustc | Implementation |
| :------------------- | :------------- |:----------| :------------- |
| **`num-bigint-dig`** | MIT/Apache-2.0 | 1.56 | pure rust |
| **`num-bigint-dig`** | MIT/Apache-2.0 | 1.60 | pure rust |
| [`num-bigint`] | MIT/Apache-2.0 | 1.15 | pure rust |
| [`ramp`] | Apache-2.0 | nightly | rust and inline assembly |
| [`rug`] | LGPL-3.0+ | 1.18 | bundles [GMP] via [`gmp-mpfr-sys`] |
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();
}
static 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
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
//!
//! ## Compatibility
//!
//! The `num-bigint-dig` crate is tested for rustc 1.56 and greater.
//! The `num-bigint-dig` crate is tested for rustc 1.60 and greater.
//!
//! ## `no_std` compatibility
//!
Expand Down 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) static BIG_1: Lazy<BigUint> = Lazy::new(|| BigUint::one());
pub(crate) static BIG_2: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(2).unwrap());
pub(crate) static 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