Skip to main content

o1_utils/
lib.rs

1//! A collection of utility functions and constants that can be reused from
2//! multiple projects
3
4// the feature `unsigned_is_multiple_of` has been stable since 1.87.0 and no longer requires an attribute to enable
5// See: https://github.com/o1-labs/mina-rust/issues/1997
6#![cfg_attr(not(feature = "std"), no_std)]
7#![deny(unsafe_code)]
8#![deny(clippy::all)]
9#![deny(clippy::pedantic)]
10#![deny(clippy::nursery)]
11
12extern crate alloc;
13
14/// Returns a parallel iterator when the `parallel` feature is enabled,
15/// otherwise returns a sequential iterator.
16#[macro_export]
17macro_rules! cfg_iter {
18    ($e:expr) => {{
19        #[cfg(feature = "parallel")]
20        let result = $e.par_iter();
21        #[cfg(not(feature = "parallel"))]
22        let result = $e.iter();
23        result
24    }};
25}
26
27/// Returns a parallel mutable iterator when the `parallel` feature is enabled,
28/// otherwise returns a sequential mutable iterator.
29#[macro_export]
30macro_rules! cfg_iter_mut {
31    ($e:expr) => {{
32        #[cfg(feature = "parallel")]
33        let result = $e.par_iter_mut();
34        #[cfg(not(feature = "parallel"))]
35        let result = $e.iter_mut();
36        result
37    }};
38}
39
40/// Returns a parallel consuming iterator when the `parallel` feature is enabled,
41/// otherwise returns a sequential consuming iterator.
42#[macro_export]
43macro_rules! cfg_into_iter {
44    ($e:expr) => {{
45        #[cfg(feature = "parallel")]
46        let result = $e.into_par_iter();
47        #[cfg(not(feature = "parallel"))]
48        let result = $e.into_iter();
49        result
50    }};
51}
52
53pub mod adjacent_pairs;
54pub mod biguint_helpers;
55pub mod bitwise_operations;
56#[cfg(feature = "std")]
57pub mod chunked_evaluations;
58pub mod chunked_polynomial;
59pub mod dense_polynomial;
60pub mod evaluations;
61pub mod field_helpers;
62pub mod foreign_field;
63pub mod hasher;
64pub mod lazy_cache;
65pub mod lazy_lock;
66pub mod math;
67pub mod serialization;
68
69pub use biguint_helpers::BigUintHelpers;
70pub use bitwise_operations::BitwiseOps;
71#[cfg(feature = "std")]
72pub use chunked_evaluations::ChunkedEvaluations;
73pub use dense_polynomial::ExtendedDensePolynomial;
74pub use evaluations::ExtendedEvaluations;
75#[cfg(feature = "std")]
76pub use field_helpers::RandomField;
77pub use field_helpers::{BigUintFieldHelpers, FieldHelpers, Two};
78pub use foreign_field::ForeignElement;
79
80/// Utils only for testing
81#[cfg(feature = "std")]
82pub mod tests {
83    use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
84
85    /// Create a new test rng with a random seed
86    #[must_use]
87    pub fn make_test_rng(seed: Option<[u8; 32]>) -> StdRng {
88        let seed = seed.unwrap_or_else(|| thread_rng().gen());
89        eprintln!("Seed: {seed:?}");
90        StdRng::from_seed(seed)
91    }
92}