From e2cdd5a9548809979a7b4ed1e777f8c51b3c89a1 Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Thu, 3 Nov 2022 08:26:03 -0700 Subject: [PATCH] feat: upgrade Sampling APIs Signed-off-by: Brandon H. Gomes --- ecdsa/Cargo.toml | 4 +- ecdsa/src/curve/ecdsa.rs | 4 +- ecdsa/src/curve/glv.rs | 2 +- ecdsa/src/gadgets/curve.rs | 4 +- ecdsa/src/gadgets/curve_fixed_base.rs | 2 +- ecdsa/src/gadgets/curve_msm.rs | 2 +- ecdsa/src/gadgets/curve_windowed_mul.rs | 6 +-- ecdsa/src/gadgets/ecdsa.rs | 2 +- ecdsa/src/gadgets/glv.rs | 2 +- ecdsa/src/gadgets/nonnative.rs | 2 +- ecdsa/src/gadgets/split_nonnative.rs | 2 +- evm/Cargo.toml | 2 +- evm/src/arithmetic/add.rs | 6 +-- evm/src/arithmetic/compare.rs | 6 +-- evm/src/arithmetic/modular.rs | 8 +-- evm/src/arithmetic/mul.rs | 6 +-- evm/src/arithmetic/sub.rs | 6 +-- evm/src/stark_testing.rs | 6 +-- evm/src/verifier.rs | 2 +- field/Cargo.toml | 10 ++-- field/src/extension/algebra.rs | 4 +- field/src/extension/quadratic.rs | 17 ++++--- field/src/extension/quartic.rs | 27 +++++----- field/src/extension/quintic.rs | 29 ++++++----- field/src/fft.rs | 4 +- field/src/field_testing.rs | 9 ++-- field/src/goldilocks_field.rs | 19 ++++--- field/src/interpolation.rs | 2 +- field/src/lib.rs | 2 +- field/src/polynomial/division.rs | 7 +-- field/src/polynomial/mod.rs | 16 +++--- field/src/prime_field_testing.rs | 2 + field/src/secp256k1_base.rs | 19 ++++--- field/src/secp256k1_scalar.rs | 19 ++++--- field/src/types.rs | 49 ++++++++++++------- insertion/src/insert_gadget.rs | 2 +- insertion/src/insertion_gate.rs | 4 +- plonky2/Cargo.toml | 14 +++--- plonky2/benches/hashing.rs | 3 +- plonky2/benches/merkle.rs | 1 + plonky2/benches/reverse_index_bits.rs | 2 +- plonky2/benches/transpose.rs | 2 +- plonky2/examples/bench_recursion.rs | 4 +- plonky2/examples/square_root.rs | 2 +- plonky2/src/gadgets/arithmetic_extension.rs | 6 +-- plonky2/src/gadgets/random_access.rs | 2 +- plonky2/src/gadgets/select.rs | 2 +- plonky2/src/gates/exponentiation.rs | 2 +- plonky2/src/gates/gate_testing.rs | 5 +- .../src/gates/high_degree_interpolation.rs | 2 +- plonky2/src/gates/interpolation.rs | 2 +- plonky2/src/gates/low_degree_interpolation.rs | 2 +- plonky2/src/gates/random_access.rs | 2 +- .../x86_64/poseidon_goldilocks_avx2_bmi2.rs | 8 +-- plonky2/src/hash/hash_types.rs | 42 ++++++++-------- plonky2/src/hash/path_compression.rs | 2 +- plonky2/src/iop/challenger.rs | 2 +- plonky2/src/iop/generator.rs | 1 - plonky2/src/plonk/proof.rs | 2 +- .../conditional_recursive_verifier.rs | 2 +- plonky2/src/recursion/cyclic_recursion.rs | 2 +- plonky2/src/recursion/recursive_verifier.rs | 8 ++- plonky2/src/util/reducing.rs | 1 + starky/Cargo.toml | 2 +- starky/src/stark_testing.rs | 10 ++-- starky/src/verifier.rs | 2 +- system_zero/src/alu/bitops.rs | 8 +-- system_zero/src/alu/division.rs | 6 +-- system_zero/src/permutation_unit.rs | 6 +-- u32/src/gates/add_many_u32.rs | 4 +- u32/src/gates/arithmetic_u32.rs | 4 +- u32/src/gates/comparison.rs | 4 +- u32/src/gates/range_check_u32.rs | 4 +- u32/src/gates/subtraction_u32.rs | 4 +- waksman/src/gates/assert_le.rs | 4 +- waksman/src/gates/switch.rs | 2 +- waksman/src/permutation.rs | 2 +- waksman/src/sorting.rs | 2 +- 78 files changed, 278 insertions(+), 224 deletions(-) diff --git a/ecdsa/Cargo.toml b/ecdsa/Cargo.toml index 99f355b9..ce00f3dc 100644 --- a/ecdsa/Cargo.toml +++ b/ecdsa/Cargo.toml @@ -13,6 +13,6 @@ plonky2_u32 = { path = "../u32" } num = "0.4.0" itertools = "0.10.0" rayon = "1.5.1" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0", default-features = false, features = ["derive"] } anyhow = "1.0.40" -rand = "0.8.4" \ No newline at end of file +rand = "0.8.4" diff --git a/ecdsa/src/curve/ecdsa.rs b/ecdsa/src/curve/ecdsa.rs index 0b2d396d..bbec08b5 100644 --- a/ecdsa/src/curve/ecdsa.rs +++ b/ecdsa/src/curve/ecdsa.rs @@ -1,4 +1,4 @@ -use plonky2_field::types::Field; +use plonky2_field::types::{Field, Sample}; use serde::{Deserialize, Serialize}; use crate::curve::curve_msm::msm_parallel; @@ -64,7 +64,7 @@ pub fn verify_message( #[cfg(test)] mod tests { use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::curve::ecdsa::{sign_message, verify_message, ECDSASecretKey}; use crate::curve::secp256k1::Secp256K1; diff --git a/ecdsa/src/curve/glv.rs b/ecdsa/src/curve/glv.rs index c58032ec..9cf9bc82 100644 --- a/ecdsa/src/curve/glv.rs +++ b/ecdsa/src/curve/glv.rs @@ -103,7 +103,7 @@ pub fn glv_mul(p: ProjectivePoint, k: Secp256K1Scalar) -> ProjectiveP mod tests { use anyhow::Result; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use crate::curve::curve_types::{Curve, CurveScalar}; use crate::curve::glv::{decompose_secp256k1_scalar, glv_mul, GLV_S}; diff --git a/ecdsa/src/gadgets/curve.rs b/ecdsa/src/gadgets/curve.rs index 4aa69733..0d50ba7f 100644 --- a/ecdsa/src/gadgets/curve.rs +++ b/ecdsa/src/gadgets/curve.rs @@ -2,7 +2,7 @@ use plonky2::hash::hash_types::RichField; use plonky2::iop::target::BoolTarget; use plonky2::plonk::circuit_builder::CircuitBuilder; use plonky2_field::extension::Extendable; -use plonky2_field::types::Field; +use plonky2_field::types::Sample; use crate::curve::curve_types::{AffinePoint, Curve, CurveScalar}; use crate::gadgets::nonnative::{CircuitBuilderNonNative, NonNativeTarget}; @@ -263,7 +263,7 @@ mod tests { use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_base::Secp256K1Base; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use crate::curve::curve_types::{AffinePoint, Curve, CurveScalar}; use crate::curve::secp256k1::Secp256K1; diff --git a/ecdsa/src/gadgets/curve_fixed_base.rs b/ecdsa/src/gadgets/curve_fixed_base.rs index 31292ddb..0468fe01 100644 --- a/ecdsa/src/gadgets/curve_fixed_base.rs +++ b/ecdsa/src/gadgets/curve_fixed_base.rs @@ -71,7 +71,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::{Field, PrimeField}; + use plonky2_field::types::{PrimeField, Sample}; use crate::curve::curve_types::{Curve, CurveScalar}; use crate::curve::secp256k1::Secp256K1; diff --git a/ecdsa/src/gadgets/curve_msm.rs b/ecdsa/src/gadgets/curve_msm.rs index e059638c..ba7c5a75 100644 --- a/ecdsa/src/gadgets/curve_msm.rs +++ b/ecdsa/src/gadgets/curve_msm.rs @@ -84,7 +84,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::curve::curve_types::{Curve, CurveScalar}; use crate::curve::secp256k1::Secp256K1; diff --git a/ecdsa/src/gadgets/curve_windowed_mul.rs b/ecdsa/src/gadgets/curve_windowed_mul.rs index bc4e1caf..dce7a0e2 100644 --- a/ecdsa/src/gadgets/curve_windowed_mul.rs +++ b/ecdsa/src/gadgets/curve_windowed_mul.rs @@ -1,4 +1,4 @@ -use std::marker::PhantomData; +use core::marker::PhantomData; use num::BigUint; use plonky2::hash::hash_types::RichField; @@ -7,7 +7,7 @@ use plonky2::iop::target::{BoolTarget, Target}; use plonky2::plonk::circuit_builder::CircuitBuilder; use plonky2::plonk::config::{GenericHashOut, Hasher}; use plonky2_field::extension::Extendable; -use plonky2_field::types::Field; +use plonky2_field::types::{Field, Sample}; use plonky2_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target}; use crate::curve::curve_types::{Curve, CurveScalar}; @@ -177,7 +177,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use rand::Rng; use crate::curve::curve_types::{Curve, CurveScalar}; diff --git a/ecdsa/src/gadgets/ecdsa.rs b/ecdsa/src/gadgets/ecdsa.rs index 3ed6342d..fe2cc397 100644 --- a/ecdsa/src/gadgets/ecdsa.rs +++ b/ecdsa/src/gadgets/ecdsa.rs @@ -57,7 +57,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use super::{ECDSAPublicKeyTarget, ECDSASignatureTarget}; use crate::curve::curve_types::{Curve, CurveScalar}; diff --git a/ecdsa/src/gadgets/glv.rs b/ecdsa/src/gadgets/glv.rs index 539b5de3..00cf64d7 100644 --- a/ecdsa/src/gadgets/glv.rs +++ b/ecdsa/src/gadgets/glv.rs @@ -137,7 +137,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::curve::curve_types::{Curve, CurveScalar}; use crate::curve::glv::glv_mul; diff --git a/ecdsa/src/gadgets/nonnative.rs b/ecdsa/src/gadgets/nonnative.rs index ad6315a1..ca865807 100644 --- a/ecdsa/src/gadgets/nonnative.rs +++ b/ecdsa/src/gadgets/nonnative.rs @@ -647,7 +647,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_base::Secp256K1Base; - use plonky2_field::types::{Field, PrimeField}; + use plonky2_field::types::{Field, PrimeField, Sample}; use crate::gadgets::nonnative::CircuitBuilderNonNative; diff --git a/ecdsa/src/gadgets/split_nonnative.rs b/ecdsa/src/gadgets/split_nonnative.rs index 5ee3a864..330c2ea1 100644 --- a/ecdsa/src/gadgets/split_nonnative.rs +++ b/ecdsa/src/gadgets/split_nonnative.rs @@ -101,7 +101,7 @@ mod tests { use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2_field::secp256k1_scalar::Secp256K1Scalar; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::gadgets::nonnative::{CircuitBuilderNonNative, NonNativeTarget}; use crate::gadgets::split_nonnative::CircuitBuilderSplit; diff --git a/evm/Cargo.toml b/evm/Cargo.toml index cdb8b5d6..314a2b50 100644 --- a/evm/Cargo.toml +++ b/evm/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" edition = "2021" [dependencies] -plonky2 = { path = "../plonky2", default-features = false, features = ["rand", "timing"] } +plonky2 = { path = "../plonky2", default-features = false, features = ["timing"] } plonky2_util = { path = "../util" } eth_trie_utils = "0.4.0" anyhow = "1.0.40" diff --git a/evm/src/arithmetic/add.rs b/evm/src/arithmetic/add.rs index 1bf798cc..b09307b0 100644 --- a/evm/src/arithmetic/add.rs +++ b/evm/src/arithmetic/add.rs @@ -161,7 +161,7 @@ pub fn eval_ext_circuit, const D: usize>( #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -177,7 +177,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_ADD == 0`, then the constraints should be met even // if all values are garbage. @@ -200,7 +200,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // set `IS_ADD == 1` and ensure all constraints are satisfied. lv[IS_ADD] = F::ONE; diff --git a/evm/src/arithmetic/compare.rs b/evm/src/arithmetic/compare.rs index 55dc5764..f4165260 100644 --- a/evm/src/arithmetic/compare.rs +++ b/evm/src/arithmetic/compare.rs @@ -162,7 +162,7 @@ pub fn eval_ext_circuit, const D: usize>( #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -176,7 +176,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_LT == 0`, then the constraints should be met even if // all values are garbage. `eval_packed_generic` handles IS_GT @@ -201,7 +201,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); const N_ITERS: usize = 1000; for _ in 0..N_ITERS { diff --git a/evm/src/arithmetic/modular.rs b/evm/src/arithmetic/modular.rs index d19768bf..09c3996e 100644 --- a/evm/src/arithmetic/modular.rs +++ b/evm/src/arithmetic/modular.rs @@ -501,7 +501,7 @@ pub(crate) fn eval_ext_circuit, const D: usize>( mod tests { use itertools::izip; use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -517,7 +517,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_ADDMOD == 0`, then the constraints should be met even // if all values are garbage. @@ -544,7 +544,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); for op_filter in [IS_ADDMOD, IS_DIV, IS_SUBMOD, IS_MOD, IS_MULMOD] { // Reset operation columns, then select one @@ -595,7 +595,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); for op_filter in [IS_ADDMOD, IS_SUBMOD, IS_DIV, IS_MOD, IS_MULMOD] { // Reset operation columns, then select one diff --git a/evm/src/arithmetic/mul.rs b/evm/src/arithmetic/mul.rs index 7dda18e2..d55ab27b 100644 --- a/evm/src/arithmetic/mul.rs +++ b/evm/src/arithmetic/mul.rs @@ -172,7 +172,7 @@ pub fn eval_ext_circuit, const D: usize>( #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -188,7 +188,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_MUL == 0`, then the constraints should be met even // if all values are garbage. @@ -211,7 +211,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // set `IS_MUL == 1` and ensure all constraints are satisfied. lv[IS_MUL] = F::ONE; diff --git a/evm/src/arithmetic/sub.rs b/evm/src/arithmetic/sub.rs index f8377651..d589f323 100644 --- a/evm/src/arithmetic/sub.rs +++ b/evm/src/arithmetic/sub.rs @@ -93,7 +93,7 @@ pub fn eval_ext_circuit, const D: usize>( #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -109,7 +109,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_SUB == 0`, then the constraints should be met even // if all values are garbage. @@ -132,7 +132,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut lv = [F::default(); NUM_ARITH_COLUMNS].map(|_| F::sample(&mut rng)); // set `IS_SUB == 1` and ensure all constraints are satisfied. lv[IS_SUB] = F::ONE; diff --git a/evm/src/stark_testing.rs b/evm/src/stark_testing.rs index bd0df385..da628403 100644 --- a/evm/src/stark_testing.rs +++ b/evm/src/stark_testing.rs @@ -1,7 +1,7 @@ use anyhow::{ensure, Result}; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::polynomial::{PolynomialCoeffs, PolynomialValues}; -use plonky2::field::types::Field; +use plonky2::field::types::{Field, Sample}; use plonky2::hash::hash_types::RichField; use plonky2::iop::witness::{PartialWitness, Witness}; use plonky2::plonk::circuit_builder::CircuitBuilder; @@ -90,8 +90,8 @@ where { // Compute native constraint evaluation on random values. let vars = StarkEvaluationVars { - local_values: &F::Extension::rand_arr::<{ S::COLUMNS }>(), - next_values: &F::Extension::rand_arr::<{ S::COLUMNS }>(), + local_values: &F::Extension::rand_array::<{ S::COLUMNS }>(), + next_values: &F::Extension::rand_array::<{ S::COLUMNS }>(), }; let alphas = F::rand_vec(1); let z_last = F::Extension::rand(); diff --git a/evm/src/verifier.rs b/evm/src/verifier.rs index 0bfbc3d4..ce15399a 100644 --- a/evm/src/verifier.rs +++ b/evm/src/verifier.rs @@ -275,7 +275,7 @@ fn eval_l_0_and_l_last(log_n: usize, x: F) -> (F, F) { mod tests { use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::field::polynomial::PolynomialValues; - use plonky2::field::types::Field; + use plonky2::field::types::Sample; use crate::verifier::eval_l_0_and_l_last; diff --git a/field/Cargo.toml b/field/Cargo.toml index 33bb76c2..1242dfe3 100644 --- a/field/Cargo.toml +++ b/field/Cargo.toml @@ -4,16 +4,12 @@ description = "Finite field arithmetic" version = "0.1.0" edition = "2021" -[features] -default = [] -rand = ["dep:rand", "num/rand"] - [dependencies] 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"] } +itertools = { version = "0.10.0", default-features = false, features = ["use_alloc"] } +num = { version = "0.4", default-features = false, features = ["alloc", "rand"] } plonky2_util = { path = "../util", default-features = false } -rand = { version = "0.8.5", optional = true, default-features = false, features = ["getrandom"] } +rand = { version = "0.8.5", 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/extension/algebra.rs b/field/src/extension/algebra.rs index 6e309ba8..8ca939b2 100644 --- a/field/src/extension/algebra.rs +++ b/field/src/extension/algebra.rs @@ -191,12 +191,14 @@ impl, const D: usize> PolynomialCoeffsAlgebra { #[cfg(test)] mod tests { + use alloc::vec::Vec; + use itertools::Itertools; use crate::extension::algebra::ExtensionAlgebra; use crate::extension::{Extendable, FieldExtension}; use crate::goldilocks_field::GoldilocksField; - use crate::types::Field; + use crate::types::{Field, Sample}; /// Tests that the multiplication on the extension algebra lifts that of the field extension. fn test_extension_algebra, const D: usize>() { diff --git a/field/src/extension/quadratic.rs b/field/src/extension/quadratic.rs index 1909e35e..c0c9758b 100644 --- a/field/src/extension/quadratic.rs +++ b/field/src/extension/quadratic.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::extension::{Extendable, FieldExtension, Frobenius, OEF}; use crate::ops::Square; -use crate::types::Field; +use crate::types::{Field, Sample}; #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] #[serde(bound = "")] @@ -48,6 +48,16 @@ impl> From for QuadraticExtension { } } +impl> Sample for QuadraticExtension { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { + Self([F::sample(rng), F::sample(rng)]) + } +} + impl> Field for QuadraticExtension { const ZERO: Self = Self([F::ZERO; 2]); const ONE: Self = Self([F::ONE, F::ZERO]); @@ -99,11 +109,6 @@ impl> Field for QuadraticExtension { fn from_noncanonical_u128(n: u128) -> Self { F::from_noncanonical_u128(n).into() } - - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { - Self([F::rand_from_rng(rng), F::rand_from_rng(rng)]) - } } impl> Display for QuadraticExtension { diff --git a/field/src/extension/quartic.rs b/field/src/extension/quartic.rs index 948c29fb..e7aba63b 100644 --- a/field/src/extension/quartic.rs +++ b/field/src/extension/quartic.rs @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize}; use crate::extension::{Extendable, FieldExtension, Frobenius, OEF}; use crate::ops::Square; -use crate::types::Field; +use crate::types::{Field, Sample}; #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] #[serde(bound = "")] @@ -49,6 +49,21 @@ impl> From for QuarticExtension { } } +impl> Sample for QuarticExtension { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { + Self::from_basefield_array([ + F::sample(rng), + F::sample(rng), + F::sample(rng), + F::sample(rng), + ]) + } +} + impl> Field for QuarticExtension { const ZERO: Self = Self([F::ZERO; 4]); const ONE: Self = Self([F::ONE, F::ZERO, F::ZERO, F::ZERO]); @@ -104,16 +119,6 @@ impl> Field for QuarticExtension { fn from_noncanonical_u128(n: u128) -> Self { F::from_noncanonical_u128(n).into() } - - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { - Self::from_basefield_array([ - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), - ]) - } } impl> Display for QuarticExtension { diff --git a/field/src/extension/quintic.rs b/field/src/extension/quintic.rs index 343e6f77..d4b605eb 100644 --- a/field/src/extension/quintic.rs +++ b/field/src/extension/quintic.rs @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize}; use crate::extension::{Extendable, FieldExtension, Frobenius, OEF}; use crate::ops::Square; -use crate::types::Field; +use crate::types::{Field, Sample}; #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] #[serde(bound = "")] @@ -49,6 +49,22 @@ impl> From for QuinticExtension { } } +impl> Sample for QuinticExtension { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { + Self::from_basefield_array([ + F::sample(rng), + F::sample(rng), + F::sample(rng), + F::sample(rng), + F::sample(rng), + ]) + } +} + impl> Field for QuinticExtension { const ZERO: Self = Self([F::ZERO; 5]); const ONE: Self = Self([F::ONE, F::ZERO, F::ZERO, F::ZERO, F::ZERO]); @@ -110,17 +126,6 @@ impl> Field for QuinticExtension { fn from_noncanonical_u128(n: u128) -> Self { F::from_noncanonical_u128(n).into() } - - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { - Self::from_basefield_array([ - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), - ]) - } } impl> Display for QuinticExtension { diff --git a/field/src/fft.rs b/field/src/fft.rs index 8fb1809c..0a0b5dd8 100644 --- a/field/src/fft.rs +++ b/field/src/fft.rs @@ -207,6 +207,8 @@ pub(crate) fn fft_classic(values: &mut [F], r: usize, root_table: &Fft #[cfg(test)] mod tests { + use alloc::vec::Vec; + use plonky2_util::{log2_ceil, log2_strict}; use crate::fft::{fft, fft_with_options, ifft}; @@ -224,7 +226,7 @@ mod tests { // "random", the last degree_padded-degree of them are zero. let coeffs = (0..degree) .map(|i| F::from_canonical_usize(i * 1337 % 100)) - .chain(std::iter::repeat(F::ZERO).take(degree_padded - degree)) + .chain(core::iter::repeat(F::ZERO).take(degree_padded - degree)) .collect::>(); assert_eq!(coeffs.len(), degree_padded); let coefficients = PolynomialCoeffs { coeffs }; diff --git a/field/src/field_testing.rs b/field/src/field_testing.rs index 5e495311..4c53c234 100644 --- a/field/src/field_testing.rs +++ b/field/src/field_testing.rs @@ -1,14 +1,17 @@ use crate::extension::{Extendable, Frobenius}; use crate::ops::Square; -use crate::types::Field; +use crate::types::{Field, Sample}; #[macro_export] macro_rules! test_field_arithmetic { ($field:ty) => { mod field_arithmetic { + use alloc::vec::Vec; + use num::bigint::BigUint; + use rand::rngs::OsRng; use rand::Rng; - use $crate::types::Field; + use $crate::types::{Field, Sample}; #[test] fn batch_inversion() { @@ -71,7 +74,7 @@ macro_rules! test_field_arithmetic { fn exponentiation_large() { type F = $field; - let mut rng = rand::thread_rng(); + let mut rng = OsRng; let base = F::rand(); let pow = BigUint::from(rng.gen::()); diff --git a/field/src/goldilocks_field.rs b/field/src/goldilocks_field.rs index 9c7e8d1b..12c8da3a 100644 --- a/field/src/goldilocks_field.rs +++ b/field/src/goldilocks_field.rs @@ -8,7 +8,7 @@ use plonky2_util::{assume, branch_hint}; use serde::{Deserialize, Serialize}; use crate::inversion::try_inverse_u64; -use crate::types::{Field, Field64, PrimeField, PrimeField64}; +use crate::types::{Field, Field64, PrimeField, PrimeField64, Sample}; const EPSILON: u64 = (1 << 32) - 1; @@ -56,6 +56,17 @@ impl Debug for GoldilocksField { } } +impl Sample for GoldilocksField { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { + use rand::Rng; + Self::from_canonical_u64(rng.gen_range(0..Self::ORDER)) + } +} + impl Field for GoldilocksField { const ZERO: Self = Self(0); const ONE: Self = Self(1); @@ -103,12 +114,6 @@ impl Field for GoldilocksField { reduce128(n) } - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { - use rand::Rng; - Self::from_canonical_u64(rng.gen_range(0..Self::ORDER)) - } - #[inline] fn multiply_accumulate(&self, x: Self, y: Self) -> Self { // u64 + u64 * u64 cannot overflow. diff --git a/field/src/interpolation.rs b/field/src/interpolation.rs index 4a42e4af..df708457 100644 --- a/field/src/interpolation.rs +++ b/field/src/interpolation.rs @@ -81,7 +81,7 @@ mod tests { use crate::extension::quartic::QuarticExtension; use crate::goldilocks_field::GoldilocksField; use crate::polynomial::PolynomialCoeffs; - use crate::types::Field; + use crate::types::{Field, Sample}; #[test] fn interpolant_random() { diff --git a/field/src/lib.rs b/field/src/lib.rs index 5459b38a..33db5c27 100644 --- a/field/src/lib.rs +++ b/field/src/lib.rs @@ -8,7 +8,7 @@ #![feature(generic_const_exprs)] #![feature(specialization)] #![feature(stdsimd)] -#![no_std] +#![cfg_attr(not(test), no_std)] extern crate alloc; diff --git a/field/src/polynomial/division.rs b/field/src/polynomial/division.rs index 14e16841..7d85d549 100644 --- a/field/src/polynomial/division.rs +++ b/field/src/polynomial/division.rs @@ -134,17 +134,18 @@ impl PolynomialCoeffs { #[cfg(test)] mod tests { - use rand::{thread_rng, Rng}; + use rand::rngs::OsRng; + use rand::Rng; use crate::extension::quartic::QuarticExtension; use crate::goldilocks_field::GoldilocksField; use crate::polynomial::PolynomialCoeffs; - use crate::types::Field; + use crate::types::{Field, Sample}; #[test] fn test_division_by_linear() { type F = QuarticExtension; - let n = thread_rng().gen_range(1..1000); + let n = OsRng.gen_range(1..1000); let poly = PolynomialCoeffs::new(F::rand_vec(n)); let z = F::rand(); let ev = poly.eval(z); diff --git a/field/src/polynomial/mod.rs b/field/src/polynomial/mod.rs index e5143292..f61ad419 100644 --- a/field/src/polynomial/mod.rs +++ b/field/src/polynomial/mod.rs @@ -442,10 +442,12 @@ impl Mul for &PolynomialCoeffs { mod tests { use std::time::Instant; - use rand::{thread_rng, Rng}; + use rand::rngs::OsRng; + use rand::Rng; use super::*; use crate::goldilocks_field::GoldilocksField; + use crate::types::Sample; #[test] fn test_trimmed() { @@ -518,7 +520,7 @@ mod tests { #[test] fn test_polynomial_multiplication() { type F = GoldilocksField; - let mut rng = thread_rng(); + let mut rng = OsRng; let (a_deg, b_deg) = (rng.gen_range(1..10_000), rng.gen_range(1..10_000)); let a = PolynomialCoeffs::new(F::rand_vec(a_deg)); let b = PolynomialCoeffs::new(F::rand_vec(b_deg)); @@ -534,7 +536,7 @@ mod tests { #[test] fn test_inv_mod_xn() { type F = GoldilocksField; - let mut rng = thread_rng(); + let mut rng = OsRng; let a_deg = rng.gen_range(0..1_000); let n = rng.gen_range(1..1_000); let mut a = PolynomialCoeffs::new(F::rand_vec(a_deg + 1)); @@ -559,7 +561,7 @@ mod tests { #[test] fn test_polynomial_long_division() { type F = GoldilocksField; - let mut rng = thread_rng(); + let mut rng = OsRng; let (a_deg, b_deg) = (rng.gen_range(1..10_000), rng.gen_range(1..10_000)); let a = PolynomialCoeffs::new(F::rand_vec(a_deg)); let b = PolynomialCoeffs::new(F::rand_vec(b_deg)); @@ -573,7 +575,7 @@ mod tests { #[test] fn test_polynomial_division() { type F = GoldilocksField; - let mut rng = thread_rng(); + let mut rng = OsRng; let (a_deg, b_deg) = (rng.gen_range(1..10_000), rng.gen_range(1..10_000)); let a = PolynomialCoeffs::new(F::rand_vec(a_deg)); let b = PolynomialCoeffs::new(F::rand_vec(b_deg)); @@ -587,7 +589,7 @@ mod tests { #[test] fn test_polynomial_division_by_constant() { type F = GoldilocksField; - let mut rng = thread_rng(); + let mut rng = OsRng; let a_deg = rng.gen_range(1..10_000); let a = PolynomialCoeffs::new(F::rand_vec(a_deg)); let b = PolynomialCoeffs::from(vec![F::rand()]); @@ -603,7 +605,7 @@ mod tests { #[test] fn test_division_linear() { type F = GoldilocksField; - let mut rng = thread_rng(); + let mut rng = OsRng; let l = 14; let n = 1 << l; let g = F::primitive_root_of_unity(l); diff --git a/field/src/prime_field_testing.rs b/field/src/prime_field_testing.rs index 7b0d9624..42dc9462 100644 --- a/field/src/prime_field_testing.rs +++ b/field/src/prime_field_testing.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use crate::types::PrimeField64; /// Generates a series of non-negative integers less than `modulus` which cover a range of diff --git a/field/src/secp256k1_base.rs b/field/src/secp256k1_base.rs index 9d90da9b..eaa964f8 100644 --- a/field/src/secp256k1_base.rs +++ b/field/src/secp256k1_base.rs @@ -9,7 +9,7 @@ use num::bigint::BigUint; use num::{Integer, One}; use serde::{Deserialize, Serialize}; -use crate::types::{Field, PrimeField}; +use crate::types::{Field, PrimeField, Sample}; /// The base field of the secp256k1 elliptic curve. /// @@ -65,6 +65,17 @@ impl Debug for Secp256K1Base { } } +impl Sample for Secp256K1Base { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { + use num::bigint::RandBigInt; + Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order())) + } +} + impl Field for Secp256K1Base { const ZERO: Self = Self([0; 4]); const ONE: Self = Self([1, 0, 0, 0]); @@ -131,12 +142,6 @@ impl Field for Secp256K1Base { fn from_noncanonical_u96(n: (u64, u32)) -> Self { Self([n.0, n.1 as u64, 0, 0]) } - - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { - use num::bigint::RandBigInt; - Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order())) - } } impl PrimeField for Secp256K1Base { diff --git a/field/src/secp256k1_scalar.rs b/field/src/secp256k1_scalar.rs index 3a98d9e7..1f1de697 100644 --- a/field/src/secp256k1_scalar.rs +++ b/field/src/secp256k1_scalar.rs @@ -9,7 +9,7 @@ use num::bigint::BigUint; use num::{Integer, One}; use serde::{Deserialize, Serialize}; -use crate::types::{Field, PrimeField}; +use crate::types::{Field, PrimeField, Sample}; /// The base field of the secp256k1 elliptic curve. /// @@ -67,6 +67,17 @@ impl Debug for Secp256K1Scalar { } } +impl Sample for Secp256K1Scalar { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { + use num::bigint::RandBigInt; + Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order())) + } +} + impl Field for Secp256K1Scalar { const ZERO: Self = Self([0; 4]); const ONE: Self = Self([1, 0, 0, 0]); @@ -139,12 +150,6 @@ impl Field for Secp256K1Scalar { fn from_noncanonical_u96(n: (u64, u32)) -> Self { Self([n.0, n.1 as u64, 0, 0]) } - - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self { - use num::bigint::RandBigInt; - Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order())) - } } impl PrimeField for Secp256K1Scalar { diff --git a/field/src/types.rs b/field/src/types.rs index 4df26ea2..0ae31847 100644 --- a/field/src/types.rs +++ b/field/src/types.rs @@ -8,12 +8,42 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAss use num::bigint::BigUint; use num::{Integer, One, ToPrimitive, Zero}; use plonky2_util::bits_u64; +use rand::rngs::OsRng; use serde::de::DeserializeOwned; use serde::Serialize; use crate::extension::Frobenius; use crate::ops::Square; +/// Sampling +pub trait Sample: Sized { + /// Samples a single value using `rng`. + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized; + + /// Samples a single value using the [`OsRng`]. + #[inline] + fn rand() -> Self { + Self::sample(&mut OsRng) + } + + /// Samples a [`Vec`] of values of length `n` using [`OsRng`]. + #[inline] + fn rand_vec(n: usize) -> Vec { + (0..n).map(|_| Self::rand()).collect() + } + + /// Samples an array of values of length `N` using [`OsRng`]. + #[inline] + fn rand_array() -> [Self; N] { + Self::rand_vec(N) + .try_into() + .ok() + .expect("This conversion can never fail.") + } +} + /// A finite field. pub trait Field: 'static @@ -35,6 +65,7 @@ pub trait Field: + Debug + Default + Display + + Sample + Send + Sync + Serialize @@ -319,9 +350,6 @@ pub trait Field: Self::from_noncanonical_u128(n) } - #[cfg(feature = "rand")] - fn rand_from_rng(rng: &mut R) -> Self; - fn exp_power_of_2(&self, power_log: usize) -> Self { let mut res = *self; for _ in 0..power_log { @@ -399,21 +427,6 @@ pub trait Field: } } - #[cfg(feature = "rand")] - fn rand() -> Self { - Self::rand_from_rng(&mut rand::rngs::OsRng) - } - - #[cfg(feature = "rand")] - fn rand_arr() -> [Self; N] { - Self::rand_vec(N).try_into().unwrap() - } - - #[cfg(feature = "rand")] - fn rand_vec(n: usize) -> Vec { - (0..n).map(|_| Self::rand()).collect() - } - /// Representative `g` of the coset used in FRI, so that LDEs in FRI are done over `gH`. fn coset_shift() -> Self { Self::MULTIPLICATIVE_GROUP_GENERATOR diff --git a/insertion/src/insert_gadget.rs b/insertion/src/insert_gadget.rs index ff0ec397..dde8e940 100644 --- a/insertion/src/insert_gadget.rs +++ b/insertion/src/insert_gadget.rs @@ -50,7 +50,7 @@ impl, const D: usize> CircuitBuilderInsert #[cfg(test)] mod tests { use anyhow::Result; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use plonky2::iop::witness::PartialWitness; use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; diff --git a/insertion/src/insertion_gate.rs b/insertion/src/insertion_gate.rs index 2757dd23..5694c2b4 100644 --- a/insertion/src/insertion_gate.rs +++ b/insertion/src/insertion_gate.rs @@ -317,11 +317,11 @@ impl, const D: usize> SimpleGenerator for Insert #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use plonky2::gates::gate::Gate; use plonky2::gates::gate_testing::{test_eval_fns, test_low_degree}; use plonky2::hash::hash_types::HashOut; diff --git a/plonky2/Cargo.toml b/plonky2/Cargo.toml index b74cc151..a9ad3de2 100644 --- a/plonky2/Cargo.toml +++ b/plonky2/Cargo.toml @@ -11,12 +11,11 @@ edition = "2021" default-run = "generate_constants" [features] -default = ["gate_testing", "parallel", "rand", "rand_chacha", "std", "timing"] -rand = ["dep:rand", "num/rand", "plonky2_field/rand"] -gate_testing = ["rand"] +default = ["gate_testing", "parallel", "rand_chacha", "std", "timing"] +gate_testing = [] parallel = ["hashbrown/rayon", "maybe_rayon/parallel"] std = ["anyhow/std", "rand/std"] -timing = [] +timing = ["std"] [dependencies] anyhow = { version = "1.0.40", default-features = false } @@ -26,10 +25,10 @@ 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 } +num = { version = "0.4", default-features = false, features = ["rand"] } 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 = { version = "0.8.4", 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 } @@ -40,6 +39,7 @@ unroll = { version = "0.1.5", default-features = false } criterion = "0.3.5" env_logger = "0.9.0" num_cpus = "1.13.1" +plonky2 = { path = "." } rand = "0.8.4" rand_chacha = "0.3.1" rayon = "1.5.1" @@ -51,7 +51,7 @@ jemallocator = "0.3.2" [[bin]] name = "generate_constants" -required-features = ["rand", "rand_chacha"] +required-features = ["rand_chacha"] [[bench]] name = "field_arithmetic" diff --git a/plonky2/benches/hashing.rs b/plonky2/benches/hashing.rs index 673e0572..fd2e0991 100644 --- a/plonky2/benches/hashing.rs +++ b/plonky2/benches/hashing.rs @@ -5,6 +5,7 @@ mod allocator; use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use plonky2::field::goldilocks_field::GoldilocksField; +use plonky2::field::types::Sample; use plonky2::hash::hash_types::{BytesHash, RichField}; use plonky2::hash::hashing::SPONGE_WIDTH; use plonky2::hash::keccak::KeccakHash; @@ -27,7 +28,7 @@ pub(crate) fn bench_poseidon(c: &mut Criterion) { &format!("poseidon<{}, {SPONGE_WIDTH}>", type_name::()), |b| { b.iter_batched( - || F::rand_arr::(), + || F::rand_array::(), |state| F::poseidon(state), BatchSize::SmallInput, ) diff --git a/plonky2/benches/merkle.rs b/plonky2/benches/merkle.rs index 88302ae9..27f23966 100644 --- a/plonky2/benches/merkle.rs +++ b/plonky2/benches/merkle.rs @@ -1,3 +1,4 @@ +#![allow(incomplete_features)] #![feature(generic_const_exprs)] mod allocator; diff --git a/plonky2/benches/reverse_index_bits.rs b/plonky2/benches/reverse_index_bits.rs index 8916fb5d..5c838a18 100644 --- a/plonky2/benches/reverse_index_bits.rs +++ b/plonky2/benches/reverse_index_bits.rs @@ -2,7 +2,7 @@ mod allocator; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use plonky2::field::goldilocks_field::GoldilocksField; -use plonky2::field::types::Field; +use plonky2::field::types::Sample; use plonky2_util::{reverse_index_bits, reverse_index_bits_in_place}; type F = GoldilocksField; diff --git a/plonky2/benches/transpose.rs b/plonky2/benches/transpose.rs index 64d103ad..c2aecd5f 100644 --- a/plonky2/benches/transpose.rs +++ b/plonky2/benches/transpose.rs @@ -2,7 +2,7 @@ mod allocator; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use plonky2::field::goldilocks_field::GoldilocksField; -use plonky2::field::types::Field; +use plonky2::field::types::Sample; use plonky2::util::transpose; fn criterion_benchmark(c: &mut Criterion) { diff --git a/plonky2/examples/bench_recursion.rs b/plonky2/examples/bench_recursion.rs index 6e196ef5..dc88f764 100644 --- a/plonky2/examples/bench_recursion.rs +++ b/plonky2/examples/bench_recursion.rs @@ -154,7 +154,7 @@ fn test_serialization, C: GenericConfig, where [(); C::Hasher::HASH_SIZE]:, { - let proof_bytes = proof.to_bytes()?; + let proof_bytes = proof.to_bytes(); info!("Proof length: {} bytes", proof_bytes.len()); let proof_from_bytes = ProofWithPublicInputs::from_bytes(proof_bytes, cd)?; assert_eq!(proof, &proof_from_bytes); @@ -167,7 +167,7 @@ where info!("{:.4}s to compress proof", now.elapsed().as_secs_f64()); assert_eq!(proof, &decompressed_compressed_proof); - let compressed_proof_bytes = compressed_proof.to_bytes()?; + let compressed_proof_bytes = compressed_proof.to_bytes(); info!( "Compressed proof length: {} bytes", compressed_proof_bytes.len() diff --git a/plonky2/examples/square_root.rs b/plonky2/examples/square_root.rs index 4411ac50..512c842c 100644 --- a/plonky2/examples/square_root.rs +++ b/plonky2/examples/square_root.rs @@ -1,7 +1,7 @@ use core::marker::PhantomData; use anyhow::Result; -use plonky2::field::types::{Field, PrimeField}; +use plonky2::field::types::{PrimeField, Sample}; use plonky2::hash::hash_types::RichField; use plonky2::iop::generator::{GeneratedValues, SimpleGenerator}; use plonky2::iop::target::Target; diff --git a/plonky2/src/gadgets/arithmetic_extension.rs b/plonky2/src/gadgets/arithmetic_extension.rs index b56d245c..6232d7cb 100644 --- a/plonky2/src/gadgets/arithmetic_extension.rs +++ b/plonky2/src/gadgets/arithmetic_extension.rs @@ -571,7 +571,7 @@ pub(crate) struct ExtensionArithmeticOperation, const mod tests { use anyhow::Result; use plonky2_field::extension::algebra::ExtensionAlgebra; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::iop::ext_target::ExtensionAlgebraTarget; use crate::iop::witness::{PartialWitness, Witness}; @@ -666,8 +666,8 @@ mod tests { builder.connect_extension(zt.0[i], comp_zt.0[i]); } - let x = ExtensionAlgebra::(FF::rand_arr()); - let y = ExtensionAlgebra::(FF::rand_arr()); + let x = ExtensionAlgebra::(FF::rand_array()); + let y = ExtensionAlgebra::(FF::rand_array()); let z = x * y; for i in 0..D { pw.set_extension_target(xt.0[i], x.0[i]); diff --git a/plonky2/src/gadgets/random_access.rs b/plonky2/src/gadgets/random_access.rs index b9561962..a3febec7 100644 --- a/plonky2/src/gadgets/random_access.rs +++ b/plonky2/src/gadgets/random_access.rs @@ -57,7 +57,7 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { use anyhow::Result; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use super::*; use crate::iop::witness::PartialWitness; diff --git a/plonky2/src/gadgets/select.rs b/plonky2/src/gadgets/select.rs index d234a003..03e18188 100644 --- a/plonky2/src/gadgets/select.rs +++ b/plonky2/src/gadgets/select.rs @@ -40,7 +40,7 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { use anyhow::Result; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::iop::witness::{PartialWitness, Witness}; use crate::plonk::circuit_builder::CircuitBuilder; diff --git a/plonky2/src/gates/exponentiation.rs b/plonky2/src/gates/exponentiation.rs index 87975f94..f7c2bc60 100644 --- a/plonky2/src/gates/exponentiation.rs +++ b/plonky2/src/gates/exponentiation.rs @@ -290,7 +290,7 @@ mod tests { use anyhow::Result; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use plonky2_util::log2_ceil; use rand::Rng; diff --git a/plonky2/src/gates/gate_testing.rs b/plonky2/src/gates/gate_testing.rs index 8492f385..0e87c262 100644 --- a/plonky2/src/gates/gate_testing.rs +++ b/plonky2/src/gates/gate_testing.rs @@ -1,7 +1,10 @@ +use alloc::vec; +use alloc::vec::Vec; + use anyhow::{ensure, Result}; use plonky2_field::extension::{Extendable, FieldExtension}; use plonky2_field::polynomial::{PolynomialCoeffs, PolynomialValues}; -use plonky2_field::types::Field; +use plonky2_field::types::{Field, Sample}; use plonky2_util::log2_ceil; use crate::gates::gate::Gate; diff --git a/plonky2/src/gates/high_degree_interpolation.rs b/plonky2/src/gates/high_degree_interpolation.rs index 71584c60..110ea167 100644 --- a/plonky2/src/gates/high_degree_interpolation.rs +++ b/plonky2/src/gates/high_degree_interpolation.rs @@ -279,7 +279,7 @@ mod tests { use anyhow::Result; use plonky2_field::goldilocks_field::GoldilocksField; use plonky2_field::polynomial::PolynomialCoeffs; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use crate::gates::gate::Gate; use crate::gates::gate_testing::{test_eval_fns, test_low_degree}; diff --git a/plonky2/src/gates/interpolation.rs b/plonky2/src/gates/interpolation.rs index a707f192..7f4953d5 100644 --- a/plonky2/src/gates/interpolation.rs +++ b/plonky2/src/gates/interpolation.rs @@ -108,7 +108,7 @@ mod tests { use anyhow::Result; use plonky2_field::extension::FieldExtension; use plonky2_field::interpolation::interpolant; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use crate::gates::high_degree_interpolation::HighDegreeInterpolationGate; use crate::gates::low_degree_interpolation::LowDegreeInterpolationGate; diff --git a/plonky2/src/gates/low_degree_interpolation.rs b/plonky2/src/gates/low_degree_interpolation.rs index 4c9c7ecc..77a84484 100644 --- a/plonky2/src/gates/low_degree_interpolation.rs +++ b/plonky2/src/gates/low_degree_interpolation.rs @@ -390,7 +390,7 @@ mod tests { use plonky2_field::extension::quadratic::QuadraticExtension; use plonky2_field::goldilocks_field::GoldilocksField; use plonky2_field::polynomial::PolynomialCoeffs; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use crate::gates::gate::Gate; use crate::gates::gate_testing::{test_eval_fns, test_low_degree}; diff --git a/plonky2/src/gates/random_access.rs b/plonky2/src/gates/random_access.rs index caf466bd..dbc3a947 100644 --- a/plonky2/src/gates/random_access.rs +++ b/plonky2/src/gates/random_access.rs @@ -384,7 +384,7 @@ mod tests { use anyhow::Result; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use rand::{thread_rng, Rng}; use crate::gates::gate::Gate; diff --git a/plonky2/src/hash/arch/x86_64/poseidon_goldilocks_avx2_bmi2.rs b/plonky2/src/hash/arch/x86_64/poseidon_goldilocks_avx2_bmi2.rs index b40b4277..ae08f568 100644 --- a/plonky2/src/hash/arch/x86_64/poseidon_goldilocks_avx2_bmi2.rs +++ b/plonky2/src/hash/arch/x86_64/poseidon_goldilocks_avx2_bmi2.rs @@ -1,9 +1,9 @@ -use std::arch::asm; -use std::arch::x86_64::*; -use std::mem::size_of; +use core::arch::asm; +use core::arch::x86_64::*; +use core::mem::size_of; -use plonky2_field::types::Field; use plonky2_field::goldilocks_field::GoldilocksField; +use plonky2_field::types::Field; use plonky2_util::branch_hint; use static_assertions::const_assert; diff --git a/plonky2/src/hash/hash_types.rs b/plonky2/src/hash/hash_types.rs index 626d5eb0..e7f29044 100644 --- a/plonky2/src/hash/hash_types.rs +++ b/plonky2/src/hash/hash_types.rs @@ -1,7 +1,7 @@ use alloc::vec::Vec; use plonky2_field::goldilocks_field::GoldilocksField; -use plonky2_field::types::{Field, PrimeField64}; +use plonky2_field::types::{Field, PrimeField64, Sample}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::hash::poseidon::Poseidon; @@ -37,24 +37,26 @@ impl HashOut { elements[0..elements_in.len()].copy_from_slice(elements_in); Self { elements } } +} - #[cfg(all(feature = "parallel", feature = "rand"))] - pub fn rand_from_rng(rng: &mut R) -> Self { +impl Sample for HashOut +where + F: Field, +{ + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { Self { elements: [ - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), - F::rand_from_rng(rng), + F::sample(rng), + F::sample(rng), + F::sample(rng), + F::sample(rng), ], } } - - pub fn rand() -> Self { - Self { - elements: [F::rand(), F::rand(), F::rand(), F::rand()], - } - } } impl GenericHashOut for HashOut { @@ -116,18 +118,16 @@ pub struct MerkleCapTarget(pub Vec); #[derive(Eq, PartialEq, Copy, Clone, Debug)] pub struct BytesHash(pub [u8; N]); -impl BytesHash { - #[cfg(feature = "rand")] - pub fn rand_from_rng(rng: &mut R) -> Self { +impl Sample for BytesHash { + #[inline] + fn sample(rng: &mut R) -> Self + where + R: rand::RngCore + ?Sized, + { let mut buf = [0; N]; rng.fill_bytes(&mut buf); Self(buf) } - - #[cfg(feature = "rand")] - pub fn rand() -> Self { - Self::rand_from_rng(&mut rand::thread_rng()) - } } impl GenericHashOut for BytesHash { diff --git a/plonky2/src/hash/path_compression.rs b/plonky2/src/hash/path_compression.rs index ed93b25d..7efe3a67 100644 --- a/plonky2/src/hash/path_compression.rs +++ b/plonky2/src/hash/path_compression.rs @@ -114,7 +114,7 @@ pub(crate) fn decompress_merkle_proofs>( #[cfg(test)] mod tests { - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use rand::{thread_rng, Rng}; use super::*; diff --git a/plonky2/src/iop/challenger.rs b/plonky2/src/iop/challenger.rs index 3e8d4148..7b666494 100644 --- a/plonky2/src/iop/challenger.rs +++ b/plonky2/src/iop/challenger.rs @@ -301,7 +301,7 @@ impl, H: AlgebraicHasher, const D: usize> #[cfg(test)] mod tests { - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::iop::challenger::{Challenger, RecursiveChallenger}; use crate::iop::generator::generate_partial_witness; diff --git a/plonky2/src/iop/generator.rs b/plonky2/src/iop/generator.rs index 6cd85971..522cee18 100644 --- a/plonky2/src/iop/generator.rs +++ b/plonky2/src/iop/generator.rs @@ -274,7 +274,6 @@ impl SimpleGenerator for RandomValueGenerator { fn run_once(&self, _witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { let random_value = F::rand(); - out_buffer.set_target(self.target, random_value); } } diff --git a/plonky2/src/plonk/proof.rs b/plonky2/src/plonk/proof.rs index a8a2f418..065e2328 100644 --- a/plonky2/src/plonk/proof.rs +++ b/plonky2/src/plonk/proof.rs @@ -383,7 +383,7 @@ impl OpeningSetTarget { #[cfg(test)] mod tests { use anyhow::Result; - use plonky2_field::types::Field; + use plonky2_field::types::Sample; use crate::fri::reduction_strategies::FriReductionStrategy; use crate::gates::noop::NoopGate; diff --git a/plonky2/src/recursion/conditional_recursive_verifier.rs b/plonky2/src/recursion/conditional_recursive_verifier.rs index f91c1d5b..fdbff5a0 100644 --- a/plonky2/src/recursion/conditional_recursive_verifier.rs +++ b/plonky2/src/recursion/conditional_recursive_verifier.rs @@ -369,9 +369,9 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { use anyhow::Result; + use plonky2_field::types::Sample; use super::*; - use crate::field::types::Field; use crate::gates::noop::NoopGate; use crate::iop::witness::{PartialWitness, Witness}; use crate::plonk::circuit_data::CircuitConfig; diff --git a/plonky2/src/recursion/cyclic_recursion.rs b/plonky2/src/recursion/cyclic_recursion.rs index b50eecfe..ab569d27 100644 --- a/plonky2/src/recursion/cyclic_recursion.rs +++ b/plonky2/src/recursion/cyclic_recursion.rs @@ -267,7 +267,7 @@ mod tests { use crate::iop::witness::PartialWitness; use crate::plonk::circuit_builder::CircuitBuilder; use crate::plonk::circuit_data::{CircuitConfig, CommonCircuitData, VerifierCircuitTarget}; - use crate::plonk::config::{AlgebraicHasher, GenericConfig, Hasher, PoseidonGoldilocksConfig}; + use crate::plonk::config::{AlgebraicHasher, GenericConfig, PoseidonGoldilocksConfig}; use crate::recursion::cyclic_recursion::{ check_cyclic_proof_verifier_data, set_cyclic_recursion_data_target, CyclicRecursionData, }; diff --git a/plonky2/src/recursion/recursive_verifier.rs b/plonky2/src/recursion/recursive_verifier.rs index 5ddfef01..a42494f4 100644 --- a/plonky2/src/recursion/recursive_verifier.rs +++ b/plonky2/src/recursion/recursive_verifier.rs @@ -194,9 +194,7 @@ mod tests { use crate::gates::noop::NoopGate; use crate::iop::witness::{PartialWitness, Witness}; use crate::plonk::circuit_data::{CircuitConfig, VerifierOnlyCircuitData}; - use crate::plonk::config::{ - GenericConfig, Hasher, KeccakGoldilocksConfig, PoseidonGoldilocksConfig, - }; + use crate::plonk::config::{GenericConfig, KeccakGoldilocksConfig, PoseidonGoldilocksConfig}; use crate::plonk::proof::{CompressedProofWithPublicInputs, ProofWithPublicInputs}; use crate::plonk::prover::prove; use crate::util::timing::TimingTree; @@ -418,7 +416,7 @@ mod tests { vd: &VerifierOnlyCircuitData, cd: &CommonCircuitData, ) -> Result<()> { - let proof_bytes = proof.to_bytes()?; + let proof_bytes = proof.to_bytes(); info!("Proof length: {} bytes", proof_bytes.len()); let proof_from_bytes = ProofWithPublicInputs::from_bytes(proof_bytes, cd)?; assert_eq!(proof, &proof_from_bytes); @@ -431,7 +429,7 @@ mod tests { info!("{:.4}s to compress proof", now.elapsed().as_secs_f64()); assert_eq!(proof, &decompressed_compressed_proof); - let compressed_proof_bytes = compressed_proof.to_bytes()?; + let compressed_proof_bytes = compressed_proof.to_bytes(); info!( "Compressed proof length: {} bytes", compressed_proof_bytes.len() diff --git a/plonky2/src/util/reducing.rs b/plonky2/src/util/reducing.rs index f91dbb19..b1a179b7 100644 --- a/plonky2/src/util/reducing.rs +++ b/plonky2/src/util/reducing.rs @@ -276,6 +276,7 @@ impl ReducingFactorTarget { #[cfg(test)] mod tests { use anyhow::Result; + use plonky2_field::types::Sample; use super::*; use crate::iop::witness::{PartialWitness, Witness}; diff --git a/starky/Cargo.toml b/starky/Cargo.toml index 43bea53e..fd39d098 100644 --- a/starky/Cargo.toml +++ b/starky/Cargo.toml @@ -9,7 +9,7 @@ default = ["parallel"] parallel = ["plonky2/parallel", "maybe_rayon/parallel"] [dependencies] -plonky2 = { path = "../plonky2", default-features = false, features = ["rand", "timing"] } +plonky2 = { path = "../plonky2", default-features = false, features = ["timing"] } plonky2_util = { path = "../util" } maybe_rayon = { path = "../maybe_rayon"} anyhow = "1.0.40" diff --git a/starky/src/stark_testing.rs b/starky/src/stark_testing.rs index 2cec5298..f9f57828 100644 --- a/starky/src/stark_testing.rs +++ b/starky/src/stark_testing.rs @@ -1,7 +1,7 @@ use anyhow::{ensure, Result}; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::polynomial::{PolynomialCoeffs, PolynomialValues}; -use plonky2::field::types::Field; +use plonky2::field::types::{Field, Sample}; use plonky2::hash::hash_types::RichField; use plonky2::iop::witness::{PartialWitness, Witness}; use plonky2::plonk::circuit_builder::CircuitBuilder; @@ -29,7 +29,7 @@ where let trace_ldes = random_low_degree_matrix::(S::COLUMNS, rate_bits); let size = trace_ldes.len(); - let public_inputs = F::rand_arr::<{ S::PUBLIC_INPUTS }>(); + let public_inputs = F::rand_array::<{ S::PUBLIC_INPUTS }>(); let lagrange_first = PolynomialValues::selector(WITNESS_SIZE, 0).lde(rate_bits); let lagrange_last = PolynomialValues::selector(WITNESS_SIZE, WITNESS_SIZE - 1).lde(rate_bits); @@ -91,9 +91,9 @@ where { // Compute native constraint evaluation on random values. let vars = StarkEvaluationVars { - local_values: &F::Extension::rand_arr::<{ S::COLUMNS }>(), - next_values: &F::Extension::rand_arr::<{ S::COLUMNS }>(), - public_inputs: &F::Extension::rand_arr::<{ S::PUBLIC_INPUTS }>(), + local_values: &F::Extension::rand_array::<{ S::COLUMNS }>(), + next_values: &F::Extension::rand_array::<{ S::COLUMNS }>(), + public_inputs: &F::Extension::rand_array::<{ S::PUBLIC_INPUTS }>(), }; let alphas = F::rand_vec(1); let z_last = F::Extension::rand(); diff --git a/starky/src/verifier.rs b/starky/src/verifier.rs index 18ae9a27..fe0e41d2 100644 --- a/starky/src/verifier.rs +++ b/starky/src/verifier.rs @@ -260,7 +260,7 @@ fn check_permutation_options< mod tests { use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::field::polynomial::PolynomialValues; - use plonky2::field::types::Field; + use plonky2::field::types::Sample; use crate::verifier::eval_l_0_and_l_last; diff --git a/system_zero/src/alu/bitops.rs b/system_zero/src/alu/bitops.rs index 1f9875ca..aed63415 100644 --- a/system_zero/src/alu/bitops.rs +++ b/system_zero/src/alu/bitops.rs @@ -237,7 +237,7 @@ pub(crate) fn eval_bitop_circuit, const D: usize>( #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::Sample; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; use starky::constraint_consumer::ConstraintConsumer; @@ -250,7 +250,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut values = [F::default(); NUM_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut values = [F::default(); NUM_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_bitop == 0`, then the constraints should be met even // if all values are garbage. @@ -275,7 +275,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut values = [F::default(); NUM_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut values = [F::default(); NUM_COLUMNS].map(|_| F::sample(&mut rng)); const BITOPS: [usize; 4] = [IS_AND, IS_IOR, IS_XOR, IS_ANDNOT]; for bitop in BITOPS { @@ -317,7 +317,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut values = [F::default(); NUM_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut values = [F::default(); NUM_COLUMNS].map(|_| F::sample(&mut rng)); const BITOPS: [usize; 4] = [IS_AND, IS_IOR, IS_XOR, IS_ANDNOT]; for bitop in BITOPS { diff --git a/system_zero/src/alu/division.rs b/system_zero/src/alu/division.rs index 65bedd8f..055aafd3 100644 --- a/system_zero/src/alu/division.rs +++ b/system_zero/src/alu/division.rs @@ -160,7 +160,7 @@ pub(crate) fn eval_division_circuit, const D: usize #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::Sample; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; use starky::constraint_consumer::ConstraintConsumer; @@ -173,7 +173,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut values = [F::default(); NUM_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut values = [F::default(); NUM_COLUMNS].map(|_| F::sample(&mut rng)); // if `IS_DIV == 0`, then the constraints should be met even if all values are garbage. values[IS_DIV] = F::ZERO; @@ -195,7 +195,7 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let mut values = [F::default(); NUM_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut values = [F::default(); NUM_COLUMNS].map(|_| F::sample(&mut rng)); // set `IS_DIV == 1` and ensure all constraints are satisfied. values[IS_DIV] = F::ONE; diff --git a/system_zero/src/permutation_unit.rs b/system_zero/src/permutation_unit.rs index 809955ca..4ba469b4 100644 --- a/system_zero/src/permutation_unit.rs +++ b/system_zero/src/permutation_unit.rs @@ -254,7 +254,7 @@ pub(crate) fn eval_permutation_unit_circuit, const #[cfg(test)] mod tests { use plonky2::field::goldilocks_field::GoldilocksField; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use plonky2::hash::poseidon::Poseidon; use rand::SeedableRng; use rand_chacha::ChaCha8Rng; @@ -296,14 +296,14 @@ mod tests { type F = GoldilocksField; let mut rng = ChaCha8Rng::seed_from_u64(0x6feb51b7ec230f25); - let state = [F::default(); SPONGE_WIDTH].map(|_| F::rand_from_rng(&mut rng)); + let state = [F::default(); SPONGE_WIDTH].map(|_| F::sample(&mut rng)); // Get true Poseidon hash let target = GoldilocksField::poseidon(state); // Get result from `generate_permutation_unit` // Initialize `values` with randomness to test that the code doesn't rely on zero-filling. - let mut values = [F::default(); NUM_COLUMNS].map(|_| F::rand_from_rng(&mut rng)); + let mut values = [F::default(); NUM_COLUMNS].map(|_| F::sample(&mut rng)); for i in 0..SPONGE_WIDTH { values[col_input(i)] = state[i]; } diff --git a/u32/src/gates/add_many_u32.rs b/u32/src/gates/add_many_u32.rs index f37075cd..8928b74d 100644 --- a/u32/src/gates/add_many_u32.rs +++ b/u32/src/gates/add_many_u32.rs @@ -340,7 +340,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use itertools::unfold; @@ -351,7 +351,7 @@ mod tests { use plonky2::plonk::vars::EvaluationVars; use plonky2_field::extension::quartic::QuarticExtension; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use rand::Rng; use crate::gates::add_many_u32::U32AddManyGate; diff --git a/u32/src/gates/arithmetic_u32.rs b/u32/src/gates/arithmetic_u32.rs index 575f3055..8946f9de 100644 --- a/u32/src/gates/arithmetic_u32.rs +++ b/u32/src/gates/arithmetic_u32.rs @@ -411,7 +411,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use plonky2::gates::gate::Gate; @@ -421,7 +421,7 @@ mod tests { use plonky2::plonk::vars::EvaluationVars; use plonky2_field::extension::Extendable; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use rand::Rng; use crate::gates::arithmetic_u32::U32ArithmeticGate; diff --git a/u32/src/gates/comparison.rs b/u32/src/gates/comparison.rs index cbb689e7..b7dc74a8 100644 --- a/u32/src/gates/comparison.rs +++ b/u32/src/gates/comparison.rs @@ -512,7 +512,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use plonky2::gates::gate::Gate; @@ -521,7 +521,7 @@ mod tests { use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2::plonk::vars::EvaluationVars; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::{Field, PrimeField64}; + use plonky2_field::types::{Field, PrimeField64, Sample}; use rand::Rng; use crate::gates::comparison::ComparisonGate; diff --git a/u32/src/gates/range_check_u32.rs b/u32/src/gates/range_check_u32.rs index 6e8f2cd5..ff99e492 100644 --- a/u32/src/gates/range_check_u32.rs +++ b/u32/src/gates/range_check_u32.rs @@ -201,7 +201,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use itertools::unfold; @@ -212,7 +212,7 @@ mod tests { use plonky2::plonk::vars::EvaluationVars; use plonky2_field::extension::quartic::QuarticExtension; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use plonky2_util::ceil_div_usize; use rand::Rng; diff --git a/u32/src/gates/subtraction_u32.rs b/u32/src/gates/subtraction_u32.rs index d29b2348..1657aec1 100644 --- a/u32/src/gates/subtraction_u32.rs +++ b/u32/src/gates/subtraction_u32.rs @@ -329,7 +329,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use plonky2::gates::gate::Gate; @@ -339,7 +339,7 @@ mod tests { use plonky2::plonk::vars::EvaluationVars; use plonky2_field::extension::quartic::QuarticExtension; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::{Field, PrimeField64}; + use plonky2_field::types::{Field, PrimeField64, Sample}; use rand::Rng; use crate::gates::subtraction_u32::U32SubtractionGate; diff --git a/waksman/src/gates/assert_le.rs b/waksman/src/gates/assert_le.rs index 7f60fcac..745bb62f 100644 --- a/waksman/src/gates/assert_le.rs +++ b/waksman/src/gates/assert_le.rs @@ -446,7 +446,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { - use std::marker::PhantomData; + use core::marker::PhantomData; use anyhow::Result; use plonky2::gates::gate::Gate; @@ -456,7 +456,7 @@ mod tests { use plonky2::plonk::vars::EvaluationVars; use plonky2_field::extension::quartic::QuarticExtension; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::{Field, PrimeField64}; + use plonky2_field::types::{Field, PrimeField64, Sample}; use rand::Rng; use crate::gates::assert_le::AssertLessThanGate; diff --git a/waksman/src/gates/switch.rs b/waksman/src/gates/switch.rs index 4509bf0a..58fad4c7 100644 --- a/waksman/src/gates/switch.rs +++ b/waksman/src/gates/switch.rs @@ -334,7 +334,7 @@ mod tests { use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; use plonky2::plonk::vars::EvaluationVars; use plonky2_field::goldilocks_field::GoldilocksField; - use plonky2_field::types::Field; + use plonky2_field::types::{Field, Sample}; use crate::gates::switch::SwitchGate; diff --git a/waksman/src/permutation.rs b/waksman/src/permutation.rs index 367386c4..b9d69f75 100644 --- a/waksman/src/permutation.rs +++ b/waksman/src/permutation.rs @@ -371,7 +371,7 @@ impl SimpleGenerator for PermutationGenerator { #[cfg(test)] mod tests { use anyhow::Result; - use plonky2::field::types::Field; + use plonky2::field::types::{Field, Sample}; use plonky2::iop::witness::PartialWitness; use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; diff --git a/waksman/src/sorting.rs b/waksman/src/sorting.rs index 010bc8b9..dbfe8a81 100644 --- a/waksman/src/sorting.rs +++ b/waksman/src/sorting.rs @@ -183,7 +183,7 @@ impl, const D: usize> SimpleGenerator #[cfg(test)] mod tests { use anyhow::Result; - use plonky2::field::types::{Field, PrimeField64}; + use plonky2::field::types::{Field, PrimeField64, Sample}; use plonky2::iop::witness::PartialWitness; use plonky2::plonk::circuit_data::CircuitConfig; use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};