diff --git a/Cargo.lock b/Cargo.lock index 674c88b2fa..7cca14ac2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,7 +240,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -527,7 +527,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -539,7 +539,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -746,7 +746,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -959,6 +959,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -2287,7 +2296,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -2511,6 +2520,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -2557,6 +2567,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/core/Cargo.toml b/core/Cargo.toml index 758a159205..8e45968ec0 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -39,6 +39,8 @@ anyhow = "1.0.79" blake3 = "1.5" blake3-zkvm = { git = "https://github.com/sp1-patches/BLAKE3.git", branch = "patch-blake3_zkvm/v.1.0.0" } cfg-if = "1.0.0" +generic-array = { version = "1.0.0", features = ["alloc"] } +typenum = "1.17.0" clap = { version = "4.4.0", features = ["derive"] } curve25519-dalek = { version = "=4.0.0" } elliptic-curve = "0.13.8" diff --git a/core/src/lib.rs b/core/src/lib.rs index 1fbedfc582..23ae115f9c 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -8,8 +8,10 @@ clippy::type_complexity, clippy::unnecessary_unwrap, clippy::default_constructed_unit_structs, - clippy::box_default + clippy::box_default, + incomplete_features )] +#![feature(generic_const_exprs)] extern crate alloc; diff --git a/core/src/operations/field/field_den.rs b/core/src/operations/field/field_den.rs index b2fd3e4e57..6d48de4275 100644 --- a/core/src/operations/field/field_den.rs +++ b/core/src/operations/field/field_den.rs @@ -1,10 +1,10 @@ use super::params::Limbs; -use super::params::NUM_WITNESS_LIMBS; use super::util::{compute_root_quotient_and_shift, split_u16_limbs_to_u8_limbs}; use super::util_air::eval_field_operation; use crate::air::Polynomial; use crate::air::SP1AirBuilder; use crate::utils::ec::field::FieldParameters; + use num::BigUint; use p3_field::PrimeField32; use sp1_derive::AlignedBorrow; @@ -19,21 +19,16 @@ use std::fmt::Debug; /// or made generic in the future. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FieldDenCols { +pub struct FieldDenCols { /// The result of `a den b`, where a, b are field elements - pub result: Limbs, - pub(crate) carry: Limbs, - pub(crate) witness_low: [T; NUM_WITNESS_LIMBS], - pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], + pub result: Limbs, + pub(crate) carry: Limbs, + pub(crate) witness_low: Limbs, + pub(crate) witness_high: Limbs, } -impl FieldDenCols { - pub fn populate( - &mut self, - a: &BigUint, - b: &BigUint, - sign: bool, - ) -> BigUint { +impl FieldDenCols { + pub fn populate(&mut self, a: &BigUint, b: &BigUint, sign: bool) -> BigUint { let p = P::modulus(); let minus_b_int = &p - b; let b_signed = if sign { b.clone() } else { minus_b_int }; @@ -53,11 +48,11 @@ impl FieldDenCols { debug_assert!(carry < p); debug_assert_eq!(&carry * &p, &equation_lhs - &equation_rhs); - let p_a: Polynomial = P::to_limbs_field::(a).into(); - let p_b: Polynomial = P::to_limbs_field::(b).into(); - let p_p: Polynomial = P::to_limbs_field::(&p).into(); - let p_result: Polynomial = P::to_limbs_field::(&result).into(); - let p_carry: Polynomial = P::to_limbs_field::(&carry).into(); + let p_a: Polynomial = P::to_limbs_field::(a).into(); + let p_b: Polynomial = P::to_limbs_field::(b).into(); + let p_p: Polynomial = P::to_limbs_field::(&p).into(); + let p_result: Polynomial = P::to_limbs_field::(&result).into(); + let p_carry: Polynomial = P::to_limbs_field::(&carry).into(); // Compute the vanishing polynomial. let vanishing_poly = if sign { @@ -76,20 +71,23 @@ impl FieldDenCols { self.result = p_result.into(); self.carry = p_carry.into(); - self.witness_low = p_witness_low.try_into().unwrap(); - self.witness_high = p_witness_high.try_into().unwrap(); + self.witness_low = Limbs(p_witness_low.try_into().unwrap()); + self.witness_high = Limbs(p_witness_high.try_into().unwrap()); result } } -impl FieldDenCols { +impl FieldDenCols +where + Limbs: Copy, +{ #[allow(unused_variables)] - pub fn eval, P: FieldParameters>( + pub fn eval>( &self, builder: &mut AB, - a: &Limbs, - b: &Limbs, + a: &Limbs, + b: &Limbs, sign: bool, ) where V: Into, @@ -115,8 +113,8 @@ impl FieldDenCols { let p_vanishing = p_lhs_minus_rhs - &p_carry * &p_limbs; - let p_witness_low = self.witness_low.iter().into(); - let p_witness_high = self.witness_high.iter().into(); + let p_witness_low = self.witness_low.0.iter().into(); + let p_witness_high = self.witness_high.0.iter().into(); eval_field_operation::(builder, &p_vanishing, &p_witness_low, &p_witness_high); } @@ -147,14 +145,15 @@ mod tests { use p3_matrix::MatrixRowSlices; use rand::thread_rng; use sp1_derive::AlignedBorrow; - #[derive(AlignedBorrow, Debug, Clone)] - pub struct TestCols { - pub a: Limbs, - pub b: Limbs, - pub a_den_b: FieldDenCols, + + #[derive(Debug, Clone, AlignedBorrow)] + pub struct TestCols { + pub a: Limbs, + pub b: Limbs, + pub a_den_b: FieldDenCols, } - pub const NUM_TEST_COLS: usize = size_of::>(); + pub const NUM_TEST_COLS: usize = size_of::>(); struct FieldDenChip { pub sign: bool, @@ -206,10 +205,10 @@ mod tests { .iter() .map(|(a, b)| { let mut row = [F::zero(); NUM_TEST_COLS]; - let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); - cols.a = P::to_limbs_field::(a); - cols.b = P::to_limbs_field::(b); - cols.a_den_b.populate::

