Skip to content
Draft
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
matrix:
rust: [
1.60.0, # MSRV
1.63.0, # rand v0.9
1.85.0, # rand v0.10
stable,
beta,
nightly
Expand Down Expand Up @@ -69,7 +71,7 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
target: thumbv6m-none-eabi
- run: cargo build --target thumbv6m-none-eabi --no-default-features --features "serde rand"
- run: cargo build --target thumbv6m-none-eabi --no-default-features --features "serde rand_0_9 rand_0_10"

fmt:
name: Format
Expand All @@ -87,9 +89,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- run: cargo doc --features std,serde,rand,quickcheck,arbitrary
env:
RUSTDOCFLAGS: --cfg docsrs
- run: cargo rustdoc --features std,serde,rand_0_9,rand_0_10,quickcheck,arbitrary -- --cfg docsrs

# One job that "summarizes" the success state of this pipeline. This can then be added to branch
# protection, rather than having to add each job separately.
Expand Down
35 changes: 30 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ categories = [ "algorithms", "data-structures", "science" ]
license = "MIT OR Apache-2.0"
name = "num-bigint"
repository = "https://github.com/rust-num/num-bigint"
version = "0.4.6"
version = "0.5.0"
readme = "README.md"
exclude = ["/ci/*", "/.github/*"]
edition = "2021"
Expand All @@ -19,24 +19,30 @@ default = ["std"]
std = ["num-integer/std", "num-traits/std"]
arbitrary = ["dep:arbitrary"]
quickcheck = ["dep:quickcheck"]
rand = ["dep:rand"]
rand_0_9 = ["rand_core_0_9", "dep:rand_0_9"]
rand_core_0_9 = ["dep:rand_core_0_9"]
rand_0_10 = ["rand_core_0_10", "dep:rand_0_10"]
rand_core_0_10 = ["dep:rand_core_0_10"]
serde = ["dep:serde"]

[package.metadata.docs.rs]
features = ["std", "serde", "rand", "quickcheck", "arbitrary"]
features = ["std", "serde", "rand_0_10", "rand_0_9", "quickcheck", "arbitrary"]
rustdoc-args = ["--cfg", "docsrs"]

[[bench]]
name = "bigint"
required-features = ["rand_0_10"]

[[bench]]
name = "factorial"

[[bench]]
name = "gcd"
required-features = ["rand_0_10"]

[[bench]]
name = "roots"
required-features = ["rand_0_10"]

[[bench]]
harness = false
Expand All @@ -54,9 +60,28 @@ version = "0.2.18"
default-features = false
features = ["i128"]

[dependencies.rand]
[dependencies.rand_0_9]
package = "rand"
optional = true
version = "0.8"
version = "0.9"
default-features = false

[dependencies.rand_core_0_9]
package = "rand_core"
optional = true
version = "0.9"
default-features = false

[dependencies.rand_0_10]
package = "rand"
optional = true
version = "0.10"
default-features = false

[dependencies.rand_core_0_10]
package = "rand_core"
optional = true
version = "0.10"
default-features = false

[dependencies.serde]
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
num-bigint = "0.4"
num-bigint = "0.5"
```

## Features
Expand All @@ -25,16 +25,18 @@ if your compiler is not new enough.

### Random Generation

`num-bigint` supports the generation of random big integers when the `rand`
feature is enabled. To enable it include rand as
`num-bigint` supports the generation of random big integers when either of the
`rand_0_9` or `rand_0_10` features are enabled. For example:

```toml
rand = "0.8"
num-bigint = { version = "0.4", features = ["rand"] }
rand = "0.10"
num-bigint = { version = "0.5", features = ["rand_0_10"] }
```

Note that you must use the version of `rand` that `num-bigint` is compatible
with: `0.8`.
Note that you must use the same version of `rand` as the feature you enable.

You can instead use `rand_core_0_9` or `rand_core_0_10` for a more restricted
subset.

## Releases

Expand Down
39 changes: 19 additions & 20 deletions benches/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![feature(test)]
#![cfg(feature = "rand")]

extern crate test;

use num_bigint::{BigInt, BigUint, RandBigInt};
use num_bigint::{BigInt, BigRng010, BigUint};
use num_traits::{FromPrimitive, Num, One, Zero};
use std::mem::replace;
use test::Bencher;
Expand All @@ -13,24 +12,24 @@ use rng::get_rng;

fn multiply_bench(b: &mut Bencher, xbits: u64, ybits: u64) {
let mut rng = get_rng();
let x = rng.gen_bigint(xbits);
let y = rng.gen_bigint(ybits);
let x = rng.random_bigint(xbits);
let y = rng.random_bigint(ybits);

b.iter(|| &x * &y);
}

fn divide_bench(b: &mut Bencher, xbits: u64, ybits: u64) {
let mut rng = get_rng();
let x = rng.gen_bigint(xbits);
let y = rng.gen_bigint(ybits);
let x = rng.random_bigint(xbits);
let y = rng.random_bigint(ybits);

b.iter(|| &x / &y);
}

fn remainder_bench(b: &mut Bencher, xbits: u64, ybits: u64) {
let mut rng = get_rng();
let x = rng.gen_bigint(xbits);
let y = rng.gen_bigint(ybits);
let x = rng.random_bigint(xbits);
let y = rng.random_bigint(ybits);

b.iter(|| &x % &y);
}
Expand Down Expand Up @@ -186,7 +185,7 @@ fn fib_to_string(b: &mut Bencher) {

fn to_str_radix_bench(b: &mut Bencher, radix: u32, bits: u64) {
let mut rng = get_rng();
let x = rng.gen_bigint(bits);
let x = rng.random_bigint(bits);
b.iter(|| x.to_str_radix(radix));
}

Expand Down Expand Up @@ -222,7 +221,7 @@ fn to_str_radix_36(b: &mut Bencher) {

fn from_str_radix_bench(b: &mut Bencher, radix: u32) {
let mut rng = get_rng();
let x = rng.gen_bigint(1009);
let x = rng.random_bigint(1009);
let s = x.to_str_radix(radix);
assert_eq!(x, BigInt::from_str_radix(&s, radix).unwrap());
b.iter(|| BigInt::from_str_radix(&s, radix));
Expand Down Expand Up @@ -256,7 +255,7 @@ fn from_str_radix_36(b: &mut Bencher) {
fn rand_bench(b: &mut Bencher, bits: u64) {
let mut rng = get_rng();

b.iter(|| rng.gen_bigint(bits));
b.iter(|| rng.random_bigint(bits));
}

#[bench]
Expand Down Expand Up @@ -327,7 +326,7 @@ fn shr(b: &mut Bencher) {
fn hash(b: &mut Bencher) {
use std::collections::HashSet;
let mut rng = get_rng();
let v: Vec<BigInt> = (1000..2000).map(|bits| rng.gen_bigint(bits)).collect();
let v: Vec<BigInt> = (1000..2000).map(|bits| rng.random_bigint(bits)).collect();
b.iter(|| {
let h: HashSet<&BigInt> = v.iter().collect();
assert_eq!(h.len(), v.len());
Expand Down Expand Up @@ -399,8 +398,8 @@ const RFC3526_2048BIT_MODP_GROUP: &str = "\
#[bench]
fn modpow(b: &mut Bencher) {
let mut rng = get_rng();
let base = rng.gen_biguint(2048);
let e = rng.gen_biguint(2048);
let base = rng.random_biguint(2048);
let e = rng.random_biguint(2048);
let m = BigUint::from_str_radix(RFC3526_2048BIT_MODP_GROUP, 16).unwrap();

b.iter(|| base.modpow(&e, &m));
Expand All @@ -409,8 +408,8 @@ fn modpow(b: &mut Bencher) {
#[bench]
fn modpow_even(b: &mut Bencher) {
let mut rng = get_rng();
let base = rng.gen_biguint(2048);
let e = rng.gen_biguint(2048);
let base = rng.random_biguint(2048);
let e = rng.random_biguint(2048);
// Make the modulus even, so monty (base-2^32) doesn't apply.
let m = BigUint::from_str_radix(RFC3526_2048BIT_MODP_GROUP, 16).unwrap() - 1u32;

Expand All @@ -420,31 +419,31 @@ fn modpow_even(b: &mut Bencher) {
#[bench]
fn to_u32_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);
let n = rng.random_biguint(2048);

b.iter(|| n.to_u32_digits());
}

#[bench]
fn iter_u32_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);
let n = rng.random_biguint(2048);

b.iter(|| n.iter_u32_digits().max());
}

#[bench]
fn to_u64_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);
let n = rng.random_biguint(2048);

b.iter(|| n.to_u64_digits());
}

#[bench]
fn iter_u64_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);
let n = rng.random_biguint(2048);

b.iter(|| n.iter_u64_digits().max());
}
7 changes: 3 additions & 4 deletions benches/gcd.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![feature(test)]
#![cfg(feature = "rand")]

extern crate test;

use num_bigint::{BigUint, RandBigInt};
use num_bigint::{BigRng010, BigUint};
use num_integer::Integer;
use num_traits::Zero;
use test::Bencher;
Expand All @@ -13,8 +12,8 @@ use rng::get_rng;

fn bench(b: &mut Bencher, bits: u64, gcd: fn(&BigUint, &BigUint) -> BigUint) {
let mut rng = get_rng();
let x = rng.gen_biguint(bits);
let y = rng.gen_biguint(bits);
let x = rng.random_biguint(bits);
let y = rng.random_biguint(bits);

assert_eq!(euclid(&x, &y), x.gcd(&y));

Expand Down
40 changes: 4 additions & 36 deletions benches/rng/mod.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@
use rand::RngCore;
use rand_0_10::rngs::Xoshiro128PlusPlus;
use rand_0_10::{Rng, SeedableRng};

pub(crate) fn get_rng() -> impl RngCore {
XorShiftStar {
a: 0x0123_4567_89AB_CDEF,
}
}

/// Simple `Rng` for benchmarking without additional dependencies
struct XorShiftStar {
a: u64,
}

impl RngCore for XorShiftStar {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}

fn next_u64(&mut self) -> u64 {
// https://en.wikipedia.org/wiki/Xorshift#xorshift*
self.a ^= self.a >> 12;
self.a ^= self.a << 25;
self.a ^= self.a >> 27;
self.a.wrapping_mul(0x2545_F491_4F6C_DD1D)
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
for chunk in dest.chunks_mut(8) {
let bytes = self.next_u64().to_le_bytes();
let slice = &bytes[..chunk.len()];
chunk.copy_from_slice(slice)
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
Ok(self.fill_bytes(dest))
}
pub(crate) fn get_rng() -> impl Rng {
Xoshiro128PlusPlus::from_seed(*b"benching bigints")
}
9 changes: 4 additions & 5 deletions benches/roots.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![feature(test)]
#![cfg(feature = "rand")]

extern crate test;

use num_bigint::{BigUint, RandBigInt};
use num_bigint::{BigRng010, BigUint};
use test::Bencher;

mod rng;
Expand Down Expand Up @@ -37,7 +36,7 @@ fn check(x: &BigUint, n: u32) {
}

fn bench_sqrt(b: &mut Bencher, bits: u64) {
let x = get_rng().gen_biguint(bits);
let x = get_rng().random_biguint(bits);
eprintln!("bench_sqrt({})", x);

check(&x, 2);
Expand Down Expand Up @@ -65,7 +64,7 @@ fn big4k_sqrt(b: &mut Bencher) {
}

fn bench_cbrt(b: &mut Bencher, bits: u64) {
let x = get_rng().gen_biguint(bits);
let x = get_rng().random_biguint(bits);
eprintln!("bench_cbrt({})", x);

check(&x, 3);
Expand Down Expand Up @@ -93,7 +92,7 @@ fn big4k_cbrt(b: &mut Bencher) {
}

fn bench_nth_root(b: &mut Bencher, bits: u64, n: u32) {
let x = get_rng().gen_biguint(bits);
let x = get_rng().random_biguint(bits);
eprintln!("bench_{}th_root({})", n, x);

check(&x, n);
Expand Down
1 change: 1 addition & 0 deletions ci/big_quickcheck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "big_quickcheck"
version = "0.1.0"
authors = ["Josh Stone <cuviper@gmail.com>"]
edition = "2018"
rust-version = "1.60"

[dependencies]
num-integer = "0.1.42"
Expand Down
16 changes: 0 additions & 16 deletions ci/big_rand/Cargo.toml

This file was deleted.

Loading