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 iterator over mutable chunks when the `parallel` feature
41/// is enabled, otherwise returns a sequential one.
42#[macro_export]
43macro_rules! cfg_chunks_mut {
44    ($e:expr, $size:expr) => {{
45        #[cfg(feature = "parallel")]
46        let result = $e.par_chunks_mut($size);
47        #[cfg(not(feature = "parallel"))]
48        let result = $e.chunks_mut($size);
49        result
50    }};
51}
52
53/// Returns a parallel consuming iterator when the `parallel` feature is enabled,
54/// otherwise returns a sequential consuming iterator.
55#[macro_export]
56macro_rules! cfg_into_iter {
57    ($e:expr) => {{
58        #[cfg(feature = "parallel")]
59        let result = $e.into_par_iter();
60        #[cfg(not(feature = "parallel"))]
61        let result = $e.into_iter();
62        result
63    }};
64}
65
66pub mod adjacent_pairs;
67pub mod biguint_helpers;
68pub mod bitwise_operations;
69#[cfg(feature = "std")]
70pub mod chunked_evaluations;
71pub mod chunked_polynomial;
72pub mod dense_polynomial;
73pub mod evaluations;
74pub mod field_helpers;
75pub mod foreign_field;
76pub mod hasher;
77pub mod lazy_cache;
78pub mod lazy_lock;
79pub mod math;
80pub mod serialization;
81
82pub use biguint_helpers::BigUintHelpers;
83pub use bitwise_operations::BitwiseOps;
84#[cfg(feature = "std")]
85pub use chunked_evaluations::ChunkedEvaluations;
86pub use dense_polynomial::ExtendedDensePolynomial;
87pub use evaluations::ExtendedEvaluations;
88#[cfg(feature = "std")]
89pub use field_helpers::RandomField;
90pub use field_helpers::{BigUintFieldHelpers, FieldHelpers, Two};
91pub use foreign_field::ForeignElement;
92
93/// Utils only for testing
94#[cfg(feature = "std")]
95pub mod tests {
96    use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
97
98    /// Create a new test rng with a random seed
99    #[must_use]
100    pub fn make_test_rng(seed: Option<[u8; 32]>) -> StdRng {
101        let seed = seed.unwrap_or_else(|| thread_rng().gen());
102        eprintln!("Seed: {seed:?}");
103        StdRng::from_seed(seed)
104    }
105}