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// Enable unstable `is_multiple_of` on nightly for Wasm builds until nightly is updated
5// See: https://github.com/o1-labs/mina-rust/issues/1997
6#![cfg_attr(target_arch = "wasm32", feature(unsigned_is_multiple_of))]
7#![cfg_attr(not(feature = "std"), no_std)]
8#![deny(unsafe_code)]
9#![deny(clippy::all)]
10#![deny(clippy::pedantic)]
11#![deny(clippy::nursery)]
12
13extern crate alloc;
14
15/// Returns a parallel iterator when the `parallel` feature is enabled,
16/// otherwise returns a sequential iterator.
17#[macro_export]
18macro_rules! cfg_iter {
19    ($e:expr) => {{
20        #[cfg(feature = "parallel")]
21        let result = $e.par_iter();
22        #[cfg(not(feature = "parallel"))]
23        let result = $e.iter();
24        result
25    }};
26}
27
28/// Returns a parallel mutable iterator when the `parallel` feature is enabled,
29/// otherwise returns a sequential mutable iterator.
30#[macro_export]
31macro_rules! cfg_iter_mut {
32    ($e:expr) => {{
33        #[cfg(feature = "parallel")]
34        let result = $e.par_iter_mut();
35        #[cfg(not(feature = "parallel"))]
36        let result = $e.iter_mut();
37        result
38    }};
39}
40
41/// Returns a parallel consuming iterator when the `parallel` feature is enabled,
42/// otherwise returns a sequential consuming iterator.
43#[macro_export]
44macro_rules! cfg_into_iter {
45    ($e:expr) => {{
46        #[cfg(feature = "parallel")]
47        let result = $e.into_par_iter();
48        #[cfg(not(feature = "parallel"))]
49        let result = $e.into_iter();
50        result
51    }};
52}
53
54pub mod adjacent_pairs;
55pub mod biguint_helpers;
56pub mod bitwise_operations;
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 math;
66pub mod serialization;
67
68pub use biguint_helpers::BigUintHelpers;
69pub use bitwise_operations::BitwiseOps;
70pub use chunked_evaluations::ChunkedEvaluations;
71pub use dense_polynomial::ExtendedDensePolynomial;
72pub use evaluations::ExtendedEvaluations;
73#[cfg(feature = "std")]
74pub use field_helpers::RandomField;
75pub use field_helpers::{BigUintFieldHelpers, FieldHelpers, Two};
76pub use foreign_field::ForeignElement;
77
78/// Utils only for testing
79#[cfg(feature = "std")]
80pub mod tests {
81    use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
82
83    /// Create a new test rng with a random seed
84    #[must_use]
85    pub fn make_test_rng(seed: Option<[u8; 32]>) -> StdRng {
86        let seed = seed.unwrap_or_else(|| thread_rng().gen());
87        eprintln!("Seed: {seed:?}");
88        StdRng::from_seed(seed)
89    }
90}