(a, b, self.sign); + let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); + cols.a = P::to_limbs_field::(a); + cols.b = P::to_limbs_field::(b); + cols.a_den_b.populate(a, b, self.sign); row }) .collect::>(); @@ -237,13 +236,14 @@ mod tests { impl Air for FieldDenChip

where AB: SP1AirBuilder, + Limbs: Copy, { fn eval(&self, builder: &mut AB) { let main = builder.main(); - let local: &TestCols = main.row_slice(0).borrow(); + let local: &TestCols = main.row_slice(0).borrow(); local .a_den_b - .eval::(builder, &local.a, &local.b, self.sign); + .eval::(builder, &local.a, &local.b, self.sign); // A dummy constraint to keep the degree 3. builder.assert_zero( @@ -262,7 +262,7 @@ mod tests { } #[test] - fn prove_babybear() { + fn prove_field() { let config = BabyBearPoseidon2::new(); let mut challenger = config.challenger(); diff --git a/core/src/operations/field/field_inner_product.rs b/core/src/operations/field/field_inner_product.rs index 080ebbf945..14b33eb18e 100644 --- a/core/src/operations/field/field_inner_product.rs +++ b/core/src/operations/field/field_inner_product.rs @@ -1,10 +1,10 @@ use super::params::Limbs; -use super::params::NUM_WITNESS_LIMBS; use super::util::{compute_root_quotient_and_shift, split_u16_limbs_to_u8_limbs}; use super::util_air::eval_field_operation; use crate::air::Polynomial; use crate::air::SP1AirBuilder; use crate::utils::ec::field::FieldParameters; + use num::BigUint; use num::Zero; use p3_field::{AbstractField, PrimeField32}; @@ -16,20 +16,24 @@ use std::fmt::Debug; /// or made generic in the future. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FieldInnerProductCols { +pub struct FieldInnerProductCols { /// The result of `a inner product b`, where a, b are field elements - pub result: Limbs, - pub(crate) carry: Limbs, - pub(crate) witness_low: [T; NUM_WITNESS_LIMBS], - pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], + pub result: Limbs, + pub(crate) carry: Limbs, + pub(crate) witness_low: Limbs, + pub(crate) witness_high: Limbs, } -impl FieldInnerProductCols { - pub fn populate(&mut self, a: &[BigUint], b: &[BigUint]) -> BigUint { - let p_a_vec: Vec> = - a.iter().map(|x| P::to_limbs_field::(x).into()).collect(); - let p_b_vec: Vec> = - b.iter().map(|x| P::to_limbs_field::(x).into()).collect(); +impl FieldInnerProductCols { + pub fn populate(&mut self, a: &[BigUint], b: &[BigUint]) -> BigUint { + let p_a_vec: Vec> = a + .iter() + .map(|x| P::to_limbs_field::(x).into()) + .collect(); + let p_b_vec: Vec> = b + .iter() + .map(|x| P::to_limbs_field::(x).into()) + .collect(); let modulus = &P::modulus(); let inner_product = a @@ -43,9 +47,9 @@ impl FieldInnerProductCols { assert!(carry < &(2u32 * modulus)); assert_eq!(carry * modulus, inner_product - result); - let p_modulus: Polynomial = P::to_limbs_field::(modulus).into(); - let p_result: Polynomial = P::to_limbs_field::(result).into(); - let p_carry: Polynomial = P::to_limbs_field::(carry).into(); + let p_modulus: Polynomial = P::to_limbs_field::(modulus).into(); + let p_result: Polynomial = P::to_limbs_field::(result).into(); + let p_carry: Polynomial = P::to_limbs_field::(carry).into(); // Compute the vanishing polynomial. let p_inner_product = p_a_vec @@ -66,20 +70,23 @@ impl FieldInnerProductCols { self.result = p_result.into(); self.carry = p_carry.into(); - self.witness_low = p_witness_low.try_into().unwrap(); - self.witness_high = p_witness_high.try_into().unwrap(); + self.witness_low = Limbs(p_witness_low.try_into().unwrap()); + self.witness_high = Limbs(p_witness_high.try_into().unwrap()); result.clone() } } -impl FieldInnerProductCols { +impl FieldInnerProductCols +where + Limbs: Copy, +{ #[allow(unused_variables)] - pub fn eval, P: FieldParameters>( + pub fn eval>( &self, builder: &mut AB, - a: &[Limbs], - b: &[Limbs], + a: &[Limbs], + b: &[Limbs], ) where V: Into, { @@ -103,8 +110,8 @@ impl FieldInnerProductCols { let p_carry_mul_modulus = &p_carry * &p_limbs; let p_vanishing = &p_inner_product_minus_result - &(&p_carry * &p_limbs); - let p_witness_low = self.witness_low.iter().into(); - let p_witness_high = self.witness_high.iter().into(); + let p_witness_low = self.witness_low.0.iter().into(); + let p_witness_high = self.witness_high.0.iter().into(); eval_field_operation::(builder, &p_vanishing, &p_witness_low, &p_witness_high); } @@ -137,13 +144,13 @@ mod tests { use sp1_derive::AlignedBorrow; #[derive(AlignedBorrow, Debug, Clone)] - pub struct TestCols { - pub a: [Limbs; 1], - pub b: [Limbs; 1], - pub a_ip_b: FieldInnerProductCols, + pub struct TestCols { + pub a: [Limbs; 1], + pub b: [Limbs; 1], + pub a_ip_b: FieldInnerProductCols, } - pub const NUM_TEST_COLS: usize = size_of::>(); + pub const NUM_TEST_COLS: usize = size_of::>(); struct FieldIpChip { pub _phantom: std::marker::PhantomData

, @@ -189,10 +196,10 @@ mod tests { .iter() .map(|(a, b)| { let mut row = [F::zero(); NUM_TEST_COLS]; - let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); - cols.a[0] = P::to_limbs_field::(&a[0]); - cols.b[0] = P::to_limbs_field::(&b[0]); - cols.a_ip_b.populate::

(a, b); + let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); + cols.a[0] = P::to_limbs_field::(&a[0]); + cols.b[0] = P::to_limbs_field::(&b[0]); + cols.a_ip_b.populate(a, b); row }) .collect::>(); @@ -222,11 +229,12 @@ mod tests { impl Air for FieldIpChip

where AB: SP1AirBuilder, + Limbs: Copy, { fn eval(&self, builder: &mut AB) { let main = builder.main(); - let local: &TestCols = main.row_slice(0).borrow(); - local.a_ip_b.eval::(builder, &local.a, &local.b); + let local: &TestCols = main.row_slice(0).borrow(); + local.a_ip_b.eval::(builder, &local.a, &local.b); // A dummy constraint to keep the degree 3. builder.assert_zero( diff --git a/core/src/operations/field/field_op.rs b/core/src/operations/field/field_op.rs index 35a3cf87e9..2ce6047584 100644 --- a/core/src/operations/field/field_op.rs +++ b/core/src/operations/field/field_op.rs @@ -1,10 +1,10 @@ use super::params::Limbs; -use super::params::NUM_WITNESS_LIMBS; use super::util::{compute_root_quotient_and_shift, split_u16_limbs_to_u8_limbs}; use super::util_air::eval_field_operation; use crate::air::Polynomial; use crate::air::SP1AirBuilder; use crate::utils::ec::field::FieldParameters; + use num::{BigUint, Zero}; use p3_air::AirBuilder; use p3_field::PrimeField32; @@ -24,21 +24,16 @@ pub enum FieldOperation { /// or made generic in the future. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FieldOpCols { +pub struct FieldOpCols { /// The result of `a op b`, where a, b are field elements - pub result: Limbs, - pub(crate) carry: Limbs, - pub(crate) witness_low: [T; NUM_WITNESS_LIMBS], - pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], + pub result: Limbs, + pub(crate) carry: Limbs, + pub(crate) witness_low: Limbs, + pub(crate) witness_high: Limbs, } -impl FieldOpCols { - pub fn populate( - &mut self, - a: &BigUint, - b: &BigUint, - op: FieldOperation, - ) -> BigUint { +impl FieldOpCols { + pub fn populate(&mut self, a: &BigUint, b: &BigUint, op: FieldOperation) -> BigUint { if b == &BigUint::zero() && op == FieldOperation::Div { // Division by 0 is allowed only when dividing 0 so that padded rows can be all 0. assert_eq!( @@ -58,8 +53,8 @@ impl FieldOpCols { // to contain the result by the user. // Note that this reversal means we have to flip result, a correspondingly in // the `eval` function. - self.populate::

(&result, b, FieldOperation::Add); - self.result = P::to_limbs_field::(&result); + self.populate(&result, b, FieldOperation::Add); + self.result = P::to_limbs_field::(&result); return result; } @@ -75,13 +70,13 @@ impl FieldOpCols { // multiplication because those columns are expected to contain the result by the user. // Note that this reversal means we have to flip result, a correspondingly in the `eval` // function. - self.populate::

(&result, b, FieldOperation::Mul); - self.result = P::to_limbs_field::(&result); + self.populate(&result, b, FieldOperation::Mul); + self.result = P::to_limbs_field::(&result); return result; } - let p_a: Polynomial = P::to_limbs_field::(a).into(); - let p_b: Polynomial = P::to_limbs_field::(b).into(); + let p_a: Polynomial = P::to_limbs_field::(a).into(); + let p_b: Polynomial = P::to_limbs_field::(b).into(); // Compute field addition in the integers. let modulus = &P::modulus(); @@ -99,9 +94,9 @@ impl FieldOpCols { } // Make little endian polynomial limbs. - let p_modulus: Polynomial = P::to_limbs_field::(modulus).into(); - let p_result: Polynomial = P::to_limbs_field::(&result).into(); - let p_carry: Polynomial = P::to_limbs_field::(&carry).into(); + let p_modulus: Polynomial = P::to_limbs_field::(modulus).into(); + let p_result: Polynomial = P::to_limbs_field::(&result).into(); + let p_carry: Polynomial = P::to_limbs_field::(&carry).into(); // Compute the vanishing polynomial. let p_op = match op { @@ -121,18 +116,18 @@ impl FieldOpCols { self.result = p_result.into(); self.carry = p_carry.into(); - self.witness_low = p_witness_low.try_into().unwrap(); - self.witness_high = p_witness_high.try_into().unwrap(); + + self.witness_low = Limbs(p_witness_low.try_into().unwrap()); + self.witness_high = Limbs(p_witness_high.try_into().unwrap()); result } } -impl FieldOpCols { +impl FieldOpCols { #[allow(unused_variables)] pub fn eval< AB: SP1AirBuilder, - P: FieldParameters, A: Into> + Clone, B: Into> + Clone, >( @@ -143,6 +138,7 @@ impl FieldOpCols { op: FieldOperation, ) where V: Into, + Limbs: Copy, { let p_a_param: Polynomial = (*a).clone().into(); let p_b: Polynomial = (*b).clone().into(); @@ -159,8 +155,8 @@ impl FieldOpCols { let p_op_minus_result: Polynomial = p_op - p_result; let p_limbs = Polynomial::from_iter(P::modulus_field_iter::().map(AB::Expr::from)); let p_vanishing = p_op_minus_result - &(&p_carry * &p_limbs); - let p_witness_low = self.witness_low.iter().into(); - let p_witness_high = self.witness_high.iter().into(); + let p_witness_low = self.witness_low.0.iter().into(); + let p_witness_high = self.witness_high.0.iter().into(); eval_field_operation::(builder, &p_vanishing, &p_witness_low, &p_witness_high); } } @@ -178,11 +174,13 @@ mod tests { use crate::stark::StarkGenericConfig; use crate::utils::ec::edwards::ed25519::Ed25519BaseField; use crate::utils::ec::field::FieldParameters; - use crate::utils::{pad_to_power_of_two, BabyBearPoseidon2}; - use crate::utils::{uni_stark_prove as prove, uni_stark_verify as verify}; + use crate::utils::ec::weierstrass::secp256k1::Secp256k1BaseField; + use crate::utils::{ + pad_to_power_of_two, uni_stark_prove as prove, uni_stark_verify as verify, + BabyBearPoseidon2, + }; use crate::{air::SP1AirBuilder, runtime::ExecutionRecord}; use core::borrow::{Borrow, BorrowMut}; - use core::mem::size_of; use num::bigint::RandBigInt; use p3_air::Air; use p3_baby_bear::BabyBear; @@ -190,15 +188,16 @@ mod tests { use p3_matrix::MatrixRowSlices; use rand::thread_rng; use sp1_derive::AlignedBorrow; + use std::mem::size_of; #[derive(AlignedBorrow, Debug, Clone)] - pub struct TestCols { - pub a: Limbs, - pub b: Limbs, - pub a_op_b: FieldOpCols, + pub struct TestCols { + pub a: Limbs, + pub b: Limbs, + pub a_op_b: FieldOpCols, } - pub const NUM_TEST_COLS: usize = size_of::>(); + pub const NUM_TEST_COLS: usize = size_of::>(); struct FieldOpChip { pub operation: FieldOperation, @@ -250,10 +249,10 @@ mod tests { .iter() .map(|(a, b)| { let mut row = [F::zero(); NUM_TEST_COLS]; - let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); - cols.a = P::to_limbs_field::(a); - cols.b = P::to_limbs_field::(b); - cols.a_op_b.populate::

(a, b, self.operation); + let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); + cols.a = P::to_limbs_field::(a); + cols.b = P::to_limbs_field::(b); + cols.a_op_b.populate(a, b, self.operation); row }) .collect::>(); @@ -283,13 +282,14 @@ mod tests { impl Air for FieldOpChip

where AB: SP1AirBuilder, + Limbs: Copy, { fn eval(&self, builder: &mut AB) { let main = builder.main(); - let local: &TestCols = main.row_slice(0).borrow(); + let local: &TestCols = main.row_slice(0).borrow(); local .a_op_b - .eval::(builder, &local.a, &local.b, self.operation); + .eval::(builder, &local.a, &local.b, self.operation); // A dummy constraint to keep the degree 3. builder.assert_zero( @@ -332,6 +332,7 @@ mod tests { let mut challenger = config.challenger(); + // TODO: test with other fields let chip: FieldOpChip = FieldOpChip::new(*op); let shard = ExecutionRecord::default(); let trace: RowMajorMatrix = diff --git a/core/src/operations/field/field_sqrt.rs b/core/src/operations/field/field_sqrt.rs index 234befdb5f..2442f078a5 100644 --- a/core/src/operations/field/field_sqrt.rs +++ b/core/src/operations/field/field_sqrt.rs @@ -11,48 +11,44 @@ use std::fmt::Debug; /// limb lives. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FieldSqrtCols { +pub struct FieldSqrtCols { /// The multiplication operation to verify that the sqrt and the input match. /// /// In order to save space, we actually store the sqrt of the input in `multiplication.result` /// since we'll receive the input again in the `eval` function. - pub multiplication: FieldOpCols, + pub multiplication: FieldOpCols, } -impl FieldSqrtCols { +impl FieldSqrtCols { /// Populates the trace. /// /// `P` is the parameter of the field that each limb lives in. - pub fn populate( - &mut self, - a: &BigUint, - sqrt_fn: impl Fn(&BigUint) -> BigUint, - ) -> BigUint { + pub fn populate(&mut self, a: &BigUint, sqrt_fn: impl Fn(&BigUint) -> BigUint) -> BigUint { let sqrt = sqrt_fn(a); // Use FieldOpCols to compute result * result. let sqrt_squared = self.multiplication - .populate::

(&sqrt, &sqrt, super::field_op::FieldOperation::Mul); + .populate(&sqrt, &sqrt, super::field_op::FieldOperation::Mul); // If the result is indeed the square root of a, then result * result = a. assert_eq!(sqrt_squared, a.clone()); // This is a hack to save a column in FieldSqrtCols. We will receive the value a again in the // eval function, so we'll overwrite it with the sqrt. - self.multiplication.result = P::to_limbs_field::(&sqrt); + self.multiplication.result = P::to_limbs_field::(&sqrt); sqrt } } -impl FieldSqrtCols { +impl FieldSqrtCols +where + Limbs: Copy, +{ /// Calculates the square root of `a`. - pub fn eval, P: FieldParameters>( - &self, - builder: &mut AB, - a: &Limbs, - ) where + pub fn eval>(&self, builder: &mut AB, a: &Limbs) + where V: Into, { // As a space-saving hack, we store the sqrt of the input in `self.multiplication.result` @@ -63,7 +59,7 @@ impl FieldSqrtCols { multiplication.result = *a; // Compute sqrt * sqrt. We pass in P since we want its BaseField to be the mod. - multiplication.eval::, Limbs>( + multiplication.eval::, Limbs>( builder, &sqrt, &sqrt, @@ -97,13 +93,14 @@ mod tests { use p3_matrix::MatrixRowSlices; use rand::thread_rng; use sp1_derive::AlignedBorrow; + #[derive(AlignedBorrow, Debug, Clone)] - pub struct TestCols { - pub a: Limbs, - pub sqrt: FieldSqrtCols, + pub struct TestCols { + pub a: Limbs, + pub sqrt: FieldSqrtCols, } - pub const NUM_TEST_COLS: usize = size_of::>(); + pub const NUM_TEST_COLS: usize = size_of::>(); struct EdSqrtChip { pub _phantom: std::marker::PhantomData

, @@ -148,9 +145,9 @@ mod tests { .iter() .map(|a| { let mut row = [F::zero(); NUM_TEST_COLS]; - let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); - cols.a = P::to_limbs_field::(a); - cols.sqrt.populate::

(a, ed25519_sqrt); + let cols: &mut TestCols = row.as_mut_slice().borrow_mut(); + cols.a = P::to_limbs_field::(a); + cols.sqrt.populate(a, ed25519_sqrt); row }) .collect::>(); @@ -180,13 +177,14 @@ mod tests { impl Air for EdSqrtChip

where AB: SP1AirBuilder, + Limbs: Copy, { fn eval(&self, builder: &mut AB) { let main = builder.main(); - let local: &TestCols = main.row_slice(0).borrow(); + let local: &TestCols = main.row_slice(0).borrow(); // eval verifies that local.sqrt.result is indeed the square root of local.a. - local.sqrt.eval::(builder, &local.a); + local.sqrt.eval::(builder, &local.a); // A dummy constraint to keep the degree 3. builder.assert_zero( diff --git a/core/src/operations/field/params.rs b/core/src/operations/field/params.rs index 605a9a0869..020e42f26b 100644 --- a/core/src/operations/field/params.rs +++ b/core/src/operations/field/params.rs @@ -1,16 +1,32 @@ use crate::air::Polynomial; +use generic_array::{ArrayLength, GenericArray}; use std::fmt::Debug; use std::ops::Index; use std::slice::Iter; +use std::usize; -pub const NUM_LIMBS: usize = 32; pub const NB_BITS_PER_LIMB: usize = 8; -pub const NUM_WITNESS_LIMBS: usize = 2 * NUM_LIMBS - 2; -#[derive(Default, Debug, Clone, Copy)] -pub struct Limbs(pub [T; NUM_LIMBS]); +#[derive(Debug, Clone)] +/// An array representing N limbs of T. +/// +/// GenericArray allows us to constrain the correct array lengths so we can have # of limbs and # of +/// witness limbs associated in NumLimbs / FieldParameters. +/// See: https://github.com/RustCrypto/traits/issues/1481 +pub struct Limbs(pub GenericArray); + +impl Copy for Limbs where N::ArrayType: Copy {} + +impl Default for Limbs +where + T: Default + Copy, +{ + fn default() -> Self { + Self(GenericArray::default()) + } +} -impl Index for Limbs { +impl Index for Limbs { type Output = T; fn index(&self, index: usize) -> &Self::Output { @@ -18,17 +34,19 @@ impl Index for Limbs { } } -impl IntoIterator for Limbs { +impl IntoIterator for Limbs { type Item = T; - type IntoIter = std::array::IntoIter; + type IntoIter = as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } -impl + Clone, Expr: Clone> From> for Polynomial { - fn from(value: Limbs) -> Self { +impl + Clone, N: ArrayLength, Expr: Clone> From> + for Polynomial +{ + fn from(value: Limbs) -> Self { Polynomial::from_coefficients(&value.0.into_iter().map(|x| x.into()).collect::>()) } } @@ -39,14 +57,14 @@ impl<'a, Var: Into + Clone, Expr: Clone> From> for Polynomia } } -impl From> for Limbs { +impl From> for Limbs { fn from(value: Polynomial) -> Self { let inner = value.as_coefficients().try_into().unwrap(); Self(inner) } } -impl<'a, T: Debug + Default + Clone> From> for Limbs { +impl<'a, T: Debug + Default + Clone, N: ArrayLength> From> for Limbs { fn from(value: Iter<'a, T>) -> Self { let vec: Vec = value.cloned().collect(); let inner = vec.try_into().unwrap(); @@ -63,7 +81,7 @@ mod tests { #[test] fn test_modulus() { // Convert the MODULUS array to BigUint - let array_modulus = BigUint::from_bytes_le(&Ed25519BaseField::MODULUS); + let array_modulus = BigUint::from_bytes_le(Ed25519BaseField::MODULUS); // Get the modulus from the function let func_modulus = Ed25519BaseField::modulus(); diff --git a/core/src/syscall/precompiles/edwards/ed_add.rs b/core/src/syscall/precompiles/edwards/ed_add.rs index 577560f716..8bd76b0bfe 100644 --- a/core/src/syscall/precompiles/edwards/ed_add.rs +++ b/core/src/syscall/precompiles/edwards/ed_add.rs @@ -8,13 +8,12 @@ use crate::operations::field::field_den::FieldDenCols; use crate::operations::field::field_inner_product::FieldInnerProductCols; use crate::operations::field::field_op::FieldOpCols; use crate::operations::field::field_op::FieldOperation; -use crate::operations::field::params::Limbs; -use crate::operations::field::params::NUM_LIMBS; use crate::runtime::ExecutionRecord; use crate::runtime::Syscall; use crate::runtime::SyscallCode; use crate::syscall::precompiles::create_ec_add_event; use crate::syscall::precompiles::SyscallContext; +use crate::utils::ec::edwards::ed25519::Ed25519BaseField; use crate::utils::ec::edwards::EdwardsParameters; use crate::utils::ec::field::FieldParameters; use crate::utils::ec::AffinePoint; @@ -51,16 +50,16 @@ pub struct EdAddAssignCols { pub clk: T, pub p_ptr: T, pub q_ptr: T, - pub p_access: [MemoryWriteCols; 16], - pub q_access: [MemoryReadCols; 16], - pub(crate) x3_numerator: FieldInnerProductCols, - pub(crate) y3_numerator: FieldInnerProductCols, - pub(crate) x1_mul_y1: FieldOpCols, - pub(crate) x2_mul_y2: FieldOpCols, - pub(crate) f: FieldOpCols, - pub(crate) d_mul_f: FieldOpCols, - pub(crate) x3_ins: FieldDenCols, - pub(crate) y3_ins: FieldDenCols, + pub p_access: [MemoryWriteCols; Ed25519BaseField::NB_LIMBS], + pub q_access: [MemoryReadCols; Ed25519BaseField::NB_LIMBS], + pub(crate) x3_numerator: FieldInnerProductCols, + pub(crate) y3_numerator: FieldInnerProductCols, + pub(crate) x1_mul_y1: FieldOpCols, + pub(crate) x2_mul_y2: FieldOpCols, + pub(crate) f: FieldOpCols, + pub(crate) d_mul_f: FieldOpCols, + pub(crate) x3_ins: FieldDenCols, + pub(crate) y3_ins: FieldDenCols, } #[derive(Default)] @@ -83,29 +82,19 @@ impl EdAddAssignChip { ) { let x3_numerator = cols .x3_numerator - .populate::(&[p_x.clone(), q_x.clone()], &[q_y.clone(), p_y.clone()]); + .populate(&[p_x.clone(), q_x.clone()], &[q_y.clone(), p_y.clone()]); let y3_numerator = cols .y3_numerator - .populate::(&[p_y.clone(), p_x.clone()], &[q_y.clone(), q_x.clone()]); - let x1_mul_y1 = cols - .x1_mul_y1 - .populate::(&p_x, &p_y, FieldOperation::Mul); - let x2_mul_y2 = cols - .x2_mul_y2 - .populate::(&q_x, &q_y, FieldOperation::Mul); - let f = cols - .f - .populate::(&x1_mul_y1, &x2_mul_y2, FieldOperation::Mul); + .populate(&[p_y.clone(), p_x.clone()], &[q_y.clone(), q_x.clone()]); + let x1_mul_y1 = cols.x1_mul_y1.populate(&p_x, &p_y, FieldOperation::Mul); + let x2_mul_y2 = cols.x2_mul_y2.populate(&q_x, &q_y, FieldOperation::Mul); + let f = cols.f.populate(&x1_mul_y1, &x2_mul_y2, FieldOperation::Mul); let d = E::d_biguint(); - let d_mul_f = cols - .d_mul_f - .populate::(&f, &d, FieldOperation::Mul); - - cols.x3_ins - .populate::(&x3_numerator, &d_mul_f, true); - cols.y3_ins - .populate::(&y3_numerator, &d_mul_f, false); + let d_mul_f = cols.d_mul_f.populate(&f, &d, FieldOperation::Mul); + + cols.x3_ins.populate(&x3_numerator, &d_mul_f, true); + cols.y3_ins.populate(&y3_numerator, &d_mul_f, false); } } @@ -220,45 +209,42 @@ where let y2 = limbs_from_prev_access(&row.q_access[8..16]); // x3_numerator = x1 * y2 + x2 * y1. - row.x3_numerator - .eval::(builder, &[x1, x2], &[y2, y1]); + row.x3_numerator.eval::(builder, &[x1, x2], &[y2, y1]); // y3_numerator = y1 * y2 + x1 * x2. - row.y3_numerator - .eval::(builder, &[y1, x1], &[y2, x2]); + row.y3_numerator.eval::(builder, &[y1, x1], &[y2, x2]); // f = x1 * x2 * y1 * y2. row.x1_mul_y1 - .eval::(builder, &x1, &y1, FieldOperation::Mul); + .eval::(builder, &x1, &y1, FieldOperation::Mul); row.x2_mul_y2 - .eval::(builder, &x2, &y2, FieldOperation::Mul); + .eval::(builder, &x2, &y2, FieldOperation::Mul); let x1_mul_y1 = row.x1_mul_y1.result; let x2_mul_y2 = row.x2_mul_y2.result; row.f - .eval::(builder, &x1_mul_y1, &x2_mul_y2, FieldOperation::Mul); + .eval::(builder, &x1_mul_y1, &x2_mul_y2, FieldOperation::Mul); // d * f. let f = row.f.result; let d_biguint = E::d_biguint(); - let d_const = E::BaseField::to_limbs_field::(&d_biguint); - let d_const_expr = Limbs::(d_const.0.map(|x| x.into())); + let d_const = E::BaseField::to_limbs_field::(&d_biguint); row.d_mul_f - .eval::(builder, &f, &d_const_expr, FieldOperation::Mul); + .eval::(builder, &f, &d_const, FieldOperation::Mul); let d_mul_f = row.d_mul_f.result; // x3 = x3_numerator / (1 + d * f). row.x3_ins - .eval::(builder, &row.x3_numerator.result, &d_mul_f, true); + .eval::(builder, &row.x3_numerator.result, &d_mul_f, true); // y3 = y3_numerator / (1 - d * f). row.y3_ins - .eval::(builder, &row.y3_numerator.result, &d_mul_f, false); + .eval::(builder, &row.y3_numerator.result, &d_mul_f, false); // Constraint self.p_access.value = [self.x3_ins.result, self.y3_ins.result] // This is to ensure that p_access is updated with the new value. - for i in 0..NUM_LIMBS { + for i in 0..E::BaseField::NB_LIMBS { builder .when(row.is_real) .assert_eq(row.x3_ins.result[i], row.p_access[i / 4].value()[i % 4]); diff --git a/core/src/syscall/precompiles/edwards/ed_decompress.rs b/core/src/syscall/precompiles/edwards/ed_decompress.rs index cf07865c93..c4da5fccbf 100644 --- a/core/src/syscall/precompiles/edwards/ed_decompress.rs +++ b/core/src/syscall/precompiles/edwards/ed_decompress.rs @@ -6,6 +6,7 @@ use crate::memory::MemoryWriteCols; use crate::operations::field::field_op::FieldOpCols; use crate::operations::field::field_op::FieldOperation; use crate::operations::field::field_sqrt::FieldSqrtCols; +use crate::operations::field::params::Limbs; use crate::runtime::ExecutionRecord; use crate::runtime::MemoryReadRecord; use crate::runtime::MemoryWriteRecord; @@ -15,8 +16,10 @@ use crate::syscall::precompiles::SyscallContext; use crate::utils::bytes_to_words_le; use crate::utils::ec::edwards::ed25519::decompress; use crate::utils::ec::edwards::ed25519::ed25519_sqrt; +use crate::utils::ec::edwards::ed25519::Ed25519BaseField; use crate::utils::ec::edwards::EdwardsParameters; use crate::utils::ec::field::FieldParameters; +use crate::utils::ec::field::NumWords; use crate::utils::ec::COMPRESSED_POINT_BYTES; use crate::utils::ec::NUM_BYTES_FIELD_ELEMENT; use crate::utils::ec::NUM_WORDS_FIELD_ELEMENT; @@ -27,6 +30,7 @@ use crate::utils::words_to_bytes_le; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; use curve25519_dalek::edwards::CompressedEdwardsY; +use generic_array::GenericArray; use num::BigUint; use num::One; use num::Zero; @@ -37,6 +41,8 @@ use p3_matrix::MatrixRowSlices; use serde::Deserialize; use serde::Serialize; use std::marker::PhantomData; +use typenum::Unsigned; +use typenum::U32; use p3_matrix::dense::RowMajorMatrix; use sp1_derive::AlignedBorrow; @@ -50,8 +56,10 @@ pub struct EdDecompressEvent { pub sign: bool, pub y_bytes: [u8; COMPRESSED_POINT_BYTES], pub decompressed_x_bytes: [u8; NUM_BYTES_FIELD_ELEMENT], - pub x_memory_records: [MemoryWriteRecord; NUM_WORDS_FIELD_ELEMENT], - pub y_memory_records: [MemoryReadRecord; NUM_WORDS_FIELD_ELEMENT], + pub x_memory_records: + [MemoryWriteRecord; <::WordsFieldElement as Unsigned>::USIZE], + pub y_memory_records: + [MemoryReadRecord; <::WordsFieldElement as Unsigned>::USIZE], } pub const NUM_ED_DECOMPRESS_COLS: usize = size_of::>(); @@ -69,15 +77,17 @@ pub struct EdDecompressCols { pub clk: T, pub ptr: T, pub sign: T, - pub x_access: [MemoryWriteCols; NUM_WORDS_FIELD_ELEMENT], - pub y_access: [MemoryReadCols; NUM_WORDS_FIELD_ELEMENT], - pub(crate) yy: FieldOpCols, - pub(crate) u: FieldOpCols, - pub(crate) dyy: FieldOpCols, - pub(crate) v: FieldOpCols, - pub(crate) u_div_v: FieldOpCols, - pub(crate) x: FieldSqrtCols, - pub(crate) neg_x: FieldOpCols, + pub x_access: + GenericArray, ::WordsFieldElement>, + pub y_access: + GenericArray, ::WordsFieldElement>, + pub(crate) yy: FieldOpCols, + pub(crate) u: FieldOpCols, + pub(crate) dyy: FieldOpCols, + pub(crate) v: FieldOpCols, + pub(crate) u_div_v: FieldOpCols, + pub(crate) x: FieldSqrtCols, + pub(crate) neg_x: FieldOpCols, } impl EdDecompressCols { @@ -98,23 +108,21 @@ impl EdDecompressCols { } let y = &BigUint::from_bytes_le(&event.y_bytes); - self.populate_field_ops::(y); + self.populate_field_ops::(y); record.add_byte_lookup_events(new_byte_lookup_events); } - fn populate_field_ops(&mut self, y: &BigUint) { + fn populate_field_ops(&mut self, y: &BigUint) { let one = BigUint::one(); - let yy = self.yy.populate::

(y, y, FieldOperation::Mul); - let u = self.u.populate::

(&yy, &one, FieldOperation::Sub); - let dyy = self - .dyy - .populate::

(&E::d_biguint(), &yy, FieldOperation::Mul); - let v = self.v.populate::

(&one, &dyy, FieldOperation::Add); - let u_div_v = self.u_div_v.populate::

(&u, &v, FieldOperation::Div); - let x = self.x.populate::

(&u_div_v, ed25519_sqrt); + let yy = self.yy.populate(y, y, FieldOperation::Mul); + let u = self.u.populate(&yy, &one, FieldOperation::Sub); + let dyy = self.dyy.populate(&E::d_biguint(), &yy, FieldOperation::Mul); + let v = self.v.populate(&one, &dyy, FieldOperation::Add); + let u_div_v = self.u_div_v.populate(&u, &v, FieldOperation::Div); + let x = self.x.populate(&u_div_v, ed25519_sqrt); self.neg_x - .populate::

(&BigUint::zero(), &x, FieldOperation::Sub); + .populate(&BigUint::zero(), &x, FieldOperation::Sub); } } @@ -127,33 +135,29 @@ impl EdDecompressCols { { builder.assert_bool(self.sign); - let y = limbs_from_prev_access(&self.y_access); + let y: Limbs = limbs_from_prev_access(&self.y_access); self.yy - .eval::(builder, &y, &y, FieldOperation::Mul); - self.u.eval::( + .eval::(builder, &y, &y, FieldOperation::Mul); + self.u.eval::( builder, &self.yy.result, &[AB::Expr::one()].iter(), FieldOperation::Sub, ); let d_biguint = E::d_biguint(); - let d_const = E::BaseField::to_limbs_field::(&d_biguint); + let d_const = E::BaseField::to_limbs_field::(&d_biguint); self.dyy - .eval::(builder, &d_const, &self.yy.result, FieldOperation::Mul); - self.v.eval::( + .eval::(builder, &d_const, &self.yy.result, FieldOperation::Mul); + self.v.eval::( builder, &[AB::Expr::one()].iter(), &self.dyy.result, FieldOperation::Add, ); - self.u_div_v.eval::( - builder, - &self.u.result, - &self.v.result, - FieldOperation::Div, - ); - self.x.eval::(builder, &self.u_div_v.result); - self.neg_x.eval::( + self.u_div_v + .eval::(builder, &self.u.result, &self.v.result, FieldOperation::Div); + self.x.eval::(builder, &self.u_div_v.result); + self.neg_x.eval::( builder, &[AB::Expr::zero()].iter(), &self.x.multiplication.result, @@ -179,7 +183,7 @@ impl EdDecompressCols { ); } - let x_limbs = limbs_from_access(&self.x_access); + let x_limbs: Limbs = limbs_from_access(&self.x_access); builder .when(self.is_real) .when(self.sign) @@ -298,7 +302,7 @@ impl MachineAir for EdDecompressChip = row.as_mut_slice().borrow_mut(); let zero = BigUint::zero(); - cols.populate_field_ops::(&zero); + cols.populate_field_ops::(&zero); row }); diff --git a/core/src/syscall/precompiles/k256/decompress.rs b/core/src/syscall/precompiles/k256/decompress.rs index 50fde52d62..9e8395d3e5 100644 --- a/core/src/syscall/precompiles/k256/decompress.rs +++ b/core/src/syscall/precompiles/k256/decompress.rs @@ -7,6 +7,7 @@ use crate::memory::MemoryReadWriteCols; use crate::operations::field::field_op::FieldOpCols; use crate::operations::field::field_op::FieldOperation; use crate::operations::field::field_sqrt::FieldSqrtCols; +use crate::operations::field::params::Limbs; use crate::runtime::ExecutionRecord; use crate::runtime::MemoryReadRecord; use crate::runtime::MemoryWriteRecord; @@ -14,6 +15,7 @@ use crate::runtime::Syscall; use crate::runtime::SyscallCode; use crate::syscall::precompiles::SyscallContext; use crate::utils::bytes_to_words_le; + use crate::utils::ec::field::FieldParameters; use crate::utils::ec::weierstrass::secp256k1::secp256k1_sqrt; use crate::utils::ec::weierstrass::secp256k1::Secp256k1BaseField; @@ -40,6 +42,7 @@ use p3_field::PrimeField32; use p3_matrix::MatrixRowSlices; use serde::{Deserialize, Serialize}; use std::str::FromStr; +use typenum::U32; use p3_matrix::dense::RowMajorMatrix; use sp1_derive::AlignedBorrow; @@ -136,11 +139,11 @@ pub struct K256DecompressCols { pub is_odd: T, pub x_access: [MemoryReadCols; NUM_WORDS_FIELD_ELEMENT], pub y_access: [MemoryReadWriteCols; NUM_WORDS_FIELD_ELEMENT], - pub(crate) x_2: FieldOpCols, - pub(crate) x_3: FieldOpCols, - pub(crate) x_3_plus_b: FieldOpCols, - pub(crate) y: FieldSqrtCols, - pub(crate) neg_y: FieldOpCols, + pub(crate) x_2: FieldOpCols, + pub(crate) x_3: FieldOpCols, + pub(crate) x_3_plus_b: FieldOpCols, + pub(crate) y: FieldSqrtCols, + pub(crate) neg_y: FieldOpCols, pub(crate) y_least_bits: [T; 8], } @@ -165,22 +168,15 @@ impl K256DecompressCols { fn populate_field_ops(&mut self, x: &BigUint) { // Y = sqrt(x^3 + b) - let x_2 = - self.x_2 - .populate::(&x.clone(), &x.clone(), FieldOperation::Mul); - let x_3 = self - .x_3 - .populate::(&x_2, x, FieldOperation::Mul); + let x_2 = self + .x_2 + .populate(&x.clone(), &x.clone(), FieldOperation::Mul); + let x_3 = self.x_3.populate(&x_2, x, FieldOperation::Mul); let b = Secp256k1Parameters::b_int(); - let x_3_plus_b = - self.x_3_plus_b - .populate::(&x_3, &b, FieldOperation::Add); - let y = self - .y - .populate::(&x_3_plus_b, secp256k1_sqrt); + let x_3_plus_b = self.x_3_plus_b.populate(&x_3, &b, FieldOperation::Add); + let y = self.y.populate(&x_3_plus_b, secp256k1_sqrt); let zero = BigUint::zero(); - self.neg_y - .populate::(&zero, &y, FieldOperation::Sub); + self.neg_y.populate(&zero, &y, FieldOperation::Sub); // Decompose bits of least significant Y byte let y_bytes = y.to_bytes_le(); let y_lsb = if y_bytes.is_empty() { 0 } else { y_bytes[0] }; @@ -197,26 +193,17 @@ impl K256DecompressCols { { builder.assert_bool(self.is_odd); - let x = limbs_from_prev_access(&self.x_access); + let x: Limbs = limbs_from_prev_access(&self.x_access); self.x_2 - .eval::(builder, &x, &x, FieldOperation::Mul); - self.x_3.eval::( - builder, - &self.x_2.result, - &x, - FieldOperation::Mul, - ); + .eval::(builder, &x, &x, FieldOperation::Mul); + self.x_3 + .eval::(builder, &self.x_2.result, &x, FieldOperation::Mul); let b = Secp256k1Parameters::b_int(); - let b_const = Secp256k1BaseField::to_limbs_field::(&b); - self.x_3_plus_b.eval::( - builder, - &self.x_3.result, - &b_const, - FieldOperation::Add, - ); - self.y - .eval::(builder, &self.x_3_plus_b.result); - self.neg_y.eval::( + let b_const = Secp256k1BaseField::to_limbs_field::(&b); + self.x_3_plus_b + .eval::(builder, &self.x_3.result, &b_const, FieldOperation::Add); + self.y.eval::(builder, &self.x_3_plus_b.result); + self.neg_y.eval::( builder, &[AB::Expr::zero()].iter(), &self.y.multiplication.result, @@ -244,7 +231,7 @@ impl K256DecompressCols { // When y_is_odd == should_be_odd, result is y // (Equivalent: y_is_odd != !should_be_odd) - let y_limbs = limbs_from_access(&self.y_access); + let y_limbs: Limbs = limbs_from_access(&self.y_access); builder .when(self.is_real) .when_ne(y_is_odd.into(), AB::Expr::one() - self.is_odd) diff --git a/core/src/syscall/precompiles/mod.rs b/core/src/syscall/precompiles/mod.rs index d0fd5f059a..bc2cbca9c1 100644 --- a/core/src/syscall/precompiles/mod.rs +++ b/core/src/syscall/precompiles/mod.rs @@ -5,13 +5,10 @@ pub mod keccak256; pub mod sha256; pub mod weierstrass; -use num::BigUint; use serde::{Deserialize, Serialize}; -use crate::air::SP1AirBuilder; -use crate::operations::field::params::Limbs; use crate::runtime::SyscallContext; -use crate::utils::ec::field::FieldParameters; + use crate::utils::ec::{AffinePoint, EllipticCurve}; use crate::{runtime::MemoryReadRecord, runtime::MemoryWriteRecord}; @@ -54,6 +51,7 @@ pub fn create_ec_add_event( let q_affine = AffinePoint::::from_words_le(&q); let result_affine = p_affine + q_affine; let result_words = result_affine.to_words_le(); + let p_memory_records = rt.mw_slice(p_ptr, &result_words).try_into().unwrap(); ECAddEvent { @@ -103,11 +101,3 @@ pub fn create_ec_double_event( p_memory_records, } } - -pub fn limbs_from_biguint(value: &BigUint) -> Limbs -where - AB: SP1AirBuilder, -{ - let a_const = F::to_limbs_field::(value); - Limbs::(a_const.0.map(|x| x.into())) -} diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs index a119ae474a..76dda57c32 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs @@ -5,21 +5,23 @@ use crate::memory::MemoryReadCols; use crate::memory::MemoryWriteCols; use crate::operations::field::field_op::FieldOpCols; use crate::operations::field::field_op::FieldOperation; -use crate::operations::field::params::NUM_LIMBS; +use crate::operations::field::params::Limbs; use crate::runtime::ExecutionRecord; use crate::runtime::Syscall; use crate::runtime::SyscallCode; use crate::syscall::precompiles::create_ec_add_event; use crate::syscall::precompiles::SyscallContext; +use crate::utils::ec::field::FieldParameters; +use crate::utils::ec::field::NumLimbs; +use crate::utils::ec::field::NumWords; use crate::utils::ec::weierstrass::WeierstrassParameters; use crate::utils::ec::AffinePoint; use crate::utils::ec::EllipticCurve; -use crate::utils::ec::NUM_WORDS_EC_POINT; -use crate::utils::ec::NUM_WORDS_FIELD_ELEMENT; use crate::utils::limbs_from_prev_access; use crate::utils::pad_rows; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; +use generic_array::GenericArray; use num::BigUint; use num::Zero; use p3_air::AirBuilder; @@ -31,8 +33,11 @@ use p3_matrix::MatrixRowSlices; use sp1_derive::AlignedBorrow; use std::fmt::Debug; use std::marker::PhantomData; +use typenum::Unsigned; -pub const NUM_WEIERSTRASS_ADD_COLS: usize = size_of::>(); +pub const fn num_weierstrass_add_cols() -> usize { + size_of::>() +} /// A set of columns to compute `WeierstrassAdd` that add two points on a Weierstrass curve. /// @@ -40,23 +45,23 @@ pub const NUM_WEIERSTRASS_ADD_COLS: usize = size_of:: { +pub struct WeierstrassAddAssignCols { pub is_real: T, pub shard: T, pub clk: T, pub p_ptr: T, pub q_ptr: T, - pub p_access: [MemoryWriteCols; NUM_WORDS_EC_POINT], - pub q_access: [MemoryReadCols; NUM_WORDS_EC_POINT], - pub(crate) slope_denominator: FieldOpCols, - pub(crate) slope_numerator: FieldOpCols, - pub(crate) slope: FieldOpCols, - pub(crate) slope_squared: FieldOpCols, - pub(crate) p_x_plus_q_x: FieldOpCols, - pub(crate) x3_ins: FieldOpCols, - pub(crate) p_x_minus_x: FieldOpCols, - pub(crate) y3_ins: FieldOpCols, - pub(crate) slope_times_p_x_minus_x: FieldOpCols, + pub p_access: GenericArray, P::WordsCurvePoint>, + pub q_access: GenericArray, P::WordsCurvePoint>, + pub(crate) slope_denominator: FieldOpCols, + pub(crate) slope_numerator: FieldOpCols, + pub(crate) slope: FieldOpCols, + pub(crate) slope_squared: FieldOpCols, + pub(crate) p_x_plus_q_x: FieldOpCols, + pub(crate) x3_ins: FieldOpCols, + pub(crate) p_x_minus_x: FieldOpCols, + pub(crate) y3_ins: FieldOpCols, + pub(crate) slope_times_p_x_minus_x: FieldOpCols, } #[derive(Default)] @@ -84,7 +89,7 @@ impl WeierstrassAddAssignChip { } fn populate_field_ops( - cols: &mut WeierstrassAddAssignCols, + cols: &mut WeierstrassAddAssignCols, p_x: BigUint, p_y: BigUint, q_x: BigUint, @@ -95,54 +100,44 @@ impl WeierstrassAddAssignChip { // slope = (q.y - p.y) / (q.x - p.x). let slope = { - let slope_numerator = - cols.slope_numerator - .populate::(&q_y, &p_y, FieldOperation::Sub); + let slope_numerator = cols + .slope_numerator + .populate(&q_y, &p_y, FieldOperation::Sub); let slope_denominator = cols.slope_denominator - .populate::(&q_x, &p_x, FieldOperation::Sub); + .populate(&q_x, &p_x, FieldOperation::Sub); - cols.slope.populate::( - &slope_numerator, - &slope_denominator, - FieldOperation::Div, - ) + cols.slope + .populate(&slope_numerator, &slope_denominator, FieldOperation::Div) }; // x = slope * slope - (p.x + q.x). let x = { - let slope_squared = - cols.slope_squared - .populate::(&slope, &slope, FieldOperation::Mul); - let p_x_plus_q_x = - cols.p_x_plus_q_x - .populate::(&p_x, &q_x, FieldOperation::Add); + let slope_squared = cols + .slope_squared + .populate(&slope, &slope, FieldOperation::Mul); + let p_x_plus_q_x = cols.p_x_plus_q_x.populate(&p_x, &q_x, FieldOperation::Add); cols.x3_ins - .populate::(&slope_squared, &p_x_plus_q_x, FieldOperation::Sub) + .populate(&slope_squared, &p_x_plus_q_x, FieldOperation::Sub) }; // y = slope * (p.x - x_3n) - p.y. { - let p_x_minus_x = - cols.p_x_minus_x - .populate::(&p_x, &x, FieldOperation::Sub); - let slope_times_p_x_minus_x = cols.slope_times_p_x_minus_x.populate::( - &slope, - &p_x_minus_x, - FieldOperation::Mul, - ); - cols.y3_ins.populate::( - &slope_times_p_x_minus_x, - &p_y, - FieldOperation::Sub, - ); + let p_x_minus_x = cols.p_x_minus_x.populate(&p_x, &x, FieldOperation::Sub); + let slope_times_p_x_minus_x = + cols.slope_times_p_x_minus_x + .populate(&slope, &p_x_minus_x, FieldOperation::Mul); + cols.y3_ins + .populate(&slope_times_p_x_minus_x, &p_y, FieldOperation::Sub); } } } impl MachineAir for WeierstrassAddAssignChip +where + [(); num_weierstrass_add_cols::()]:, { type Record = ExecutionRecord; @@ -160,9 +155,10 @@ impl MachineAir let mut new_byte_lookup_events = Vec::new(); for i in 0..input.weierstrass_add_events.len() { - let event = input.weierstrass_add_events[i].clone(); - let mut row = [F::zero(); NUM_WEIERSTRASS_ADD_COLS]; - let cols: &mut WeierstrassAddAssignCols = row.as_mut_slice().borrow_mut(); + let event = &input.weierstrass_add_events[i]; + let mut row = [F::zero(); num_weierstrass_add_cols::()]; + let cols: &mut WeierstrassAddAssignCols = + row.as_mut_slice().borrow_mut(); // Decode affine points. let p = &event.p; @@ -182,10 +178,10 @@ impl MachineAir Self::populate_field_ops(cols, p_x, p_y, q_x, q_y); // Populate the memory access columns. - for i in 0..NUM_WORDS_EC_POINT { + for i in 0..cols.q_access.len() { cols.q_access[i].populate(event.q_memory_records[i], &mut new_byte_lookup_events); } - for i in 0..NUM_WORDS_EC_POINT { + for i in 0..cols.p_access.len() { cols.p_access[i].populate(event.p_memory_records[i], &mut new_byte_lookup_events); } @@ -194,8 +190,9 @@ impl MachineAir output.add_byte_lookup_events(new_byte_lookup_events); pad_rows(&mut rows, || { - let mut row = [F::zero(); NUM_WEIERSTRASS_ADD_COLS]; - let cols: &mut WeierstrassAddAssignCols = row.as_mut_slice().borrow_mut(); + let mut row = [F::zero(); num_weierstrass_add_cols::()]; + let cols: &mut WeierstrassAddAssignCols = + row.as_mut_slice().borrow_mut(); let zero = BigUint::zero(); Self::populate_field_ops(cols, zero.clone(), zero.clone(), zero.clone(), zero); row @@ -204,7 +201,7 @@ impl MachineAir // Convert the trace to a row major matrix. RowMajorMatrix::new( rows.into_iter().flatten().collect::>(), - NUM_WEIERSTRASS_ADD_COLS, + num_weierstrass_add_cols::(), ) } @@ -215,89 +212,76 @@ impl MachineAir impl BaseAir for WeierstrassAddAssignChip { fn width(&self) -> usize { - NUM_WEIERSTRASS_ADD_COLS + num_weierstrass_add_cols::() } } impl Air for WeierstrassAddAssignChip where AB: SP1AirBuilder, + Limbs::Limbs>: Copy, { fn eval(&self, builder: &mut AB) { let main = builder.main(); - let row: &WeierstrassAddAssignCols = main.row_slice(0).borrow(); + let row: &WeierstrassAddAssignCols = main.row_slice(0).borrow(); + + let num_words_field_element = ::Limbs::USIZE / 4; - let p_x = limbs_from_prev_access(&row.p_access[0..NUM_WORDS_FIELD_ELEMENT]); - let p_y = limbs_from_prev_access(&row.p_access[NUM_WORDS_FIELD_ELEMENT..]); + let p_x = limbs_from_prev_access(&row.p_access[0..num_words_field_element]); + let p_y = limbs_from_prev_access(&row.p_access[num_words_field_element..]); - let q_x = limbs_from_prev_access(&row.q_access[0..NUM_WORDS_FIELD_ELEMENT]); - let q_y = limbs_from_prev_access(&row.q_access[NUM_WORDS_FIELD_ELEMENT..]); + let q_x = limbs_from_prev_access(&row.q_access[0..num_words_field_element]); + let q_y = limbs_from_prev_access(&row.q_access[num_words_field_element..]); // slope = (q.y - p.y) / (q.x - p.x). let slope = { - row.slope_numerator.eval::( - builder, - &q_y, - &p_y, - FieldOperation::Sub, - ); + row.slope_numerator + .eval::(builder, &q_y, &p_y, FieldOperation::Sub); - row.slope_denominator.eval::( - builder, - &q_x, - &p_x, - FieldOperation::Sub, - ); + row.slope_denominator + .eval::(builder, &q_x, &p_x, FieldOperation::Sub); - row.slope.eval::( + row.slope.eval::( builder, &row.slope_numerator.result, &row.slope_denominator.result, FieldOperation::Div, ); - row.slope.result + &row.slope.result }; // x = slope * slope - self.x - other.x. let x = { - row.slope_squared.eval::( - builder, - &slope, - &slope, - FieldOperation::Mul, - ); + row.slope_squared + .eval::(builder, slope, slope, FieldOperation::Mul); - row.p_x_plus_q_x.eval::( - builder, - &p_x, - &q_x, - FieldOperation::Add, - ); + row.p_x_plus_q_x + .eval::(builder, &p_x, &q_x, FieldOperation::Add); - row.x3_ins.eval::( + row.x3_ins.eval::( builder, &row.slope_squared.result, &row.p_x_plus_q_x.result, FieldOperation::Sub, ); - row.x3_ins.result + &row.x3_ins.result }; // y = slope * (p.x - x_3n) - q.y. { row.p_x_minus_x - .eval::(builder, &p_x, &x, FieldOperation::Sub); + .eval::(builder, &p_x, x, FieldOperation::Sub); - row.slope_times_p_x_minus_x.eval::( + row.slope_times_p_x_minus_x.eval::( builder, - &slope, + slope, &row.p_x_minus_x.result, FieldOperation::Mul, ); - row.y3_ins.eval::( + row.y3_ins.eval::( builder, &row.slope_times_p_x_minus_x.result, &p_y, @@ -307,7 +291,7 @@ where // Constraint self.p_access.value = [self.x3_ins.result, self.y3_ins.result]. This is to // ensure that p_access is updated with the new value. - for i in 0..NUM_LIMBS { + for i in 0..E::BaseField::NB_LIMBS { builder .when(row.is_real) .assert_eq(row.x3_ins.result[i], row.p_access[i / 4].value()[i % 4]); diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs index 07025c881c..9281dfa76f 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs @@ -4,23 +4,25 @@ use crate::memory::MemoryCols; use crate::memory::MemoryWriteCols; use crate::operations::field::field_op::FieldOpCols; use crate::operations::field::field_op::FieldOperation; -use crate::operations::field::params::NUM_LIMBS; +use crate::operations::field::params::Limbs; use crate::runtime::ExecutionRecord; use crate::runtime::Syscall; use crate::runtime::SyscallCode; use crate::stark::MachineRecord; use crate::syscall::precompiles::create_ec_double_event; -use crate::syscall::precompiles::limbs_from_biguint; use crate::syscall::precompiles::SyscallContext; +use crate::utils::ec::field::FieldParameters; +use crate::utils::ec::field::NumLimbs; +use crate::utils::ec::field::NumWords; use crate::utils::ec::weierstrass::WeierstrassParameters; use crate::utils::ec::AffinePoint; use crate::utils::ec::EllipticCurve; -use crate::utils::ec::NUM_WORDS_EC_POINT; use crate::utils::ec::NUM_WORDS_FIELD_ELEMENT; use crate::utils::limbs_from_prev_access; use crate::utils::pad_rows; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; +use generic_array::GenericArray; use num::BigUint; use num::Zero; use p3_air::AirBuilder; @@ -36,7 +38,9 @@ use std::fmt::Debug; use std::marker::PhantomData; use tracing::instrument; -pub const NUM_WEIERSTRASS_DOUBLE_COLS: usize = size_of::>(); +pub const fn num_weierstrass_double_cols() -> usize { + size_of::>() +} /// A set of columns to double a point on a Weierstrass curve. /// @@ -44,23 +48,23 @@ pub const NUM_WEIERSTRASS_DOUBLE_COLS: usize = size_of:: { +pub struct WeierstrassDoubleAssignCols { pub is_real: T, pub shard: T, pub clk: T, pub p_ptr: T, - pub p_access: [MemoryWriteCols; NUM_WORDS_EC_POINT], - pub(crate) slope_denominator: FieldOpCols, - pub(crate) slope_numerator: FieldOpCols, - pub(crate) slope: FieldOpCols, - pub(crate) p_x_squared: FieldOpCols, - pub(crate) p_x_squared_times_3: FieldOpCols, - pub(crate) slope_squared: FieldOpCols, - pub(crate) p_x_plus_p_x: FieldOpCols, - pub(crate) x3_ins: FieldOpCols, - pub(crate) p_x_minus_x: FieldOpCols, - pub(crate) y3_ins: FieldOpCols, - pub(crate) slope_times_p_x_minus_x: FieldOpCols, + pub p_access: GenericArray, P::WordsCurvePoint>, + pub(crate) slope_denominator: FieldOpCols, + pub(crate) slope_numerator: FieldOpCols, + pub(crate) slope: FieldOpCols, + pub(crate) p_x_squared: FieldOpCols, + pub(crate) p_x_squared_times_3: FieldOpCols, + pub(crate) slope_squared: FieldOpCols, + pub(crate) p_x_plus_p_x: FieldOpCols, + pub(crate) x3_ins: FieldOpCols, + pub(crate) p_x_minus_x: FieldOpCols, + pub(crate) y3_ins: FieldOpCols, + pub(crate) slope_times_p_x_minus_x: FieldOpCols, } #[derive(Default)] @@ -88,7 +92,7 @@ impl WeierstrassDoubleAssignChip { } fn populate_field_ops( - cols: &mut WeierstrassDoubleAssignCols, + cols: &mut WeierstrassDoubleAssignCols, p_x: BigUint, p_y: BigUint, ) { @@ -100,68 +104,51 @@ impl WeierstrassDoubleAssignChip { let slope = { // slope_numerator = a + (p.x * p.x) * 3. let slope_numerator = { - let p_x_squared = - cols.p_x_squared - .populate::(&p_x, &p_x, FieldOperation::Mul); - let p_x_squared_times_3 = cols.p_x_squared_times_3.populate::( + let p_x_squared = cols.p_x_squared.populate(&p_x, &p_x, FieldOperation::Mul); + let p_x_squared_times_3 = cols.p_x_squared_times_3.populate( &p_x_squared, &BigUint::from(3u32), FieldOperation::Mul, ); - cols.slope_numerator.populate::( - &a, - &p_x_squared_times_3, - FieldOperation::Add, - ) + cols.slope_numerator + .populate(&a, &p_x_squared_times_3, FieldOperation::Add) }; // slope_denominator = 2 * y. - let slope_denominator = cols.slope_denominator.populate::( - &BigUint::from(2u32), - &p_y, - FieldOperation::Mul, - ); + let slope_denominator = + cols.slope_denominator + .populate(&BigUint::from(2u32), &p_y, FieldOperation::Mul); - cols.slope.populate::( - &slope_numerator, - &slope_denominator, - FieldOperation::Div, - ) + cols.slope + .populate(&slope_numerator, &slope_denominator, FieldOperation::Div) }; // x = slope * slope - (p.x + p.x). let x = { - let slope_squared = - cols.slope_squared - .populate::(&slope, &slope, FieldOperation::Mul); - let p_x_plus_p_x = - cols.p_x_plus_p_x - .populate::(&p_x, &p_x, FieldOperation::Add); + let slope_squared = cols + .slope_squared + .populate(&slope, &slope, FieldOperation::Mul); + let p_x_plus_p_x = cols.p_x_plus_p_x.populate(&p_x, &p_x, FieldOperation::Add); cols.x3_ins - .populate::(&slope_squared, &p_x_plus_p_x, FieldOperation::Sub) + .populate(&slope_squared, &p_x_plus_p_x, FieldOperation::Sub) }; // y = slope * (p.x - x) - p.y. { - let p_x_minus_x = - cols.p_x_minus_x - .populate::(&p_x, &x, FieldOperation::Sub); - let slope_times_p_x_minus_x = cols.slope_times_p_x_minus_x.populate::( - &slope, - &p_x_minus_x, - FieldOperation::Mul, - ); - cols.y3_ins.populate::( - &slope_times_p_x_minus_x, - &p_y, - FieldOperation::Sub, - ); + let p_x_minus_x = cols.p_x_minus_x.populate(&p_x, &x, FieldOperation::Sub); + let slope_times_p_x_minus_x = + cols.slope_times_p_x_minus_x + .populate(&slope, &p_x_minus_x, FieldOperation::Mul); + cols.y3_ins + .populate(&slope_times_p_x_minus_x, &p_y, FieldOperation::Sub); } } } impl MachineAir for WeierstrassDoubleAssignChip +where + [(); num_weierstrass_double_cols::()]:, { type Record = ExecutionRecord; @@ -192,8 +179,8 @@ impl MachineAir let rows = events .iter() .map(|event| { - let mut row = [F::zero(); NUM_WEIERSTRASS_DOUBLE_COLS]; - let cols: &mut WeierstrassDoubleAssignCols = + let mut row = [F::zero(); num_weierstrass_double_cols::()]; + let cols: &mut WeierstrassDoubleAssignCols = row.as_mut_slice().borrow_mut(); // Decode affine points. @@ -210,7 +197,7 @@ impl MachineAir Self::populate_field_ops(cols, p_x, p_y); // Populate the memory access columns. - for i in 0..NUM_WORDS_EC_POINT { + for i in 0..cols.p_access.len() { cols.p_access[i] .populate(event.p_memory_records[i], &mut new_byte_lookup_events); } @@ -230,8 +217,9 @@ impl MachineAir } pad_rows(&mut rows, || { - let mut row = [F::zero(); NUM_WEIERSTRASS_DOUBLE_COLS]; - let cols: &mut WeierstrassDoubleAssignCols = row.as_mut_slice().borrow_mut(); + let mut row = [F::zero(); num_weierstrass_double_cols::()]; + let cols: &mut WeierstrassDoubleAssignCols = + row.as_mut_slice().borrow_mut(); let zero = BigUint::zero(); Self::populate_field_ops(cols, zero.clone(), zero.clone()); row @@ -240,7 +228,7 @@ impl MachineAir // Convert the trace to a row major matrix. RowMajorMatrix::new( rows.into_iter().flatten().collect::>(), - NUM_WEIERSTRASS_DOUBLE_COLS, + num_weierstrass_double_cols::(), ) } @@ -251,43 +239,42 @@ impl MachineAir impl BaseAir for WeierstrassDoubleAssignChip { fn width(&self) -> usize { - NUM_WEIERSTRASS_DOUBLE_COLS + num_weierstrass_double_cols::() } } impl Air for WeierstrassDoubleAssignChip where AB: SP1AirBuilder, + Limbs::Limbs>: Copy, { fn eval(&self, builder: &mut AB) { let main = builder.main(); - let row: &WeierstrassDoubleAssignCols = main.row_slice(0).borrow(); + let row: &WeierstrassDoubleAssignCols = main.row_slice(0).borrow(); - let p_x = limbs_from_prev_access(&row.p_access[0..NUM_WORDS_FIELD_ELEMENT]); - let p_y = limbs_from_prev_access(&row.p_access[NUM_WORDS_FIELD_ELEMENT..]); + let num_words_field_element = E::BaseField::NB_LIMBS / 4; + let p_x = limbs_from_prev_access(&row.p_access[0..num_words_field_element]); + let p_y = limbs_from_prev_access(&row.p_access[num_words_field_element..]); // a in the Weierstrass form: y^2 = x^3 + a * x + b. - let a = limbs_from_biguint::(&E::a_int()); + // TODO: U32 can't be hardcoded here? + let a = E::BaseField::to_limbs_field::(&E::a_int()); // slope = slope_numerator / slope_denominator. let slope = { // slope_numerator = a + (p.x * p.x) * 3. { - row.p_x_squared.eval::( - builder, - &p_x, - &p_x, - FieldOperation::Mul, - ); + row.p_x_squared + .eval::(builder, &p_x, &p_x, FieldOperation::Mul); - row.p_x_squared_times_3.eval::( + row.p_x_squared_times_3.eval::( builder, &row.p_x_squared.result, - &limbs_from_biguint::(&BigUint::from(3u32)), + &E::BaseField::to_limbs_field::(&BigUint::from(3u32)), FieldOperation::Mul, ); - row.slope_numerator.eval::( + row.slope_numerator.eval::( builder, &a, &row.p_x_squared_times_3.result, @@ -296,57 +283,49 @@ where }; // slope_denominator = 2 * y. - row.slope_denominator.eval::( + row.slope_denominator.eval::( builder, - &limbs_from_biguint::(&BigUint::from(2u32)), + &E::BaseField::to_limbs_field::(&BigUint::from(2u32)), &p_y, FieldOperation::Mul, ); - row.slope.eval::( + row.slope.eval::( builder, &row.slope_numerator.result, &row.slope_denominator.result, FieldOperation::Div, ); - row.slope.result + &row.slope.result }; // x = slope * slope - (p.x + p.x). let x = { - row.slope_squared.eval::( - builder, - &slope, - &slope, - FieldOperation::Mul, - ); - row.p_x_plus_p_x.eval::( - builder, - &p_x, - &p_x, - FieldOperation::Add, - ); - row.x3_ins.eval::( + row.slope_squared + .eval::(builder, slope, slope, FieldOperation::Mul); + row.p_x_plus_p_x + .eval::(builder, &p_x, &p_x, FieldOperation::Add); + row.x3_ins.eval::( builder, &row.slope_squared.result, &row.p_x_plus_p_x.result, FieldOperation::Sub, ); - row.x3_ins.result + &row.x3_ins.result }; // y = slope * (p.x - x) - p.y. { row.p_x_minus_x - .eval::(builder, &p_x, &x, FieldOperation::Sub); - row.slope_times_p_x_minus_x.eval::( + .eval::(builder, &p_x, x, FieldOperation::Sub); + row.slope_times_p_x_minus_x.eval::( builder, - &slope, + slope, &row.p_x_minus_x.result, FieldOperation::Mul, ); - row.y3_ins.eval::( + row.y3_ins.eval::( builder, &row.slope_times_p_x_minus_x.result, &p_y, @@ -356,7 +335,7 @@ where // Constraint self.p_access.value = [self.x3_ins.result, self.y3_ins.result]. This is to // ensure that p_access is updated with the new value. - for i in 0..NUM_LIMBS { + for i in 0..E::BaseField::NB_LIMBS { builder .when(row.is_real) .assert_eq(row.x3_ins.result[i], row.p_access[i / 4].value()[i % 4]); diff --git a/core/src/utils/ec/edwards/ed25519.rs b/core/src/utils/ec/edwards/ed25519.rs index 01f99e3b58..0a794304bd 100644 --- a/core/src/utils/ec/edwards/ed25519.rs +++ b/core/src/utils/ec/edwards/ed25519.rs @@ -1,11 +1,13 @@ use curve25519_dalek::edwards::CompressedEdwardsY; +use generic_array::GenericArray; use num::{BigUint, Num, One}; use serde::{Deserialize, Serialize}; use std::str::FromStr; +use typenum::{U32, U62}; -use crate::operations::field::params::{NB_BITS_PER_LIMB, NUM_LIMBS}; use crate::utils::ec::edwards::{EdwardsCurve, EdwardsParameters}; -use crate::utils::ec::field::{FieldParameters, MAX_NB_LIMBS}; +use crate::utils::ec::field::FieldParameters; +use crate::utils::ec::field::NumLimbs; use crate::utils::ec::{AffinePoint, EllipticCurveParameters}; pub type Ed25519 = EdwardsCurve; @@ -17,13 +19,11 @@ pub struct Ed25519Parameters; pub struct Ed25519BaseField; impl FieldParameters for Ed25519BaseField { - const NB_BITS_PER_LIMB: usize = NB_BITS_PER_LIMB; - const NB_LIMBS: usize = NUM_LIMBS; - const NB_WITNESS_LIMBS: usize = 2 * Self::NB_LIMBS - 2; - const MODULUS: [u8; NUM_LIMBS] = [ + const MODULUS: &'static [u8] = &[ 237, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127, ]; + const WITNESS_OFFSET: usize = 1usize << 13; fn modulus() -> BigUint { @@ -31,15 +31,20 @@ impl FieldParameters for Ed25519BaseField { } } +impl NumLimbs for Ed25519BaseField { + type Limbs = U32; + type Witness = U62; +} + impl EllipticCurveParameters for Ed25519Parameters { type BaseField = Ed25519BaseField; } impl EdwardsParameters for Ed25519Parameters { - const D: [u16; MAX_NB_LIMBS] = [ - 30883, 4953, 19914, 30187, 55467, 16705, 2637, 112, 59544, 30585, 16505, 36039, 65139, - 11119, 27886, 20995, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; + const D: GenericArray = GenericArray::from_array([ + 163, 120, 89, 19, 202, 77, 235, 117, 171, 216, 65, 65, 77, 10, 112, 0, 152, 232, 121, 119, + 121, 64, 199, 140, 115, 254, 111, 43, 238, 108, 3, 82, + ]); fn prime_group_order() -> BigUint { BigUint::from(2u32).pow(252) + BigUint::from(27742317777372353535851937790883648493u128) diff --git a/core/src/utils/ec/edwards/mod.rs b/core/src/utils/ec/edwards/mod.rs index b608231d3d..391b0f07db 100644 --- a/core/src/utils/ec/edwards/mod.rs +++ b/core/src/utils/ec/edwards/mod.rs @@ -1,13 +1,16 @@ pub mod ed25519; +use generic_array::GenericArray; use num::{BigUint, Zero}; use serde::{Deserialize, Serialize}; -use crate::utils::ec::field::{FieldParameters, MAX_NB_LIMBS}; +use crate::utils::ec::field::FieldParameters; use crate::utils::ec::{AffinePoint, EllipticCurve, EllipticCurveParameters}; +use super::field::NumLimbs; + pub trait EdwardsParameters: EllipticCurveParameters { - const D: [u16; MAX_NB_LIMBS]; + const D: GenericArray::Limbs>; fn generator() -> (BigUint, BigUint); @@ -16,7 +19,7 @@ pub trait EdwardsParameters: EllipticCurveParameters { fn d_biguint() -> BigUint { let mut modulus = BigUint::zero(); for (i, limb) in Self::D.iter().enumerate() { - modulus += BigUint::from(*limb) << (16 * i); + modulus += BigUint::from(*limb) << (8 * i); } modulus } @@ -31,7 +34,7 @@ pub trait EdwardsParameters: EllipticCurveParameters { pub struct EdwardsCurve(pub E); impl EdwardsParameters for EdwardsCurve { - const D: [u16; MAX_NB_LIMBS] = E::D; + const D: GenericArray::Limbs> = E::D; fn generator() -> (BigUint, BigUint) { E::generator() @@ -94,7 +97,7 @@ impl AffinePoint> { &self, other: &AffinePoint>, ) -> AffinePoint> { - let p = E::BaseField::modulus(); + let p = ::BaseField::modulus(); let x_3n = (&self.x * &other.y + &self.y * &other.x) % &p; let y_3n = (&self.y * &other.y + &self.x * &other.x) % &p; diff --git a/core/src/utils/ec/field.rs b/core/src/utils/ec/field.rs index 2cff8134b9..f1920cbcba 100644 --- a/core/src/utils/ec/field.rs +++ b/core/src/utils/ec/field.rs @@ -1,25 +1,26 @@ use super::utils::biguint_from_limbs; -use crate::operations::field::params::Limbs; -use crate::operations::field::params::NB_BITS_PER_LIMB; -use crate::operations::field::params::NUM_LIMBS; +use crate::operations::field::params::{Limbs, NB_BITS_PER_LIMB}; +use generic_array::sequence::GenericSequence; +use generic_array::{ArrayLength, GenericArray}; use num::BigUint; use p3_field::Field; use serde::{de::DeserializeOwned, Serialize}; use std::fmt::Debug; - -pub const MAX_NB_LIMBS: usize = 32; +use std::ops::Div; +use typenum::Unsigned; +use typenum::{U2, U4}; pub trait FieldParameters: - Send + Sync + Copy + 'static + Debug + Serialize + DeserializeOwned + Send + Sync + Copy + 'static + Debug + Serialize + DeserializeOwned + NumLimbs { const NB_BITS_PER_LIMB: usize = NB_BITS_PER_LIMB; - const NB_LIMBS: usize = NUM_LIMBS; + const NB_LIMBS: usize = Self::Limbs::USIZE; const NB_WITNESS_LIMBS: usize = 2 * Self::NB_LIMBS - 2; const WITNESS_OFFSET: usize = 1usize << 13; - const MODULUS: [u8; NUM_LIMBS]; + const MODULUS: &'static [u8]; fn modulus() -> BigUint { - biguint_from_limbs(&Self::MODULUS) + biguint_from_limbs(Self::MODULUS) } fn nb_bits() -> usize { @@ -28,28 +29,69 @@ pub trait FieldParameters: fn modulus_field_iter() -> impl Iterator { Self::MODULUS - .into_iter() - .map(|x| F::from_canonical_u8(x)) + .iter() + .map(|x| F::from_canonical_u8(*x)) .take(Self::NB_LIMBS) } - fn to_limbs(x: &BigUint) -> Limbs { + /// Convert a BigUint to a Vec of u8 limbs (with len NB_LIMBS). + fn to_limbs(x: &BigUint) -> Vec { let mut bytes = x.to_bytes_le(); - bytes.resize(NUM_LIMBS, 0u8); - let mut limbs = [0u8; NUM_LIMBS]; - limbs.copy_from_slice(&bytes); - Limbs(limbs) + bytes.resize(Self::NB_LIMBS, 0u8); + bytes + } + + /// Convert a BigUint to a Vec of F limbs (with len NB_LIMBS). + fn to_limbs_field_vec, F: Field>(x: &BigUint) -> Vec { + Self::to_limbs(x) + .into_iter() + .map(|x| F::from_canonical_u8(x).into()) + .collect::>() + } + + /// Convert a BigUint to Limbs. + fn to_limbs_field, F: Field>(x: &BigUint) -> Limbs { + limbs_from_vec(Self::to_limbs_field_vec(x)) } +} - fn to_limbs_field(x: &BigUint) -> Limbs { - Limbs( - Self::to_limbs(x) - .0 - .into_iter() - .map(|x| F::from_canonical_u8(x)) - .collect::>() - .try_into() - .unwrap(), - ) +/// Convert a vec of u8 limbs to a Limbs of N length. +pub fn limbs_from_vec, N: ArrayLength, F: Field>(limbs: Vec) -> Limbs { + debug_assert_eq!(limbs.len(), N::USIZE); + let mut result = GenericArray::::generate(|_i| F::zero().into()); + for (i, limb) in limbs.into_iter().enumerate() { + result[i] = limb; } + Limbs(result) +} + +/// Trait that holds the typenum values for # of limbs and # of witness limbs. +pub trait NumLimbs: Clone + Debug { + type Limbs: ArrayLength + Debug; + type Witness: ArrayLength + Debug; +} + +/// Trait that holds number of words needed to represent a field element and a curve point. +pub trait NumWords: Clone + Debug { + /// The number of words needed to represent a field element. + type WordsFieldElement: ArrayLength + Debug; + /// The number of words needed to represent a curve point (two field elements). + type WordsCurvePoint: ArrayLength + Debug; +} + +/// Implement NumWords for NumLimbs where # Limbs is divisible by 4. +/// +/// Using typenum we can do N/4 and N/2 in type-level arithmetic. Having it as a separate trait +/// avoids needing the Div where clauses everywhere. +impl NumWords for N +where + N::Limbs: Div, + N::Limbs: Div, + >::Output: ArrayLength + Debug, + >::Output: ArrayLength + Debug, +{ + /// Each word has 4 limbs so we divide by 4. + type WordsFieldElement = >::Output; + /// Curve point has 2 field elements so we divide by 2. + type WordsCurvePoint = >::Output; } diff --git a/core/src/utils/ec/mod.rs b/core/src/utils/ec/mod.rs index 55bae646ec..a6449393ce 100644 --- a/core/src/utils/ec/mod.rs +++ b/core/src/utils/ec/mod.rs @@ -11,7 +11,8 @@ use std::fmt::Debug; use std::ops::{Add, Neg}; use crate::air::WORD_SIZE; -use crate::operations::field::params::NUM_LIMBS; + +use self::field::NumWords; pub const NUM_WORDS_FIELD_ELEMENT: usize = 8; pub const NUM_BYTES_FIELD_ELEMENT: usize = NUM_WORDS_FIELD_ELEMENT * WORD_SIZE; @@ -58,9 +59,9 @@ impl AffinePoint { pub fn to_words_le(&self) -> [u32; 16] { let mut x_bytes = self.x.to_bytes_le(); - x_bytes.resize(NUM_LIMBS, 0u8); + x_bytes.resize(32, 0u8); let mut y_bytes = self.y.to_bytes_le(); - y_bytes.resize(NUM_LIMBS, 0u8); + y_bytes.resize(32, 0u8); let mut words = [0u32; 16]; for i in 0..8 { @@ -84,11 +85,14 @@ impl AffinePoint { pub trait EllipticCurveParameters: Debug + Send + Sync + Copy + Serialize + DeserializeOwned + 'static { - type BaseField: FieldParameters; + type BaseField: FieldParameters + NumWords; } /// An interface for elliptic curve groups. pub trait EllipticCurve: EllipticCurveParameters { + const NB_LIMBS: usize = Self::BaseField::NB_LIMBS; + + const NB_WITNESS_LIMBS: usize = Self::BaseField::NB_WITNESS_LIMBS; /// Adds two different points on the curve. /// /// Warning: This method assumes that the two points are different. diff --git a/core/src/utils/ec/utils.rs b/core/src/utils/ec/utils.rs index 834e112fdb..523fa8b10b 100644 --- a/core/src/utils/ec/utils.rs +++ b/core/src/utils/ec/utils.rs @@ -1,7 +1,5 @@ use num::BigUint; -use crate::operations::field::params::NUM_LIMBS; - pub fn biguint_to_bits_le(integer: &BigUint, num_bits: usize) -> Vec { let byte_vec = integer.to_bytes_le(); let mut bits = Vec::new(); @@ -18,14 +16,11 @@ pub fn biguint_to_bits_le(integer: &BigUint, num_bits: usize) -> Vec { bits } -pub fn biguint_to_limbs(integer: &BigUint) -> [u8; NUM_LIMBS] { +pub fn biguint_to_limbs(integer: &BigUint) -> [u8; N] { let mut bytes = integer.to_bytes_le(); - debug_assert!( - bytes.len() <= NUM_LIMBS, - "Number too large to fit in {NUM_LIMBS} limbs" - ); - bytes.resize(NUM_LIMBS, 0u8); - let mut limbs = [0u8; NUM_LIMBS]; + debug_assert!(bytes.len() <= N, "Number too large to fit in {N} limbs"); + bytes.resize(N, 0u8); + let mut limbs = [0u8; N]; limbs.copy_from_slice(&bytes); limbs } diff --git a/core/src/utils/ec/weierstrass/bn254.rs b/core/src/utils/ec/weierstrass/bn254.rs index a84c172672..4ab5ac5756 100644 --- a/core/src/utils/ec/weierstrass/bn254.rs +++ b/core/src/utils/ec/weierstrass/bn254.rs @@ -1,8 +1,11 @@ +use generic_array::GenericArray; use num::{BigUint, Num, Zero}; use serde::{Deserialize, Serialize}; +use typenum::{U32, U62}; use super::{SwCurve, WeierstrassParameters}; -use crate::utils::ec::field::{FieldParameters, MAX_NB_LIMBS}; +use crate::utils::ec::field::FieldParameters; +use crate::utils::ec::field::NumLimbs; use crate::utils::ec::EllipticCurveParameters; #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] @@ -16,13 +19,7 @@ pub type Bn254 = SwCurve; pub struct Bn254BaseField; impl FieldParameters for Bn254BaseField { - const NB_BITS_PER_LIMB: usize = 16; - - const NB_LIMBS: usize = 16; - - const NB_WITNESS_LIMBS: usize = 2 * Self::NB_LIMBS - 2; - - const MODULUS: [u8; MAX_NB_LIMBS] = [ + const MODULUS: &'static [u8] = &[ 71, 253, 124, 216, 22, 140, 32, 60, 141, 202, 113, 104, 145, 106, 129, 151, 93, 88, 129, 129, 182, 69, 80, 184, 41, 160, 49, 225, 114, 78, 100, 48, ]; @@ -38,20 +35,25 @@ impl FieldParameters for Bn254BaseField { } } +impl NumLimbs for Bn254BaseField { + type Limbs = U32; + type Witness = U62; +} + impl EllipticCurveParameters for Bn254Parameters { type BaseField = Bn254BaseField; } impl WeierstrassParameters for Bn254Parameters { - const A: [u16; MAX_NB_LIMBS] = [ + const A: GenericArray = GenericArray::from_array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; + ]); - const B: [u16; MAX_NB_LIMBS] = [ + const B: GenericArray = GenericArray::from_array([ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; + ]); fn generator() -> (BigUint, BigUint) { let x = BigUint::from(1u32); let y = BigUint::from(2u32); @@ -84,7 +86,7 @@ mod tests { #[test] fn test_weierstrass_biguint_scalar_mul() { assert_eq!( - biguint_from_limbs(&Bn254BaseField::MODULUS), + biguint_from_limbs(Bn254BaseField::MODULUS), Bn254BaseField::modulus() ); } diff --git a/core/src/utils/ec/weierstrass/mod.rs b/core/src/utils/ec/weierstrass/mod.rs index 17f2a97ed4..84ebe130d2 100644 --- a/core/src/utils/ec/weierstrass/mod.rs +++ b/core/src/utils/ec/weierstrass/mod.rs @@ -1,17 +1,20 @@ +use generic_array::GenericArray; use num::{BigUint, Zero}; use serde::{Deserialize, Serialize}; -use crate::utils::ec::field::{FieldParameters, MAX_NB_LIMBS}; +use crate::utils::ec::field::FieldParameters; use crate::utils::ec::utils::biguint_to_bits_le; use crate::utils::ec::{AffinePoint, EllipticCurve, EllipticCurveParameters}; +use super::field::NumLimbs; + pub mod bn254; pub mod secp256k1; /// Parameters that specify a short Weierstrass curve : y^2 = x^3 + ax + b. pub trait WeierstrassParameters: EllipticCurveParameters { - const A: [u16; MAX_NB_LIMBS]; - const B: [u16; MAX_NB_LIMBS]; + const A: GenericArray::Limbs>; + const B: GenericArray::Limbs>; fn generator() -> (BigUint, BigUint); @@ -20,7 +23,7 @@ pub trait WeierstrassParameters: EllipticCurveParameters { fn a_int() -> BigUint { let mut modulus = BigUint::zero(); for (i, limb) in Self::A.iter().enumerate() { - modulus += BigUint::from(*limb) << (16 * i); + modulus += BigUint::from(*limb) << (8 * i); } modulus } @@ -28,7 +31,7 @@ pub trait WeierstrassParameters: EllipticCurveParameters { fn b_int() -> BigUint { let mut modulus = BigUint::zero(); for (i, limb) in Self::B.iter().enumerate() { - modulus += BigUint::from(*limb) << (16 * i); + modulus += BigUint::from(*limb) << (8 * i); } modulus } @@ -42,8 +45,8 @@ pub trait WeierstrassParameters: EllipticCurveParameters { pub struct SwCurve(pub E); impl WeierstrassParameters for SwCurve { - const A: [u16; MAX_NB_LIMBS] = E::A; - const B: [u16; MAX_NB_LIMBS] = E::B; + const A: GenericArray::Limbs> = E::A; + const B: GenericArray::Limbs> = E::B; fn a_int() -> BigUint { E::a_int() @@ -71,6 +74,9 @@ impl EllipticCurveParameters for SwCurve { } impl EllipticCurve for SwCurve { + const NB_LIMBS: usize = Self::BaseField::NB_LIMBS; + const NB_WITNESS_LIMBS: usize = Self::BaseField::NB_WITNESS_LIMBS; + fn ec_add(p: &AffinePoint, q: &AffinePoint) -> AffinePoint { p.sw_add(q) } diff --git a/core/src/utils/ec/weierstrass/secp256k1.rs b/core/src/utils/ec/weierstrass/secp256k1.rs index 4ebcc49719..2f61b8e908 100644 --- a/core/src/utils/ec/weierstrass/secp256k1.rs +++ b/core/src/utils/ec/weierstrass/secp256k1.rs @@ -3,12 +3,14 @@ use std::str::FromStr; +use generic_array::GenericArray; use num::{BigUint, Zero}; use serde::{Deserialize, Serialize}; +use typenum::{U32, U62}; use super::{SwCurve, WeierstrassParameters}; -use crate::operations::field::params::{NB_BITS_PER_LIMB, NUM_LIMBS}; -use crate::utils::ec::field::{FieldParameters, MAX_NB_LIMBS}; +use crate::utils::ec::field::FieldParameters; +use crate::utils::ec::field::NumLimbs; use crate::utils::ec::EllipticCurveParameters; use k256::FieldElement; use num::traits::FromBytes; @@ -25,13 +27,7 @@ pub type Secp256k1 = SwCurve; pub struct Secp256k1BaseField; impl FieldParameters for Secp256k1BaseField { - const NB_BITS_PER_LIMB: usize = NB_BITS_PER_LIMB; - - const NB_LIMBS: usize = NUM_LIMBS; - - const NB_WITNESS_LIMBS: usize = 2 * Self::NB_LIMBS - 2; - - const MODULUS: [u8; MAX_NB_LIMBS] = [ + const MODULUS: &'static [u8] = &[ 0x2f, 0xfc, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -41,24 +37,29 @@ impl FieldParameters for Secp256k1BaseField { const WITNESS_OFFSET: usize = 1usize << 14; fn modulus() -> BigUint { - BigUint::from_bytes_le(&Self::MODULUS) + BigUint::from_bytes_le(Self::MODULUS) } } +impl NumLimbs for Secp256k1BaseField { + type Limbs = U32; + type Witness = U62; +} + impl EllipticCurveParameters for Secp256k1Parameters { type BaseField = Secp256k1BaseField; } impl WeierstrassParameters for Secp256k1Parameters { - const A: [u16; MAX_NB_LIMBS] = [ + const A: GenericArray = GenericArray::from_array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; + ]); - const B: [u16; MAX_NB_LIMBS] = [ + const B: GenericArray = GenericArray::from_array([ 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; + ]); fn generator() -> (BigUint, BigUint) { let x = BigUint::from_str( "55066263022277343669578718895168534326250603453777594175500187360389116729240", @@ -107,7 +108,7 @@ mod tests { #[test] fn test_weierstrass_biguint_scalar_mul() { assert_eq!( - biguint_from_limbs(&Secp256k1BaseField::MODULUS), + biguint_from_limbs(Secp256k1BaseField::MODULUS), Secp256k1BaseField::modulus() ); } diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index 2f79b0cfb9..69b975ae10 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -16,6 +16,7 @@ pub use tracer::*; pub use programs::*; use crate::{memory::MemoryCols, operations::field::params::Limbs}; +use generic_array::ArrayLength; pub const fn indices_arr() -> [usize; N] { let mut indices_arr = [0; N]; @@ -36,7 +37,9 @@ pub fn pad_to_power_of_two(values: &mut Vec< values.resize(n_real_rows.next_power_of_two() * N, T::default()); } -pub fn limbs_from_prev_access>(cols: &[M]) -> Limbs { +pub fn limbs_from_prev_access>( + cols: &[M], +) -> Limbs { let vec = cols .iter() .flat_map(|access| access.prev_value().0) @@ -48,7 +51,7 @@ pub fn limbs_from_prev_access>(cols: &[M]) -> Limbs Limbs(sized) } -pub fn limbs_from_access>(cols: &[M]) -> Limbs { +pub fn limbs_from_access>(cols: &[M]) -> Limbs { let vec = cols .iter() .flat_map(|access| access.value().0) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 3835701f1b..d3679de47d 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -38,41 +38,53 @@ pub fn aligned_borrow_derive(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let name = &ast.ident; - // Ensure the first generic parameter is the type generic, and rest all are const generics. - let mut generics_iter = ast.generics.params.iter(); - - // Extract the first generic parameter and ensure it's a type. - let type_generic = match generics_iter.next().expect("No generic parameters found") { - GenericParam::Type(type_param) => &type_param.ident, - _ => panic!("The first generic parameter must be a type."), - }; - - // Collect the remaining generic parameters, ensuring they are all const generics. - let const_generics: Vec<_> = generics_iter.map(|param| match param { - GenericParam::Const(const_param) => &const_param.ident, - _ => panic!("`AlignedBorrow` supports only a type as the first generic parameter and const generics after that"), - }).collect(); - - let methods = { - quote! { - impl<#type_generic: Copy #(, const #const_generics: usize)*> core::borrow::Borrow<#name<#type_generic #(, #const_generics)*>> for [#type_generic] { - fn borrow(&self) -> &#name<#type_generic #(, #const_generics)*> { - debug_assert_eq!(self.len(), std::mem::size_of::<#name>()); - let (prefix, shorts, _suffix) = unsafe { self.align_to::<#name<#type_generic #(, #const_generics)*>>() }; - debug_assert!(prefix.is_empty(), "Alignment should match"); - debug_assert_eq!(shorts.len(), 1); - &shorts[0] - } + // Get first generic which must be type (ex. `T`) for input + let type_generic = ast + .generics + .params + .iter() + .map(|param| match param { + GenericParam::Type(type_param) => &type_param.ident, + _ => panic!("Expected first generic to be a type"), + }) + .next() + .expect("Expected at least one generic"); + + // Get generics after the first (ex. `N: NumLimbs, const M: usize`) + // We need this because when we assert the size, we want to substitute u8 for T. + let non_first_generics = ast + .generics + .params + .iter() + .skip(1) + .filter_map(|param| match param { + GenericParam::Type(type_param) => Some(&type_param.ident), + GenericParam::Const(const_param) => Some(&const_param.ident), + _ => None, + }) + .collect::>(); + + // Get impl generics (``), type generics (``), where clause (`where T: Clone`) + let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl(); + + let methods = quote! { + impl #impl_generics core::borrow::Borrow<#name #type_generics> for [#type_generic] #where_clause { + fn borrow(&self) -> &#name #type_generics { + debug_assert_eq!(self.len(), std::mem::size_of::<#name>()); + let (prefix, shorts, _suffix) = unsafe { self.align_to::<#name #type_generics>() }; + debug_assert!(prefix.is_empty(), "Alignment should match"); + debug_assert_eq!(shorts.len(), 1); + &shorts[0] } + } - impl<#type_generic: Copy #(, const #const_generics: usize)*> core::borrow::BorrowMut<#name<#type_generic #(, #const_generics)*>> for [#type_generic] { - fn borrow_mut(&mut self) -> &mut #name<#type_generic #(, #const_generics)*> { - debug_assert_eq!(self.len(), std::mem::size_of::<#name>()); - let (prefix, shorts, _suffix) = unsafe { self.align_to_mut::<#name<#type_generic #(, #const_generics)*>>() }; - debug_assert!(prefix.is_empty(), "Alignment should match"); - debug_assert_eq!(shorts.len(), 1); - &mut shorts[0] - } + impl #impl_generics core::borrow::BorrowMut<#name #type_generics> for [#type_generic] #where_clause { + fn borrow_mut(&mut self) -> &mut #name #type_generics { + debug_assert_eq!(self.len(), std::mem::size_of::<#name>()); + let (prefix, shorts, _suffix) = unsafe { self.align_to_mut::<#name #type_generics>() }; + debug_assert!(prefix.is_empty(), "Alignment should match"); + debug_assert_eq!(shorts.len(), 1); + &mut shorts[0] } } }; diff --git a/examples/chess/script/Cargo.lock b/examples/chess/script/Cargo.lock index b478c5466c..413158fd22 100644 --- a/examples/chess/script/Cargo.lock +++ b/examples/chess/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -403,7 +403,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -415,7 +415,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -568,7 +568,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -733,6 +733,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -849,6 +858,15 @@ dependencies = [ "serde", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.12.1" @@ -1094,7 +1112,7 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "p3-air" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "p3-field", "p3-matrix", @@ -1103,9 +1121,13 @@ dependencies = [ [[package]] name = "p3-baby-bear" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ + "num-bigint", "p3-field", + "p3-mds", + "p3-poseidon2", + "p3-symmetric", "rand", "serde", ] @@ -1113,7 +1135,7 @@ dependencies = [ [[package]] name = "p3-blake3" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "blake3", "p3-symmetric", @@ -1122,7 +1144,7 @@ dependencies = [ [[package]] name = "p3-challenger" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "p3-field", "p3-maybe-rayon", @@ -1134,31 +1156,36 @@ dependencies = [ [[package]] name = "p3-commit" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ + "itertools 0.12.1", "p3-challenger", "p3-field", "p3-matrix", + "p3-util", "serde", ] [[package]] name = "p3-dft" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "p3-field", "p3-matrix", "p3-maybe-rayon", "p3-util", + "tracing", ] [[package]] name = "p3-field" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "itertools", + "itertools 0.12.1", + "num-bigint", + "num-traits", "p3-util", "rand", "serde", @@ -1167,9 +1194,9 @@ dependencies = [ [[package]] name = "p3-fri" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "itertools", + "itertools 0.12.1", "p3-challenger", "p3-commit", "p3-dft", @@ -1185,9 +1212,14 @@ dependencies = [ [[package]] name = "p3-goldilocks" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ + "num-bigint", + "p3-dft", "p3-field", + "p3-mds", + "p3-poseidon2", + "p3-symmetric", "p3-util", "rand", "serde", @@ -1196,7 +1228,7 @@ dependencies = [ [[package]] name = "p3-interpolation" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "p3-field", "p3-matrix", @@ -1206,7 +1238,7 @@ dependencies = [ [[package]] name = "p3-keccak" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "p3-symmetric", "tiny-keccak", @@ -1215,7 +1247,7 @@ dependencies = [ [[package]] name = "p3-keccak-air" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "p3-air", "p3-field", @@ -1227,19 +1259,21 @@ dependencies = [ [[package]] name = "p3-matrix" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ + "itertools 0.12.1", "p3-field", "p3-maybe-rayon", "p3-util", "rand", "serde", + "tracing", ] [[package]] name = "p3-maybe-rayon" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "rayon", ] @@ -1247,14 +1281,12 @@ dependencies = [ [[package]] name = "p3-mds" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "p3-baby-bear", + "itertools 0.11.0", "p3-dft", "p3-field", - "p3-goldilocks", "p3-matrix", - "p3-mersenne-31", "p3-symmetric", "p3-util", "rand", @@ -1263,9 +1295,9 @@ dependencies = [ [[package]] name = "p3-merkle-tree" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "itertools", + "itertools 0.12.1", "p3-commit", "p3-field", "p3-matrix", @@ -1276,31 +1308,12 @@ dependencies = [ "tracing", ] -[[package]] -name = "p3-mersenne-31" -version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" -dependencies = [ - "itertools", - "p3-dft", - "p3-field", - "p3-matrix", - "p3-maybe-rayon", - "p3-util", - "rand", - "serde", -] - [[package]] name = "p3-poseidon2" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "p3-baby-bear", "p3-field", - "p3-goldilocks", - "p3-mds", - "p3-mersenne-31", "p3-symmetric", "rand", ] @@ -1308,9 +1321,9 @@ dependencies = [ [[package]] name = "p3-symmetric" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "itertools", + "itertools 0.12.1", "p3-field", "serde", ] @@ -1318,9 +1331,9 @@ dependencies = [ [[package]] name = "p3-uni-stark" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ - "itertools", + "itertools 0.12.1", "p3-air", "p3-challenger", "p3-commit", @@ -1336,7 +1349,7 @@ dependencies = [ [[package]] name = "p3-util" version = "0.1.0" -source = "git+https://github.com/succinctlabs/plonky3.git#7d11ba55fa2c939449b593df298d04d7e15979d5" +source = "git+https://github.com/Plonky3/Plonky3.git?branch=sp1#4809fa7bedd9ba8f6f5d3267b1592618e3776c57" dependencies = [ "serde", ] @@ -1625,7 +1638,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1791,9 +1804,10 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", - "itertools", + "itertools 0.12.1", "k256", "lazy_static", "log", @@ -1836,6 +1850,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/ed25519/program/elf/riscv32im-succinct-zkvm-elf b/examples/ed25519/program/elf/riscv32im-succinct-zkvm-elf index 71cb9c6a38..a18b63297b 100755 Binary files a/examples/ed25519/program/elf/riscv32im-succinct-zkvm-elf and b/examples/ed25519/program/elf/riscv32im-succinct-zkvm-elf differ diff --git a/examples/ed25519/script/Cargo.lock b/examples/ed25519/script/Cargo.lock index d642420837..6aa695291f 100644 --- a/examples/ed25519/script/Cargo.lock +++ b/examples/ed25519/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -392,7 +392,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -404,7 +404,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -565,7 +565,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -730,6 +730,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1626,7 +1635,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1792,6 +1801,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1837,6 +1847,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/fibonacci-io/program/elf/riscv32im-succinct-zkvm-elf b/examples/fibonacci-io/program/elf/riscv32im-succinct-zkvm-elf index b0791a9e50..cff309c4c7 100755 Binary files a/examples/fibonacci-io/program/elf/riscv32im-succinct-zkvm-elf and b/examples/fibonacci-io/program/elf/riscv32im-succinct-zkvm-elf differ diff --git a/examples/fibonacci-io/script/Cargo.lock b/examples/fibonacci-io/script/Cargo.lock index d12ba8b9a2..9e1ecc19f8 100644 --- a/examples/fibonacci-io/script/Cargo.lock +++ b/examples/fibonacci-io/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -392,7 +392,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -404,7 +404,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -557,7 +557,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -730,6 +730,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1626,7 +1635,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1792,6 +1801,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1837,6 +1847,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/fibonacci/program/elf/riscv32im-succinct-zkvm-elf b/examples/fibonacci/program/elf/riscv32im-succinct-zkvm-elf index 6ddfa99127..33a33ed26c 100755 Binary files a/examples/fibonacci/program/elf/riscv32im-succinct-zkvm-elf and b/examples/fibonacci/program/elf/riscv32im-succinct-zkvm-elf differ diff --git a/examples/fibonacci/script/Cargo.lock b/examples/fibonacci/script/Cargo.lock index 4348f3bffa..16a714ad1c 100644 --- a/examples/fibonacci/script/Cargo.lock +++ b/examples/fibonacci/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -395,7 +395,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -407,7 +407,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -560,7 +560,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -733,6 +733,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1629,7 +1638,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1795,6 +1804,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1840,6 +1850,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/io/program/elf/riscv32im-succinct-zkvm-elf b/examples/io/program/elf/riscv32im-succinct-zkvm-elf index a5f708378b..4d4a4f2c23 100755 Binary files a/examples/io/program/elf/riscv32im-succinct-zkvm-elf and b/examples/io/program/elf/riscv32im-succinct-zkvm-elf differ diff --git a/examples/io/script/Cargo.lock b/examples/io/script/Cargo.lock index 08181d95ab..ad815d3d28 100644 --- a/examples/io/script/Cargo.lock +++ b/examples/io/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -395,7 +395,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -407,7 +407,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -560,7 +560,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -725,6 +725,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1630,7 +1639,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1796,6 +1805,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1841,6 +1851,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/json/script/Cargo.lock b/examples/json/script/Cargo.lock index cf7ab83372..20b80fcf18 100644 --- a/examples/json/script/Cargo.lock +++ b/examples/json/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -395,7 +395,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -407,7 +407,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -560,7 +560,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -725,6 +725,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1639,7 +1648,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1805,6 +1814,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1850,6 +1860,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/regex/script/Cargo.lock b/examples/regex/script/Cargo.lock index 29bc0cddb3..4b3ca1d638 100644 --- a/examples/regex/script/Cargo.lock +++ b/examples/regex/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -395,7 +395,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -407,7 +407,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -560,7 +560,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -725,6 +725,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1629,7 +1638,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1795,6 +1804,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1840,6 +1850,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/rsa/program/elf/riscv32im-succinct-zkvm-elf b/examples/rsa/program/elf/riscv32im-succinct-zkvm-elf index 7e65713c6f..2023b313d9 100755 Binary files a/examples/rsa/program/elf/riscv32im-succinct-zkvm-elf and b/examples/rsa/program/elf/riscv32im-succinct-zkvm-elf differ diff --git a/examples/rsa/script/Cargo.lock b/examples/rsa/script/Cargo.lock index 3f5fd0b0fc..03c80c8e36 100644 --- a/examples/rsa/script/Cargo.lock +++ b/examples/rsa/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -395,7 +395,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -407,7 +407,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -560,7 +560,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -725,6 +725,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1629,7 +1638,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1795,6 +1804,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1840,6 +1850,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/ssz-withdrawals/script/Cargo.lock b/examples/ssz-withdrawals/script/Cargo.lock index 251785f13a..14473b8b36 100644 --- a/examples/ssz-withdrawals/script/Cargo.lock +++ b/examples/ssz-withdrawals/script/Cargo.lock @@ -207,7 +207,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -395,7 +395,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -407,7 +407,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -560,7 +560,7 @@ dependencies = [ "crypto-bigint", "digest", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -725,6 +725,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -1621,7 +1630,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -1787,6 +1796,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -1832,6 +1842,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]] diff --git a/examples/tendermint/script/Cargo.lock b/examples/tendermint/script/Cargo.lock index 8942ac614b..76837ab5e0 100644 --- a/examples/tendermint/script/Cargo.lock +++ b/examples/tendermint/script/Cargo.lock @@ -231,7 +231,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -240,7 +240,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -450,7 +450,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ - "generic-array", + "generic-array 0.14.7", "rand_core", "subtle", "zeroize", @@ -462,7 +462,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] @@ -591,7 +591,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" dependencies = [ - "generic-array", + "generic-array 0.14.7", ] [[package]] @@ -671,7 +671,7 @@ dependencies = [ "crypto-bigint", "digest 0.10.7", "ff", - "generic-array", + "generic-array 0.14.7", "group", "pkcs8", "rand_core", @@ -878,6 +878,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -2105,7 +2114,7 @@ checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" dependencies = [ "base16ct", "der", - "generic-array", + "generic-array 0.14.7", "pkcs8", "subtle", "zeroize", @@ -2368,6 +2377,7 @@ dependencies = [ "elf", "elliptic-curve", "flate2", + "generic-array 1.0.0", "hashbrown 0.14.3", "hex", "itertools 0.12.1", @@ -2413,6 +2423,7 @@ dependencies = [ "tracing-forest", "tracing-log", "tracing-subscriber", + "typenum", ] [[package]]