From 7a81c5d46a1b43fdada42cfbfc0bce94ca5df14e Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Wed, 2 Nov 2022 19:59:12 -0700 Subject: [PATCH] feat: move to alloc for Vec/String/Box Signed-off-by: Brandon H. Gomes --- field/Cargo.toml | 20 ++++---- .../src/arch/x86_64/avx2_goldilocks_field.rs | 22 +++++---- .../arch/x86_64/avx512_goldilocks_field.rs | 22 +++++---- field/src/cosets.rs | 2 + field/src/extension/algebra.rs | 11 +++-- field/src/extension/mod.rs | 2 +- field/src/extension/quadratic.rs | 12 ++--- field/src/extension/quartic.rs | 12 ++--- field/src/extension/quintic.rs | 12 ++--- field/src/fft.rs | 4 +- field/src/goldilocks_extensions.rs | 2 +- field/src/goldilocks_field.rs | 14 +++--- field/src/interpolation.rs | 2 + field/src/inversion.rs | 8 ++-- field/src/lib.rs | 8 +++- field/src/ops.rs | 2 +- field/src/packed.rs | 12 ++--- field/src/polynomial/division.rs | 5 +- field/src/polynomial/mod.rs | 8 ++-- field/src/prime_field_testing.rs | 2 +- field/src/secp256k1_base.rs | 12 ++--- field/src/secp256k1_scalar.rs | 13 +++-- field/src/types.rs | 14 +++--- field/src/zero_poly_coset.rs | 2 + plonky2/Cargo.toml | 47 ++++++++++++------- plonky2/src/fri/challenges.rs | 2 + plonky2/src/fri/mod.rs | 2 + plonky2/src/fri/oracle.rs | 3 ++ plonky2/src/fri/proof.rs | 2 + plonky2/src/fri/prover.rs | 2 + plonky2/src/fri/recursive_verifier.rs | 3 ++ plonky2/src/fri/reduction_strategies.rs | 5 ++ plonky2/src/fri/structure.rs | 1 + plonky2/src/fri/verifier.rs | 2 + plonky2/src/gadgets/arithmetic.rs | 2 + plonky2/src/gadgets/arithmetic_extension.rs | 2 + plonky2/src/gadgets/polynomial.rs | 2 + plonky2/src/gadgets/random_access.rs | 2 + plonky2/src/gadgets/range_check.rs | 3 ++ plonky2/src/gadgets/split_base.rs | 2 + plonky2/src/gadgets/split_join.rs | 3 ++ plonky2/src/gates/arithmetic_base.rs | 5 ++ plonky2/src/gates/arithmetic_extension.rs | 4 ++ plonky2/src/gates/base_sum.rs | 4 ++ plonky2/src/gates/constant.rs | 5 ++ plonky2/src/gates/exponentiation.rs | 4 ++ plonky2/src/gates/gate.rs | 6 ++- .../src/gates/high_degree_interpolation.rs | 4 ++ plonky2/src/gates/interpolation.rs | 1 + plonky2/src/gates/low_degree_interpolation.rs | 4 ++ plonky2/src/gates/multiplication_extension.rs | 4 ++ plonky2/src/gates/noop.rs | 4 ++ plonky2/src/gates/packed_util.rs | 3 ++ plonky2/src/gates/poseidon.rs | 4 ++ plonky2/src/gates/poseidon_mds.rs | 4 ++ plonky2/src/gates/public_input.rs | 3 ++ plonky2/src/gates/random_access.rs | 4 ++ plonky2/src/gates/reducing.rs | 4 ++ plonky2/src/gates/reducing_extension.rs | 4 ++ plonky2/src/gates/selectors.rs | 2 + .../arch/aarch64/poseidon_goldilocks_neon.rs | 6 +-- plonky2/src/hash/hash_types.rs | 2 + plonky2/src/hash/hashing.rs | 2 + plonky2/src/hash/keccak.rs | 2 + plonky2/src/hash/merkle_proofs.rs | 3 ++ plonky2/src/hash/merkle_tree.rs | 1 + plonky2/src/hash/path_compression.rs | 2 + plonky2/src/hash/poseidon.rs | 3 ++ plonky2/src/iop/challenger.rs | 3 +- plonky2/src/iop/ext_target.rs | 1 + plonky2/src/iop/generator.rs | 2 + plonky2/src/iop/mod.rs | 1 + plonky2/src/iop/target.rs | 1 + plonky2/src/iop/wire.rs | 1 + plonky2/src/iop/witness.rs | 2 + plonky2/src/lib.rs | 3 ++ plonky2/src/plonk/circuit_builder.rs | 6 ++- plonky2/src/plonk/circuit_data.rs | 5 +- plonky2/src/plonk/config.rs | 2 + plonky2/src/plonk/copy_constraint.rs | 2 + plonky2/src/plonk/get_challenges.rs | 2 + plonky2/src/plonk/permutation_argument.rs | 1 + plonky2/src/plonk/plonk_common.rs | 3 ++ plonky2/src/plonk/proof.rs | 5 ++ plonky2/src/plonk/prover.rs | 2 + plonky2/src/plonk/vanishing_poly.rs | 3 ++ .../conditional_recursive_verifier.rs | 5 +- plonky2/src/recursion/cyclic_recursion.rs | 11 +++-- plonky2/src/util/context_tree.rs | 4 ++ plonky2/src/util/mod.rs | 7 ++- plonky2/src/util/partial_products.rs | 1 + plonky2/src/util/reducing.rs | 2 + util/src/lib.rs | 14 ++++-- util/src/transpose_util.rs | 2 +- 94 files changed, 356 insertions(+), 137 deletions(-) diff --git a/field/Cargo.toml b/field/Cargo.toml index 1a72bd6c..33bb76c2 100644 --- a/field/Cargo.toml +++ b/field/Cargo.toml @@ -5,15 +5,15 @@ version = "0.1.0" edition = "2021" [features] -default = ["rand"] -rand = ["dep:rand"] +default = [] +rand = ["dep:rand", "num/rand"] [dependencies] -plonky2_util = { path = "../util" } -anyhow = "1.0.40" -itertools = "0.10.0" -num = { version = "0.4", features = [ "rand" ] } -rand = { optional = true, version = "0.8.4" } -serde = { version = "1.0", features = ["derive"] } -unroll = "0.1.5" -static_assertions = "1.1.0" +anyhow = { version = "1.0.40", default-features = false } +itertools = { version = "0.10.0", default-features = false } +num = { version = "0.4", default-features = false, features = ["alloc"] } +plonky2_util = { path = "../util", default-features = false } +rand = { version = "0.8.5", optional = true, default-features = false, features = ["getrandom"] } +serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] } +static_assertions = { version = "1.1.0", default-features = false } +unroll = { version = "0.1.5", default-features = false } diff --git a/field/src/arch/x86_64/avx2_goldilocks_field.rs b/field/src/arch/x86_64/avx2_goldilocks_field.rs index adef1fac..dafd1503 100644 --- a/field/src/arch/x86_64/avx2_goldilocks_field.rs +++ b/field/src/arch/x86_64/avx2_goldilocks_field.rs @@ -1,20 +1,22 @@ use core::arch::x86_64::*; -use std::fmt; -use std::fmt::{Debug, Formatter}; -use std::iter::{Product, Sum}; -use std::mem::transmute; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::fmt; +use core::fmt::{Debug, Formatter}; +use core::iter::{Product, Sum}; +use core::mem::transmute; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use crate::goldilocks_field::GoldilocksField; use crate::ops::Square; use crate::packed::PackedField; use crate::types::{Field, Field64}; -// Ideally `Avx2GoldilocksField` would wrap `__m256i`. Unfortunately, `__m256i` has an alignment of -// 32B, which would preclude us from casting `[GoldilocksField; 4]` (alignment 8B) to -// `Avx2GoldilocksField`. We need to ensure that `Avx2GoldilocksField` has the same alignment as -// `GoldilocksField`. Thus we wrap `[GoldilocksField; 4]` and use the `new` and `get` methods to -// convert to and from `__m256i`. +/// AVX2 Goldilocks Field +/// +/// Ideally `Avx2GoldilocksField` would wrap `__m256i`. Unfortunately, `__m256i` has an alignment of +/// 32B, which would preclude us from casting `[GoldilocksField; 4]` (alignment 8B) to +/// `Avx2GoldilocksField`. We need to ensure that `Avx2GoldilocksField` has the same alignment as +/// `GoldilocksField`. Thus we wrap `[GoldilocksField; 4]` and use the `new` and `get` methods to +/// convert to and from `__m256i`. #[derive(Copy, Clone)] #[repr(transparent)] pub struct Avx2GoldilocksField(pub [GoldilocksField; 4]); diff --git a/field/src/arch/x86_64/avx512_goldilocks_field.rs b/field/src/arch/x86_64/avx512_goldilocks_field.rs index f67e5000..2939e70e 100644 --- a/field/src/arch/x86_64/avx512_goldilocks_field.rs +++ b/field/src/arch/x86_64/avx512_goldilocks_field.rs @@ -1,20 +1,22 @@ use core::arch::x86_64::*; -use std::fmt; -use std::fmt::{Debug, Formatter}; -use std::iter::{Product, Sum}; -use std::mem::transmute; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::fmt; +use core::fmt::{Debug, Formatter}; +use core::iter::{Product, Sum}; +use core::mem::transmute; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use crate::goldilocks_field::GoldilocksField; use crate::ops::Square; use crate::packed::PackedField; use crate::types::{Field, Field64}; -// Ideally `Avx512GoldilocksField` would wrap `__m512i`. Unfortunately, `__m512i` has an alignment -// of 64B, which would preclude us from casting `[GoldilocksField; 8]` (alignment 8B) to -// `Avx512GoldilocksField`. We need to ensure that `Avx512GoldilocksField` has the same alignment as -// `GoldilocksField`. Thus we wrap `[GoldilocksField; 8]` and use the `new` and `get` methods to -// convert to and from `__m512i`. +/// AVX512 Goldilocks Field +/// +/// Ideally `Avx512GoldilocksField` would wrap `__m512i`. Unfortunately, `__m512i` has an alignment +/// of 64B, which would preclude us from casting `[GoldilocksField; 8]` (alignment 8B) to +/// `Avx512GoldilocksField`. We need to ensure that `Avx512GoldilocksField` has the same alignment as +/// `GoldilocksField`. Thus we wrap `[GoldilocksField; 8]` and use the `new` and `get` methods to +/// convert to and from `__m512i`. #[derive(Copy, Clone)] #[repr(transparent)] pub struct Avx512GoldilocksField(pub [GoldilocksField; 8]); diff --git a/field/src/cosets.rs b/field/src/cosets.rs index 46b43d90..5a59a963 100644 --- a/field/src/cosets.rs +++ b/field/src/cosets.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use num::bigint::BigUint; use crate::types::Field; diff --git a/field/src/extension/algebra.rs b/field/src/extension/algebra.rs index 5840ae81..6e309ba8 100644 --- a/field/src/extension/algebra.rs +++ b/field/src/extension/algebra.rs @@ -1,6 +1,7 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use alloc::vec::Vec; +use core::fmt::{self, Debug, Display, Formatter}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use crate::extension::OEF; @@ -42,7 +43,7 @@ impl, const D: usize> From for ExtensionAlgebra { } impl, const D: usize> Display for ExtensionAlgebra { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "({})", self.0[0])?; for i in 1..D { write!(f, " + ({})*b^{i}", self.0[i])?; @@ -52,7 +53,7 @@ impl, const D: usize> Display for ExtensionAlgebra { } impl, const D: usize> Debug for ExtensionAlgebra { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Display::fmt(self, f) } } diff --git a/field/src/extension/mod.rs b/field/src/extension/mod.rs index ed596764..bbbaca25 100644 --- a/field/src/extension/mod.rs +++ b/field/src/extension/mod.rs @@ -1,4 +1,4 @@ -use std::convert::TryInto; +use alloc::vec::Vec; use crate::types::Field; diff --git a/field/src/extension/quadratic.rs b/field/src/extension/quadratic.rs index 278abba9..1909e35e 100644 --- a/field/src/extension/quadratic.rs +++ b/field/src/extension/quadratic.rs @@ -1,6 +1,6 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::fmt::{self, Debug, Display, Formatter}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use num::bigint::BigUint; use serde::{Deserialize, Serialize}; @@ -101,19 +101,19 @@ impl> Field for QuadraticExtension { } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { + fn rand_from_rng(rng: &mut R) -> Self { Self([F::rand_from_rng(rng), F::rand_from_rng(rng)]) } } impl> Display for QuadraticExtension { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{} + {}*a", self.0[0], self.0[1]) } } impl> Debug for QuadraticExtension { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Display::fmt(self, f) } } diff --git a/field/src/extension/quartic.rs b/field/src/extension/quartic.rs index 6df39903..948c29fb 100644 --- a/field/src/extension/quartic.rs +++ b/field/src/extension/quartic.rs @@ -1,6 +1,6 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::fmt::{self, Debug, Display, Formatter}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use num::bigint::BigUint; use num::traits::Pow; @@ -106,7 +106,7 @@ impl> Field for QuarticExtension { } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { + fn rand_from_rng(rng: &mut R) -> Self { Self::from_basefield_array([ F::rand_from_rng(rng), F::rand_from_rng(rng), @@ -117,7 +117,7 @@ impl> Field for QuarticExtension { } impl> Display for QuarticExtension { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!( f, "{} + {}*a + {}*a^2 + {}*a^3", @@ -127,7 +127,7 @@ impl> Display for QuarticExtension { } impl> Debug for QuarticExtension { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Display::fmt(self, f) } } diff --git a/field/src/extension/quintic.rs b/field/src/extension/quintic.rs index 6680ebc7..343e6f77 100644 --- a/field/src/extension/quintic.rs +++ b/field/src/extension/quintic.rs @@ -1,6 +1,6 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::fmt::{self, Debug, Display, Formatter}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use num::bigint::BigUint; use num::traits::Pow; @@ -112,7 +112,7 @@ impl> Field for QuinticExtension { } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { + fn rand_from_rng(rng: &mut R) -> Self { Self::from_basefield_array([ F::rand_from_rng(rng), F::rand_from_rng(rng), @@ -124,7 +124,7 @@ impl> Field for QuinticExtension { } impl> Display for QuinticExtension { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!( f, "{} + {}*a + {}*a^2 + {}*a^3 + {}*a^4", @@ -134,7 +134,7 @@ impl> Display for QuinticExtension { } impl> Debug for QuinticExtension { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Display::fmt(self, f) } } diff --git a/field/src/fft.rs b/field/src/fft.rs index 6ede8af6..8fb1809c 100644 --- a/field/src/fft.rs +++ b/field/src/fft.rs @@ -1,5 +1,5 @@ -use std::cmp::{max, min}; -use std::option::Option; +use alloc::vec::Vec; +use core::cmp::{max, min}; use plonky2_util::{log2_strict, reverse_index_bits_in_place}; use unroll::unroll_for_loops; diff --git a/field/src/goldilocks_extensions.rs b/field/src/goldilocks_extensions.rs index 2175494f..8b53f8b5 100644 --- a/field/src/goldilocks_extensions.rs +++ b/field/src/goldilocks_extensions.rs @@ -1,4 +1,4 @@ -use std::ops::Mul; +use core::ops::Mul; use static_assertions::const_assert; diff --git a/field/src/goldilocks_field.rs b/field/src/goldilocks_field.rs index e036ab9b..9c7e8d1b 100644 --- a/field/src/goldilocks_field.rs +++ b/field/src/goldilocks_field.rs @@ -1,8 +1,7 @@ -use std::fmt; -use std::fmt::{Debug, Display, Formatter}; -use std::hash::{Hash, Hasher}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::fmt::{self, Debug, Display, Formatter}; +use core::hash::{Hash, Hasher}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use num::{BigUint, Integer}; use plonky2_util::{assume, branch_hint}; @@ -105,7 +104,8 @@ impl Field for GoldilocksField { } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { + fn rand_from_rng(rng: &mut R) -> Self { + use rand::Rng; Self::from_canonical_u64(rng.gen_range(0..Self::ORDER)) } @@ -300,7 +300,7 @@ impl DivAssign for GoldilocksField { #[inline(always)] #[cfg(target_arch = "x86_64")] unsafe fn add_no_canonicalize_trashing_input(x: u64, y: u64) -> u64 { - use std::arch::asm; + use core::arch::asm; let res_wrapped: u64; let adjustment: u64; asm!( diff --git a/field/src/interpolation.rs b/field/src/interpolation.rs index 8f64e9d7..4a42e4af 100644 --- a/field/src/interpolation.rs +++ b/field/src/interpolation.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use plonky2_util::log2_ceil; use crate::fft::ifft; diff --git a/field/src/inversion.rs b/field/src/inversion.rs index 740e6562..45d17ab5 100644 --- a/field/src/inversion.rs +++ b/field/src/inversion.rs @@ -6,8 +6,8 @@ use crate::types::PrimeField64; #[inline(always)] fn safe_iteration(f: &mut u64, g: &mut u64, c: &mut i128, d: &mut i128, k: &mut u32) { if f < g { - std::mem::swap(f, g); - std::mem::swap(c, d); + core::mem::swap(f, g); + core::mem::swap(c, d); } if *f & 3 == *g & 3 { // f - g = 0 (mod 4) @@ -36,8 +36,8 @@ fn safe_iteration(f: &mut u64, g: &mut u64, c: &mut i128, d: &mut i128, k: &mut #[inline(always)] unsafe fn unsafe_iteration(f: &mut u64, g: &mut u64, c: &mut i128, d: &mut i128, k: &mut u32) { if *f < *g { - std::mem::swap(f, g); - std::mem::swap(c, d); + core::mem::swap(f, g); + core::mem::swap(c, d); } if *f & 3 == *g & 3 { // f - g = 0 (mod 4) diff --git a/field/src/lib.rs b/field/src/lib.rs index ec5fc80e..5459b38a 100644 --- a/field/src/lib.rs +++ b/field/src/lib.rs @@ -8,8 +8,14 @@ #![feature(generic_const_exprs)] #![feature(specialization)] #![feature(stdsimd)] +#![no_std] + +extern crate alloc; + +mod inversion; pub(crate) mod arch; + pub mod batch_util; pub mod cosets; pub mod extension; @@ -17,7 +23,6 @@ pub mod fft; pub mod goldilocks_extensions; pub mod goldilocks_field; pub mod interpolation; -mod inversion; pub mod ops; pub mod packable; pub mod packed; @@ -29,5 +34,6 @@ pub mod zero_poly_coset; #[cfg(test)] mod field_testing; + #[cfg(test)] mod prime_field_testing; diff --git a/field/src/ops.rs b/field/src/ops.rs index bf8ff8a9..ce05ea78 100644 --- a/field/src/ops.rs +++ b/field/src/ops.rs @@ -1,4 +1,4 @@ -use std::ops::Mul; +use core::ops::Mul; pub trait Square { fn square(&self) -> Self; diff --git a/field/src/packed.rs b/field/src/packed.rs index 9708f9e3..531071fd 100644 --- a/field/src/packed.rs +++ b/field/src/packed.rs @@ -1,7 +1,7 @@ -use std::fmt::Debug; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; -use std::slice; +use core::fmt::Debug; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign}; +use core::slice; use crate::ops::Square; use crate::types::Field; @@ -82,7 +82,7 @@ where ); let buf_ptr = buf.as_ptr().cast::(); let n = buf.len() / Self::WIDTH; - unsafe { std::slice::from_raw_parts(buf_ptr, n) } + unsafe { slice::from_raw_parts(buf_ptr, n) } } fn pack_slice_mut(buf: &mut [Self::Scalar]) -> &mut [Self] { assert!( @@ -93,7 +93,7 @@ where ); let buf_ptr = buf.as_mut_ptr().cast::(); let n = buf.len() / Self::WIDTH; - unsafe { std::slice::from_raw_parts_mut(buf_ptr, n) } + unsafe { slice::from_raw_parts_mut(buf_ptr, n) } } fn doubles(&self) -> Self { diff --git a/field/src/polynomial/division.rs b/field/src/polynomial/division.rs index 561b9661..14e16841 100644 --- a/field/src/polynomial/division.rs +++ b/field/src/polynomial/division.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use plonky2_util::log2_ceil; use crate::polynomial::PolynomialCoeffs; @@ -68,7 +71,7 @@ impl PolynomialCoeffs { } /// Let `self=p(X)`, this returns `(p(X)-p(z))/(X-z)`. - /// See https://en.wikipedia.org/wiki/Horner%27s_method + /// See pub fn divide_by_linear(&self, z: F) -> PolynomialCoeffs { let mut bs = self .coeffs diff --git a/field/src/polynomial/mod.rs b/field/src/polynomial/mod.rs index 09ed69c7..e5143292 100644 --- a/field/src/polynomial/mod.rs +++ b/field/src/polynomial/mod.rs @@ -1,8 +1,10 @@ pub(crate) mod division; -use std::cmp::max; -use std::iter::Sum; -use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; +use alloc::vec; +use alloc::vec::Vec; +use core::cmp::max; +use core::iter::Sum; +use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; use anyhow::{ensure, Result}; use itertools::Itertools; diff --git a/field/src/prime_field_testing.rs b/field/src/prime_field_testing.rs index 13431265..7b0d9624 100644 --- a/field/src/prime_field_testing.rs +++ b/field/src/prime_field_testing.rs @@ -68,7 +68,7 @@ where macro_rules! test_prime_field_arithmetic { ($field:ty) => { mod prime_field_arithmetic { - use std::ops::{Add, Mul, Neg, Sub}; + use core::ops::{Add, Mul, Neg, Sub}; use $crate::ops::Square; use $crate::types::{Field, Field64}; diff --git a/field/src/secp256k1_base.rs b/field/src/secp256k1_base.rs index 504d63d7..9d90da9b 100644 --- a/field/src/secp256k1_base.rs +++ b/field/src/secp256k1_base.rs @@ -1,8 +1,8 @@ -use std::fmt; -use std::fmt::{Debug, Display, Formatter}; -use std::hash::{Hash, Hasher}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use alloc::vec::Vec; +use core::fmt::{self, Debug, Display, Formatter}; +use core::hash::{Hash, Hasher}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use itertools::Itertools; use num::bigint::BigUint; @@ -133,7 +133,7 @@ impl Field for Secp256K1Base { } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { + fn rand_from_rng(rng: &mut R) -> Self { use num::bigint::RandBigInt; Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order())) } diff --git a/field/src/secp256k1_scalar.rs b/field/src/secp256k1_scalar.rs index e70b154d..3a98d9e7 100644 --- a/field/src/secp256k1_scalar.rs +++ b/field/src/secp256k1_scalar.rs @@ -1,9 +1,8 @@ -use std::convert::TryInto; -use std::fmt; -use std::fmt::{Debug, Display, Formatter}; -use std::hash::{Hash, Hasher}; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use alloc::vec::Vec; +use core::fmt::{self, Debug, Display, Formatter}; +use core::hash::{Hash, Hasher}; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use itertools::Itertools; use num::bigint::BigUint; @@ -142,7 +141,7 @@ impl Field for Secp256K1Scalar { } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { + fn rand_from_rng(rng: &mut R) -> Self { use num::bigint::RandBigInt; Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order())) } diff --git a/field/src/types.rs b/field/src/types.rs index 545f90c5..4df26ea2 100644 --- a/field/src/types.rs +++ b/field/src/types.rs @@ -1,7 +1,9 @@ -use std::fmt::{Debug, Display}; -use std::hash::Hash; -use std::iter::{Product, Sum}; -use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; +use alloc::vec; +use alloc::vec::Vec; +use core::fmt::{Debug, Display}; +use core::hash::Hash; +use core::iter::{Product, Sum}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use num::bigint::BigUint; use num::{Integer, One, ToPrimitive, Zero}; @@ -318,7 +320,7 @@ pub trait Field: } #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self; + fn rand_from_rng(rng: &mut R) -> Self; fn exp_power_of_2(&self, power_log: usize) -> Self { let mut res = *self; @@ -399,7 +401,7 @@ pub trait Field: #[cfg(feature = "rand")] fn rand() -> Self { - Self::rand_from_rng(&mut rand::thread_rng()) + Self::rand_from_rng(&mut rand::rngs::OsRng) } #[cfg(feature = "rand")] diff --git a/field/src/zero_poly_coset.rs b/field/src/zero_poly_coset.rs index 8d63bc69..53b66a75 100644 --- a/field/src/zero_poly_coset.rs +++ b/field/src/zero_poly_coset.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use crate::packed::PackedField; use crate::types::Field; diff --git a/plonky2/Cargo.toml b/plonky2/Cargo.toml index 73ff55a5..6de787d2 100644 --- a/plonky2/Cargo.toml +++ b/plonky2/Cargo.toml @@ -11,29 +11,40 @@ edition = "2021" default-run = "generate_constants" [features] -default = ["parallel", "rand", "rand_chacha", "timing", "gate_testing"] -parallel = ["maybe_rayon/parallel"] -rand = ["dep:rand", "plonky2_field/rand"] +default = [ + "gate_testing", + "parallel", + "rand", + "rand_chacha", + "std", + "timing", +] +rand = [ + "dep:rand", + "num/rand", + "plonky2_field/rand" +] gate_testing = ["rand"] -rand_chacha = ["dep:rand_chacha"] +parallel = ["maybe_rayon/parallel"] +std = ["anyhow/std"] timing = [] [dependencies] -anyhow = "1.0.40" +anyhow = { version = "1.0.40", default-features = false } derivative = { version = "2.2.0", default-features = false, features = ["use_core"] } -itertools = "0.10.0" -keccak-hash = "0.8.0" -log = "0.4.14" -maybe_rayon = { path = "../maybe_rayon" } -num = { version = "0.4", features = [ "rand" ] } -plonky2_field = { path = "../field" } -plonky2_util = { path = "../util" } -rand = { version = "0.8.4", optional = true } -rand_chacha = { version = "0.3.1", optional = true } -serde = { version = "1.0", features = ["derive"] } -serde_cbor = "0.11.1" -static_assertions = "1.1.0" -unroll = "0.1.5" +itertools = { version = "0.10.0", default-features = false } +keccak-hash = { version = "0.8.0", default-features = false } +log = { version = "0.4.14", default-features = false } +maybe_rayon = { path = "../maybe_rayon", default-features = false } +num = { version = "0.4", default-features = false } +plonky2_field = { path = "../field", default-features = false } +plonky2_util = { path = "../util", default-features = false } +rand = { version = "0.8.4", optional = true, default-features = false } +rand_chacha = { version = "0.3.1", optional = true, default-features = false } +serde = { version = "1.0", default-features = false, features = ["derive"] } +serde_cbor = { version = "0.11.1", default-features = false } +static_assertions = { version = "1.1.0", default-features = false } +unroll = { version = "0.1.5", default-features = false } [dev-dependencies] criterion = "0.3.5" diff --git a/plonky2/src/fri/challenges.rs b/plonky2/src/fri/challenges.rs index 011a8897..3a750dd6 100644 --- a/plonky2/src/fri/challenges.rs +++ b/plonky2/src/fri/challenges.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use plonky2_field::polynomial::PolynomialCoeffs; diff --git a/plonky2/src/fri/mod.rs b/plonky2/src/fri/mod.rs index 90f1c940..ca800b98 100644 --- a/plonky2/src/fri/mod.rs +++ b/plonky2/src/fri/mod.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use crate::fri::reduction_strategies::FriReductionStrategy; mod challenges; diff --git a/plonky2/src/fri/oracle.rs b/plonky2/src/fri/oracle.rs index 10a93a18..b670944d 100644 --- a/plonky2/src/fri/oracle.rs +++ b/plonky2/src/fri/oracle.rs @@ -1,3 +1,6 @@ +use alloc::format; +use alloc::vec::Vec; + use itertools::Itertools; use maybe_rayon::*; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/fri/proof.rs b/plonky2/src/fri/proof.rs index 6a8adcf5..2404a7bd 100644 --- a/plonky2/src/fri/proof.rs +++ b/plonky2/src/fri/proof.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use std::collections::HashMap; use itertools::izip; diff --git a/plonky2/src/fri/prover.rs b/plonky2/src/fri/prover.rs index 32e41ff3..adbf5e29 100644 --- a/plonky2/src/fri/prover.rs +++ b/plonky2/src/fri/prover.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use itertools::Itertools; use maybe_rayon::*; use plonky2_field::extension::{flatten, unflatten, Extendable}; diff --git a/plonky2/src/fri/recursive_verifier.rs b/plonky2/src/fri/recursive_verifier.rs index c5cb9f07..a5b50041 100644 --- a/plonky2/src/fri/recursive_verifier.rs +++ b/plonky2/src/fri/recursive_verifier.rs @@ -1,3 +1,6 @@ +use alloc::vec::Vec; +use alloc::{format, vec}; + use itertools::Itertools; use plonky2_field::extension::Extendable; use plonky2_util::{log2_strict, reverse_index_bits_in_place}; diff --git a/plonky2/src/fri/reduction_strategies.rs b/plonky2/src/fri/reduction_strategies.rs index 4252564e..7035100d 100644 --- a/plonky2/src/fri/reduction_strategies.rs +++ b/plonky2/src/fri/reduction_strategies.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; +#[cfg(feature = "timing")] use std::time::Instant; use log::debug; @@ -63,11 +66,13 @@ fn min_size_arity_bits( // in an optimal sequence, we would need a really massive polynomial. let max_arity_bits = opt_max_arity_bits.unwrap_or(4); + #[cfg(feature = "timing")] let start = Instant::now(); let (mut arity_bits, fri_proof_size) = min_size_arity_bits_helper(degree_bits, rate_bits, num_queries, max_arity_bits, vec![]); arity_bits.shrink_to_fit(); + #[cfg(feature = "timing")] debug!( "min_size_arity_bits took {:.3}s", start.elapsed().as_secs_f32() diff --git a/plonky2/src/fri/structure.rs b/plonky2/src/fri/structure.rs index abcccccd..7d7436d5 100644 --- a/plonky2/src/fri/structure.rs +++ b/plonky2/src/fri/structure.rs @@ -1,6 +1,7 @@ //! Information about the structure of a FRI instance, in terms of the oracles and polynomials //! involved, and the points they are opened at. +use alloc::vec::Vec; use core::ops::Range; use crate::field::extension::Extendable; diff --git a/plonky2/src/fri/verifier.rs b/plonky2/src/fri/verifier.rs index 9d01ef6c..7060ce95 100644 --- a/plonky2/src/fri/verifier.rs +++ b/plonky2/src/fri/verifier.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use anyhow::{ensure, Result}; use plonky2_field::extension::{flatten, Extendable, FieldExtension}; use plonky2_field::interpolation::{barycentric_weights, interpolate}; diff --git a/plonky2/src/gadgets/arithmetic.rs b/plonky2/src/gadgets/arithmetic.rs index 87c5878c..c18ea034 100644 --- a/plonky2/src/gadgets/arithmetic.rs +++ b/plonky2/src/gadgets/arithmetic.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::borrow::Borrow; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/gadgets/arithmetic_extension.rs b/plonky2/src/gadgets/arithmetic_extension.rs index 296127b1..b56d245c 100644 --- a/plonky2/src/gadgets/arithmetic_extension.rs +++ b/plonky2/src/gadgets/arithmetic_extension.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::borrow::Borrow; use plonky2_field::extension::{Extendable, FieldExtension, OEF}; diff --git a/plonky2/src/gadgets/polynomial.rs b/plonky2/src/gadgets/polynomial.rs index d61cf830..bf5c2207 100644 --- a/plonky2/src/gadgets/polynomial.rs +++ b/plonky2/src/gadgets/polynomial.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use crate::hash::hash_types::RichField; diff --git a/plonky2/src/gadgets/random_access.rs b/plonky2/src/gadgets/random_access.rs index 0d32c360..b9561962 100644 --- a/plonky2/src/gadgets/random_access.rs +++ b/plonky2/src/gadgets/random_access.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use plonky2_util::log2_strict; diff --git a/plonky2/src/gadgets/range_check.rs b/plonky2/src/gadgets/range_check.rs index 267b1040..fd22c17d 100644 --- a/plonky2/src/gadgets/range_check.rs +++ b/plonky2/src/gadgets/range_check.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use crate::hash::hash_types::RichField; diff --git a/plonky2/src/gadgets/split_base.rs b/plonky2/src/gadgets/split_base.rs index 37853c2b..43e67cb8 100644 --- a/plonky2/src/gadgets/split_base.rs +++ b/plonky2/src/gadgets/split_base.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::borrow::Borrow; use itertools::Itertools; diff --git a/plonky2/src/gadgets/split_join.rs b/plonky2/src/gadgets/split_join.rs index cd2bbc93..ee4bd7ef 100644 --- a/plonky2/src/gadgets/split_join.rs +++ b/plonky2/src/gadgets/split_join.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use plonky2_util::ceil_div_usize; diff --git a/plonky2/src/gates/arithmetic_base.rs b/plonky2/src/gates/arithmetic_base.rs index 03560faf..69f2c179 100644 --- a/plonky2/src/gates/arithmetic_base.rs +++ b/plonky2/src/gates/arithmetic_base.rs @@ -1,3 +1,8 @@ +use alloc::boxed::Box; +use alloc::format; +use alloc::string::String; +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use plonky2_field::packed::PackedField; diff --git a/plonky2/src/gates/arithmetic_extension.rs b/plonky2/src/gates/arithmetic_extension.rs index da522b9e..da686739 100644 --- a/plonky2/src/gates/arithmetic_extension.rs +++ b/plonky2/src/gates/arithmetic_extension.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::format; +use alloc::string::String; +use alloc::vec::Vec; use core::ops::Range; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/plonky2/src/gates/base_sum.rs b/plonky2/src/gates/base_sum.rs index 916d1b84..dd3542e1 100644 --- a/plonky2/src/gates/base_sum.rs +++ b/plonky2/src/gates/base_sum.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::ops::Range; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/gates/constant.rs b/plonky2/src/gates/constant.rs index 513caad9..dcb730af 100644 --- a/plonky2/src/gates/constant.rs +++ b/plonky2/src/gates/constant.rs @@ -1,3 +1,8 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; + use plonky2_field::extension::Extendable; use plonky2_field::packed::PackedField; diff --git a/plonky2/src/gates/exponentiation.rs b/plonky2/src/gates/exponentiation.rs index 9a3079e0..87975f94 100644 --- a/plonky2/src/gates/exponentiation.rs +++ b/plonky2/src/gates/exponentiation.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::marker::PhantomData; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/gates/gate.rs b/plonky2/src/gates/gate.rs index 4faac919..648bf779 100644 --- a/plonky2/src/gates/gate.rs +++ b/plonky2/src/gates/gate.rs @@ -1,8 +1,12 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::sync::Arc; +use alloc::vec; +use alloc::vec::Vec; use core::fmt::{Debug, Error, Formatter}; use core::hash::{Hash, Hasher}; use core::ops::Range; use std::collections::HashMap; -use std::sync::Arc; use plonky2_field::batch_util::batch_multiply_inplace; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/plonky2/src/gates/high_degree_interpolation.rs b/plonky2/src/gates/high_degree_interpolation.rs index 7460a26d..71584c60 100644 --- a/plonky2/src/gates/high_degree_interpolation.rs +++ b/plonky2/src/gates/high_degree_interpolation.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::marker::PhantomData; use core::ops::Range; diff --git a/plonky2/src/gates/interpolation.rs b/plonky2/src/gates/interpolation.rs index fb61b97e..a707f192 100644 --- a/plonky2/src/gates/interpolation.rs +++ b/plonky2/src/gates/interpolation.rs @@ -1,3 +1,4 @@ +use alloc::vec; use core::ops::Range; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/gates/low_degree_interpolation.rs b/plonky2/src/gates/low_degree_interpolation.rs index de4292a5..4c9c7ecc 100644 --- a/plonky2/src/gates/low_degree_interpolation.rs +++ b/plonky2/src/gates/low_degree_interpolation.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::marker::PhantomData; use core::ops::Range; diff --git a/plonky2/src/gates/multiplication_extension.rs b/plonky2/src/gates/multiplication_extension.rs index 97ff538f..5941f978 100644 --- a/plonky2/src/gates/multiplication_extension.rs +++ b/plonky2/src/gates/multiplication_extension.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::format; +use alloc::string::String; +use alloc::vec::Vec; use core::ops::Range; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/plonky2/src/gates/noop.rs b/plonky2/src/gates/noop.rs index fb5326bd..a4d41a5c 100644 --- a/plonky2/src/gates/noop.rs +++ b/plonky2/src/gates/noop.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use crate::gates::gate::Gate; diff --git a/plonky2/src/gates/packed_util.rs b/plonky2/src/gates/packed_util.rs index 0889f9ae..62bc2d54 100644 --- a/plonky2/src/gates/packed_util.rs +++ b/plonky2/src/gates/packed_util.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use plonky2_field::packable::Packable; use plonky2_field::packed::PackedField; diff --git a/plonky2/src/gates/poseidon.rs b/plonky2/src/gates/poseidon.rs index 0977d7b4..d491cce6 100644 --- a/plonky2/src/gates/poseidon.rs +++ b/plonky2/src/gates/poseidon.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::marker::PhantomData; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/gates/poseidon_mds.rs b/plonky2/src/gates/poseidon_mds.rs index d41fdf17..880a21ff 100644 --- a/plonky2/src/gates/poseidon_mds.rs +++ b/plonky2/src/gates/poseidon_mds.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::marker::PhantomData; use core::ops::Range; diff --git a/plonky2/src/gates/public_input.rs b/plonky2/src/gates/public_input.rs index f84cd82d..d2ab2568 100644 --- a/plonky2/src/gates/public_input.rs +++ b/plonky2/src/gates/public_input.rs @@ -1,3 +1,6 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; use core::ops::Range; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/gates/random_access.rs b/plonky2/src/gates/random_access.rs index 217f5e8a..caf466bd 100644 --- a/plonky2/src/gates/random_access.rs +++ b/plonky2/src/gates/random_access.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::marker::PhantomData; use itertools::Itertools; diff --git a/plonky2/src/gates/reducing.rs b/plonky2/src/gates/reducing.rs index c2366558..12880b28 100644 --- a/plonky2/src/gates/reducing.rs +++ b/plonky2/src/gates/reducing.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::ops::Range; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/plonky2/src/gates/reducing_extension.rs b/plonky2/src/gates/reducing_extension.rs index 61377b1b..f952c0c8 100644 --- a/plonky2/src/gates/reducing_extension.rs +++ b/plonky2/src/gates/reducing_extension.rs @@ -1,3 +1,7 @@ +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::{format, vec}; use core::ops::Range; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/plonky2/src/gates/selectors.rs b/plonky2/src/gates/selectors.rs index d9be7024..bfd13073 100644 --- a/plonky2/src/gates/selectors.rs +++ b/plonky2/src/gates/selectors.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::ops::Range; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/hash/arch/aarch64/poseidon_goldilocks_neon.rs b/plonky2/src/hash/arch/aarch64/poseidon_goldilocks_neon.rs index 352456e7..5cf25dce 100644 --- a/plonky2/src/hash/arch/aarch64/poseidon_goldilocks_neon.rs +++ b/plonky2/src/hash/arch/aarch64/poseidon_goldilocks_neon.rs @@ -1,8 +1,8 @@ #![allow(clippy::assertions_on_constants)] -use std::arch::aarch64::*; -use std::arch::asm; -use std::mem::transmute; +use core::arch::aarch64::*; +use core::arch::asm; +use core::mem::transmute; use plonky2_field::goldilocks_field::GoldilocksField; use plonky2_util::branch_hint; diff --git a/plonky2/src/hash/hash_types.rs b/plonky2/src/hash/hash_types.rs index f69285f7..626d5eb0 100644 --- a/plonky2/src/hash/hash_types.rs +++ b/plonky2/src/hash/hash_types.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use plonky2_field::goldilocks_field::GoldilocksField; use plonky2_field::types::{Field, PrimeField64}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; diff --git a/plonky2/src/hash/hashing.rs b/plonky2/src/hash/hashing.rs index e4ba2719..ac115152 100644 --- a/plonky2/src/hash/hashing.rs +++ b/plonky2/src/hash/hashing.rs @@ -1,5 +1,7 @@ //! Concrete instantiation of a hash function. +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use crate::hash::hash_types::{HashOut, HashOutTarget, RichField}; diff --git a/plonky2/src/hash/keccak.rs b/plonky2/src/hash/keccak.rs index 6e4fcdcb..1a4f5472 100644 --- a/plonky2/src/hash/keccak.rs +++ b/plonky2/src/hash/keccak.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::iter; use core::mem::size_of; diff --git a/plonky2/src/hash/merkle_proofs.rs b/plonky2/src/hash/merkle_proofs.rs index 04a7787f..01a4a395 100644 --- a/plonky2/src/hash/merkle_proofs.rs +++ b/plonky2/src/hash/merkle_proofs.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use anyhow::{ensure, Result}; use plonky2_field::extension::Extendable; use serde::{Deserialize, Serialize}; diff --git a/plonky2/src/hash/merkle_tree.rs b/plonky2/src/hash/merkle_tree.rs index c0799d98..6958bb9c 100644 --- a/plonky2/src/hash/merkle_tree.rs +++ b/plonky2/src/hash/merkle_tree.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::mem::MaybeUninit; use core::slice; diff --git a/plonky2/src/hash/path_compression.rs b/plonky2/src/hash/path_compression.rs index c57b5e03..ed3f49b8 100644 --- a/plonky2/src/hash/path_compression.rs +++ b/plonky2/src/hash/path_compression.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use std::collections::HashMap; use num::Integer; diff --git a/plonky2/src/hash/poseidon.rs b/plonky2/src/hash/poseidon.rs index 248ab32d..6a30c2bd 100644 --- a/plonky2/src/hash/poseidon.rs +++ b/plonky2/src/hash/poseidon.rs @@ -1,6 +1,9 @@ //! Implementation of the Poseidon hash function, as described in //! +use alloc::vec; +use alloc::vec::Vec; + use plonky2_field::extension::{Extendable, FieldExtension}; use plonky2_field::types::{Field, PrimeField64}; use unroll::unroll_for_loops; diff --git a/plonky2/src/iop/challenger.rs b/plonky2/src/iop/challenger.rs index 06b7d4ba..3e8d4148 100644 --- a/plonky2/src/iop/challenger.rs +++ b/plonky2/src/iop/challenger.rs @@ -1,4 +1,5 @@ -use core::convert::TryInto; +use alloc::vec; +use alloc::vec::Vec; use core::marker::PhantomData; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/plonky2/src/iop/ext_target.rs b/plonky2/src/iop/ext_target.rs index eb7e572b..73effa8e 100644 --- a/plonky2/src/iop/ext_target.rs +++ b/plonky2/src/iop/ext_target.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::ops::Range; use plonky2_field::extension::algebra::ExtensionAlgebra; diff --git a/plonky2/src/iop/generator.rs b/plonky2/src/iop/generator.rs index 3d28ae2e..6cd85971 100644 --- a/plonky2/src/iop/generator.rs +++ b/plonky2/src/iop/generator.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::fmt::Debug; use core::marker::PhantomData; diff --git a/plonky2/src/iop/mod.rs b/plonky2/src/iop/mod.rs index de315a09..47642edc 100644 --- a/plonky2/src/iop/mod.rs +++ b/plonky2/src/iop/mod.rs @@ -1,4 +1,5 @@ //! Logic common to multiple IOPs. + pub mod challenger; pub mod ext_target; pub mod generator; diff --git a/plonky2/src/iop/target.rs b/plonky2/src/iop/target.rs index 14e77da1..15be6943 100644 --- a/plonky2/src/iop/target.rs +++ b/plonky2/src/iop/target.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::ops::Range; use crate::iop::ext_target::ExtensionTarget; diff --git a/plonky2/src/iop/wire.rs b/plonky2/src/iop/wire.rs index daf0126e..e6161bf3 100644 --- a/plonky2/src/iop/wire.rs +++ b/plonky2/src/iop/wire.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::ops::Range; use crate::plonk::circuit_data::CircuitConfig; diff --git a/plonky2/src/iop/witness.rs b/plonky2/src/iop/witness.rs index 99d6313d..ef0ff7ae 100644 --- a/plonky2/src/iop/witness.rs +++ b/plonky2/src/iop/witness.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use std::collections::HashMap; use itertools::Itertools; diff --git a/plonky2/src/lib.rs b/plonky2/src/lib.rs index db7a213b..49bc8248 100644 --- a/plonky2/src/lib.rs +++ b/plonky2/src/lib.rs @@ -1,5 +1,8 @@ #![allow(clippy::too_many_arguments)] #![allow(clippy::needless_range_loop)] +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; pub use plonky2_field as field; diff --git a/plonky2/src/plonk/circuit_builder.rs b/plonky2/src/plonk/circuit_builder.rs index f071d69c..06b11c05 100644 --- a/plonky2/src/plonk/circuit_builder.rs +++ b/plonky2/src/plonk/circuit_builder.rs @@ -1,5 +1,9 @@ +use alloc::boxed::Box; +use alloc::collections::BTreeMap; +use alloc::vec; +use alloc::vec::Vec; use core::cmp::max; -use std::collections::{BTreeMap, HashMap, HashSet}; +use std::collections::{HashMap, HashSet}; use std::time::Instant; use itertools::Itertools; diff --git a/plonky2/src/plonk/circuit_data.rs b/plonky2/src/plonk/circuit_data.rs index 7a0af7b8..791160ad 100644 --- a/plonky2/src/plonk/circuit_data.rs +++ b/plonky2/src/plonk/circuit_data.rs @@ -1,5 +1,8 @@ +use alloc::boxed::Box; +use alloc::collections::BTreeMap; +use alloc::vec; +use alloc::vec::Vec; use core::ops::{Range, RangeFrom}; -use std::collections::BTreeMap; use anyhow::Result; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/plonk/config.rs b/plonky2/src/plonk/config.rs index 87395348..ac7399f7 100644 --- a/plonky2/src/plonk/config.rs +++ b/plonky2/src/plonk/config.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::fmt::Debug; use plonky2_field::extension::quadratic::QuadraticExtension; diff --git a/plonky2/src/plonk/copy_constraint.rs b/plonky2/src/plonk/copy_constraint.rs index a838ed37..50e85fbf 100644 --- a/plonky2/src/plonk/copy_constraint.rs +++ b/plonky2/src/plonk/copy_constraint.rs @@ -1,3 +1,5 @@ +use alloc::string::String; + use crate::iop::target::Target; /// A named copy constraint. diff --git a/plonky2/src/plonk/get_challenges.rs b/plonky2/src/plonk/get_challenges.rs index 116529e7..f0635d93 100644 --- a/plonky2/src/plonk/get_challenges.rs +++ b/plonky2/src/plonk/get_challenges.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use std::collections::HashSet; use plonky2_field::extension::Extendable; diff --git a/plonky2/src/plonk/permutation_argument.rs b/plonky2/src/plonk/permutation_argument.rs index 44d492d9..d9d56f93 100644 --- a/plonky2/src/plonk/permutation_argument.rs +++ b/plonky2/src/plonk/permutation_argument.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use std::collections::HashMap; use maybe_rayon::*; diff --git a/plonky2/src/plonk/plonk_common.rs b/plonky2/src/plonk/plonk_common.rs index 24a94bb3..fdd53035 100644 --- a/plonky2/src/plonk/plonk_common.rs +++ b/plonky2/src/plonk/plonk_common.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use plonky2_field::extension::Extendable; use plonky2_field::packed::PackedField; use plonky2_field::types::Field; diff --git a/plonky2/src/plonk/proof.rs b/plonky2/src/plonk/proof.rs index d408d900..a2a60d8c 100644 --- a/plonky2/src/plonk/proof.rs +++ b/plonky2/src/plonk/proof.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use anyhow::ensure; use maybe_rayon::*; use plonky2_field::extension::Extendable; @@ -223,12 +226,14 @@ impl, C: GenericConfig, const D: usize> C::InnerHasher::hash_no_pad(&self.public_inputs) } + #[cfg(feature = "std")] pub fn to_bytes(&self) -> anyhow::Result> { let mut buffer = Buffer::new(Vec::new()); buffer.write_compressed_proof_with_public_inputs(self)?; Ok(buffer.bytes()) } + #[cfg(feature = "std")] pub fn from_bytes( bytes: Vec, common_data: &CommonCircuitData, diff --git a/plonky2/src/plonk/prover.rs b/plonky2/src/plonk/prover.rs index c10febde..35490d7f 100644 --- a/plonky2/src/plonk/prover.rs +++ b/plonky2/src/plonk/prover.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; +use alloc::{format, vec}; use core::mem::swap; use anyhow::{ensure, Result}; diff --git a/plonky2/src/plonk/vanishing_poly.rs b/plonky2/src/plonk/vanishing_poly.rs index 28d43f4f..f27eba48 100644 --- a/plonky2/src/plonk/vanishing_poly.rs +++ b/plonky2/src/plonk/vanishing_poly.rs @@ -1,3 +1,6 @@ +use alloc::vec::Vec; +use alloc::{format, vec}; + use plonky2_field::batch_util::batch_add_inplace; use plonky2_field::extension::{Extendable, FieldExtension}; use plonky2_field::types::Field; diff --git a/plonky2/src/recursion/conditional_recursive_verifier.rs b/plonky2/src/recursion/conditional_recursive_verifier.rs index c78078e8..f91c1d5b 100644 --- a/plonky2/src/recursion/conditional_recursive_verifier.rs +++ b/plonky2/src/recursion/conditional_recursive_verifier.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + use anyhow::{ensure, Result}; use itertools::Itertools; use plonky2_field::extension::Extendable; @@ -186,7 +189,7 @@ impl, const D: usize> CircuitBuilder { h1: HashOutTarget, ) -> HashOutTarget { HashOutTarget { - elements: std::array::from_fn(|i| self.select(b, h0.elements[i], h1.elements[i])), + elements: core::array::from_fn(|i| self.select(b, h0.elements[i], h1.elements[i])), } } diff --git a/plonky2/src/recursion/cyclic_recursion.rs b/plonky2/src/recursion/cyclic_recursion.rs index 9df74c56..b50eecfe 100644 --- a/plonky2/src/recursion/cyclic_recursion.rs +++ b/plonky2/src/recursion/cyclic_recursion.rs @@ -1,4 +1,7 @@ #![allow(clippy::int_plus_one)] // Makes more sense for some inequalities below. + +use alloc::vec; + use anyhow::{ensure, Result}; use itertools::Itertools; use plonky2_field::extension::Extendable; @@ -47,7 +50,7 @@ impl, const D: usize> VerifierOnlyCircuitData { let constants_sigmas_cap = MerkleCap( (0..cap_len) .map(|i| HashOut { - elements: std::array::from_fn(|j| slice[len - 4 * (cap_len - i) + j]), + elements: core::array::from_fn(|j| slice[len - 4 * (cap_len - i) + j]), }) .collect(), ); @@ -72,12 +75,12 @@ impl VerifierCircuitTarget { let constants_sigmas_cap = MerkleCapTarget( (0..cap_len) .map(|i| HashOutTarget { - elements: std::array::from_fn(|j| slice[len - 4 * (cap_len - i) + j]), + elements: core::array::from_fn(|j| slice[len - 4 * (cap_len - i) + j]), }) .collect(), ); let circuit_digest = HashOutTarget { - elements: std::array::from_fn(|i| slice[len - 4 - 4 * cap_len + i]), + elements: core::array::from_fn(|i| slice[len - 4 - 4 * cap_len + i]), }; Ok(Self { @@ -420,7 +423,7 @@ mod tests { let mut h: [F; 4] = initial_hash.try_into().unwrap(); assert_eq!( hash, - std::iter::repeat_with(|| { + core::iter::repeat_with(|| { h = hash_n_to_hash_no_pad::(&h).elements; h }) diff --git a/plonky2/src/util/context_tree.rs b/plonky2/src/util/context_tree.rs index 7ec5214c..bc37b2b0 100644 --- a/plonky2/src/util/context_tree.rs +++ b/plonky2/src/util/context_tree.rs @@ -1,3 +1,7 @@ +use alloc::string::String; +use alloc::vec; +use alloc::vec::Vec; + use log::{log, Level}; /// The hierarchy of contexts, and the gate count contributed by each one. Useful for debugging. diff --git a/plonky2/src/util/mod.rs b/plonky2/src/util/mod.rs index 9f15b8a1..d978dbc5 100644 --- a/plonky2/src/util/mod.rs +++ b/plonky2/src/util/mod.rs @@ -1,13 +1,18 @@ +use alloc::vec; +use alloc::vec::Vec; + use plonky2_field::polynomial::PolynomialValues; use plonky2_field::types::Field; pub(crate) mod context_tree; pub(crate) mod partial_products; pub mod reducing; -pub mod serialization; pub mod strided_view; pub mod timing; +#[cfg(feature = "std")] +pub mod serialization; + pub(crate) fn transpose_poly_values(polys: Vec>) -> Vec> { let poly_values = polys.into_iter().map(|p| p.values).collect::>(); transpose(&poly_values) diff --git a/plonky2/src/util/partial_products.rs b/plonky2/src/util/partial_products.rs index 9fad9dc8..c1b2e979 100644 --- a/plonky2/src/util/partial_products.rs +++ b/plonky2/src/util/partial_products.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::iter; use itertools::Itertools; diff --git a/plonky2/src/util/reducing.rs b/plonky2/src/util/reducing.rs index 9702e196..f91dbb19 100644 --- a/plonky2/src/util/reducing.rs +++ b/plonky2/src/util/reducing.rs @@ -1,3 +1,5 @@ +use alloc::vec; +use alloc::vec::Vec; use core::borrow::Borrow; use plonky2_field::extension::{Extendable, FieldExtension}; diff --git a/util/src/lib.rs b/util/src/lib.rs index b22a4236..6662f7c0 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -4,16 +4,20 @@ #![allow(clippy::len_without_is_empty)] #![allow(clippy::needless_range_loop)] #![allow(clippy::return_self_not_must_use)] +#![no_std] -use std::arch::asm; -use std::hint::unreachable_unchecked; -use std::mem::size_of; -use std::ptr::{swap, swap_nonoverlapping}; +extern crate alloc; -mod transpose_util; +use alloc::vec::Vec; +use core::arch::asm; +use core::hint::unreachable_unchecked; +use core::mem::size_of; +use core::ptr::{swap, swap_nonoverlapping}; use crate::transpose_util::transpose_in_place_square; +mod transpose_util; + pub fn bits_u64(n: u64) -> usize { (64 - n.leading_zeros()) as usize } diff --git a/util/src/transpose_util.rs b/util/src/transpose_util.rs index 1c8280a8..a79a7adb 100644 --- a/util/src/transpose_util.rs +++ b/util/src/transpose_util.rs @@ -1,4 +1,4 @@ -use std::ptr::swap; +use core::ptr::swap; const LB_BLOCK_SIZE: usize = 3;