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