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

Re-add the fast_test feature #101

Merged
merged 7 commits into from
Jan 2, 2025
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This file contains the changes to the crate since version 0.4.8.

## 0.10.2

- Re-added the `fast_test` feature, as the feature issue has been resolved.

## 0.10.1

- Removed reference to no longer existing feature from docstring of `is_prime`.
Expand Down
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "const-primes"
authors = ["Johanna Sörngård <[email protected]>"]
version = "0.10.1"
version = "0.10.2"
edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["const", "primes", "no_std", "prime-numbers"]
Expand All @@ -12,6 +12,7 @@ documentation = "https://docs.rs/const-primes"
rust-version = "1.81.0"

[dependencies]
machine-prime = { version = "1.5.2", optional = true, default-features = false, features = ["lucas"] }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
serde_arrays = { version = "0.1.0", optional = true }
zerocopy = { version = "0.8", default-features = false, features = ["derive"], optional = true }
Expand All @@ -23,6 +24,10 @@ rand = { version = "0.8", default-features = false, features = ["small_rng"] }
serde_json = "1.0"

[features]

# Significantly speed up primality testing by depending on the [`machine-prime`](https://crates.io/crates/machine_prime) crate.
fast_test = ["dep:machine-prime"]

# Derives the `Serialize` and `Deserialize` traits from the [`serde`](https://crates.io/crates/serde) crate for the `Primes` struct, as well as a few others.
# Uses the [`serde_arrays`](https://crates.io/crates/serde_arrays) crate to do this, and that crate uses the standard library.
serde = ["dep:serde", "dep:serde_arrays"]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ and more!

## Features

