Skip to content

Commit 82922e7

Browse files
committed
feat: add no-std feature to o1-utils crate
- Add `no-std` and `std` features to Cargo.toml (std is default) - Add conditional `#![no_std]` attribute to lib.rs - Replace `std::` imports with `core::` equivalents where possible - Add conditional `alloc` crate imports for Vec and other types - Feature-gate std-only modules (serialization, lazy_cache, chunked_evaluations) - Enable o1-utils in no-std CI workflow Closes: o1-labs/mina-rust#1988
1 parent 1609a72 commit 82922e7

File tree

13 files changed

+53
-12
lines changed

13 files changed

+53
-12
lines changed

.github/workflows/no-std.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ jobs:
2525
- { name: "mina-poseidon", path: "poseidon" } # https://github.com/o1-labs/mina-rust/issues/1996
2626
- { name: "mina-signer", path: "signer" } # https://github.com/o1-labs/mina-rust/issues/1995
2727
- { name: "groupmap", path: "groupmap" } # https://github.com/o1-labs/mina-rust/issues/1985
28+
- { name: "o1-utils", path: "utils" } # https://github.com/o1-labs/mina-rust/issues/1988
2829
# TODO: Add no-std feature to these crates
2930
# - { name: "wasm-types", path: "crates/wasm-types" }
3031
# - { name: "turshi", path: "turshi" } # https://github.com/o1-labs/mina-rust/issues/1986
3132
# - { name: "mina-curves", path: "curves" } # https://github.com/o1-labs/mina-rust/issues/1987
32-
# - { name: "o1-utils", path: "utils" } # https://github.com/o1-labs/mina-rust/issues/1988
3333
# - { name: "internal-tracing", path: "internal-tracing" } # https://github.com/o1-labs/mina-rust/issues/1989
3434
# - { name: "poly-commitment", path: "poly-commitment" } # https://github.com/o1-labs/mina-rust/issues/1990
3535
# - { name: "kimchi", path: "kimchi" } # https://github.com/o1-labs/mina-rust/issues/1991

utils/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ mina-curves.workspace = true
3939
secp256k1.workspace = true
4040

4141
[features]
42+
default = ["std"]
43+
std = []
44+
no-std = []
4245
diagnostics = ["tikv-jemalloc-ctl", "tikv-jemallocator"]

utils/src/array.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
//! better performance, either optimise this code, or use
77
//! (non-fixed-sized) vectors.
88
9+
#[cfg(feature = "no-std")]
10+
use alloc::{boxed::Box, vec, vec::Vec};
11+
912
// Use a generic function so that the pointer cast remains type-safe
1013
/// Converts a vector of elements to a boxed one. Semantically
1114
/// equivalent to `vector.into_boxed_slice().try_into().unwrap()`.
@@ -26,7 +29,7 @@ pub fn vec_to_boxed_array2<T: Clone, const N: usize, const M: usize>(
2629
let z: Box<[T; N]> = y
2730
.try_into()
2831
.unwrap_or_else(|_| panic!("vec_to_boxed_array2: length mismatch inner array"));
29-
let zz: &[[T; N]] = std::slice::from_ref(z.as_ref());
32+
let zz: &[[T; N]] = core::slice::from_ref(z.as_ref());
3033
vec_of_slices2.extend_from_slice(zz);
3134
});
3235

@@ -43,7 +46,7 @@ pub fn vec_to_boxed_array3<T: Clone, const N: usize, const M: usize, const K: us
4346
let mut vec_of_slices2: Vec<[[T; N]; M]> = vec![];
4447
vec.into_iter().for_each(|x| {
4548
let r: Box<[[T; N]; M]> = vec_to_boxed_array2(x);
46-
let zz: &[[[T; N]; M]] = std::slice::from_ref(r.as_ref());
49+
let zz: &[[[T; N]; M]] = core::slice::from_ref(r.as_ref());
4750
vec_of_slices2.extend_from_slice(zz);
4851
});
4952

@@ -96,7 +99,7 @@ macro_rules! box_array2 {
9699
let z: Box<[T; N]> = y
97100
.try_into()
98101
.unwrap_or_else(|_| panic!("vec_to_boxed_array2: length mismatch inner array"));
99-
let zz: &[[T; N]] = std::slice::from_ref(z.as_ref());
102+
let zz: &[[T; N]] = core::slice::from_ref(z.as_ref());
100103
vec_of_slices2.extend_from_slice(zz);
101104
});
102105

