Skip to main content

lib_q_ring/
lib.rs

1//! Shared ring arithmetic for ML-DSA / module-lattice constructions over
2//! \(R_q = \mathbb{Z}_q\[X\]/(X^{256}+1)\), \(q = 8\,380\,417\).
3//!
4//! Portable NTT (Cooley–Tukey forward, Gentleman–Sande inverse with Montgomery
5//! scaling) is bit-compatible with the non-`hardened` path in `lib-q-ml-dsa`.
6#![forbid(unsafe_code)]
7#![allow(missing_docs)]
8// Only the explicit `no_std` feature enables `#![no_std]` (dependents often use `alloc` without `std`).
9#![cfg_attr(feature = "no_std", no_std)]
10
11#[cfg(feature = "alloc")]
12extern crate alloc;
13
14#[cfg(all(not(feature = "std"), feature = "no_std_panic_handler"))]
15mod no_std_panic_handler {
16    use core::panic::PanicInfo;
17
18    #[panic_handler]
19    #[allow(clippy::empty_loop)]
20    fn panic(_info: &PanicInfo) -> ! {
21        loop {}
22    }
23}
24
25pub mod challenge;
26pub mod coeff;
27pub mod constants;
28pub mod encoding;
29pub mod field;
30
31mod generated_invntt;
32mod generated_ntt;
33
34pub mod ntt;
35pub mod params;
36pub mod poly;
37pub mod uniform;
38
39#[cfg(feature = "alloc")]
40pub mod expand;
41#[cfg(feature = "alloc")]
42pub mod module;
43
44pub use challenge::sample_in_ball;
45#[cfg(feature = "alloc")]
46pub use expand::expand_a_from_seed;
47pub use field::{
48    FieldElementTimesMontgomeryR,
49    add_coeffs,
50    montgomery_multiply_by_constant,
51    montgomery_multiply_coeffs,
52    montgomery_multiply_fe_by_fer,
53    montgomery_reduce_element,
54    reduce_element,
55    reduce_poly_simd,
56    subtract_coeffs,
57};
58#[cfg(feature = "alloc")]
59pub use module::{
60    ModuleMatrix,
61    ModuleVec,
62};
63pub use ntt::{
64    intt_montgomery,
65    ntt_forward_simd,
66    ntt_multiply_montgomery,
67};
68pub use poly::{
69    NttPoly,
70    Poly,
71    polys_norm_within_bound,
72};
73pub use uniform::{
74    sample_uniform_coeff_mod_q,
75    sample_uniform_field_coefficient,
76    try_uniform_coeff_mod_q_from_u32,
77    uniform_mod_u32_rejection_threshold,
78};
79
80#[cfg(feature = "wasm")]
81pub mod wasm;