`fast_test`: significantly speed up primality testing by depending on [`machine-prime`](https://crates.io/crates/machine_prime).

`serde`: derives the `Serialize` and `Deserialize` traits from [`serde`](https://crates.io/crates/serde)
for the `Primes` struct, as well as a few others.
Uses the [`serde_arrays`](https://crates.io/crates/serde_arrays)
Expand Down
109 changes: 60 additions & 49 deletions src/check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! This module contains an implementation of a deterministic Miller-Rabin primality test

#[cfg(not(feature = "fast_test"))]
use crate::integer_math::{mod_mul, mod_pow};

/// Returns whether `n` is prime.
Expand All @@ -17,66 +19,75 @@ use crate::integer_math::{mod_mul, mod_pow};
/// ```
#[must_use]
pub const fn is_prime(n: u64) -> bool {
// Since we know the maximum size of the numbers we test against
// we can use the fact that there are known perfect bases
// in order to make the test both fast and deterministic.
// This list of witnesses was taken from
// <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>.
const NUM_BASES: usize = 11;
const WITNESSES: [(u64, &[u64]); NUM_BASES] = [
(2_046, &[2]),
(1_373_652, &[2, 3]),
(9_080_190, &[31, 73]),
(25_326_000, &[2, 3, 5]),
(4_759_123_140, &[2, 7, 61]),
(1_112_004_669_632, &[2, 13, 23, 1_662_803]),
(2_152_302_898_746, &[2, 3, 5, 7, 11]),
(3_474_749_660_382, &[2, 3, 5, 7, 11, 13]),
(341_550_071_728_320, &[2, 3, 5, 7, 11, 13, 17]),
(3_825_123_056_546_413_050, &[2, 3, 5, 7, 11, 13, 17, 19, 23]),
(u64::MAX, &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]),
];

if n == 2 || n == 3 {
return true;
} else if n <= 1 || n % 2 == 0 || n % 3 == 0 {
return false;
#[cfg(feature = "fast_test")]
{
machine_prime::is_prime(n)
}

// Use a small wheel to check up to log2(n).
// This keeps the complexity at O(log(n)).
let mut candidate_factor = 5;
let trial_limit = n.ilog2() as u64;
while candidate_factor <= trial_limit {
if n % candidate_factor == 0 || n % (candidate_factor + 2) == 0 {
#[cfg(not(feature = "fast_test"))]
{
// Since we know the maximum size of the numbers we test against
// we can use the fact that there are known perfect bases
// in order to make the test both fast and deterministic.
// This list of witnesses was taken from
// <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>.
const NUM_BASES: usize = 11;
const WITNESSES: [(u64, &[u64]); NUM_BASES] = [
(2_046, &[2]),
(1_373_652, &[2, 3]),
(9_080_190, &[31, 73]),
(25_326_000, &[2, 3, 5]),
(4_759_123_140, &[2, 7, 61]),
(1_112_004_669_632, &[2, 13, 23, 1_662_803]),
(2_152_302_898_746, &[2, 3, 5, 7, 11]),
(3_474_749_660_382, &[2, 3, 5, 7, 11, 13]),
(341_550_071_728_320, &[2, 3, 5, 7, 11, 13, 17]),
(3_825_123_056_546_413_050, &[2, 3, 5, 7, 11, 13, 17, 19, 23]),
(u64::MAX, &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]),
];

if n == 2 || n == 3 {
return true;
} else if n <= 1 || n % 2 == 0 || n % 3 == 0 {
return false;
}
candidate_factor += 6;
}

// Find r such that n = 2^d * r + 1 for some r >= 1
let mut d = n - 1;
while d % 2 == 0 {
d >>= 1;
}
// Use a small wheel to check up to log2(n).
// This keeps the complexity at O(log(n)).
let mut candidate_factor = 5;
let trial_limit = n.ilog2() as u64;
while candidate_factor <= trial_limit {
if n % candidate_factor == 0 || n % (candidate_factor + 2) == 0 {
return false;
}
candidate_factor += 6;
}

let mut i = 0;
while i < NUM_BASES && WITNESSES[i].0 < n {
i += 1;
}
let witnesses = WITNESSES[i].1;
// Find r such that n = 2^d * r + 1 for some r >= 1
let mut d = n - 1;
while d % 2 == 0 {
d >>= 1;
}

let mut i = 0;
while i < witnesses.len() && witnesses[i] < n {
if !miller_test(d, n, witnesses[i]) {
return false;
let mut i = 0;
while i < NUM_BASES && WITNESSES[i].0 < n {
i += 1;
}
i += 1;
}
let witnesses = WITNESSES[i].1;

true
let mut i = 0;
while i < witnesses.len() && witnesses[i] < n {
if !miller_test(d, n, witnesses[i]) {
return false;
}
i += 1;
}

true
}
}

#[cfg(not(feature = "fast_test"))]
/// Performs a Miller-Rabin test with the witness k.
const fn miller_test(mut d: u64, n: u64, k: u64) -> bool {
let mut x = mod_pow(k, d, n);
Expand Down
2 changes: 2 additions & 0 deletions src/integer_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const fn isqrt(n: u64) -> u64 {
}
}

#[cfg(not(feature = "fast_test"))]
/// Calculates (`base` ^ `exp`) mod `modulo` without overflow.
#[must_use]
pub const fn mod_pow(mut base: u64, mut exp: u64, modulo: u64) -> u64 {
Expand All @@ -50,6 +51,7 @@ pub const fn mod_pow(mut base: u64, mut exp: u64, modulo: u64) -> u64 {
res
}

#[cfg(not(feature = "fast_test"))]
/// Calculates (`a` * `b`) mod `modulo` without overflow.
#[must_use]
pub const fn mod_mul(a: u64, b: u64, modulo: u64) -> u64 {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
//!
//! # Features
//!
//! `serde`: derives the [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize) traits from the [`serde`] crte for the [`Primes`] struct, as well as a few others.
//! `fast_test`: Significantly speed up the [`is_prime`] function by depending on the [`machine_prime`] crate.
//!
//! `serde`: derives the [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize) traits from the [`serde`] crate for the [`Primes`] struct, as well as a few others.
//! Uses the [`serde_arrays`](https://docs.rs/serde_arrays/0.1.0) crate to do this, and that crate uses the standard library.
//!
//! `zerocopy`: derives the [`IntoBytes`](zerocopy::IntoBytes) trait from the [`zerocopy`] crate for the [`Primes`] struct.
Expand Down
Loading