Skip to main content

det_prime/
lib.rs

1#![no_std]
2#![forbid(unsafe_code)]
3#![doc = include_str!("../README.md")]
4
5#[cfg(feature = "std")]
6extern crate std;
7
8pub mod bounds;
9pub mod field;
10pub mod primality;
11pub mod sieve;
12pub mod uint;
13pub mod wasm;
14
15use core::fmt;
16
17pub use bounds::{
18    bach_bound_from_km, bach_witness_bound, lls_bound_from_km, lls_nonresidue_bound,
19    ln_upper_scaled, progression_prime_bound, rh_next_prime_step_budget_u64, MAX_SIEVE_BITS,
20};
21pub use field::{
22    euler_totient_u64, hash_to_prime_256, hash_to_prime_256_bounded, jacobi_prime_mod_odd,
23    jacobi_u64, least_nonresidue, next_prime_bounded, prime_in_progression_u64, sqrt_mod_prime,
24    DEFAULT_HASH_TO_PRIME_STEPS,
25};
26pub use primality::{
27    is_prime, is_prime_u64, verify_miller_bach, CompositeWitness, PrimalityCertificate,
28};
29pub use sieve::{PrimeIter, MAX_SIEVE_LIMIT};
30pub use uint::{MontCtx, Uint, U1024, U128, U2048, U256, U384, U4096, U512};
31pub use wasm::{
32    hash_to_prime_be_32, is_prime_be_32, is_prime_be_slice, sqrt_mod_prime_be, sqrt_mod_prime_be_32,
33};
34
35/// Error type for deterministic prime and finite-field operations.
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum Error {
38    /// Malformed hexadecimal, decimal, or byte input.
39    InvalidInput,
40    /// Value exceeds target fixed-width limb capacity.
41    Overflow,
42    /// Input residue `a` does not satisfy `0 <= a < p`.
43    OutOfRange,
44    /// Modulus is even or below the required threshold.
45    InvalidModulus,
46    /// Modulus failed a compositeness or Euler criterion check.
47    CompositeModulus,
48    /// Progression residue `a` is not coprime to modulus `q`.
49    NotCoprime,
50    /// Candidate scan exceeded the configured deterministic step cap.
51    StepBudgetExceeded,
52}
53
54impl fmt::Display for Error {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        let msg = match self {
57            Self::InvalidInput => "invalid numeric input",
58            Self::Overflow => "integer overflow for target width",
59            Self::OutOfRange => "input residue out of range for modulus",
60            Self::InvalidModulus => "modulus must be odd and above threshold",
61            Self::CompositeModulus => "modulus failed primality or Euler check",
62            Self::NotCoprime => "progression residue is not coprime to modulus",
63            Self::StepBudgetExceeded => "deterministic candidate step cap exceeded",
64        };
65        f.write_str(msg)
66    }
67}
68
69#[cfg(feature = "std")]
70impl std::error::Error for Error {}