utils/src/bitwise_operations.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
//! This module provides a set of functions to perform bit operations on big integers.
22
//! In particular, it gives XOR and NOT for BigUint.
3+
#[cfg(feature = "no-std")]
4+
use alloc::vec;
5+
#[cfg(feature = "no-std")]
6+
use alloc::vec::Vec;
7+
8+
use core::cmp::{max, Ordering};
39
use num_bigint::BigUint;
4-
use std::cmp::{max, Ordering};
510

611
use crate::BigUintHelpers;
712

utils/src/chunked_evaluations.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg(not(feature = "no-std"))]
12
//! This module contains a type [ChunkedEvaluations],
23
34
use ark_ff::PrimeField;

utils/src/chunked_polynomial.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//! and a number of helper methods to deal with chunked polynomials.
33
//! Polynomials that cut in several polynomials of the same length.
44
5+
#[cfg(feature = "no-std")]
6+
use alloc::{vec, vec::Vec};
7+
58
use ark_ff::Field;
69
use ark_poly::polynomial::{univariate::DensePolynomial, Polynomial};
710

utils/src/dense_polynomial.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! This adds a few utility functions for the [DensePolynomial] arkworks type.
22
3+
#[cfg(feature = "no-std")]
4+
use alloc::{vec, vec::Vec};
5+
36
use ark_ff::Field;
47
use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial, Polynomial};
58
use rayon::prelude::*;

utils/src/evaluations.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! This adds a few utility functions for the [Evaluations] arkworks type.
22
3+
#[cfg(feature = "no-std")]
4+
use alloc::vec::Vec;
5+
36
use ark_ff::FftField;
47
use ark_poly::{Evaluations, Radix2EvaluationDomain};
58
use rayon::prelude::*;

utils/src/field_helpers.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Useful helper methods to extend [ark_ff::Field].
22
3+
#[cfg(feature = "no-std")]
4+
use alloc::{string::String, vec, vec::Vec};
5+
use core::ops::Neg;
6+
37
use ark_ff::{BigInteger, Field, PrimeField};
48
use num_bigint::{BigInt, BigUint, RandBigInt, Sign};
59
use rand::rngs::StdRng;
6-
use std::ops::Neg;
710
use thiserror::Error;
811

912
/// Field helpers error
@@ -21,7 +24,7 @@ pub enum FieldHelpersError {
2124
}
2225

2326
/// Result alias using [FieldHelpersError]
24-
pub type Result<T> = std::result::Result<T, FieldHelpersError>;
27+
pub type Result<T> = core::result::Result<T, FieldHelpersError>;
2528

2629
/// Helper to generate random field elements
2730
pub trait RandomField<F> {

utils/src/foreign_field.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
//! - `B` is a bit length of one limb
44
//! - `N` is a number of limbs that is used to represent one foreign field element
55
6+
#[cfg(feature = "no-std")]
7+
use alloc::vec;
8+
#[cfg(feature = "no-std")]
9+
use alloc::vec::Vec;
10+
use core::{
11+
fmt::{Debug, Formatter},
12+
ops::{Index, IndexMut},
13+
};
14+
615
use crate::{
716
field_helpers::FieldHelpers,
817
math::{div_ceil, is_multiple_of},
918
};
1019
use ark_ff::{Field, PrimeField};
1120
use num_bigint::BigUint;
12-
use std::{
13-
fmt::{Debug, Formatter},
14-
ops::{Index, IndexMut},
15-
};
1621

1722
/// Represents a foreign field element
1823
#[derive(Clone, PartialEq, Eq)]
@@ -154,7 +159,7 @@ impl<F: Field, const B: usize, const N: usize> IndexMut<usize> for ForeignElemen
154159
}
155160

156161
impl<F: Field, const B: usize, const N: usize> Debug for ForeignElement<F, B, N> {
157-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
162+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
158163
write!(f, "ForeignElement(")?;
159164
for i in 0..self.len {
160165
write!(f, "{:?}", self.limbs[i].to_hex())?;

0 commit comments

Comments
 (0)