Merge branch 'openzklib-feat/no-std-support'

This commit is contained in:
Daniel Lubarov 2022-11-17 13:08:27 -08:00
commit 7691758084
197 changed files with 1938 additions and 1598 deletions

View File

@ -1,5 +1,5 @@
[workspace]
members = ["field", "insertion", "plonky2", "starky", "system_zero", "util", "waksman", "ecdsa", "u32", "evm", "maybe_rayon"]
members = ["ecdsa", "evm", "field", "insertion", "maybe_rayon", "plonky2", "starky", "system_zero", "u32", "util", "waksman"]
[profile.release]
opt-level = 3

View File

@ -3,16 +3,17 @@ name = "plonky2_ecdsa"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
parallel = ["maybe_rayon/parallel", "plonky2/parallel"]
[dependencies]
plonky2 = { path = "../plonky2" }
plonky2_util = { path = "../util" }
plonky2_field = { path = "../field" }
plonky2_u32 = { path = "../u32" }
num = "0.4.0"
itertools = "0.10.0"
rayon = "1.5.1"
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0.40"
rand = "0.8.4"
anyhow = { version = "1.0.40", default-features = false }
itertools = { version = "0.10.0", default-features = false }
maybe_rayon = { path = "../maybe_rayon", default-features = false }
num = { version = "0.4.0", default-features = false }
plonky2 = { path = "../plonky2", default-features = false }
plonky2_u32 = { path = "../u32", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
[dev-dependencies]
rand = { version = "0.8.4", default-features = false, features = ["getrandom"] }

View File

@ -1,7 +1,7 @@
use std::ops::Add;
use core::ops::Add;
use plonky2_field::ops::Square;
use plonky2_field::types::Field;
use plonky2::field::ops::Square;
use plonky2::field::types::Field;
use crate::curve::curve_types::{AffinePoint, Curve, ProjectivePoint};

View File

@ -1,7 +1,8 @@
use alloc::vec::Vec;
use itertools::Itertools;
use plonky2_field::types::Field;
use plonky2_field::types::PrimeField;
use rayon::prelude::*;
use maybe_rayon::*;
use plonky2::field::types::{Field, PrimeField};
use crate::curve::curve_summation::affine_multisummation_best;
use crate::curve::curve_types::{AffinePoint, Curve, ProjectivePoint};
@ -186,13 +187,12 @@ pub(crate) fn to_digits<C: Curve>(x: &C::ScalarField, w: usize) -> Vec<usize> {
#[cfg(test)]
mod tests {
use num::BigUint;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use plonky2_field::types::PrimeField;
use alloc::vec;
use crate::curve::curve_msm::{msm_execute, msm_precompute, to_digits};
use crate::curve::curve_types::Curve;
use num::BigUint;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use super::*;
use crate::curve::secp256k1::Secp256K1;
#[test]

View File

@ -1,7 +1,7 @@
use std::ops::Mul;
use alloc::vec::Vec;
use core::ops::Mul;
use plonky2_field::types::Field;
use plonky2_field::types::PrimeField;
use plonky2::field::types::{Field, PrimeField};
use crate::curve::curve_types::{Curve, CurveScalar, ProjectivePoint};

View File

@ -1,7 +1,9 @@
use std::iter::Sum;
use alloc::vec;
use alloc::vec::Vec;
use core::iter::Sum;
use plonky2_field::ops::Square;
use plonky2_field::types::Field;
use plonky2::field::ops::Square;
use plonky2::field::types::Field;
use crate::curve::curve_types::{AffinePoint, Curve, ProjectivePoint};
@ -188,10 +190,7 @@ pub fn affine_multisummation_batch_inversion<C: Curve>(
#[cfg(test)]
mod tests {
use crate::curve::curve_summation::{
affine_summation_batch_inversion, affine_summation_pairwise,
};
use crate::curve::curve_types::{Curve, ProjectivePoint};
use super::*;
use crate::curve::secp256k1::Secp256K1;
#[test]

View File

@ -1,9 +1,10 @@
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::Neg;
use alloc::vec::Vec;
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use core::ops::Neg;
use plonky2_field::ops::Square;
use plonky2_field::types::{Field, PrimeField};
use plonky2::field::ops::Square;
use plonky2::field::types::{Field, PrimeField};
use serde::{Deserialize, Serialize};
// To avoid implementation conflicts from associated types,
@ -123,7 +124,7 @@ impl<C: Curve> PartialEq for AffinePoint<C> {
impl<C: Curve> Eq for AffinePoint<C> {}
impl<C: Curve> Hash for AffinePoint<C> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
fn hash<H: Hasher>(&self, state: &mut H) {
if self.zero {
self.zero.hash(state);
} else {

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;
@ -63,8 +63,8 @@ pub fn verify_message<C: Curve>(
#[cfg(test)]
mod tests {
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::Sample;
use crate::curve::ecdsa::{sign_message, verify_message, ECDSASecretKey};
use crate::curve::secp256k1::Secp256K1;

View File

@ -1,8 +1,8 @@
use num::rational::Ratio;
use num::BigUint;
use plonky2_field::secp256k1_base::Secp256K1Base;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::{Field, PrimeField};
use plonky2::field::secp256k1_base::Secp256K1Base;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::{Field, PrimeField};
use crate::curve::curve_msm::msm_parallel;
use crate::curve::curve_types::{AffinePoint, ProjectivePoint};
@ -102,8 +102,8 @@ pub fn glv_mul(p: ProjectivePoint<Secp256K1>, k: Secp256K1Scalar) -> ProjectiveP
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
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

@ -1,6 +1,6 @@
use plonky2_field::secp256k1_base::Secp256K1Base;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use plonky2::field::secp256k1_base::Secp256K1Base;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::Field;
use serde::{Deserialize, Serialize};
use crate::curve::curve_types::{AffinePoint, Curve};
@ -40,9 +40,8 @@ const SECP256K1_GENERATOR_Y: Secp256K1Base = Secp256K1Base([
#[cfg(test)]
mod tests {
use num::BigUint;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use plonky2_field::types::PrimeField;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::{Field, PrimeField};
use crate::curve::curve_types::{AffinePoint, Curve, ProjectivePoint};
use crate::curve::secp256k1::Secp256K1;

View File

@ -1,13 +1,15 @@
use std::marker::PhantomData;
use alloc::vec;
use alloc::vec::Vec;
use core::marker::PhantomData;
use num::{BigUint, Integer, Zero};
use plonky2::field::extension::Extendable;
use plonky2::field::types::{PrimeField, PrimeField64};
use plonky2::hash::hash_types::RichField;
use plonky2::iop::generator::{GeneratedValues, SimpleGenerator};
use plonky2::iop::target::{BoolTarget, Target};
use plonky2::iop::witness::{PartitionWitness, Witness};
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2_field::extension::Extendable;
use plonky2_field::types::{PrimeField, PrimeField64};
use plonky2_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target};
use plonky2_u32::gadgets::multiple_comparison::list_le_u32_circuit;
use plonky2_u32::witness::{GeneratedValuesU32, WitnessU32};
@ -346,11 +348,11 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
mod tests {
use anyhow::Result;
use num::{BigUint, FromPrimitive, Integer};
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2::{
iop::witness::PartialWitness,
plonk::{circuit_builder::CircuitBuilder, circuit_data::CircuitConfig},
};
use rand::rngs::OsRng;
use rand::Rng;
use crate::gadgets::biguint::{CircuitBuilderBiguint, WitnessBigUint};
@ -360,7 +362,7 @@ mod tests {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut rng = rand::thread_rng();
let mut rng = OsRng;
let x_value = BigUint::from_u128(rng.gen()).unwrap();
let y_value = BigUint::from_u128(rng.gen()).unwrap();
@ -390,7 +392,7 @@ mod tests {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut rng = rand::thread_rng();
let mut rng = OsRng;
let mut x_value = BigUint::from_u128(rng.gen()).unwrap();
let mut y_value = BigUint::from_u128(rng.gen()).unwrap();
@ -420,7 +422,7 @@ mod tests {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut rng = rand::thread_rng();
let mut rng = OsRng;
let x_value = BigUint::from_u128(rng.gen()).unwrap();
let y_value = BigUint::from_u128(rng.gen()).unwrap();
@ -450,7 +452,7 @@ mod tests {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut rng = rand::thread_rng();
let mut rng = OsRng;
let x_value = BigUint::from_u128(rng.gen()).unwrap();
let y_value = BigUint::from_u128(rng.gen()).unwrap();
@ -476,7 +478,7 @@ mod tests {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
let mut rng = rand::thread_rng();
let mut rng = OsRng;
let mut x_value = BigUint::from_u128(rng.gen()).unwrap();
let mut y_value = BigUint::from_u128(rng.gen()).unwrap();

View File

@ -1,8 +1,11 @@
use alloc::vec;
use alloc::vec::Vec;
use plonky2::field::extension::Extendable;
use plonky2::field::types::Sample;
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 crate::curve::curve_types::{AffinePoint, Curve, CurveScalar};
use crate::gadgets::nonnative::{CircuitBuilderNonNative, NonNativeTarget};
@ -254,16 +257,16 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderCurve<F, D>
#[cfg(test)]
mod tests {
use std::ops::Neg;
use core::ops::Neg;
use anyhow::Result;
use plonky2::field::secp256k1_base::Secp256K1Base;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::{Field, Sample};
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2_field::secp256k1_base::Secp256K1Base;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use crate::curve::curve_types::{AffinePoint, Curve, CurveScalar};
use crate::curve::secp256k1::Secp256K1;

View File

@ -1,10 +1,12 @@
use alloc::vec::Vec;
use num::BigUint;
use plonky2::field::extension::Extendable;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::hash::keccak::KeccakHash;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::config::{GenericHashOut, Hasher};
use plonky2_field::extension::Extendable;
use plonky2_field::types::Field;
use crate::curve::curve_types::{AffinePoint, Curve, CurveScalar};
use crate::gadgets::curve::{AffinePointTarget, CircuitBuilderCurve};
@ -66,13 +68,12 @@ pub fn fixed_base_curve_mul_circuit<C: Curve, F: RichField + Extendable<D>, cons
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::{PrimeField, Sample};
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
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::PrimeField;
use crate::curve::curve_types::{Curve, CurveScalar};
use crate::curve::secp256k1::Secp256K1;

View File

@ -1,10 +1,12 @@
use alloc::vec;
use num::BigUint;
use plonky2::field::extension::Extendable;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::hash::keccak::KeccakHash;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::config::{GenericHashOut, Hasher};
use plonky2_field::extension::Extendable;
use plonky2_field::types::Field;
use crate::curve::curve_types::{Curve, CurveScalar};
use crate::gadgets::curve::{AffinePointTarget, CircuitBuilderCurve};
@ -79,12 +81,12 @@ pub fn curve_msm_circuit<C: Curve, F: RichField + Extendable<D>, const D: usize>
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::Sample;
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use crate::curve::curve_types::{Curve, CurveScalar};
use crate::curve::secp256k1::Secp256K1;

View File

@ -1,13 +1,15 @@
use std::marker::PhantomData;
use alloc::vec;
use alloc::vec::Vec;
use core::marker::PhantomData;
use num::BigUint;
use plonky2::field::extension::Extendable;
use plonky2::field::types::{Field, Sample};
use plonky2::hash::hash_types::RichField;
use plonky2::hash::keccak::KeccakHash;
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_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target};
use crate::curve::curve_types::{Curve, CurveScalar};
@ -169,22 +171,18 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderWindowedMul<F,
#[cfg(test)]
mod tests {
use std::ops::Neg;
use core::ops::Neg;
use anyhow::Result;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use rand::rngs::OsRng;
use rand::Rng;
use crate::curve::curve_types::{Curve, CurveScalar};
use super::*;
use crate::curve::secp256k1::Secp256K1;
use crate::gadgets::curve::CircuitBuilderCurve;
use crate::gadgets::curve_windowed_mul::CircuitBuilderWindowedMul;
use crate::gadgets::nonnative::CircuitBuilderNonNative;
#[test]
fn test_random_access_curve_points() -> Result<()> {
@ -206,7 +204,7 @@ mod tests {
})
.collect();
let mut rng = rand::thread_rng();
let mut rng = OsRng;
let access_index = rng.gen::<usize>() % num_points;
let access_index_target = builder.constant(F::from_canonical_usize(access_index));

View File

@ -1,9 +1,9 @@
use std::marker::PhantomData;
use core::marker::PhantomData;
use plonky2::field::extension::Extendable;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::hash::hash_types::RichField;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2_field::extension::Extendable;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use crate::curve::curve_types::Curve;
use crate::curve::secp256k1::Secp256K1;
@ -52,20 +52,14 @@ pub fn verify_message_circuit<F: RichField + Extendable<D>, const D: usize>(
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::types::Sample;
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use super::{ECDSAPublicKeyTarget, ECDSASignatureTarget};
use crate::curve::curve_types::{Curve, CurveScalar};
use super::*;
use crate::curve::curve_types::CurveScalar;
use crate::curve::ecdsa::{sign_message, ECDSAPublicKey, ECDSASecretKey, ECDSASignature};
use crate::curve::secp256k1::Secp256K1;
use crate::gadgets::curve::CircuitBuilderCurve;
use crate::gadgets::ecdsa::verify_message_circuit;
use crate::gadgets::nonnative::CircuitBuilderNonNative;
fn test_ecdsa_circuit_with_config(config: CircuitConfig) -> Result<()> {
const D: usize = 2;

View File

@ -1,14 +1,15 @@
use std::marker::PhantomData;
use alloc::vec::Vec;
use core::marker::PhantomData;
use plonky2::field::extension::Extendable;
use plonky2::field::secp256k1_base::Secp256K1Base;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::{Field, PrimeField};
use plonky2::hash::hash_types::RichField;
use plonky2::iop::generator::{GeneratedValues, SimpleGenerator};
use plonky2::iop::target::{BoolTarget, Target};
use plonky2::iop::witness::PartitionWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2_field::extension::Extendable;
use plonky2_field::secp256k1_base::Secp256K1Base;
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::{Field, PrimeField};
use crate::curve::glv::{decompose_secp256k1_scalar, GLV_BETA, GLV_S};
use crate::curve::secp256k1::Secp256K1;
@ -132,12 +133,12 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::Sample;
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use crate::curve::curve_types::{Curve, CurveScalar};
use crate::curve::glv::glv_mul;

View File

@ -1,17 +1,19 @@
use std::marker::PhantomData;
use alloc::vec;
use alloc::vec::Vec;
use core::marker::PhantomData;
use num::{BigUint, Integer, One, Zero};
use plonky2::field::extension::Extendable;
use plonky2::field::types::{Field, PrimeField};
use plonky2::hash::hash_types::RichField;
use plonky2::iop::generator::{GeneratedValues, SimpleGenerator};
use plonky2::iop::target::{BoolTarget, Target};
use plonky2::iop::witness::PartitionWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2_field::types::PrimeField;
use plonky2_field::{extension::Extendable, types::Field};
use plonky2::util::ceil_div_usize;
use plonky2_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target};
use plonky2_u32::gadgets::range_check::range_check_u32_circuit;
use plonky2_u32::witness::GeneratedValuesU32;
use plonky2_util::ceil_div_usize;
use crate::gadgets::biguint::{
BigUintTarget, CircuitBuilderBiguint, GeneratedValuesBigUint, WitnessBigUint,
@ -642,12 +644,12 @@ impl<F: RichField + Extendable<D>, const D: usize, FF: PrimeField> SimpleGenerat
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::secp256k1_base::Secp256K1Base;
use plonky2::field::types::{Field, PrimeField, Sample};
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
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 crate::gadgets::nonnative::CircuitBuilderNonNative;

View File

@ -1,11 +1,12 @@
use std::marker::PhantomData;
use alloc::vec::Vec;
use core::marker::PhantomData;
use itertools::Itertools;
use plonky2::field::extension::Extendable;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::target::Target;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2_field::extension::Extendable;
use plonky2_field::types::Field;
use plonky2_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target};
use crate::gadgets::biguint::BigUintTarget;
@ -96,15 +97,14 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilderSplit<F, D>
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::secp256k1_scalar::Secp256K1Scalar;
use plonky2::field::types::Sample;
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2_field::secp256k1_scalar::Secp256K1Scalar;
use plonky2_field::types::Field;
use super::*;
use crate::gadgets::nonnative::{CircuitBuilderNonNative, NonNativeTarget};
use crate::gadgets::split_nonnative::CircuitBuilderSplit;
#[test]
fn test_split_nonnative() -> Result<()> {

View File

@ -1,7 +1,7 @@
#![allow(clippy::needless_range_loop)]
// Below lint is currently broken and produces false positives.
// TODO: Remove this override when Clippy is patched.
#![allow(clippy::derive_partial_eq_without_eq)]
#![cfg_attr(not(test), no_std)]
extern crate alloc;
pub mod curve;
pub mod gadgets;

View File

@ -5,22 +5,22 @@ version = "0.1.0"
edition = "2021"
[dependencies]
plonky2 = { path = "../plonky2", default-features = false, features = ["rand", "timing"] }
plonky2_util = { path = "../util" }
eth_trie_utils = "0.4.0"
anyhow = "1.0.40"
env_logger = "0.9.0"
eth_trie_utils = "0.4.0"
ethereum-types = "0.14.0"
hex = { version = "0.4.3", optional = true }
hex-literal = "0.3.4"
itertools = "0.10.3"
keccak-hash = "0.10.0"
log = "0.4.14"
num = "0.4.0"
maybe_rayon = { path = "../maybe_rayon" }
num = "0.4.0"
once_cell = "1.13.0"
pest = "2.1.3"
pest_derive = "2.1.0"
plonky2 = { path = "../plonky2", default-features = false, features = ["timing"] }
plonky2_util = { path = "../util" }
rand = "0.8.5"
rand_chacha = "0.3.1"
ripemd = "0.1.3"

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

@ -6,12 +6,7 @@ use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::field::packed::PackedField;
use plonky2::hash::hash_types::RichField;
use crate::arithmetic::add;
use crate::arithmetic::columns;
use crate::arithmetic::compare;
use crate::arithmetic::modular;
use crate::arithmetic::mul;
use crate::arithmetic::sub;
use crate::arithmetic::{add, columns, compare, modular, mul, sub};
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::stark::Stark;
use crate::vars::{StarkEvaluationTargets, StarkEvaluationVars};

View File

@ -160,7 +160,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;
@ -174,7 +174,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
@ -199,7 +199,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

@ -87,7 +87,8 @@
//! In the case of DIV, we do something similar, except that we "replace"
//! the modulus with "2^256" to force the quotient to be zero.
use num::{bigint::Sign, BigInt, One, Zero};
use num::bigint::Sign;
use num::{BigInt, One, Zero};
use plonky2::field::extension::Extendable;
use plonky2::field::packed::PackedField;
use plonky2::field::types::Field;
@ -500,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;
@ -516,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.
@ -543,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
@ -594,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,5 +1,4 @@
use std::env;
use std::fs;
use std::{env, fs};
use hex::encode;
use plonky2_evm::cpu::kernel::assemble_to_bytes;

View File

@ -7,15 +7,12 @@ use plonky2_util::ceil_div_usize;
use super::ast::PushTarget;
use crate::cpu::kernel::ast::Item::LocalLabelDeclaration;
use crate::cpu::kernel::ast::StackReplacement;
use crate::cpu::kernel::ast::{File, Item, StackReplacement};
use crate::cpu::kernel::keccak_util::hash_kernel;
use crate::cpu::kernel::opcodes::{get_opcode, get_push_opcode};
use crate::cpu::kernel::optimizer::optimize_asm;
use crate::cpu::kernel::stack::stack_manipulation::expand_stack_manipulation;
use crate::cpu::kernel::utils::u256_to_trimmed_be_bytes;
use crate::cpu::kernel::{
ast::{File, Item},
opcodes::{get_opcode, get_push_opcode},
};
use crate::generation::prover_input::ProverInputFn;
use crate::keccak_sponge::columns::KECCAK_RATE_BYTES;
@ -387,8 +384,9 @@ mod tests {
use itertools::Itertools;
use crate::cpu::kernel::assembler::*;
use crate::cpu::kernel::ast::*;
use crate::cpu::kernel::parser::parse;
use crate::cpu::kernel::{assembler::*, ast::*};
#[test]
fn two_files() {

View File

@ -5,11 +5,9 @@ use plonky2::hash::hash_types::RichField;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::keccak::columns::reg_step;
use crate::keccak::columns::NUM_COLUMNS;
use crate::keccak::columns::{reg_step, NUM_COLUMNS};
use crate::keccak::keccak_stark::NUM_ROUNDS;
use crate::vars::StarkEvaluationTargets;
use crate::vars::StarkEvaluationVars;
use crate::vars::{StarkEvaluationTargets, StarkEvaluationVars};
pub(crate) fn eval_round_flags<F: Field, P: PackedField<Scalar = F>>(
vars: StarkEvaluationVars<F, P, NUM_COLUMNS>,

View File

@ -15,8 +15,7 @@ use crate::keccak_memory::columns::*;
use crate::memory::segments::Segment;
use crate::stark::Stark;
use crate::util::trace_rows_to_poly_values;
use crate::vars::StarkEvaluationTargets;
use crate::vars::StarkEvaluationVars;
use crate::vars::{StarkEvaluationTargets, StarkEvaluationVars};
pub(crate) fn ctl_looked_data<F: Field>() -> Vec<Column<F>> {
Column::singles([COL_CONTEXT, COL_SEGMENT, COL_VIRTUAL, COL_READ_TIMESTAMP]).collect()

View File

@ -21,8 +21,7 @@ use crate::keccak_sponge::columns::*;
use crate::memory::segments::Segment;
use crate::stark::Stark;
use crate::util::trace_rows_to_poly_values;
use crate::vars::StarkEvaluationTargets;
use crate::vars::StarkEvaluationVars;
use crate::vars::{StarkEvaluationTargets, StarkEvaluationVars};
#[allow(unused)] // TODO: Should be used soon.
pub(crate) fn ctl_looked_data<F: Field>() -> Vec<Column<F>> {

View File

@ -27,9 +27,9 @@ use crate::keccak::keccak_stark::KeccakStark;
use crate::keccak_memory::keccak_memory_stark::KeccakMemoryStark;
use crate::logic::LogicStark;
use crate::memory::memory_stark::MemoryStark;
use crate::permutation::PermutationCheckVars;
use crate::permutation::{
compute_permutation_z_polys, get_n_grand_product_challenge_sets, GrandProductChallengeSet,
PermutationCheckVars,
};
use crate::proof::{AllProof, PublicValues, StarkOpeningSet, StarkProof};
use crate::stark::Stark;

View File

@ -13,13 +13,12 @@ use plonky2::iop::target::Target;
use plonky2::iop::witness::Witness;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::{CircuitConfig, VerifierCircuitData, VerifierCircuitTarget};
use plonky2::plonk::config::Hasher;
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, Hasher};
use plonky2::plonk::proof::{ProofWithPublicInputs, ProofWithPublicInputsTarget};
use plonky2::util::reducing::ReducingFactorTarget;
use plonky2::with_context;
use crate::all_stark::NUM_TABLES;
use crate::all_stark::{AllStark, Table, NUM_TABLES};
use crate::config::StarkConfig;
use crate::constraint_consumer::RecursiveConstraintConsumer;
use crate::cpu::cpu_stark::CpuStark;
@ -41,13 +40,9 @@ use crate::proof::{
TrieRootsTarget,
};
use crate::stark::Stark;
use crate::util::h160_limbs;
use crate::util::{h160_limbs, h256_limbs};
use crate::vanishing_poly::eval_vanishing_poly_circuit;
use crate::vars::StarkEvaluationTargets;
use crate::{
all_stark::{AllStark, Table},
util::h256_limbs,
};
/// Table-wise recursive proofs of an `AllProof`.
pub struct RecursiveAllProof<
@ -850,8 +845,7 @@ pub(crate) mod tests {
use plonky2::iop::witness::{PartialWitness, Witness};
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::{CircuitConfig, VerifierCircuitData};
use plonky2::plonk::config::Hasher;
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, Hasher};
use plonky2::plonk::proof::ProofWithPublicInputs;
use crate::all_stark::{AllStark, Table};

View File

@ -13,8 +13,7 @@ use plonky2_util::ceil_div_usize;
use crate::config::StarkConfig;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::permutation::PermutationPair;
use crate::vars::StarkEvaluationTargets;
use crate::vars::StarkEvaluationVars;
use crate::vars::{StarkEvaluationTargets, StarkEvaluationVars};
const TRACE_ORACLE_INDEX: usize = 0;
const PERMUTATION_CTL_ORACLE_INDEX: usize = 1;

View File

@ -1,15 +1,12 @@
use anyhow::{ensure, Result};
use plonky2::field::extension::Extendable;
use plonky2::field::extension::FieldExtension;
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;
use plonky2::iop::witness::Witness;
use plonky2::iop::witness::{PartialWitness, Witness};
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::GenericConfig;
use plonky2::plonk::config::Hasher;
use plonky2::plonk::config::{GenericConfig, Hasher};
use plonky2::util::transpose;
use plonky2_util::{log2_ceil, log2_strict};
@ -93,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"]
rand = ["dep:rand"]
[dependencies]
plonky2_util = { path = "../util" }
anyhow = "1.0.40"
itertools = "0.10.0"
num = { version = "0.4", features = [ "rand" ] }
rand = { optional = true, version = "0.8.4" }
serde = { version = "1.0", features = ["derive"] }
unroll = "0.1.5"
static_assertions = "1.1.0"
anyhow = { version = "1.0.40", default-features = false }
itertools = { version = "0.10.0", default-features = false, 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", 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

@ -1,20 +1,22 @@
use core::arch::x86_64::*;
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::iter::{Product, Sum};
use std::mem::transmute;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::iter::{Product, Sum};
use core::mem::transmute;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::goldilocks_field::GoldilocksField;
use crate::ops::Square;
use crate::packed::PackedField;
use crate::types::{Field, Field64};
// Ideally `Avx2GoldilocksField` would wrap `__m256i`. Unfortunately, `__m256i` has an alignment of
// 32B, which would preclude us from casting `[GoldilocksField; 4]` (alignment 8B) to
// `Avx2GoldilocksField`. We need to ensure that `Avx2GoldilocksField` has the same alignment as
// `GoldilocksField`. Thus we wrap `[GoldilocksField; 4]` and use the `new` and `get` methods to
// convert to and from `__m256i`.
/// AVX2 Goldilocks Field
///
/// Ideally `Avx2GoldilocksField` would wrap `__m256i`. Unfortunately, `__m256i` has an alignment of
/// 32B, which would preclude us from casting `[GoldilocksField; 4]` (alignment 8B) to
/// `Avx2GoldilocksField`. We need to ensure that `Avx2GoldilocksField` has the same alignment as
/// `GoldilocksField`. Thus we wrap `[GoldilocksField; 4]` and use the `new` and `get` methods to
/// convert to and from `__m256i`.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Avx2GoldilocksField(pub [GoldilocksField; 4]);

View File

@ -1,20 +1,22 @@
use core::arch::x86_64::*;
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::iter::{Product, Sum};
use std::mem::transmute;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::iter::{Product, Sum};
use core::mem::transmute;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::goldilocks_field::GoldilocksField;
use crate::ops::Square;
use crate::packed::PackedField;
use crate::types::{Field, Field64};
// Ideally `Avx512GoldilocksField` would wrap `__m512i`. Unfortunately, `__m512i` has an alignment
// of 64B, which would preclude us from casting `[GoldilocksField; 8]` (alignment 8B) to
// `Avx512GoldilocksField`. We need to ensure that `Avx512GoldilocksField` has the same alignment as
// `GoldilocksField`. Thus we wrap `[GoldilocksField; 8]` and use the `new` and `get` methods to
// convert to and from `__m512i`.
/// AVX512 Goldilocks Field
///
/// Ideally `Avx512GoldilocksField` would wrap `__m512i`. Unfortunately, `__m512i` has an alignment
/// of 64B, which would preclude us from casting `[GoldilocksField; 8]` (alignment 8B) to
/// `Avx512GoldilocksField`. We need to ensure that `Avx512GoldilocksField` has the same alignment as
/// `GoldilocksField`. Thus we wrap `[GoldilocksField; 8]` and use the `new` and `get` methods to
/// convert to and from `__m512i`.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Avx512GoldilocksField(pub [GoldilocksField; 8]);

View File

@ -1,3 +1,5 @@
use alloc::vec::Vec;
use num::bigint::BigUint;
use crate::types::Field;

View File

@ -1,6 +1,7 @@
use std::fmt::{Debug, Display, Formatter};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use alloc::vec::Vec;
use core::fmt::{self, Debug, Display, Formatter};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::extension::OEF;
@ -42,7 +43,7 @@ impl<F: OEF<D>, const D: usize> From<F> for ExtensionAlgebra<F, D> {
}
impl<F: OEF<D>, const D: usize> Display for ExtensionAlgebra<F, D> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "({})", self.0[0])?;
for i in 1..D {
write!(f, " + ({})*b^{i}", self.0[i])?;
@ -52,7 +53,7 @@ impl<F: OEF<D>, const D: usize> Display for ExtensionAlgebra<F, D> {
}
impl<F: OEF<D>, const D: usize> Debug for ExtensionAlgebra<F, D> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}
@ -190,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

@ -1,4 +1,4 @@
use std::convert::TryInto;
use alloc::vec::Vec;
use crate::types::Field;

View File

@ -1,13 +1,13 @@
use std::fmt::{Debug, Display, Formatter};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::fmt::{self, Debug, Display, Formatter};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::bigint::BigUint;
use serde::{Deserialize, Serialize};
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,21 +109,16 @@ 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::Rng>(rng: &mut R) -> Self {
Self([F::rand_from_rng(rng), F::rand_from_rng(rng)])
}
}
impl<F: Extendable<2>> Display for QuadraticExtension<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} + {}*a", self.0[0], self.0[1])
}
}
impl<F: Extendable<2>> Debug for QuadraticExtension<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}

View File

@ -1,6 +1,6 @@
use std::fmt::{Debug, Display, Formatter};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::fmt::{self, Debug, Display, Formatter};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::bigint::BigUint;
use num::traits::Pow;
@ -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,20 +119,10 @@ 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::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<F: Extendable<4>> Display for QuarticExtension<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{} + {}*a + {}*a^2 + {}*a^3",
@ -127,7 +132,7 @@ impl<F: Extendable<4>> Display for QuarticExtension<F> {
}
impl<F: Extendable<4>> Debug for QuarticExtension<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}

View File

@ -1,6 +1,6 @@
use std::fmt::{Debug, Display, Formatter};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::fmt::{self, Debug, Display, Formatter};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::bigint::BigUint;
use num::traits::Pow;
@ -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,21 +126,10 @@ 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::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<F: Extendable<5>> Display for QuinticExtension<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{} + {}*a + {}*a^2 + {}*a^3 + {}*a^4",
@ -134,7 +139,7 @@ impl<F: Extendable<5>> Display for QuinticExtension<F> {
}
impl<F: Extendable<5>> Debug for QuinticExtension<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}

View File

@ -1,5 +1,5 @@
use std::cmp::{max, min};
use std::option::Option;
use alloc::vec::Vec;
use core::cmp::{max, min};
use plonky2_util::{log2_strict, reverse_index_bits_in_place};
use unroll::unroll_for_loops;
@ -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,15 +1,17 @@
use crate::extension::Extendable;
use crate::extension::Frobenius;
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() {
@ -72,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

@ -1,4 +1,4 @@
use std::ops::Mul;
use core::ops::Mul;
use static_assertions::const_assert;

View File

@ -1,15 +1,14 @@
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::fmt::{self, Debug, Display, Formatter};
use core::hash::{Hash, Hasher};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::{BigUint, Integer};
use plonky2_util::{assume, branch_hint};
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;
@ -57,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);
@ -104,11 +114,6 @@ impl Field for GoldilocksField {
reduce128(n)
}
#[cfg(feature = "rand")]
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
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.
@ -300,10 +305,9 @@ impl DivAssign for GoldilocksField {
#[inline(always)]
#[cfg(target_arch = "x86_64")]
unsafe fn add_no_canonicalize_trashing_input(x: u64, y: u64) -> u64 {
use std::arch::asm;
let res_wrapped: u64;
let adjustment: u64;
asm!(
core::arch::asm!(
"add {0}, {1}",
// Trick. The carry flag is set iff the addition overflowed.
// sbb x, y does x := x - y - CF. In our case, x and y are both {1:e}, so it simply does

View File

@ -1,3 +1,5 @@
use alloc::vec::Vec;
use plonky2_util::log2_ceil;
use crate::fft::ifft;
@ -79,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

@ -6,8 +6,8 @@ use crate::types::PrimeField64;
#[inline(always)]
fn safe_iteration(f: &mut u64, g: &mut u64, c: &mut i128, d: &mut i128, k: &mut u32) {
if f < g {
std::mem::swap(f, g);
std::mem::swap(c, d);
core::mem::swap(f, g);
core::mem::swap(c, d);
}
if *f & 3 == *g & 3 {
// f - g = 0 (mod 4)
@ -36,8 +36,8 @@ fn safe_iteration(f: &mut u64, g: &mut u64, c: &mut i128, d: &mut i128, k: &mut
#[inline(always)]
unsafe fn unsafe_iteration(f: &mut u64, g: &mut u64, c: &mut i128, d: &mut i128, k: &mut u32) {
if *f < *g {
std::mem::swap(f, g);
std::mem::swap(c, d);
core::mem::swap(f, g);
core::mem::swap(c, d);
}
if *f & 3 == *g & 3 {
// f - g = 0 (mod 4)

View File

@ -7,9 +7,14 @@
#![allow(clippy::return_self_not_must_use)]
#![feature(generic_const_exprs)]
#![feature(specialization)]
#![feature(stdsimd)]
#![cfg_attr(not(test), no_std)]
extern crate alloc;
mod inversion;
pub(crate) mod arch;
pub mod batch_util;
pub mod cosets;
pub mod extension;
@ -17,7 +22,6 @@ pub mod fft;
pub mod goldilocks_extensions;
pub mod goldilocks_field;
pub mod interpolation;
mod inversion;
pub mod ops;
pub mod packable;
pub mod packed;
@ -29,5 +33,6 @@ pub mod zero_poly_coset;
#[cfg(test)]
mod field_testing;
#[cfg(test)]
mod prime_field_testing;

View File

@ -1,4 +1,4 @@
use std::ops::Mul;
use core::ops::Mul;
pub trait Square {
fn square(&self) -> Self;

View File

@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use std::slice;
use core::fmt::Debug;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use core::slice;
use crate::ops::Square;
use crate::types::Field;
@ -82,7 +82,7 @@ where
);
let buf_ptr = buf.as_ptr().cast::<Self>();
let n = buf.len() / Self::WIDTH;
unsafe { std::slice::from_raw_parts(buf_ptr, n) }
unsafe { slice::from_raw_parts(buf_ptr, n) }
}
fn pack_slice_mut(buf: &mut [Self::Scalar]) -> &mut [Self] {
assert!(
@ -93,7 +93,7 @@ where
);
let buf_ptr = buf.as_mut_ptr().cast::<Self>();
let n = buf.len() / Self::WIDTH;
unsafe { std::slice::from_raw_parts_mut(buf_ptr, n) }
unsafe { slice::from_raw_parts_mut(buf_ptr, n) }
}
fn doubles(&self) -> Self {

View File

@ -1,3 +1,6 @@
use alloc::vec;
use alloc::vec::Vec;
use plonky2_util::log2_ceil;
use crate::polynomial::PolynomialCoeffs;
@ -68,7 +71,7 @@ impl<F: Field> PolynomialCoeffs<F> {
}
/// Let `self=p(X)`, this returns `(p(X)-p(z))/(X-z)`.
/// See https://en.wikipedia.org/wiki/Horner%27s_method
/// See <https://en.wikipedia.org/wiki/Horner%27s_method>
pub fn divide_by_linear(&self, z: F) -> PolynomialCoeffs<F> {
let mut bs = self
.coeffs
@ -131,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

@ -1,8 +1,10 @@
pub(crate) mod division;
use std::cmp::max;
use std::iter::Sum;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::max;
use core::iter::Sum;
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use anyhow::{ensure, Result};
use itertools::Itertools;
@ -440,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() {
@ -516,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));
@ -532,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));
@ -557,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));
@ -571,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));
@ -585,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()]);
@ -601,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
@ -68,7 +70,7 @@ where
macro_rules! test_prime_field_arithmetic {
($field:ty) => {
mod prime_field_arithmetic {
use std::ops::{Add, Mul, Neg, Sub};
use core::ops::{Add, Mul, Neg, Sub};
use $crate::ops::Square;
use $crate::types::{Field, Field64};

View File

@ -1,15 +1,15 @@
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use alloc::vec::Vec;
use core::fmt::{self, Debug, Display, Formatter};
use core::hash::{Hash, Hasher};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use itertools::Itertools;
use num::bigint::BigUint;
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::Rng>(rng: &mut R) -> Self {
use num::bigint::RandBigInt;
Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order()))
}
}
impl PrimeField for Secp256K1Base {

View File

@ -1,16 +1,15 @@
use std::convert::TryInto;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use alloc::vec::Vec;
use core::fmt::{self, Debug, Display, Formatter};
use core::hash::{Hash, Hasher};
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use itertools::Itertools;
use num::bigint::BigUint;
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.
///
@ -68,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]);
@ -140,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::Rng>(rng: &mut R) -> Self {
use num::bigint::RandBigInt;
Self::from_noncanonical_biguint(rng.gen_biguint_below(&Self::order()))
}
}
impl PrimeField for Secp256K1Scalar {

View File

@ -1,17 +1,49 @@
use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::iter::{Product, Sum};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::{Debug, Display};
use core::hash::Hash;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::bigint::BigUint;
use num::{Integer, One, ToPrimitive, Zero};
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
@ -33,6 +65,7 @@ pub trait Field:
+ Debug
+ Default
+ Display
+ Sample
+ Send
+ Sync
+ Serialize
@ -317,9 +350,6 @@ pub trait Field:
Self::from_noncanonical_u128(n)
}
#[cfg(feature = "rand")]
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self;
fn exp_power_of_2(&self, power_log: usize) -> Self {
let mut res = *self;
for _ in 0..power_log {
@ -397,21 +427,6 @@ pub trait Field:
}
}
#[cfg(feature = "rand")]
fn rand() -> Self {
Self::rand_from_rng(&mut rand::thread_rng())
}
#[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

@ -1,3 +1,5 @@
use alloc::vec::Vec;
use crate::packed::PackedField;
use crate::types::Field;

View File

@ -5,5 +5,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = { version = "1.0.40", default-features = false }
plonky2 = { path = "../plonky2", default-features = false }
[dev-dependencies]
plonky2 = { path = "../plonky2" }
anyhow = "1.0.40"

View File

@ -1,3 +1,6 @@
use alloc::vec;
use alloc::vec::Vec;
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
@ -50,7 +53,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

@ -1,5 +1,9 @@
use std::marker::PhantomData;
use std::ops::Range;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use alloc::{format, vec};
use core::marker::PhantomData;
use core::ops::Range;
use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::field::types::Field;
@ -317,18 +321,14 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F> for Insert
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
use anyhow::Result;
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field;
use plonky2::gates::gate::Gate;
use plonky2::field::types::Sample;
use plonky2::gates::gate_testing::{test_eval_fns, test_low_degree};
use plonky2::hash::hash_types::HashOut;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2::plonk::vars::EvaluationVars;
use crate::insertion_gate::InsertionGate;
use super::*;
#[test]
fn wire_indices() {

View File

@ -4,6 +4,9 @@
#![allow(clippy::len_without_is_empty)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::return_self_not_must_use)]
#![no_std]
extern crate alloc;
pub mod insert_gadget;
pub mod insertion_gate;

View File

@ -3,7 +3,6 @@ name = "maybe_rayon"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
parallel = ["rayon"]

View File

@ -1,13 +1,16 @@
#[cfg(not(feature = "parallel"))]
use std::{
use core::{
iter::{FlatMap, IntoIterator, Iterator},
slice::{Chunks, ChunksExact, ChunksExactMut, ChunksMut},
};
#[cfg(feature = "parallel")]
pub use rayon::prelude::{
IndexedParallelIterator, ParallelDrainFull, ParallelDrainRange, ParallelExtend,
ParallelIterator,
pub use rayon::{
self,
prelude::{
IndexedParallelIterator, ParallelDrainFull, ParallelDrainRange, ParallelExtend,
ParallelIterator,
},
};
#[cfg(feature = "parallel")]
use rayon::{

View File

@ -11,45 +11,46 @@ edition = "2021"
default-run = "generate_constants"
[features]
default = ["parallel", "rand", "rand_chacha", "timing", "gate_testing"]
parallel = ["maybe_rayon/parallel"]
rand = ["dep:rand", "plonky2_field/rand"]
gate_testing = ["rand"]
rand_chacha = ["dep:rand_chacha"]
timing = []
default = ["gate_testing", "parallel", "rand_chacha", "std", "timing"]
gate_testing = []
parallel = ["hashbrown/rayon", "maybe_rayon/parallel"]
std = ["anyhow/std", "rand/std"]
timing = ["std"]
[dependencies]
plonky2_field = { path = "../field" }
plonky2_util = { path = "../util" }
log = "0.4.14"
itertools = "0.10.0"
num = { version = "0.4", features = [ "rand" ] }
rand = { version = "0.8.4", optional = true }
rand_chacha = { version = "0.3.1", optional = true }
maybe_rayon = { path = "../maybe_rayon" }
unroll = "0.1.5"
anyhow = "1.0.40"
serde = { version = "1.0", features = ["derive"] }
serde_cbor = "0.11.1"
keccak-hash = "0.8.0"
static_assertions = "1.1.0"
ahash = { version = "0.7.6", default-features = false, features = ["compile-time-rng"] } # NOTE: Be sure to keep this version the same as the dependency in `hashbrown`.
anyhow = { version = "1.0.40", default-features = false }
hashbrown = { version = "0.12.3", default-features = false, features = ["ahash", "serde"] } # NOTE: When upgrading, see `ahash` dependency.
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, features = ["rand"] }
plonky2_field = { path = "../field", default-features = false }
plonky2_util = { path = "../util", 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"] }
static_assertions = { version = "1.1.0", default-features = false }
unroll = { version = "0.1.5", default-features = false }
[dev-dependencies]
rand = "0.8.4"
rand_chacha = "0.3.1"
criterion = "0.4.0"
env_logger = "0.9.0"
tynm = "0.1.6"
structopt = "0.3.26"
num_cpus = "1.13.1"
rayon = "1.5.1"
criterion = { version = "0.4.0", default-features = false }
env_logger = { version = "0.9.0", default-features = false }
num_cpus = { version = "1.14.0", default-features = false }
plonky2 = { path = "." }
rand = { version = "0.8.4", default-features = false, features = ["getrandom"] }
rand_chacha = { version = "0.3.1", default-features = false }
serde_cbor = { version = "0.11.2" }
structopt = { version = "0.3.26", default-features = false }
tynm = { version = "0.1.6", default-features = false }
[target.'cfg(not(target_env = "msvc"))'.dev-dependencies]
jemallocator = "0.3.2"
[[bin]]
name = "generate_constants"
required-features = ["rand", "rand_chacha"]
required-features = ["rand_chacha"]
[[bench]]
name = "field_arithmetic"

View File

@ -1,10 +1,8 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
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 +25,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,5 +1,3 @@
#![feature(generic_const_exprs)]
mod allocator;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
@ -13,10 +11,7 @@ use tynm::type_name;
const ELEMS_PER_LEAF: usize = 135;
pub(crate) fn bench_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criterion)
where
[(); H::HASH_SIZE]:,
{
pub(crate) fn bench_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criterion) {
let mut group = c.benchmark_group(&format!(
"merkle-tree<{}, {}>",
type_name::<F>(),

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

@ -2,30 +2,28 @@
// custom CLI argument parsing (even with harness disabled). We could also have
// put it in `src/bin/`, but then we wouldn't have access to
// `[dev-dependencies]`.
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
use std::{num::ParseIntError, ops::RangeInclusive, str::FromStr};
use core::num::ParseIntError;
use core::ops::RangeInclusive;
use core::str::FromStr;
use anyhow::{anyhow, Context as _, Result};
use log::{info, Level, LevelFilter};
use plonky2::{
gates::noop::NoopGate,
hash::hash_types::RichField,
iop::witness::{PartialWitness, Witness},
plonk::{
circuit_builder::CircuitBuilder,
circuit_data::{
CircuitConfig, CommonCircuitData, VerifierCircuitTarget, VerifierOnlyCircuitData,
},
config::{AlgebraicHasher, GenericConfig, Hasher, PoseidonGoldilocksConfig},
proof::{CompressedProofWithPublicInputs, ProofWithPublicInputs},
prover::prove,
},
util::timing::TimingTree,
use maybe_rayon::rayon;
use plonky2::gates::noop::NoopGate;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::witness::{PartialWitness, Witness};
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::{
CircuitConfig, CommonCircuitData, VerifierCircuitTarget, VerifierOnlyCircuitData,
};
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, PoseidonGoldilocksConfig};
use plonky2::plonk::proof::{CompressedProofWithPublicInputs, ProofWithPublicInputs};
use plonky2::plonk::prover::prove;
use plonky2::util::timing::TimingTree;
use plonky2_field::extension::Extendable;
use rand::{rngs::OsRng, RngCore, SeedableRng};
use rand::rngs::OsRng;
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
use structopt::StructOpt;
@ -66,10 +64,7 @@ struct Options {
fn dummy_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
config: &CircuitConfig,
log2_size: usize,
) -> Result<ProofTuple<F, C, D>>
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> Result<ProofTuple<F, C, D>> {
// 'size' is in degree, but we want number of noop gates. A non-zero amount of padding will be added and size will be rounded to the next power of two. To hit our target size, we go just under the previous power of two and hope padding is less than half the proof.
let num_dummy_gates = match log2_size {
0 => return Err(anyhow!("size must be at least 1")),
@ -107,7 +102,6 @@ fn recursive_proof<
) -> Result<ProofTuple<F, C, D>>
where
InnerC::Hasher: AlgebraicHasher<F>,
[(); C::Hasher::HASH_SIZE]:,
{
let (inner_proof, inner_vd, inner_cd) = inner;
let mut builder = CircuitBuilder::<F, D>::new(config.clone());
@ -151,11 +145,8 @@ fn test_serialization<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>,
proof: &ProofWithPublicInputs<F, C, D>,
vd: &VerifierOnlyCircuitData<C, D>,
cd: &CommonCircuitData<F, D>,
) -> Result<()>
where
[(); C::Hasher::HASH_SIZE]:,
{
let proof_bytes = proof.to_bytes()?;
) -> Result<()> {
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);
@ -168,7 +159,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 std::marker::PhantomData;
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

@ -2,8 +2,8 @@
#![allow(clippy::needless_range_loop)]
use plonky2_field::goldilocks_field::GoldilocksField;
use plonky2_field::types::Field64;
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field64;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;

View File

@ -1,6 +1,5 @@
use plonky2_field::extension::Extendable;
use plonky2_field::polynomial::PolynomialCoeffs;
use crate::field::extension::Extendable;
use crate::field::polynomial::PolynomialCoeffs;
use crate::fri::proof::{FriChallenges, FriChallengesTarget};
use crate::fri::structure::{FriOpenings, FriOpeningsTarget};
use crate::fri::FriConfig;

View File

@ -1,3 +1,5 @@
use alloc::vec::Vec;
use crate::fri::reduction_strategies::FriReductionStrategy;
mod challenges;

View File

@ -1,12 +1,14 @@
use alloc::format;
use alloc::vec::Vec;
use itertools::Itertools;
use maybe_rayon::*;
use plonky2_field::extension::Extendable;
use plonky2_field::fft::FftRootTable;
use plonky2_field::packed::PackedField;
use plonky2_field::polynomial::{PolynomialCoeffs, PolynomialValues};
use plonky2_field::types::Field;
use plonky2_util::{log2_strict, reverse_index_bits_in_place};
use crate::field::extension::Extendable;
use crate::field::fft::FftRootTable;
use crate::field::packed::PackedField;
use crate::field::polynomial::{PolynomialCoeffs, PolynomialValues};
use crate::field::types::Field;
use crate::fri::proof::FriProof;
use crate::fri::prover::fri_proof;
use crate::fri::structure::{FriBatchInfo, FriInstanceInfo};
@ -14,12 +16,11 @@ use crate::fri::FriParams;
use crate::hash::hash_types::RichField;
use crate::hash::merkle_tree::MerkleTree;
use crate::iop::challenger::Challenger;
use crate::plonk::config::{GenericConfig, Hasher};
use crate::plonk::config::GenericConfig;
use crate::timed;
use crate::util::reducing::ReducingFactor;
use crate::util::reverse_bits;
use crate::util::timing::TimingTree;
use crate::util::transpose;
use crate::util::{log2_strict, reverse_bits, reverse_index_bits_in_place, transpose};
/// Four (~64 bit) field elements gives ~128 bit security.
pub const SALT_SIZE: usize = 4;
@ -45,10 +46,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
cap_height: usize,
timing: &mut TimingTree,
fft_root_table: Option<&FftRootTable<F>>,
) -> Self
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> Self {
let coeffs = timed!(
timing,
"IFFT",
@ -73,10 +71,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
cap_height: usize,
timing: &mut TimingTree,
fft_root_table: Option<&FftRootTable<F>>,
) -> Self
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> Self {
let degree = polynomials[0].len();
let lde_values = timed!(
timing,
@ -169,10 +164,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
challenger: &mut Challenger<F, C::Hasher>,
fri_params: &FriParams,
timing: &mut TimingTree,
) -> FriProof<F, C::Hasher, D>
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> FriProof<F, C::Hasher, D> {
assert!(D > 1, "Not implemented for D=1.");
let alpha = challenger.get_extension_challenge::<D>();
let mut alpha = ReducingFactor::new(alpha);

View File

@ -1,14 +1,15 @@
use std::collections::HashMap;
use alloc::vec;
use alloc::vec::Vec;
use hashbrown::HashMap;
use itertools::izip;
use plonky2_field::extension::{flatten, unflatten, Extendable};
use plonky2_field::polynomial::PolynomialCoeffs;
use serde::{Deserialize, Serialize};
use crate::field::extension::{flatten, unflatten, Extendable};
use crate::field::polynomial::PolynomialCoeffs;
use crate::fri::FriParams;
use crate::gadgets::polynomial::PolynomialCoeffsExtTarget;
use crate::hash::hash_types::MerkleCapTarget;
use crate::hash::hash_types::RichField;
use crate::hash::hash_types::{MerkleCapTarget, RichField};
use crate::hash::merkle_proofs::{MerkleProof, MerkleProofTarget};
use crate::hash::merkle_tree::MerkleCap;
use crate::hash::path_compression::{compress_merkle_proofs, decompress_merkle_proofs};
@ -245,10 +246,7 @@ impl<F: RichField + Extendable<D>, H: Hasher<F>, const D: usize> CompressedFriPr
challenges: &ProofChallenges<F, D>,
fri_inferred_elements: FriInferredElements<F, D>,
params: &FriParams,
) -> FriProof<F, H, D>
where
[(); H::HASH_SIZE]:,
{
) -> FriProof<F, H, D> {
let CompressedFriProof {
commit_phase_merkle_caps,
query_round_proofs,

View File

@ -1,8 +1,9 @@
use maybe_rayon::*;
use plonky2_field::extension::{flatten, unflatten, Extendable};
use plonky2_field::polynomial::{PolynomialCoeffs, PolynomialValues};
use plonky2_util::reverse_index_bits_in_place;
use alloc::vec::Vec;
use maybe_rayon::*;
use crate::field::extension::{flatten, unflatten, Extendable};
use crate::field::polynomial::{PolynomialCoeffs, PolynomialValues};
use crate::fri::proof::{FriInitialTreeProof, FriProof, FriQueryRound, FriQueryStep};
use crate::fri::{FriConfig, FriParams};
use crate::hash::hash_types::RichField;
@ -12,6 +13,7 @@ use crate::iop::challenger::Challenger;
use crate::plonk::config::{GenericConfig, Hasher};
use crate::plonk::plonk_common::reduce_with_powers;
use crate::timed;
use crate::util::reverse_index_bits_in_place;
use crate::util::timing::TimingTree;
/// Builds a FRI proof.
@ -24,10 +26,7 @@ pub fn fri_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const
challenger: &mut Challenger<F, C::Hasher>,
fri_params: &FriParams,
timing: &mut TimingTree,
) -> FriProof<F, C::Hasher, D>
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> FriProof<F, C::Hasher, D> {
let n = lde_polynomial_values.len();
assert_eq!(lde_polynomial_coeffs.len(), n);
@ -62,18 +61,17 @@ where
}
}
type FriCommitedTrees<F, C, const D: usize> = (
Vec<MerkleTree<F, <C as GenericConfig<D>>::Hasher>>,
PolynomialCoeffs<<F as Extendable<D>>::Extension>,
);
fn fri_committed_trees<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
mut coeffs: PolynomialCoeffs<F::Extension>,
mut values: PolynomialValues<F::Extension>,
challenger: &mut Challenger<F, C::Hasher>,
fri_params: &FriParams,
) -> (
Vec<MerkleTree<F, C::Hasher>>,
PolynomialCoeffs<F::Extension>,
)
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> FriCommitedTrees<F, C, D> {
let mut trees = Vec::new();
let mut shift = F::MULTIPLICATIVE_GROUP_GENERATOR;

View File

@ -1,7 +1,9 @@
use itertools::Itertools;
use plonky2_field::extension::Extendable;
use plonky2_util::{log2_strict, reverse_index_bits_in_place};
use alloc::vec::Vec;
use alloc::{format, vec};
use itertools::Itertools;
use crate::field::extension::Extendable;
use crate::fri::proof::{
FriChallengesTarget, FriInitialTreeProofTarget, FriProofTarget, FriQueryRoundTarget,
FriQueryStepTarget,
@ -13,13 +15,13 @@ use crate::gates::high_degree_interpolation::HighDegreeInterpolationGate;
use crate::gates::interpolation::InterpolationGate;
use crate::gates::low_degree_interpolation::LowDegreeInterpolationGate;
use crate::gates::random_access::RandomAccessGate;
use crate::hash::hash_types::MerkleCapTarget;
use crate::hash::hash_types::RichField;
use crate::hash::hash_types::{MerkleCapTarget, RichField};
use crate::iop::ext_target::{flatten_target, ExtensionTarget};
use crate::iop::target::{BoolTarget, Target};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::config::{AlgebraicHasher, GenericConfig};
use crate::util::reducing::ReducingFactorTarget;
use crate::util::{log2_strict, reverse_index_bits_in_place};
use crate::with_context;
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {

View File

@ -1,3 +1,6 @@
use alloc::vec;
use alloc::vec::Vec;
#[cfg(feature = "timing")]
use std::time::Instant;
use log::debug;
@ -32,7 +35,6 @@ impl FriReductionStrategy {
) -> Vec<usize> {
match self {
FriReductionStrategy::Fixed(reduction_arity_bits) => reduction_arity_bits.to_vec(),
&FriReductionStrategy::ConstantArityBits(arity_bits, final_poly_bits) => {
let mut result = Vec::new();
while degree_bits > final_poly_bits
@ -45,7 +47,6 @@ impl FriReductionStrategy {
result.shrink_to_fit();
result
}
FriReductionStrategy::MinSize(opt_max_arity_bits) => {
min_size_arity_bits(degree_bits, rate_bits, num_queries, *opt_max_arity_bits)
}
@ -63,11 +64,13 @@ fn min_size_arity_bits(
// in an optimal sequence, we would need a really massive polynomial.
let max_arity_bits = opt_max_arity_bits.unwrap_or(4);
#[cfg(feature = "timing")]
let start = Instant::now();
let (mut arity_bits, fri_proof_size) =
min_size_arity_bits_helper(degree_bits, rate_bits, num_queries, max_arity_bits, vec![]);
arity_bits.shrink_to_fit();
#[cfg(feature = "timing")]
debug!(
"min_size_arity_bits took {:.3}s",
start.elapsed().as_secs_f32()

View File

@ -1,7 +1,8 @@
//! Information about the structure of a FRI instance, in terms of the oracles and polynomials
//! involved, and the points they are opened at.
use std::ops::Range;
use alloc::vec::Vec;
use core::ops::Range;
use crate::field::extension::Extendable;
use crate::hash::hash_types::RichField;

View File

@ -1,6 +1,6 @@
use anyhow::ensure;
use plonky2_field::extension::Extendable;
use crate::field::extension::Extendable;
use crate::fri::proof::{FriProof, FriQueryRound, FriQueryStep};
use crate::fri::structure::FriInstanceInfo;
use crate::fri::FriParams;

View File

@ -1,9 +1,10 @@
use anyhow::{ensure, Result};
use plonky2_field::extension::{flatten, Extendable, FieldExtension};
use plonky2_field::interpolation::{barycentric_weights, interpolate};
use plonky2_field::types::Field;
use plonky2_util::{log2_strict, reverse_index_bits_in_place};
use alloc::vec::Vec;
use anyhow::{ensure, Result};
use crate::field::extension::{flatten, Extendable, FieldExtension};
use crate::field::interpolation::{barycentric_weights, interpolate};
use crate::field::types::Field;
use crate::fri::proof::{FriChallenges, FriInitialTreeProof, FriProof, FriQueryRound};
use crate::fri::structure::{FriBatchInfo, FriInstanceInfo, FriOpenings};
use crate::fri::validate_shape::validate_fri_proof_shape;
@ -13,7 +14,7 @@ use crate::hash::merkle_proofs::verify_merkle_proof_to_cap;
use crate::hash::merkle_tree::MerkleCap;
use crate::plonk::config::{GenericConfig, Hasher};
use crate::util::reducing::ReducingFactor;
use crate::util::reverse_bits;
use crate::util::{log2_strict, reverse_bits, reverse_index_bits_in_place};
/// Computes P'(x^arity) from {P(x*g^i)}_(i=0..arity), where g is a `arity`-th root of unity
/// and P' is the FRI reduced polynomial.
@ -57,17 +58,18 @@ pub(crate) fn fri_verify_proof_of_work<F: RichField + Extendable<D>, const D: us
Ok(())
}
pub fn verify_fri_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
pub fn verify_fri_proof<
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
const D: usize,
>(
instance: &FriInstanceInfo<F, D>,
openings: &FriOpenings<F, D>,
challenges: &FriChallenges<F, D>,
initial_merkle_caps: &[MerkleCap<F, C::Hasher>],
proof: &FriProof<F, C::Hasher, D>,
params: &FriParams,
) -> Result<()>
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> Result<()> {
validate_fri_proof_shape::<F, C, D>(proof, instance, params)?;
// Size of the LDE domain.
@ -109,10 +111,7 @@ fn fri_verify_initial_proof<F: RichField, H: Hasher<F>>(
x_index: usize,
proof: &FriInitialTreeProof<F, H>,
initial_merkle_caps: &[MerkleCap<F, H>],
) -> Result<()>
where
[(); H::HASH_SIZE]:,
{
) -> Result<()> {
for ((evals, merkle_proof), cap) in proof.evals_proofs.iter().zip(initial_merkle_caps) {
verify_merkle_proof_to_cap::<F, H>(evals.clone(), x_index, cap, merkle_proof)?;
}
@ -177,10 +176,7 @@ fn fri_verifier_query_round<
n: usize,
round_proof: &FriQueryRound<F, C::Hasher, D>,
params: &FriParams,
) -> Result<()>
where
[(); C::Hasher::HASH_SIZE]:,
{
) -> Result<()> {
fri_verify_initial_proof::<F, C::Hasher>(
x_index,
&round_proof.initial_trees_proof,

View File

@ -1,6 +1,6 @@
use itertools::Itertools;
use plonky2_field::extension::Extendable;
use crate::field::extension::Extendable;
use crate::fri::proof::{FriProof, FriProofTarget};
use crate::hash::hash_types::RichField;
use crate::iop::witness::Witness;

View File

@ -1,8 +1,9 @@
use std::borrow::Borrow;
use plonky2_field::extension::Extendable;
use plonky2_field::types::Field64;
use alloc::vec;
use alloc::vec::Vec;
use core::borrow::Borrow;
use crate::field::extension::Extendable;
use crate::field::types::Field64;
use crate::gates::arithmetic_base::ArithmeticGate;
use crate::gates::exponentiation::ExponentiationGate;
use crate::hash::hash_types::RichField;

View File

@ -1,10 +1,9 @@
use std::borrow::Borrow;
use plonky2_field::extension::FieldExtension;
use plonky2_field::extension::{Extendable, OEF};
use plonky2_field::types::{Field, Field64};
use plonky2_util::bits_u64;
use alloc::vec;
use alloc::vec::Vec;
use core::borrow::Borrow;
use crate::field::extension::{Extendable, FieldExtension, OEF};
use crate::field::types::{Field, Field64};
use crate::gates::arithmetic_extension::ArithmeticExtensionGate;
use crate::gates::multiplication_extension::MulExtensionGate;
use crate::hash::hash_types::RichField;
@ -13,6 +12,7 @@ use crate::iop::generator::{GeneratedValues, SimpleGenerator};
use crate::iop::target::Target;
use crate::iop::witness::{PartitionWitness, Witness};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::util::bits_u64;
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
pub fn arithmetic_extension(
@ -569,9 +569,9 @@ pub(crate) struct ExtensionArithmeticOperation<F: Field64 + Extendable<D>, const
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::extension::algebra::ExtensionAlgebra;
use plonky2_field::types::Field;
use crate::field::extension::algebra::ExtensionAlgebra;
use crate::field::types::Sample;
use crate::iop::ext_target::ExtensionAlgebraTarget;
use crate::iop::witness::{PartialWitness, Witness};
use crate::plonk::circuit_builder::CircuitBuilder;
@ -665,8 +665,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

@ -1,5 +1,4 @@
use plonky2_field::extension::Extendable;
use crate::field::extension::Extendable;
use crate::hash::hash_types::RichField;
use crate::hash::hashing::SPONGE_WIDTH;
use crate::iop::target::{BoolTarget, Target};

View File

@ -1,5 +1,6 @@
use plonky2_field::extension::Extendable;
use alloc::vec::Vec;
use crate::field::extension::Extendable;
use crate::hash::hash_types::RichField;
use crate::iop::ext_target::{ExtensionAlgebraTarget, ExtensionTarget};
use crate::iop::target::Target;
@ -14,6 +15,10 @@ impl<const D: usize> PolynomialCoeffsExtTarget<D> {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn eval_scalar<F: RichField + Extendable<D>>(
&self,
builder: &mut CircuitBuilder<F, D>,

View File

@ -1,11 +1,12 @@
use plonky2_field::extension::Extendable;
use plonky2_util::log2_strict;
use alloc::vec::Vec;
use crate::field::extension::Extendable;
use crate::gates::random_access::RandomAccessGate;
use crate::hash::hash_types::RichField;
use crate::iop::ext_target::ExtensionTarget;
use crate::iop::target::Target;
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::util::log2_strict;
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Checks that a `Target` matches a vector at a non-deterministic index.
@ -55,9 +56,9 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::types::Field;
use super::*;
use crate::field::types::{Field, Sample};
use crate::iop::witness::PartialWitness;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

View File

@ -1,5 +1,7 @@
use plonky2_field::extension::Extendable;
use alloc::vec;
use alloc::vec::Vec;
use crate::field::extension::Extendable;
use crate::hash::hash_types::RichField;
use crate::iop::generator::{GeneratedValues, SimpleGenerator};
use crate::iop::target::{BoolTarget, Target};

View File

@ -1,5 +1,4 @@
use plonky2_field::extension::Extendable;
use crate::field::extension::Extendable;
use crate::hash::hash_types::RichField;
use crate::iop::ext_target::ExtensionTarget;
use crate::iop::target::{BoolTarget, Target};
@ -40,8 +39,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::types::Field;
use crate::field::types::Sample;
use crate::iop::witness::{PartialWitness, Witness};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;

View File

@ -1,16 +1,18 @@
use std::borrow::Borrow;
use alloc::vec;
use alloc::vec::Vec;
use core::borrow::Borrow;
use itertools::Itertools;
use plonky2_field::extension::Extendable;
use plonky2_field::types::Field;
use plonky2_util::log_floor;
use crate::field::extension::Extendable;
use crate::field::types::Field;
use crate::gates::base_sum::BaseSumGate;
use crate::hash::hash_types::RichField;
use crate::iop::generator::{GeneratedValues, SimpleGenerator};
use crate::iop::target::{BoolTarget, Target};
use crate::iop::witness::{PartitionWitness, Witness};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::util::log_floor;
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Split the given element into a list of targets, where each one represents a
@ -105,11 +107,11 @@ impl<F: Field, const B: usize> SimpleGenerator<F> for BaseSumGenerator<B> {
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::types::Field;
use rand::{thread_rng, Rng};
use rand::rngs::OsRng;
use rand::Rng;
use super::*;
use crate::iop::witness::PartialWitness;
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use crate::plonk::verifier::verify;
@ -151,7 +153,7 @@ mod tests {
let pw = PartialWitness::new();
let mut builder = CircuitBuilder::<F, D>::new(config);
let n = thread_rng().gen_range(0..(1 << 30));
let n = OsRng.gen_range(0..(1 << 30));
let x = builder.constant(F::from_canonical_usize(n));
let zero = builder._false();

View File

@ -1,12 +1,14 @@
use plonky2_field::extension::Extendable;
use plonky2_util::ceil_div_usize;
use alloc::vec;
use alloc::vec::Vec;
use crate::field::extension::Extendable;
use crate::gates::base_sum::BaseSumGate;
use crate::hash::hash_types::RichField;
use crate::iop::generator::{GeneratedValues, SimpleGenerator};
use crate::iop::target::{BoolTarget, Target};
use crate::iop::witness::{PartitionWitness, Witness};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::util::ceil_div_usize;
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Split the given integer into a list of wires, where each one represents a

View File

@ -1,6 +1,10 @@
use plonky2_field::extension::Extendable;
use plonky2_field::packed::PackedField;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use crate::field::extension::Extendable;
use crate::field::packed::PackedField;
use crate::gates::gate::Gate;
use crate::gates::packed_util::PackedEvaluableBase;
use crate::gates::util::StridedConstraintConsumer;
@ -209,8 +213,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::goldilocks_field::GoldilocksField;
use crate::field::goldilocks_field::GoldilocksField;
use crate::gates::arithmetic_base::ArithmeticGate;
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
use crate::plonk::circuit_data::CircuitConfig;

View File

@ -1,8 +1,10 @@
use std::ops::Range;
use plonky2_field::extension::Extendable;
use plonky2_field::extension::FieldExtension;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::ops::Range;
use crate::field::extension::{Extendable, FieldExtension};
use crate::gates::gate::Gate;
use crate::gates::util::StridedConstraintConsumer;
use crate::hash::hash_types::RichField;
@ -204,8 +206,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::goldilocks_field::GoldilocksField;
use crate::field::goldilocks_field::GoldilocksField;
use crate::gates::arithmetic_extension::ArithmeticExtensionGate;
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
use crate::plonk::circuit_data::CircuitConfig;

View File

@ -1,10 +1,12 @@
use std::ops::Range;
use plonky2_field::extension::Extendable;
use plonky2_field::packed::PackedField;
use plonky2_field::types::{Field, Field64};
use plonky2_util::log_floor;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use alloc::{format, vec};
use core::ops::Range;
use crate::field::extension::Extendable;
use crate::field::packed::PackedField;
use crate::field::types::{Field, Field64};
use crate::gates::gate::Gate;
use crate::gates::packed_util::PackedEvaluableBase;
use crate::gates::util::StridedConstraintConsumer;
@ -20,6 +22,7 @@ use crate::plonk::vars::{
EvaluationTargets, EvaluationVars, EvaluationVarsBase, EvaluationVarsBaseBatch,
EvaluationVarsBasePacked,
};
use crate::util::log_floor;
/// A gate which can decompose a number into base B little-endian limbs.
#[derive(Copy, Clone, Debug)]
@ -197,8 +200,8 @@ impl<F: RichField, const B: usize> SimpleGenerator<F> for BaseSplitGenerator<B>
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::goldilocks_field::GoldilocksField;
use crate::field::goldilocks_field::GoldilocksField;
use crate::gates::base_sum::BaseSumGate;
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

View File

@ -1,6 +1,10 @@
use plonky2_field::extension::Extendable;
use plonky2_field::packed::PackedField;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use alloc::{format, vec};
use crate::field::extension::Extendable;
use crate::field::packed::PackedField;
use crate::gates::gate::Gate;
use crate::gates::packed_util::PackedEvaluableBase;
use crate::gates::util::StridedConstraintConsumer;
@ -113,8 +117,8 @@ impl<F: RichField + Extendable<D>, const D: usize> PackedEvaluableBase<F, D> for
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2_field::goldilocks_field::GoldilocksField;
use crate::field::goldilocks_field::GoldilocksField;
use crate::gates::constant::ConstantGate;
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
use crate::plonk::circuit_data::CircuitConfig;

Some files were not shown because too many files have changed in this diff Show More