Skip to content

Commit 06f9ab2

Browse files
authored
Merge pull request #4 from yugocabrio/feature/no_std
Feature/no std
2 parents 503f6b9 + 55c113c commit 06f9ab2

14 files changed

Lines changed: 160 additions & 52 deletions

File tree

.github/workflows/ci.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v4
1818
- uses: dtolnay/rust-toolchain@stable
19-
with: { components: rustfmt }
19+
with:
20+
components: rustfmt
2021
- run: cargo fmt --all -- --check
2122

2223
# ---------- Tests (independent) ----------
@@ -29,7 +30,14 @@ jobs:
2930
# Rust toolchain
3031
- uses: dtolnay/rust-toolchain@stable
3132

32-
# Cache Cargo deps
33+
# --- Pin legacy Noir toolchain (Poseidon2 still in stdlib) -----------
34+
- name: Install Noir toolchain 1.0.0-beta.3
35+
uses: noir-lang/noirup@v0.1.2
36+
with:
37+
toolchain: "1.0.0-beta.3"
38+
# --------------------------------------------------------------------
39+
40+
# Cache Cargo dependencies
3341
- uses: actions/cache@v3
3442
with:
3543
path: |
@@ -46,3 +54,11 @@ jobs:
4654
- name: Run tests
4755
run: cargo test --verbose
4856

57+
# ---------- Test alloc feature ----------
58+
- name: Test alloc feature
59+
run: cargo test alloc --verbose
60+
61+
# ---------- Test std feature ----------
62+
- name: Test std feature
63+
run: cargo test std --verbose
64+

Cargo.lock

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,45 @@ description = "Rust verifier for UltraHonk proofs"
88
repository = "https://github.com/yugocabrio/ultrahonk-rust-verifier"
99

1010
[dependencies]
11-
sha3 = "0.10"
11+
sha3 = { version = "0.10", default-features = false }
1212

13-
ark-ff = "0.5"
14-
ark-ec = "0.5"
15-
ark-serialize = "0.5"
16-
ark-std = { version = "0.5", features = ["std"] }
17-
ark-bn254 = { version = "0.5", features = ["std"] }
13+
ark-ff = { version = "0.5", default-features = false }
14+
ark-ec = { version = "0.5", default-features = false }
15+
ark-serialize = { version = "0.5", default-features = false }
16+
ark-std = { version = "0.5", default-features = false }
17+
ark-bn254 = { version = "0.5", default-features = false, features = ["curve"] }
1818

19-
anyhow = "1.0"
19+
serde = { version = "1.0", features = ["derive"], default-features = false, optional = true }
20+
serde_json = { version = "1.0", default-features = false, features = ["alloc"], optional = true }
21+
hex = { version = "0.4", default-features = false, features = ["alloc"] }
2022

21-
serde = { version = "1.0", features = ["derive"] }
22-
serde_json = "1.0"
23-
hex = "0.4"
23+
num-bigint = { version = "0.4", default-features = false }
24+
num-traits = { version = "0.2", default-features = false }
2425

25-
num-bigint = "0.4"
26-
num-traits = "0.2"
27-
28-
lazy_static = "1.4"
26+
lazy_static = { version = "1.4", optional = true }
27+
once_cell = { version = "1.19", default-features = false, features = ["alloc", "race"] }
2928

3029
[features]
31-
default = []
32-
trace = []
30+
default = ["alloc"]
31+
std = [
32+
"sha3/std",
33+
"ark-ff/std",
34+
"ark-ec/std",
35+
"ark-serialize/std",
36+
"ark-std/std",
37+
"ark-bn254/std",
38+
"serde",
39+
"serde_json",
40+
"hex/std",
41+
"num-bigint/std",
42+
"num-traits/std",
43+
"lazy_static",
44+
"once_cell/std"
45+
]
46+
trace = []
47+
48+
alloc = [
49+
"serde_json/alloc",
50+
"hex/alloc",
51+
"once_cell/alloc",
52+
]

src/debug.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use crate::field::Fr;
44
use crate::types::G1Point;
55
use ark_ff::{BigInteger256, PrimeField};
66

7+
#[cfg(not(feature = "std"))]
8+
use alloc::{format, string::String};
9+
710
/// trace! macro is a lightweight debug print macro that only outputs when the `trace` feature is enabled.
811
/// you can use it like this: cargo test --features trace -- --nocapture / cargo run --features trace
912
#[macro_export]

src/field.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ use ark_bn254::Fr as ArkFr;
44
use ark_ff::BigInteger256;
55
use ark_ff::{Field, PrimeField, Zero};
66
use ark_serialize::CanonicalSerialize;
7+
use core::ops::{Add, Mul, Neg, Sub};
78
use hex;
8-
use std::ops::{Add, Mul, Neg, Sub};
9+
10+
#[cfg(not(feature = "std"))]
11+
use alloc::{borrow::ToOwned, format, string::String};
912

