feat: upgrade Sampling APIs

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>
This commit is contained in:
Brandon H. Gomes 2022-11-03 08:26:03 -07:00
parent 4aaf57e9a9
commit e2cdd5a954
No known key found for this signature in database
GPG Key ID: 773D44E6A904B222
78 changed files with 278 additions and 224 deletions

View File

@ -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"
rand = "0.8.4"

View File

@ -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<C: Curve>(
#[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;

View File

@ -103,7 +103,7 @@ pub fn glv_mul(p: ProjectivePoint<Secp256K1>, 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};

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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};

View File

@ -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};

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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"

View File

@ -161,7 +161,7 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, 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;

View File

@ -162,7 +162,7 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, 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 {

View File

@ -501,7 +501,7 @@ pub(crate) fn eval_ext_circuit<F: RichField + Extendable<D>, 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

View File

@ -172,7 +172,7 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, 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;

View File

@ -93,7 +93,7 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, 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;

View File

@ -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();

View File

@ -275,7 +275,7 @@ fn eval_l_0_and_l_last<F: Field>(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;

View File

@ -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 }

View File

@ -191,12 +191,14 @@ impl<F: OEF<D>, const D: usize> PolynomialCoeffsAlgebra<F, D> {
#[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<F: Extendable<D>, const D: usize>() {

View File

@ -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<F: Extendable<2>> From<F> for QuadraticExtension<F> {
}
}
impl<F: Extendable<2>> Sample for QuadraticExtension<F> {
#[inline]
fn sample<R>(rng: &mut R) -> Self
where
R: rand::RngCore + ?Sized,
{
Self([F::sample(rng), F::sample(rng)])
}
}
impl<F: Extendable<2>> Field for QuadraticExtension<F> {
const ZERO: Self = Self([F::ZERO; 2]);
const ONE: Self = Self([F::ONE, F::ZERO]);
@ -99,11 +109,6 @@ impl<F: Extendable<2>> Field for QuadraticExtension<F> {
fn from_noncanonical_u128(n: u128) -> Self {
F::from_noncanonical_u128(n).into()
}
#[cfg(feature = "rand")]
fn rand_from_rng<R: rand::RngCore>(rng: &mut R) -> Self {
Self([F::rand_from_rng(rng), F::rand_from_rng(rng)])
}
}
impl<F: Extendable<2>> Display for QuadraticExtension<F> {

View File

@ -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<F: Extendable<4>> From<F> for QuarticExtension<F> {
}
}
impl<F: Extendable<4>> Sample for QuarticExtension<F> {
#[inline]
fn sample<R>(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<F: Extendable<4>> Field for QuarticExtension<F> {
const ZERO: Self = Self([F::ZERO; 4]);
const ONE: Self = Self([F::ONE, F::ZERO, F::ZERO, F::ZERO]);
@ -104,16 +119,6 @@ impl<F: Extendable<4>> Field for QuarticExtension<F> {
fn from_noncanonical_u128(n: u128) -> Self {
F::from_noncanonical_u128(n).into()
}
#[cfg(feature = "rand")]
fn rand_from_rng<R: rand::RngCore>(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<F: Extendable<4>> Display for QuarticExtension<F> {

View File

@ -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<F: Extendable<5>> From<F> for QuinticExtension<F> {
}
}
impl<F: Extendable<5>> Sample for QuinticExtension<F> {
#[inline]
fn sample<R>(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<F: Extendable<5>> Field for QuinticExtension<F> {
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<F: Extendable<5>> Field for QuinticExtension<F> {
fn from_noncanonical_u128(n: u128) -> Self {
F::from_noncanonical_u128(n).into()
}
#[cfg(feature = "rand")]
fn rand_from_rng<R: rand::RngCore>(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<F: Extendable<5>> Display for QuinticExtension<F> {

View File

@ -207,6 +207,8 @@ pub(crate) fn fft_classic<F: Field>(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::<Vec<_>>();
assert_eq!(coeffs.len(), degree_padded);
let coefficients = PolynomialCoeffs { coeffs };

View File

@ -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::<u64>());

View File

@ -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<R>(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<R: rand::RngCore>(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.

View File

@ -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() {

View File

@ -8,7 +8,7 @@
#![feature(generic_const_exprs)]
#![feature(specialization)]
#![feature(stdsimd)]
#![no_std]
#![cfg_attr(not(test), no_std)]
extern crate alloc;

View File

@ -134,17 +134,18 @@ impl<F: Field> PolynomialCoeffs<F> {
#[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<GoldilocksField>;
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);

View File

@ -442,10 +442,12 @@ impl<F: Field> Mul for &PolynomialCoeffs<F> {
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);

View File

@ -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

View File

@ -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<R>(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<R: rand::RngCore>(rng: &mut R) -> Self {
use num::bigint::RandBigInt;
Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order()))
}
}
impl PrimeField for Secp256K1Base {

View File

@ -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<R>(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<R: rand::RngCore>(rng: &mut R) -> Self {
use num::bigint::RandBigInt;
Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order()))
}
}
impl PrimeField for Secp256K1Scalar {

View File

@ -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<R>(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<Self> {
(0..n).map(|_| Self::rand()).collect()
}
/// Samples an array of values of length `N` using [`OsRng`].
#[inline]
fn rand_array<const N: usize>() -> [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<R: rand::RngCore>(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<const N: usize>() -> [Self; N] {
Self::rand_vec(N).try_into().unwrap()
}
#[cfg(feature = "rand")]
fn rand_vec(n: usize) -> Vec<Self> {
(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

View File

@ -50,7 +50,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderInsert<F, D>
#[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};

View File

@ -317,11 +317,11 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F> 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;

View File

@ -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"

View File

@ -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<F: Poseidon>(c: &mut Criterion) {
&format!("poseidon<{}, {SPONGE_WIDTH}>", type_name::<F>()),
|b| {
b.iter_batched(
|| F::rand_arr::<SPONGE_WIDTH>(),
|| F::rand_array::<SPONGE_WIDTH>(),
|state| F::poseidon(state),
BatchSize::SmallInput,
)

View File

@ -1,3 +1,4 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
mod allocator;

View File

@ -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;

View File

@ -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) {

View File

@ -154,7 +154,7 @@ fn test_serialization<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>,
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()

View File

@ -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;

View File

@ -571,7 +571,7 @@ pub(crate) struct ExtensionArithmeticOperation<F: Field64 + Extendable<D>, 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, D>(FF::rand_arr());
let y = ExtensionAlgebra::<FF, D>(FF::rand_arr());
let x = ExtensionAlgebra::<FF, D>(FF::rand_array());
let y = ExtensionAlgebra::<FF, D>(FF::rand_array());
let z = x * y;
for i in 0..D {
pw.set_extension_target(xt.0[i], x.0[i]);

View File

@ -57,7 +57,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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;

View File

@ -40,7 +40,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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;

View File

@ -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;

View File

@ -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;

View File

@ -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};

View File

@ -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;

View File

@ -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};

View File

@ -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;

View File

@ -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;

View File

@ -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<F: Field> HashOut<F> {
elements[0..elements_in.len()].copy_from_slice(elements_in);
Self { elements }
}
}
#[cfg(all(feature = "parallel", feature = "rand"))]
pub fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
impl<F> Sample for HashOut<F>
where
F: Field,
{
#[inline]
fn sample<R>(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<F: RichField> GenericHashOut<F> for HashOut<F> {
@ -116,18 +118,16 @@ pub struct MerkleCapTarget(pub Vec<HashOutTarget>);
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub struct BytesHash<const N: usize>(pub [u8; N]);
impl<const N: usize> BytesHash<N> {
#[cfg(feature = "rand")]
pub fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
impl<const N: usize> Sample for BytesHash<N> {
#[inline]
fn sample<R>(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<F: RichField, const N: usize> GenericHashOut<F> for BytesHash<N> {

View File

@ -114,7 +114,7 @@ pub(crate) fn decompress_merkle_proofs<F: RichField, H: Hasher<F>>(
#[cfg(test)]
mod tests {
use plonky2_field::types::Field;
use plonky2_field::types::Sample;
use rand::{thread_rng, Rng};
use super::*;

View File

@ -301,7 +301,7 @@ impl<F: RichField + Extendable<D>, H: AlgebraicHasher<F>, 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;

View File

@ -274,7 +274,6 @@ impl<F: Field> SimpleGenerator<F> for RandomValueGenerator {
fn run_once(&self, _witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let random_value = F::rand();
out_buffer.set_target(self.target, random_value);
}
}

View File

@ -383,7 +383,7 @@ impl<const D: usize> OpeningSetTarget<D> {
#[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;

View File

@ -369,9 +369,9 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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;

View File

@ -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,
};

View File

@ -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<C, D>,
cd: &CommonCircuitData<F, D>,
) -> 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()

View File

@ -276,6 +276,7 @@ impl<const D: usize> ReducingFactorTarget<D> {
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::types::Sample;
use super::*;
use crate::iop::witness::{PartialWitness, Witness};

View File

@ -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"

View File

@ -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::<F>(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();

View File

@ -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;

View File

@ -237,7 +237,7 @@ pub(crate) fn eval_bitop_circuit<F: RichField + Extendable<D>, 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 {

View File

@ -160,7 +160,7 @@ pub(crate) fn eval_division_circuit<F: RichField + Extendable<D>, 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;

View File

@ -254,7 +254,7 @@ pub(crate) fn eval_permutation_unit_circuit<F: RichField + Extendable<D>, 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];
}

View File

@ -340,7 +340,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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;

View File

@ -411,7 +411,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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;

View File

@ -512,7 +512,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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;

View File

@ -201,7 +201,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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;

View File

@ -329,7 +329,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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;

View File

@ -446,7 +446,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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;

View File

@ -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;

View File

@ -371,7 +371,7 @@ impl<F: Field> SimpleGenerator<F> for PermutationGenerator<F> {
#[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};

View File

@ -183,7 +183,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[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};