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

Remove bit precision from the public API #46

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ categories = ["cryptography", "no-std"]
rust-version = "1.73"

[dependencies]
crypto-bigint = { version = "0.6.0-pre.7", default-features = false, features = ["rand_core"] }
crypto-bigint = { version = "0.6.0-pre.7", default-features = false, features = ["rand_core", "zeroize"] }
rand_core = { version = "0.6.4", default-features = false }
openssl = { version = "0.10.39", optional = true, features = ["vendored"] }
rug = { version = "1.26", default-features = false, features = ["integer"], optional = true }
Expand Down
71 changes: 37 additions & 34 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ fn make_rng() -> ChaCha8Rng {
fn random_odd_uint<T: RandomBits + Integer>(
rng: &mut impl CryptoRngCore,
bit_length: u32,
bits_precision: u32,
) -> Odd<T> {
random_odd_integer::<T>(rng, NonZeroU32::new(bit_length).unwrap(), bits_precision)
random_odd_integer::<T>(rng, NonZeroU32::new(bit_length).unwrap())
}

fn make_sieve<const L: usize>(rng: &mut impl CryptoRngCore) -> Sieve<Uint<L>> {
let start = random_odd_uint::<Uint<L>>(rng, Uint::<L>::BITS, Uint::<L>::BITS);
Sieve::new(&start, NonZeroU32::new(Uint::<L>::BITS).unwrap(), false)
let start = random_odd_uint::<Uint<L>>(rng, Uint::<L>::BITS);
Sieve::new(
start.get(),
NonZeroU32::new(Uint::<L>::BITS).unwrap(),
false,
)
}

fn make_presieved_num<const L: usize>(rng: &mut impl CryptoRngCore) -> Odd<Uint<L>> {
Expand All @@ -46,13 +49,13 @@ fn bench_sieve(c: &mut Criterion) {
let mut group = c.benchmark_group("Sieve");

group.bench_function("(U128) random start", |b| {
b.iter(|| random_odd_uint::<U128>(&mut OsRng, 128, 128))
b.iter(|| random_odd_uint::<U128>(&mut OsRng, 128))
});

group.bench_function("(U128) creation", |b| {
b.iter_batched(
|| random_odd_uint::<U128>(&mut OsRng, 128, 128),
|start| Sieve::new(start.as_ref(), NonZeroU32::new(128).unwrap(), false),
|| random_odd_uint::<U128>(&mut OsRng, 128),
|start| Sieve::new(start.get(), NonZeroU32::new(128).unwrap(), false),
BatchSize::SmallInput,
)
});
Expand All @@ -67,13 +70,13 @@ fn bench_sieve(c: &mut Criterion) {
});

group.bench_function("(U1024) random start", |b| {
b.iter(|| random_odd_uint::<U1024>(&mut OsRng, 1024, 1024))
b.iter(|| random_odd_uint::<U1024>(&mut OsRng, 1024))
});

group.bench_function("(U1024) creation", |b| {
b.iter_batched(
|| random_odd_uint::<U1024>(&mut OsRng, 1024, 1024),
|start| Sieve::new(start.as_ref(), NonZeroU32::new(1024).unwrap(), false),
|| random_odd_uint::<U1024>(&mut OsRng, 1024),
|start| Sieve::new(start.get(), NonZeroU32::new(1024).unwrap(), false),
BatchSize::SmallInput,
)
});
Expand All @@ -94,31 +97,31 @@ fn bench_miller_rabin(c: &mut Criterion) {

group.bench_function("(U128) creation", |b| {
b.iter_batched(
|| random_odd_uint::<U128>(&mut OsRng, 128, 128),
|n| MillerRabin::new(&n),
|| random_odd_uint::<U128>(&mut OsRng, 128),
MillerRabin::<U128>::new,
BatchSize::SmallInput,
)
});

group.bench_function("(U128) random base test (pre-sieved)", |b| {
b.iter_batched(
|| MillerRabin::new(&make_presieved_num::<{ nlimbs!(128) }>(&mut OsRng)),
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(128) }>(&mut OsRng)),
|mr| mr.test_random_base(&mut OsRng),
BatchSize::SmallInput,
)
});

group.bench_function("(U1024) creation", |b| {
b.iter_batched(
|| random_odd_uint::<U1024>(&mut OsRng, 1024, 1024),
|n| MillerRabin::new(&n),
|| random_odd_uint::<U1024>(&mut OsRng, 1024),
MillerRabin::<U1024>::new,
BatchSize::SmallInput,
)
});

group.bench_function("(U1024) random base test (pre-sieved)", |b| {
b.iter_batched(
|| MillerRabin::new(&make_presieved_num::<{ nlimbs!(1024) }>(&mut OsRng)),
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(1024) }>(&mut OsRng)),
|mr| mr.test_random_base(&mut OsRng),
BatchSize::SmallInput,
)
Expand All @@ -132,7 +135,7 @@ fn bench_lucas(c: &mut Criterion) {
group.bench_function("(U128) Selfridge base, strong check (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(128) }>(&mut rng),
|n| lucas_test(&n, SelfridgeBase, LucasCheck::Strong),
|n| lucas_test(n, SelfridgeBase, LucasCheck::Strong),
BatchSize::SmallInput,
)
});
Expand All @@ -141,7 +144,7 @@ fn bench_lucas(c: &mut Criterion) {
group.bench_function("(U1024) Selfridge base, strong check (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, SelfridgeBase, LucasCheck::Strong),
|n| lucas_test(n, SelfridgeBase, LucasCheck::Strong),
BatchSize::SmallInput,
)
});
Expand All @@ -150,7 +153,7 @@ fn bench_lucas(c: &mut Criterion) {
group.bench_function("(U1024) A* base, Lucas-V check (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, AStarBase, LucasCheck::LucasV),
|n| lucas_test(n, AStarBase, LucasCheck::LucasV),
BatchSize::SmallInput,
)
});
Expand All @@ -161,7 +164,7 @@ fn bench_lucas(c: &mut Criterion) {
|b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, BruteForceBase, LucasCheck::AlmostExtraStrong),
|n| lucas_test(n, BruteForceBase, LucasCheck::AlmostExtraStrong),
BatchSize::SmallInput,
)
},
Expand All @@ -171,7 +174,7 @@ fn bench_lucas(c: &mut Criterion) {
group.bench_function("(U1024) brute force base, extra strong (pre-sieved)", |b| {
b.iter_batched(
|| make_presieved_num::<{ nlimbs!(1024) }>(&mut rng),
|n| lucas_test(&n, BruteForceBase, LucasCheck::ExtraStrong),
|n| lucas_test(n, BruteForceBase, LucasCheck::ExtraStrong),
BatchSize::SmallInput,
)
});
Expand All @@ -191,7 +194,7 @@ fn bench_lucas(c: &mut Criterion) {

group.bench_function("(U1024) Selfridge base, strong check, slow path", |b| {
b.iter(|| {
lucas_test(&slow_path, SelfridgeBase, LucasCheck::Strong);
lucas_test(slow_path, SelfridgeBase, LucasCheck::Strong);
})
});

Expand All @@ -203,50 +206,50 @@ fn bench_presets(c: &mut Criterion) {

group.bench_function("(U128) Prime test", |b| {
b.iter_batched(
|| random_odd_uint::<U128>(&mut OsRng, 128, 128),
|| random_odd_uint::<U128>(&mut OsRng, 128),
|num| is_prime_with_rng(&mut OsRng, num.as_ref()),
BatchSize::SmallInput,
)
});

group.bench_function("(U128) Safe prime test", |b| {
b.iter_batched(
|| random_odd_uint::<U128>(&mut OsRng, 128, 128),
|| random_odd_uint::<U128>(&mut OsRng, 128),
|num| is_safe_prime_with_rng(&mut OsRng, num.as_ref()),
BatchSize::SmallInput,
)
});

let mut rng = make_rng();
group.bench_function("(U128) Random prime", |b| {
b.iter(|| generate_prime_with_rng::<U128>(&mut rng, 128, 128))
b.iter(|| generate_prime_with_rng::<U128>(&mut rng, 128))
});

let mut rng = make_rng();
group.bench_function("(U1024) Random prime", |b| {
b.iter(|| generate_prime_with_rng::<U1024>(&mut rng, 1024, 1024))
b.iter(|| generate_prime_with_rng::<U1024>(&mut rng, 1024))
});

let mut rng = make_rng();
group.bench_function("(U128) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<U128>(&mut rng, 128, 128))
b.iter(|| generate_safe_prime_with_rng::<U128>(&mut rng, 128))
});

group.sample_size(20);
let mut rng = make_rng();
group.bench_function("(U1024) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<U1024>(&mut rng, 1024, 1024))
b.iter(|| generate_safe_prime_with_rng::<U1024>(&mut rng, 1024))
});

let mut rng = make_rng();
group.bench_function("(Boxed128) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 128, 128))
b.iter(|| generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 128))
});