1013
#[inline(always)]
1114
fn normalize_hex(s: &str) -> String {
@@ -118,7 +121,7 @@ impl Neg for Fr {
118121
}
119122

120123
impl CanonicalSerialize for Fr {
121-
fn serialize_with_mode<W: std::io::Write>(
124+
fn serialize_with_mode<W: ark_serialize::Write>(
122125
&self,
123126
mut writer: W,
124127
_compress: ark_serialize::Compress,

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// src/lib.rs
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
3+
#[cfg(not(feature = "std"))]
4+
extern crate alloc;
25

36
pub mod debug;
47
pub mod field;

src/relations.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@
77
88
use crate::field::Fr;
99
use crate::types::{RelationParameters, Wire};
10-
use std::ops::Neg;
10+
use core::ops::Neg;
11+
12+
#[cfg(not(feature = "std"))]
13+
use alloc::vec;
14+
15+
#[cfg(feature = "std")]
16+
macro_rules! println {
17+
($($args:tt)*) => { std::println!($($args)*) };
18+
}
19+
20+
#[cfg(not(feature = "std"))]
21+
macro_rules! println {
22+
($($args:tt)*) => {};
23+
}
1124

1225
/// Precomputed NEG_HALF = (p - 1)/2 in BN254 scalar field.
1326
fn neg_half() -> Fr {

src/shplemini.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use ark_bn254::{Bn254, Fq, Fq2, G1Affine, G1Projective, G2Affine};
1111
use ark_ec::{pairing::Pairing, CurveGroup, PrimeGroup};
1212
use ark_ff::{BigInteger, Field, One, PrimeField, Zero};
1313

14+
#[cfg(not(feature = "std"))]
15+
use alloc::{string::String, vec, vec::Vec};
16+
1417
pub const NUMBER_UNSHIFTED: usize = 35; // = 40 – 5
1518
pub const NUMBER_SHIFTED: usize = 5; // Final 5 are shifted
1619

src/sumcheck.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ use crate::{
88
types::{Transcript, VerificationKey},
99
};
1010

11-
lazy_static::lazy_static! {
11+
#[cfg(not(feature = "std"))]
12+
use alloc::{boxed, format, string::String};
13+
14+
#[cfg(feature = "std")]
15+
use lazy_static::lazy_static;
16+
17+
#[cfg(not(feature = "std"))]
18+
use once_cell::race::OnceBox;
19+
20+
#[cfg(feature = "std")]
21+
lazy_static! {
1222
/// 8-point barycentric coefficients
1323
static ref BARY: [Fr; 8] = [
1424
"0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffec51",
@@ -22,6 +32,25 @@ lazy_static::lazy_static! {
2232
].map(Fr::from_str);
2333
}
2434

35+
#[cfg(not(feature = "std"))]
36+
static BARY_BOX: OnceBox<[Fr; 8]> = OnceBox::new();
37+
38+
#[cfg(not(feature = "std"))]
39+
fn get_bary() -> &'static [Fr; 8] {
40+
BARY_BOX.get_or_init(|| {
41+
alloc::boxed::Box::new([
42+
Fr::from_str("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffec51"),
43+
Fr::from_str("0x00000000000000000000000000000000000000000000000000000000000002d0"),
44+
Fr::from_str("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffff11"),
45+
Fr::from_str("0x0000000000000000000000000000000000000000000000000000000000000090"),
46+
Fr::from_str("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffff71"),
47+
Fr::from_str("0x00000000000000000000000000000000000000000000000000000000000000f0"),
48+
Fr::from_str("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffd31"),
49+
Fr::from_str("0x00000000000000000000000000000000000000000000000000000000000013b0"),
50+
])
51+
})
52+
}
53+
2554
/// Check if the sum of two univariates equals the target value
2655
#[inline(always)]
2756
fn check_round_sum(u: &[Fr], target: Fr) -> bool {
@@ -40,7 +69,12 @@ fn next_target(u: &[Fr], chi: Fr) -> Fr {
4069
// Σ u_i / (BARY[i] * (χ - i))
4170
let mut acc = Fr::zero();
4271
for i in 0..8 {
43-
let inv = (BARY[i] * (chi - Fr::from_u64(i as u64))).inverse();
72+
#[cfg(feature = "std")]
73+
let bary_val = BARY[i];
74+
#[cfg(not(feature = "std"))]
75+
let bary_val = get_bary()[i];
76+
77+
let inv = (bary_val * (chi - Fr::from_u64(i as u64))).inverse();
4478
acc = acc + (u[i] * inv);
4579
}
4680

src/transcript.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use crate::{
1111
};
1212
use ark_bn254::G1Affine;
1313

14+
#[cfg(not(feature = "std"))]
15+
use alloc::vec::Vec;
16+
1417
fn push_point(buf: &mut Vec<u8>, pt: &G1Affine) {
1518
let (x_lo, x_hi) = fq_to_halves_be(&pt.x);
1619
let (y_lo, y_hi) = fq_to_halves_be(&pt.y);

0 commit comments

Comments
 (0)