group.sample_size(20);
let mut rng = make_rng();
group.bench_function("(Boxed1024) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 1024, 1024))
b.iter(|| generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 1024))
});

group.finish();
Expand All @@ -256,19 +259,19 @@ fn bench_presets(c: &mut Criterion) {

let mut rng = make_rng();
group.bench_function("(U128) Random safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<U128>(&mut rng, 128, 128))
b.iter(|| generate_safe_prime_with_rng::<U128>(&mut rng, 128))
});

// The performance should scale with the prime size, not with the Uint size.
// So we should strive for this test's result to be as close as possible
// to that of the previous one and as far away as possible from the next one.
group.bench_function("(U256) Random 128 bit safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<U256>(&mut rng, 128, 256))
b.iter(|| generate_safe_prime_with_rng::<U256>(&mut rng, 128))
});

// The upper bound for the previous test.
group.bench_function("(U256) Random 256 bit safe prime", |b| {
b.iter(|| generate_safe_prime_with_rng::<U256>(&mut rng, 256, 256))
b.iter(|| generate_safe_prime_with_rng::<U256>(&mut rng, 256))
});

group.finish();
Expand All @@ -279,7 +282,7 @@ fn bench_gmp(c: &mut Criterion) {
let mut group = c.benchmark_group("GMP");

fn random<const L: usize>(rng: &mut impl CryptoRngCore) -> GmpInteger {
let num = random_odd_uint::<Uint<L>>(rng, Uint::<L>::BITS, Uint::<L>::BITS).get();
let num = random_odd_uint::<Uint<L>>(rng, Uint::<L>::BITS).get();
GmpInteger::from_digits(num.as_words(), Order::Lsf)
}

Expand Down
Loading
Loading