From 51177c0b3ed77192a4ddd23de6b450376240e863 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:08:45 -0500 Subject: [PATCH 01/11] add test.sh --- test.sh | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test.sh diff --git a/test.sh b/test.sh new file mode 100644 index 0000000000..95863be19d --- /dev/null +++ b/test.sh @@ -0,0 +1,5 @@ +cargo test --package curta-core --lib --release -- operations::field::fp_sqrt::tests --nocapture && \ + cargo test --package curta-core --lib --release -- operations::field::fp_op::tests --nocapture && \ + cargo test --package curta-core --lib --release -- operations::field::fp_op::tests --nocapture && \ + cargo test --package curta-core --lib --release -- operations::field::fp_inner_product::tests --nocapture && \ + cargo test --package curta-core --lib --release -- operations::field::fp_den::tests --nocapture From be3fc772bd688563ba334a2e390415f4e3e06cab Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:14:19 -0500 Subject: [PATCH 02/11] Field => Primefield32 attempt, doesn't work yet --- core/src/operations/field/fp_den.rs | 20 +++++------- core/src/operations/field/fp_inner_product.rs | 31 ++++++------------- core/src/operations/field/fp_op.rs | 26 ++++++---------- core/src/operations/field/params.rs | 7 ++--- 4 files changed, 29 insertions(+), 55 deletions(-) diff --git a/core/src/operations/field/fp_den.rs b/core/src/operations/field/fp_den.rs index 2d9432e2fc..3a7785b3f3 100644 --- a/core/src/operations/field/fp_den.rs +++ b/core/src/operations/field/fp_den.rs @@ -8,8 +8,7 @@ use crate::utils::ec::field::FieldParameters; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; use num::BigUint; -use p3_baby_bear::BabyBear; -use p3_field::Field; +use p3_field::PrimeField32; use std::fmt::Debug; use valida_derive::AlignedBorrow; @@ -28,18 +27,13 @@ pub struct FpDenCols { pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], } -impl FpDenCols { +impl FpDenCols { pub fn populate( &mut self, a: &BigUint, b: &BigUint, sign: bool, ) -> BigUint { - /// TODO: This operation relies on `F` being a PrimeField32, but our traits do not - /// support that. This is a hack, since we always use BabyBear, to get around that, but - /// all operations using "PF" should use "F" in the future. - type PF = BabyBear; - let p = P::modulus(); let minus_b_int = &p - b; let b_signed = if sign { b.clone() } else { minus_b_int }; @@ -59,11 +53,11 @@ impl FpDenCols { 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 { diff --git a/core/src/operations/field/fp_inner_product.rs b/core/src/operations/field/fp_inner_product.rs index 1de1cdccc5..cc86b104de 100644 --- a/core/src/operations/field/fp_inner_product.rs +++ b/core/src/operations/field/fp_inner_product.rs @@ -9,9 +9,7 @@ use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; use num::BigUint; use num::Zero; -use p3_baby_bear::BabyBear; -use p3_field::AbstractField; -use p3_field::Field; +use p3_field::{AbstractField, PrimeField32}; use std::fmt::Debug; use valida_derive::AlignedBorrow; @@ -28,21 +26,12 @@ pub struct FpInnerProductCols { pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], } -impl FpInnerProductCols { +impl FpInnerProductCols { pub fn populate(&mut self, a: &[BigUint], b: &[BigUint]) -> BigUint { - /// TODO: This operation relies on `F` being a PrimeField32, but our traits do not - /// support that. This is a hack, since we always use BabyBear, to get around that, but - /// all operations using "PF" should use "F" in the future. - type PF = BabyBear; - - 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 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 @@ -56,15 +45,15 @@ impl FpInnerProductCols { 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 .into_iter() .zip(p_b_vec) - .fold(Polynomial::::new(vec![PF::zero()]), |acc, (c, d)| { + .fold(Polynomial::::new(vec![F::zero()]), |acc, (c, d)| { acc + &c * &d }); let p_vanishing = p_inner_product - &p_result - &p_carry * &p_modulus; diff --git a/core/src/operations/field/fp_op.rs b/core/src/operations/field/fp_op.rs index b2930c94eb..50580b1472 100644 --- a/core/src/operations/field/fp_op.rs +++ b/core/src/operations/field/fp_op.rs @@ -9,8 +9,7 @@ use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; use num::{BigUint, Zero}; use p3_air::AirBuilder; -use p3_baby_bear::BabyBear; -use p3_field::Field; +use p3_field::PrimeField32; use std::fmt::Debug; use valida_derive::AlignedBorrow; @@ -35,18 +34,13 @@ pub struct FpOpCols { pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], } -impl FpOpCols { +impl FpOpCols { pub fn populate( &mut self, a: &BigUint, b: &BigUint, op: FpOperation, ) -> BigUint { - /// TODO: This operation relies on `F` being a PrimeField32, but our traits do not - /// support that. This is a hack, since we always use BabyBear, to get around that, but - /// all operations using "PF" should use "F" in the future. - type PF = BabyBear; - if b == &BigUint::zero() && op == FpOperation::Div { // Division by 0 is allowed only when dividing 0 so that padded rows can be all 0. assert_eq!( @@ -67,7 +61,7 @@ impl FpOpCols { // Note that this reversal means we have to flip result, a correspondingly in // the `eval` function. self.populate::

(&result, b, FpOperation::Add); - let p_result: Polynomial = P::to_limbs_field::(&result).into(); + let p_result: Polynomial = P::to_limbs_field::(&result).into(); self.result = convert_polynomial(p_result); return result; } @@ -85,13 +79,13 @@ impl FpOpCols { // Note that this reversal means we have to flip result, a correspondingly in the `eval` // function. self.populate::

(&result, b, FpOperation::Mul); - let p_result: Polynomial = P::to_limbs_field::(&result).into(); + let p_result: Polynomial = P::to_limbs_field::(&result).into(); self.result = convert_polynomial(p_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(); @@ -109,9 +103,9 @@ impl FpOpCols { } // 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 { @@ -119,7 +113,7 @@ impl FpOpCols { FpOperation::Mul => &p_a * &p_b, FpOperation::Sub | FpOperation::Div => unreachable!(), }; - let p_vanishing: Polynomial = &p_op - &p_result - &p_carry * &p_modulus; + let p_vanishing: Polynomial = &p_op - &p_result - &p_carry * &p_modulus; debug_assert_eq!(p_vanishing.degree(), P::NB_WITNESS_LIMBS); let p_witness = compute_root_quotient_and_shift( diff --git a/core/src/operations/field/params.rs b/core/src/operations/field/params.rs index 8cd90a38df..0ed6c668b8 100644 --- a/core/src/operations/field/params.rs +++ b/core/src/operations/field/params.rs @@ -1,6 +1,4 @@ use crate::air::Polynomial; -use p3_baby_bear::BabyBear; -use p3_field::Field; use p3_field::PrimeField32; use std::fmt::Debug; use std::ops::Index; @@ -58,7 +56,7 @@ impl<'a, T: Debug + Default + Clone> From> for Limbs { } // TODO: we probably won't need this in the future when we do things properly. -pub fn convert_polynomial(value: Polynomial) -> Limbs { +pub fn convert_polynomial(value: Polynomial) -> Limbs { let inner_u8 = value .as_coefficients() .iter() @@ -69,8 +67,7 @@ pub fn convert_polynomial(value: Polynomial) -> Limbs { Limbs(inner) } -// TODO: we probably won't need this in the future when we do things properly. -pub fn convert_vec(value: Vec) -> Vec { +pub fn convert_vec(value: Vec) -> Vec { value .iter() .map(|x| x.as_canonical_u32() as u8) From c260d6abe58645d43000273477ed3d8ea0dfbe76 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:17:27 -0500 Subject: [PATCH 03/11] compiles...? --- core/src/operations/field/fp_den.rs | 4 ++-- core/src/operations/field/fp_inner_product.rs | 4 ++-- core/src/operations/field/fp_op.rs | 4 ++-- core/src/operations/field/fp_sqrt.rs | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/operations/field/fp_den.rs b/core/src/operations/field/fp_den.rs index 3a7785b3f3..c3e6a9ba9c 100644 --- a/core/src/operations/field/fp_den.rs +++ b/core/src/operations/field/fp_den.rs @@ -126,7 +126,7 @@ impl FpDenCols { mod tests { use num::BigUint; use p3_air::BaseAir; - use p3_field::Field; + use p3_field::{Field, PrimeField32}; use super::{FpDenCols, Limbs}; use crate::air::MachineAir; @@ -167,7 +167,7 @@ mod tests { } } - impl MachineAir for FpDenChip

{ + impl MachineAir for FpDenChip

{ fn name(&self) -> String { "FpDen".to_string() } diff --git a/core/src/operations/field/fp_inner_product.rs b/core/src/operations/field/fp_inner_product.rs index cc86b104de..3fb3d312ac 100644 --- a/core/src/operations/field/fp_inner_product.rs +++ b/core/src/operations/field/fp_inner_product.rs @@ -116,7 +116,7 @@ impl FpInnerProductCols { mod tests { use num::BigUint; use p3_air::BaseAir; - use p3_field::Field; + use p3_field::{Field, PrimeField32}; use super::{FpInnerProductCols, Limbs}; use crate::air::MachineAir; @@ -156,7 +156,7 @@ mod tests { } } - impl MachineAir for FpIpChip

{ + impl MachineAir for FpIpChip

{ fn name(&self) -> String { "FpInnerProduct".to_string() } diff --git a/core/src/operations/field/fp_op.rs b/core/src/operations/field/fp_op.rs index 50580b1472..8899b5037b 100644 --- a/core/src/operations/field/fp_op.rs +++ b/core/src/operations/field/fp_op.rs @@ -173,7 +173,7 @@ impl FpOpCols { mod tests { use num::BigUint; use p3_air::BaseAir; - use p3_field::Field; + use p3_field::{Field, PrimeField32}; use super::{FpOpCols, FpOperation, Limbs}; use crate::air::MachineAir; @@ -215,7 +215,7 @@ mod tests { } } - impl MachineAir for FpOpChip

{ + impl MachineAir for FpOpChip

{ fn name(&self) -> String { format!("FpOp{:?}", self.operation) } diff --git a/core/src/operations/field/fp_sqrt.rs b/core/src/operations/field/fp_sqrt.rs index ca80683c81..56aad3c464 100644 --- a/core/src/operations/field/fp_sqrt.rs +++ b/core/src/operations/field/fp_sqrt.rs @@ -5,7 +5,7 @@ use crate::utils::ec::field::FieldParameters; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; use num::BigUint; -use p3_field::Field; +use p3_field::{Field, PrimeField32}; use std::fmt::Debug; use valida_derive::AlignedBorrow; @@ -21,7 +21,7 @@ pub struct FpSqrtCols { pub multiplication: FpOpCols, } -impl FpSqrtCols { +impl FpSqrtCols { /// Populates the trace. /// /// `P` is the parameter of the field that each limb lives in. @@ -79,7 +79,7 @@ impl FpSqrtCols { mod tests { use num::{BigUint, One, Zero}; use p3_air::BaseAir; - use p3_field::Field; + use p3_field::{Field, PrimeField32}; use super::{FpSqrtCols, Limbs}; use crate::air::MachineAir; @@ -117,7 +117,7 @@ mod tests { } } - impl MachineAir for EdSqrtChip

{ + impl MachineAir for EdSqrtChip

{ fn name(&self) -> String { "EdSqrtChip".to_string() } From 39f911938cbd4b0cf47189d720ab42040c3c9493 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:20:35 -0500 Subject: [PATCH 04/11] actually it doesn't compile yet, but i'm making some progress --- core/src/syscall/precompiles/edwards/ed_add.rs | 6 +++--- core/src/syscall/precompiles/edwards/ed_decompress.rs | 6 +++--- core/src/syscall/precompiles/k256/decompress.rs | 6 +++--- core/src/syscall/precompiles/weierstrass/weierstrass_add.rs | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/core/src/syscall/precompiles/edwards/ed_add.rs b/core/src/syscall/precompiles/edwards/ed_add.rs index e17e54aea1..9e169a0500 100644 --- a/core/src/syscall/precompiles/edwards/ed_add.rs +++ b/core/src/syscall/precompiles/edwards/ed_add.rs @@ -27,7 +27,7 @@ use num::Zero; use p3_air::AirBuilder; use p3_air::{Air, BaseAir}; use p3_field::AbstractField; -use p3_field::Field; +use p3_field::PrimeField32; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::MatrixRowSlices; use p3_maybe_rayon::prelude::IntoParallelRefIterator; @@ -74,7 +74,7 @@ impl EdAddAssignChip { _marker: PhantomData, } } - fn populate_fp_ops( + fn populate_fp_ops( cols: &mut EdAddAssignCols, p_x: BigUint, p_y: BigUint, @@ -121,7 +121,7 @@ impl Syscall for EdAddAssignChip { } } -impl MachineAir for EdAddAssignChip { +impl MachineAir for EdAddAssignChip { fn name(&self) -> String { "EdAddAssign".to_string() } diff --git a/core/src/syscall/precompiles/edwards/ed_decompress.rs b/core/src/syscall/precompiles/edwards/ed_decompress.rs index 2094e9f430..9b45c29fe4 100644 --- a/core/src/syscall/precompiles/edwards/ed_decompress.rs +++ b/core/src/syscall/precompiles/edwards/ed_decompress.rs @@ -32,7 +32,7 @@ use num::One; use num::Zero; use p3_air::{Air, AirBuilder, BaseAir}; use p3_field::AbstractField; -use p3_field::Field; +use p3_field::PrimeField32; use p3_matrix::MatrixRowSlices; use std::marker::PhantomData; @@ -77,7 +77,7 @@ pub struct EdDecompressCols { pub(crate) neg_x: FpOpCols, } -impl EdDecompressCols { +impl EdDecompressCols { pub fn populate( &mut self, event: EdDecompressEvent, @@ -267,7 +267,7 @@ impl EdDecompressChip { } } -impl MachineAir for EdDecompressChip { +impl MachineAir for EdDecompressChip { fn name(&self) -> String { "EdDecompress".to_string() } diff --git a/core/src/syscall/precompiles/k256/decompress.rs b/core/src/syscall/precompiles/k256/decompress.rs index f89b52053c..2c79794188 100644 --- a/core/src/syscall/precompiles/k256/decompress.rs +++ b/core/src/syscall/precompiles/k256/decompress.rs @@ -35,7 +35,7 @@ use num::Zero; use p3_air::AirBuilder; use p3_air::{Air, BaseAir}; use p3_field::AbstractField; -use p3_field::Field; +use p3_field::PrimeField32; use p3_matrix::MatrixRowSlices; use std::str::FromStr; @@ -152,7 +152,7 @@ pub struct K256DecompressCols { pub(crate) y_least_bits: [T; 8], } -impl K256DecompressCols { +impl K256DecompressCols { pub fn populate(&mut self, event: K256DecompressEvent, shard: &mut ExecutionRecord) { let mut new_field_events = Vec::new(); self.is_real = F::from_bool(true); @@ -289,7 +289,7 @@ impl K256DecompressCols { } } -impl MachineAir for K256DecompressChip { +impl MachineAir for K256DecompressChip { fn name(&self) -> String { "K256Decompress".to_string() } diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs index 986159c9bb..25958c4355 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs @@ -26,6 +26,7 @@ use p3_air::AirBuilder; use p3_air::{Air, BaseAir}; use p3_field::AbstractField; use p3_field::Field; +use p3_field::PrimeField32; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::MatrixRowSlices; use std::fmt::Debug; @@ -84,7 +85,7 @@ impl WeierstrassAddAssignChip { } } - fn populate_fp_ops( + fn populate_fp_ops( cols: &mut WeierstrassAddAssignCols, p_x: BigUint, p_y: BigUint, From fea23e7c34180e387fa9a7a8fd9e57d1a874cb4c Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:21:49 -0500 Subject: [PATCH 05/11] okay maybe now it compiles --- core/src/syscall/precompiles/weierstrass/weierstrass_add.rs | 3 +-- .../syscall/precompiles/weierstrass/weierstrass_double.rs | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs index 25958c4355..536b3a5884 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs @@ -25,7 +25,6 @@ use num::Zero; use p3_air::AirBuilder; use p3_air::{Air, BaseAir}; use p3_field::AbstractField; -use p3_field::Field; use p3_field::PrimeField32; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::MatrixRowSlices; @@ -140,7 +139,7 @@ impl WeierstrassAddAssignChip { } } -impl MachineAir +impl MachineAir for WeierstrassAddAssignChip { fn name(&self) -> String { diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs index cfaeb3e17d..2cd5f91b08 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs @@ -24,7 +24,7 @@ use num::Zero; use p3_air::AirBuilder; use p3_air::{Air, BaseAir}; use p3_field::AbstractField; -use p3_field::Field; +use p3_field::PrimeField32; use p3_matrix::dense::RowMajorMatrix; use p3_matrix::MatrixRowSlices; use std::fmt::Debug; @@ -82,7 +82,7 @@ impl WeierstrassDoubleAssignChip { } } - fn populate_fp_ops( + fn populate_fp_ops( cols: &mut WeierstrassDoubleAssignCols, p_x: BigUint, p_y: BigUint, @@ -152,7 +152,7 @@ impl WeierstrassDoubleAssignChip { } } -impl MachineAir +impl MachineAir for WeierstrassDoubleAssignChip { fn name(&self) -> String { From 48da121a9d9ed9d4c97c2fdb26ef529a47fd759a Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:22:07 -0500 Subject: [PATCH 06/11] clean up an unused import --- core/src/operations/field/fp_sqrt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/operations/field/fp_sqrt.rs b/core/src/operations/field/fp_sqrt.rs index 56aad3c464..67fb0b9732 100644 --- a/core/src/operations/field/fp_sqrt.rs +++ b/core/src/operations/field/fp_sqrt.rs @@ -5,7 +5,7 @@ use crate::utils::ec::field::FieldParameters; use core::borrow::{Borrow, BorrowMut}; use core::mem::size_of; use num::BigUint; -use p3_field::{Field, PrimeField32}; +use p3_field::PrimeField32; use std::fmt::Debug; use valida_derive::AlignedBorrow; From 33d2de9c5daeb9c2265e417d3afe27385d3216a3 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 15:26:44 -0500 Subject: [PATCH 07/11] remove test.sh --- test.sh | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 test.sh diff --git a/test.sh b/test.sh deleted file mode 100644 index 95863be19d..0000000000 --- a/test.sh +++ /dev/null @@ -1,5 +0,0 @@ -cargo test --package curta-core --lib --release -- operations::field::fp_sqrt::tests --nocapture && \ - cargo test --package curta-core --lib --release -- operations::field::fp_op::tests --nocapture && \ - cargo test --package curta-core --lib --release -- operations::field::fp_op::tests --nocapture && \ - cargo test --package curta-core --lib --release -- operations::field::fp_inner_product::tests --nocapture && \ - cargo test --package curta-core --lib --release -- operations::field::fp_den::tests --nocapture From 55d4e395e598efd0886c294a7c7abf56d4754717 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 16:24:25 -0500 Subject: [PATCH 08/11] start removing convert_polynomial and convert_vec --- core/src/operations/field/fp_den.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/operations/field/fp_den.rs b/core/src/operations/field/fp_den.rs index c3e6a9ba9c..9223fbd9a2 100644 --- a/core/src/operations/field/fp_den.rs +++ b/core/src/operations/field/fp_den.rs @@ -74,10 +74,10 @@ impl FpDenCols { ); let (p_witness_low, p_witness_high) = split_u16_limbs_to_u8_limbs(&p_witness); - self.result = convert_polynomial(p_result); - self.carry = convert_polynomial(p_carry); - self.witness_low = convert_vec(p_witness_low).try_into().unwrap(); - self.witness_high = convert_vec(p_witness_high).try_into().unwrap(); + 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(); result } From 3d8ce1fb291f623264d1b7d247076782ef145d72 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 16:28:05 -0500 Subject: [PATCH 09/11] remove outdated utility functions --- core/src/operations/field/fp_den.rs | 2 +- core/src/operations/field/fp_inner_product.rs | 10 ++++----- core/src/operations/field/fp_op.rs | 16 +++++++------- core/src/operations/field/params.rs | 21 ------------------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/core/src/operations/field/fp_den.rs b/core/src/operations/field/fp_den.rs index 9223fbd9a2..fb8867abc3 100644 --- a/core/src/operations/field/fp_den.rs +++ b/core/src/operations/field/fp_den.rs @@ -1,5 +1,5 @@ +use super::params::Limbs; use super::params::NUM_WITNESS_LIMBS; -use super::params::{convert_polynomial, convert_vec, 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::CurtaAirBuilder; diff --git a/core/src/operations/field/fp_inner_product.rs b/core/src/operations/field/fp_inner_product.rs index 3fb3d312ac..3a75ff0a58 100644 --- a/core/src/operations/field/fp_inner_product.rs +++ b/core/src/operations/field/fp_inner_product.rs @@ -1,5 +1,5 @@ +use super::params::Limbs; use super::params::NUM_WITNESS_LIMBS; -use super::params::{convert_polynomial, convert_vec, 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::CurtaAirBuilder; @@ -66,10 +66,10 @@ impl FpInnerProductCols { ); let (p_witness_low, p_witness_high) = split_u16_limbs_to_u8_limbs(&p_witness); - self.result = convert_polynomial(p_result); - self.carry = convert_polynomial(p_carry); - self.witness_low = convert_vec(p_witness_low).try_into().unwrap(); - self.witness_high = convert_vec(p_witness_high).try_into().unwrap(); + 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(); result.clone() } diff --git a/core/src/operations/field/fp_op.rs b/core/src/operations/field/fp_op.rs index 8899b5037b..f1b0870feb 100644 --- a/core/src/operations/field/fp_op.rs +++ b/core/src/operations/field/fp_op.rs @@ -1,5 +1,5 @@ +use super::params::Limbs; use super::params::NUM_WITNESS_LIMBS; -use super::params::{convert_polynomial, convert_vec, 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::CurtaAirBuilder; @@ -61,8 +61,7 @@ impl FpOpCols { // Note that this reversal means we have to flip result, a correspondingly in // the `eval` function. self.populate::

(&result, b, FpOperation::Add); - let p_result: Polynomial = P::to_limbs_field::(&result).into(); - self.result = convert_polynomial(p_result); + self.result = P::to_limbs_field::(&result); return result; } @@ -79,8 +78,7 @@ impl FpOpCols { // Note that this reversal means we have to flip result, a correspondingly in the `eval` // function. self.populate::

(&result, b, FpOperation::Mul); - let p_result: Polynomial = P::to_limbs_field::(&result).into(); - self.result = convert_polynomial(p_result); + self.result = P::to_limbs_field::(&result); return result; } @@ -123,10 +121,10 @@ impl FpOpCols { ); let (p_witness_low, p_witness_high) = split_u16_limbs_to_u8_limbs(&p_witness); - self.result = convert_polynomial(p_result); - self.carry = convert_polynomial(p_carry); - self.witness_low = convert_vec(p_witness_low).try_into().unwrap(); - self.witness_high = convert_vec(p_witness_high).try_into().unwrap(); + 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(); result } diff --git a/core/src/operations/field/params.rs b/core/src/operations/field/params.rs index 0ed6c668b8..605a9a0869 100644 --- a/core/src/operations/field/params.rs +++ b/core/src/operations/field/params.rs @@ -1,5 +1,4 @@ use crate::air::Polynomial; -use p3_field::PrimeField32; use std::fmt::Debug; use std::ops::Index; use std::slice::Iter; @@ -55,26 +54,6 @@ impl<'a, T: Debug + Default + Clone> From> for Limbs { } } -// TODO: we probably won't need this in the future when we do things properly. -pub fn convert_polynomial(value: Polynomial) -> Limbs { - let inner_u8 = value - .as_coefficients() - .iter() - .map(|x| x.as_canonical_u32() as u8) - .map(|x| F::from_canonical_u8(x)) - .collect::>(); - let inner = inner_u8.try_into().unwrap(); - Limbs(inner) -} - -pub fn convert_vec(value: Vec) -> Vec { - value - .iter() - .map(|x| x.as_canonical_u32() as u8) - .map(|x| F::from_canonical_u8(x)) - .collect::>() -} - #[cfg(test)] mod tests { use num::BigUint; From 48c4f53063c69663814091906030fa2ef8b1c2d0 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 16:33:34 -0500 Subject: [PATCH 10/11] rename fp -> field --- core/src/operations/field/{fp_den.rs => field_den.rs} | 0 .../field/{fp_inner_product.rs => field_inner_product.rs} | 0 core/src/operations/field/{fp_op.rs => field_op.rs} | 0 core/src/operations/field/{fp_sqrt.rs => field_sqrt.rs} | 6 +++--- core/src/operations/field/mod.rs | 8 ++++---- 5 files changed, 7 insertions(+), 7 deletions(-) rename core/src/operations/field/{fp_den.rs => field_den.rs} (100%) rename core/src/operations/field/{fp_inner_product.rs => field_inner_product.rs} (100%) rename core/src/operations/field/{fp_op.rs => field_op.rs} (100%) rename core/src/operations/field/{fp_sqrt.rs => field_sqrt.rs} (97%) diff --git a/core/src/operations/field/fp_den.rs b/core/src/operations/field/field_den.rs similarity index 100% rename from core/src/operations/field/fp_den.rs rename to core/src/operations/field/field_den.rs diff --git a/core/src/operations/field/fp_inner_product.rs b/core/src/operations/field/field_inner_product.rs similarity index 100% rename from core/src/operations/field/fp_inner_product.rs rename to core/src/operations/field/field_inner_product.rs diff --git a/core/src/operations/field/fp_op.rs b/core/src/operations/field/field_op.rs similarity index 100% rename from core/src/operations/field/fp_op.rs rename to core/src/operations/field/field_op.rs diff --git a/core/src/operations/field/fp_sqrt.rs b/core/src/operations/field/field_sqrt.rs similarity index 97% rename from core/src/operations/field/fp_sqrt.rs rename to core/src/operations/field/field_sqrt.rs index 67fb0b9732..d1fe874fe8 100644 --- a/core/src/operations/field/fp_sqrt.rs +++ b/core/src/operations/field/field_sqrt.rs @@ -1,4 +1,4 @@ -use super::fp_op::FpOpCols; +use super::field_op::FpOpCols; use super::params::Limbs; use crate::air::CurtaAirBuilder; use crate::utils::ec::field::FieldParameters; @@ -35,7 +35,7 @@ impl FpSqrtCols { // Use FpOpCols to compute result * result. let sqrt_squared = self.multiplication - .populate::

(&sqrt, &sqrt, super::fp_op::FpOperation::Mul); + .populate::

(&sqrt, &sqrt, super::field_op::FpOperation::Mul); // If the result is indeed the square root of a, then result * result = a. assert_eq!(sqrt_squared, a.clone()); @@ -70,7 +70,7 @@ impl FpSqrtCols { builder, &sqrt, &sqrt, - super::fp_op::FpOperation::Mul, + super::field_op::FpOperation::Mul, ); } } diff --git a/core/src/operations/field/mod.rs b/core/src/operations/field/mod.rs index a0e9cf2f43..9c5d54e529 100644 --- a/core/src/operations/field/mod.rs +++ b/core/src/operations/field/mod.rs @@ -1,7 +1,7 @@ -pub mod fp_den; -pub mod fp_inner_product; -pub mod fp_op; -pub mod fp_sqrt; +pub mod field_den; +pub mod field_inner_product; +pub mod field_op; +pub mod field_sqrt; pub mod params; pub mod util; pub mod util_air; From cc74170a41d8fef43d8499fc0c11ebcaf258ed91 Mon Sep 17 00:00:00 2001 From: Hidenori Shinohara Date: Mon, 12 Feb 2024 16:37:09 -0500 Subject: [PATCH 11/11] fp => field --- core/src/operations/field/field_den.rs | 34 +++---- .../operations/field/field_inner_product.rs | 28 +++--- core/src/operations/field/field_op.rs | 90 +++++++++--------- core/src/operations/field/field_sqrt.rs | 23 +++-- .../src/syscall/precompiles/edwards/ed_add.rs | 46 +++++----- .../precompiles/edwards/ed_decompress.rs | 56 ++++++------ .../syscall/precompiles/k256/decompress.rs | 46 +++++----- .../weierstrass/weierstrass_add.rs | 79 ++++++++-------- .../weierstrass/weierstrass_double.rs | 91 ++++++++++--------- 9 files changed, 259 insertions(+), 234 deletions(-) diff --git a/core/src/operations/field/field_den.rs b/core/src/operations/field/field_den.rs index fb8867abc3..219971d57a 100644 --- a/core/src/operations/field/field_den.rs +++ b/core/src/operations/field/field_den.rs @@ -12,14 +12,16 @@ use p3_field::PrimeField32; use std::fmt::Debug; use valida_derive::AlignedBorrow; -// a / (1 + b) if sign -// a/ -b if !sign -/// A set of columns to compute `FpDen(a, b)` where a, b are field elements. +/// A set of columns to compute `FieldDen(a, b)` where `a`, `b` are field elements. +/// +/// `a / (1 + b)` if `sign` +/// `a / -b` if `!sign` +/// /// Right now the number of limbs is assumed to be a constant, although this could be macro-ed /// or made generic in the future. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FpDenCols { +pub struct FieldDenCols { /// The result of `a den b`, where a, b are field elements pub result: Limbs, pub(crate) carry: Limbs, @@ -27,7 +29,7 @@ pub struct FpDenCols { pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], } -impl FpDenCols { +impl FieldDenCols { pub fn populate( &mut self, a: &BigUint, @@ -83,7 +85,7 @@ impl FpDenCols { } } -impl FpDenCols { +impl FieldDenCols { #[allow(unused_variables)] pub fn eval, P: FieldParameters>( &self, @@ -128,7 +130,7 @@ mod tests { use p3_air::BaseAir; use p3_field::{Field, PrimeField32}; - use super::{FpDenCols, Limbs}; + use super::{FieldDenCols, Limbs}; use crate::air::MachineAir; use crate::utils::ec::edwards::ed25519::Ed25519BaseField; use crate::utils::ec::field::FieldParameters; @@ -148,17 +150,17 @@ mod tests { pub struct TestCols { pub a: Limbs, pub b: Limbs, - pub a_den_b: FpDenCols, + pub a_den_b: FieldDenCols, } pub const NUM_TEST_COLS: usize = size_of::>(); - struct FpDenChip { + struct FieldDenChip { pub sign: bool, pub _phantom: std::marker::PhantomData

, } - impl FpDenChip

{ + impl FieldDenChip

{ pub fn new(sign: bool) -> Self { Self { sign, @@ -167,9 +169,9 @@ mod tests { } } - impl MachineAir for FpDenChip

{ + impl MachineAir for FieldDenChip

{ fn name(&self) -> String { - "FpDen".to_string() + "FieldDen".to_string() } fn shard(&self, _: &ExecutionRecord, _: &mut Vec) {} @@ -221,13 +223,13 @@ mod tests { } } - impl BaseAir for FpDenChip

{ + impl BaseAir for FieldDenChip

{ fn width(&self) -> usize { NUM_TEST_COLS } } - impl Air for FpDenChip

+ impl Air for FieldDenChip

where AB: CurtaAirBuilder, { @@ -248,7 +250,7 @@ mod tests { #[test] fn generate_trace() { let mut shard = ExecutionRecord::default(); - let chip: FpDenChip = FpDenChip::new(true); + let chip: FieldDenChip = FieldDenChip::new(true); let trace: RowMajorMatrix = chip.generate_trace(&mut shard); println!("{:?}", trace.values) } @@ -260,7 +262,7 @@ mod tests { let mut shard = ExecutionRecord::default(); - let chip: FpDenChip = FpDenChip::new(true); + let chip: FieldDenChip = FieldDenChip::new(true); let trace: RowMajorMatrix = chip.generate_trace(&mut shard); // This it to test that the proof DOESN'T work if messed up. // let row = trace.row_mut(0); diff --git a/core/src/operations/field/field_inner_product.rs b/core/src/operations/field/field_inner_product.rs index 3a75ff0a58..6c01e58912 100644 --- a/core/src/operations/field/field_inner_product.rs +++ b/core/src/operations/field/field_inner_product.rs @@ -13,12 +13,12 @@ use p3_field::{AbstractField, PrimeField32}; use std::fmt::Debug; use valida_derive::AlignedBorrow; -/// A set of columns to compute `FpInnerProduct(Vec, Vec)` where a, b are field elements. +/// A set of columns to compute `FieldInnerProduct(Vec, Vec)` where a, b are field elements. /// Right now the number of limbs is assumed to be a constant, although this could be macro-ed /// or made generic in the future. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FpInnerProductCols { +pub struct FieldInnerProductCols { /// The result of `a inner product b`, where a, b are field elements pub result: Limbs, pub(crate) carry: Limbs, @@ -26,7 +26,7 @@ pub struct FpInnerProductCols { pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], } -impl FpInnerProductCols { +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(); @@ -75,7 +75,7 @@ impl FpInnerProductCols { } } -impl FpInnerProductCols { +impl FieldInnerProductCols { #[allow(unused_variables)] pub fn eval, P: FieldParameters>( &self, @@ -118,7 +118,7 @@ mod tests { use p3_air::BaseAir; use p3_field::{Field, PrimeField32}; - use super::{FpInnerProductCols, Limbs}; + use super::{FieldInnerProductCols, Limbs}; use crate::air::MachineAir; use crate::utils::ec::edwards::ed25519::Ed25519BaseField; use crate::utils::ec::field::FieldParameters; @@ -139,16 +139,16 @@ mod tests { pub struct TestCols { pub a: [Limbs; 1], pub b: [Limbs; 1], - pub a_ip_b: FpInnerProductCols, + pub a_ip_b: FieldInnerProductCols, } pub const NUM_TEST_COLS: usize = size_of::>(); - struct FpIpChip { + struct FieldIpChip { pub _phantom: std::marker::PhantomData

, } - impl FpIpChip

{ + impl FieldIpChip

{ pub fn new() -> Self { Self { _phantom: std::marker::PhantomData, @@ -156,9 +156,9 @@ mod tests { } } - impl MachineAir for FpIpChip

{ + impl MachineAir for FieldIpChip

{ fn name(&self) -> String { - "FpInnerProduct".to_string() + "FieldInnerProduct".to_string() } fn shard(&self, _: &ExecutionRecord, _: &mut Vec) {} @@ -208,13 +208,13 @@ mod tests { } } - impl BaseAir for FpIpChip

{ + impl BaseAir for FieldIpChip

{ fn width(&self) -> usize { NUM_TEST_COLS } } - impl Air for FpIpChip

+ impl Air for FieldIpChip

where AB: CurtaAirBuilder, { @@ -234,7 +234,7 @@ mod tests { #[test] fn generate_trace() { let mut shard = ExecutionRecord::default(); - let chip: FpIpChip = FpIpChip::new(); + let chip: FieldIpChip = FieldIpChip::new(); let trace: RowMajorMatrix = chip.generate_trace(&mut shard); println!("{:?}", trace.values) } @@ -246,7 +246,7 @@ mod tests { let mut shard = ExecutionRecord::default(); - let chip: FpIpChip = FpIpChip::new(); + let chip: FieldIpChip = FieldIpChip::new(); let trace: RowMajorMatrix = chip.generate_trace(&mut shard); let proof = prove::(&config, &chip, &mut challenger, trace); diff --git a/core/src/operations/field/field_op.rs b/core/src/operations/field/field_op.rs index f1b0870feb..12e951b7ca 100644 --- a/core/src/operations/field/field_op.rs +++ b/core/src/operations/field/field_op.rs @@ -14,19 +14,19 @@ use std::fmt::Debug; use valida_derive::AlignedBorrow; #[derive(PartialEq, Copy, Clone, Debug)] -pub enum FpOperation { +pub enum FieldOperation { Add, Mul, Sub, Div, // We don't constrain that the divisor is non-zero. } -/// A set of columns to compute `FpOperation(a, b)` where a, b are field elements. +/// A set of columns to compute `FieldOperation(a, b)` where a, b are field elements. /// Right now the number of limbs is assumed to be a constant, although this could be macro-ed /// or made generic in the future. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FpOpCols { +pub struct FieldOpCols { /// The result of `a op b`, where a, b are field elements pub result: Limbs, pub(crate) carry: Limbs, @@ -34,14 +34,14 @@ pub struct FpOpCols { pub(crate) witness_high: [T; NUM_WITNESS_LIMBS], } -impl FpOpCols { +impl FieldOpCols { pub fn populate( &mut self, a: &BigUint, b: &BigUint, - op: FpOperation, + op: FieldOperation, ) -> BigUint { - if b == &BigUint::zero() && op == FpOperation::Div { + 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!( *a, @@ -53,20 +53,20 @@ impl FpOpCols { let modulus = P::modulus(); // If doing the subtraction operation, a - b = result, equivalent to a = result + b. - if op == FpOperation::Sub { + if op == FieldOperation::Sub { let result = (modulus.clone() + a - b) % &modulus; // We populate the carry, witness_low, witness_high as if we were doing an addition with result + b. // But we populate `result` with the actual result of the subtraction 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, FpOperation::Add); + self.populate::

(&result, b, FieldOperation::Add); self.result = P::to_limbs_field::(&result); return result; } // a / b = result is equivalent to a = result * b. - if op == FpOperation::Div { + if op == FieldOperation::Div { // As modulus is prime, we can use Fermat's little theorem to compute the // inverse. let result = @@ -77,7 +77,7 @@ impl FpOpCols { // 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, FpOperation::Mul); + self.populate::

(&result, b, FieldOperation::Mul); self.result = P::to_limbs_field::(&result); return result; } @@ -88,16 +88,16 @@ impl FpOpCols { // Compute field addition in the integers. let modulus = &P::modulus(); let (result, carry) = match op { - FpOperation::Add => ((a + b) % modulus, (a + b - (a + b) % modulus) / modulus), - FpOperation::Mul => ((a * b) % modulus, (a * b - (a * b) % modulus) / modulus), - FpOperation::Sub | FpOperation::Div => unreachable!(), + FieldOperation::Add => ((a + b) % modulus, (a + b - (a + b) % modulus) / modulus), + FieldOperation::Mul => ((a * b) % modulus, (a * b - (a * b) % modulus) / modulus), + FieldOperation::Sub | FieldOperation::Div => unreachable!(), }; debug_assert!(&result < modulus); debug_assert!(&carry < modulus); match op { - FpOperation::Add => debug_assert_eq!(&carry * modulus, a + b - &result), - FpOperation::Mul => debug_assert_eq!(&carry * modulus, a * b - &result), - FpOperation::Sub | FpOperation::Div => unreachable!(), + FieldOperation::Add => debug_assert_eq!(&carry * modulus, a + b - &result), + FieldOperation::Mul => debug_assert_eq!(&carry * modulus, a * b - &result), + FieldOperation::Sub | FieldOperation::Div => unreachable!(), } // Make little endian polynomial limbs. @@ -107,9 +107,9 @@ impl FpOpCols { // Compute the vanishing polynomial. let p_op = match op { - FpOperation::Add => &p_a + &p_b, - FpOperation::Mul => &p_a * &p_b, - FpOperation::Sub | FpOperation::Div => unreachable!(), + FieldOperation::Add => &p_a + &p_b, + FieldOperation::Mul => &p_a * &p_b, + FieldOperation::Sub | FieldOperation::Div => unreachable!(), }; let p_vanishing: Polynomial = &p_op - &p_result - &p_carry * &p_modulus; debug_assert_eq!(p_vanishing.degree(), P::NB_WITNESS_LIMBS); @@ -130,7 +130,7 @@ impl FpOpCols { } } -impl FpOpCols { +impl FieldOpCols { #[allow(unused_variables)] pub fn eval< AB: CurtaAirBuilder, @@ -142,7 +142,7 @@ impl FpOpCols { builder: &mut AB, a: &A, b: &B, - op: FpOperation, + op: FieldOperation, ) where V: Into, { @@ -150,13 +150,13 @@ impl FpOpCols { let p_b: Polynomial = (*b).clone().into(); let (p_a, p_result): (Polynomial<_>, Polynomial<_>) = match op { - FpOperation::Add | FpOperation::Mul => (p_a_param, self.result.into()), - FpOperation::Sub | FpOperation::Div => (self.result.into(), p_a_param), + FieldOperation::Add | FieldOperation::Mul => (p_a_param, self.result.into()), + FieldOperation::Sub | FieldOperation::Div => (self.result.into(), p_a_param), }; let p_carry: Polynomial<::Expr> = self.carry.into(); let p_op = match op { - FpOperation::Add | FpOperation::Sub => p_a + p_b, - FpOperation::Mul | FpOperation::Div => p_a * p_b, + FieldOperation::Add | FieldOperation::Sub => p_a + p_b, + FieldOperation::Mul | FieldOperation::Div => p_a * p_b, }; let p_op_minus_result: Polynomial = p_op - p_result; let p_limbs = Polynomial::from_iter(P::modulus_field_iter::().map(AB::Expr::from)); @@ -173,7 +173,7 @@ mod tests { use p3_air::BaseAir; use p3_field::{Field, PrimeField32}; - use super::{FpOpCols, FpOperation, Limbs}; + use super::{FieldOpCols, FieldOperation, Limbs}; use crate::air::MachineAir; use crate::utils::ec::edwards::ed25519::Ed25519BaseField; use crate::utils::ec::field::FieldParameters; @@ -194,18 +194,18 @@ mod tests { pub struct TestCols { pub a: Limbs, pub b: Limbs, - pub a_op_b: FpOpCols, + pub a_op_b: FieldOpCols, } pub const NUM_TEST_COLS: usize = size_of::>(); - struct FpOpChip { - pub operation: FpOperation, + struct FieldOpChip { + pub operation: FieldOperation, pub _phantom: std::marker::PhantomData

, } - impl FpOpChip

{ - pub fn new(operation: FpOperation) -> Self { + impl FieldOpChip

{ + pub fn new(operation: FieldOperation) -> Self { Self { operation, _phantom: std::marker::PhantomData, @@ -213,9 +213,9 @@ mod tests { } } - impl MachineAir for FpOpChip

{ + impl MachineAir for FieldOpChip

{ fn name(&self) -> String { - format!("FpOp{:?}", self.operation) + format!("FieldOp{:?}", self.operation) } fn shard(&self, _: &ExecutionRecord, _: &mut Vec) {} @@ -269,13 +269,13 @@ mod tests { } } - impl BaseAir for FpOpChip

{ + impl BaseAir for FieldOpChip

{ fn width(&self) -> usize { NUM_TEST_COLS } } - impl Air for FpOpChip

+ impl Air for FieldOpChip

where AB: CurtaAirBuilder, { @@ -295,9 +295,15 @@ mod tests { #[test] fn generate_trace() { - for op in [FpOperation::Add, FpOperation::Mul, FpOperation::Sub].iter() { + for op in [ + FieldOperation::Add, + FieldOperation::Mul, + FieldOperation::Sub, + ] + .iter() + { println!("op: {:?}", op); - let chip: FpOpChip = FpOpChip::new(*op); + let chip: FieldOpChip = FieldOpChip::new(*op); let mut shard = ExecutionRecord::default(); let _: RowMajorMatrix = chip.generate_trace(&mut shard); // println!("{:?}", trace.values) @@ -309,10 +315,10 @@ mod tests { let config = BabyBearPoseidon2::new(); for op in [ - FpOperation::Add, - FpOperation::Sub, - FpOperation::Mul, - FpOperation::Div, + FieldOperation::Add, + FieldOperation::Sub, + FieldOperation::Mul, + FieldOperation::Div, ] .iter() { @@ -320,7 +326,7 @@ mod tests { let mut challenger = config.challenger(); - let chip: FpOpChip = FpOpChip::new(*op); + let chip: FieldOpChip = FieldOpChip::new(*op); let mut shard = ExecutionRecord::default(); let trace: RowMajorMatrix = chip.generate_trace(&mut shard); let proof = prove::(&config, &chip, &mut challenger, trace); diff --git a/core/src/operations/field/field_sqrt.rs b/core/src/operations/field/field_sqrt.rs index d1fe874fe8..604fa98785 100644 --- a/core/src/operations/field/field_sqrt.rs +++ b/core/src/operations/field/field_sqrt.rs @@ -1,4 +1,4 @@ -use super::field_op::FpOpCols; +use super::field_op::FieldOpCols; use super::params::Limbs; use crate::air::CurtaAirBuilder; use crate::utils::ec::field::FieldParameters; @@ -13,15 +13,15 @@ use valida_derive::AlignedBorrow; /// limb lives. #[derive(Debug, Clone, AlignedBorrow)] #[repr(C)] -pub struct FpSqrtCols { +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: FpOpCols, + pub multiplication: FieldOpCols, } -impl FpSqrtCols { +impl FieldSqrtCols { /// Populates the trace. /// /// `P` is the parameter of the field that each limb lives in. @@ -32,24 +32,23 @@ impl FpSqrtCols { ) -> BigUint { let sqrt = sqrt_fn(a); - // Use FpOpCols to compute result * result. + // Use FieldOpCols to compute result * result. let sqrt_squared = self.multiplication - .populate::

(&sqrt, &sqrt, super::field_op::FpOperation::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 FpSqrtCols. We will receive the value a again in the + // 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 FpSqrtCols { +impl FieldSqrtCols { /// Calculates the square root of `a`. pub fn eval, P: FieldParameters>( &self, @@ -70,7 +69,7 @@ impl FpSqrtCols { builder, &sqrt, &sqrt, - super::field_op::FpOperation::Mul, + super::field_op::FieldOperation::Mul, ); } } @@ -81,7 +80,7 @@ mod tests { use p3_air::BaseAir; use p3_field::{Field, PrimeField32}; - use super::{FpSqrtCols, Limbs}; + use super::{FieldSqrtCols, Limbs}; use crate::air::MachineAir; use crate::utils::ec::edwards::ed25519::{ed25519_sqrt, Ed25519BaseField}; use crate::utils::ec::field::FieldParameters; @@ -100,7 +99,7 @@ mod tests { #[derive(AlignedBorrow, Debug, Clone)] pub struct TestCols { pub a: Limbs, - pub sqrt: FpSqrtCols, + pub sqrt: FieldSqrtCols, } pub const NUM_TEST_COLS: usize = size_of::>(); diff --git a/core/src/syscall/precompiles/edwards/ed_add.rs b/core/src/syscall/precompiles/edwards/ed_add.rs index 9e169a0500..0bdf3a1595 100644 --- a/core/src/syscall/precompiles/edwards/ed_add.rs +++ b/core/src/syscall/precompiles/edwards/ed_add.rs @@ -4,10 +4,10 @@ use crate::field::event::FieldEvent; use crate::memory::MemoryCols; use crate::memory::MemoryReadCols; use crate::memory::MemoryWriteCols; -use crate::operations::field::fp_den::FpDenCols; -use crate::operations::field::fp_inner_product::FpInnerProductCols; -use crate::operations::field::fp_op::FpOpCols; -use crate::operations::field::fp_op::FpOperation; +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; @@ -53,14 +53,14 @@ pub struct EdAddAssignCols { pub q_ptr_access: MemoryReadCols, pub p_access: [MemoryWriteCols; 16], pub q_access: [MemoryReadCols; 16], - pub(crate) x3_numerator: FpInnerProductCols, - pub(crate) y3_numerator: FpInnerProductCols, - pub(crate) x1_mul_y1: FpOpCols, - pub(crate) x2_mul_y2: FpOpCols, - pub(crate) f: FpOpCols, - pub(crate) d_mul_f: FpOpCols, - pub(crate) x3_ins: FpDenCols, - pub(crate) y3_ins: FpDenCols, + 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)] @@ -74,7 +74,7 @@ impl EdAddAssignChip { _marker: PhantomData, } } - fn populate_fp_ops( + fn populate_field_ops( cols: &mut EdAddAssignCols, p_x: BigUint, p_y: BigUint, @@ -89,18 +89,18 @@ impl EdAddAssignChip { .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, FpOperation::Mul); + .populate::(&p_x, &p_y, FieldOperation::Mul); let x2_mul_y2 = cols .x2_mul_y2 - .populate::(&q_x, &q_y, FpOperation::Mul); + .populate::(&q_x, &q_y, FieldOperation::Mul); let f = cols .f - .populate::(&x1_mul_y1, &x2_mul_y2, FpOperation::Mul); + .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, FpOperation::Mul); + .populate::(&f, &d, FieldOperation::Mul); cols.x3_ins .populate::(&x3_numerator, &d_mul_f, true); @@ -159,7 +159,7 @@ impl MachineAir for Ed cols.p_ptr = F::from_canonical_u32(event.p_ptr); cols.q_ptr = F::from_canonical_u32(event.q_ptr); - Self::populate_fp_ops(cols, p_x, p_y, q_x, q_y); + Self::populate_field_ops(cols, p_x, p_y, q_x, q_y); // Populate the memory access columns. let mut new_field_events = Vec::new(); @@ -184,7 +184,7 @@ impl MachineAir for Ed let mut row = [F::zero(); NUM_ED_ADD_COLS]; let cols: &mut EdAddAssignCols = row.as_mut_slice().borrow_mut(); let zero = BigUint::zero(); - Self::populate_fp_ops(cols, zero.clone(), zero.clone(), zero.clone(), zero); + Self::populate_field_ops(cols, zero.clone(), zero.clone(), zero.clone(), zero); row }); @@ -225,14 +225,14 @@ where // f = x1 * x2 * y1 * y2. row.x1_mul_y1 - .eval::(builder, &x1, &y1, FpOperation::Mul); + .eval::(builder, &x1, &y1, FieldOperation::Mul); row.x2_mul_y2 - .eval::(builder, &x2, &y2, FpOperation::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, FpOperation::Mul); + .eval::(builder, &x1_mul_y1, &x2_mul_y2, FieldOperation::Mul); // d * f. let f = row.f.result; @@ -240,7 +240,7 @@ where let d_const = E::BaseField::to_limbs_field::(&d_biguint); let d_const_expr = Limbs::(d_const.0.map(|x| x.into())); row.d_mul_f - .eval::(builder, &f, &d_const_expr, FpOperation::Mul); + .eval::(builder, &f, &d_const_expr, FieldOperation::Mul); let d_mul_f = row.d_mul_f.result; diff --git a/core/src/syscall/precompiles/edwards/ed_decompress.rs b/core/src/syscall/precompiles/edwards/ed_decompress.rs index 9b45c29fe4..292f0733c7 100644 --- a/core/src/syscall/precompiles/edwards/ed_decompress.rs +++ b/core/src/syscall/precompiles/edwards/ed_decompress.rs @@ -6,9 +6,9 @@ use crate::cpu::MemoryReadRecord; use crate::cpu::MemoryWriteRecord; use crate::memory::MemoryReadCols; use crate::memory::MemoryWriteCols; -use crate::operations::field::fp_op::FpOpCols; -use crate::operations::field::fp_op::FpOperation; -use crate::operations::field::fp_sqrt::FpSqrtCols; +use crate::operations::field::field_op::FieldOpCols; +use crate::operations::field::field_op::FieldOperation; +use crate::operations::field::field_sqrt::FieldSqrtCols; use crate::runtime::ExecutionRecord; use crate::runtime::Syscall; use crate::syscall::precompiles::SyscallContext; @@ -68,13 +68,13 @@ pub struct EdDecompressCols { pub ptr: T, pub x_access: [MemoryWriteCols; NUM_WORDS_FIELD_ELEMENT], pub y_access: [MemoryReadCols; NUM_WORDS_FIELD_ELEMENT], - pub(crate) yy: FpOpCols, - pub(crate) u: FpOpCols, - pub(crate) dyy: FpOpCols, - pub(crate) v: FpOpCols, - pub(crate) u_div_v: FpOpCols, - pub(crate) x: FpSqrtCols, - pub(crate) neg_x: FpOpCols, + 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 { @@ -94,23 +94,23 @@ impl EdDecompressCols { } let y = &BigUint::from_bytes_le(&event.y_bytes); - self.populate_fp_ops::(y); + self.populate_field_ops::(y); shard.field_events.append(&mut new_field_events); } - fn populate_fp_ops(&mut self, y: &BigUint) { + fn populate_field_ops(&mut self, y: &BigUint) { let one = BigUint::one(); - let yy = self.yy.populate::

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

(&yy, &one, FpOperation::Sub); + 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, FpOperation::Mul); - let v = self.v.populate::

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

(&u, &v, FpOperation::Div); + .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, FpOperation::Sub); + .populate::

(&BigUint::zero(), &x, FieldOperation::Sub); } } @@ -128,31 +128,35 @@ impl EdDecompressCols { let y = limbs_from_prev_access(&self.y_access); self.yy - .eval::(builder, &y, &y, FpOperation::Mul); + .eval::(builder, &y, &y, FieldOperation::Mul); self.u.eval::( builder, &self.yy.result, &[AB::Expr::one()].iter(), - FpOperation::Sub, + FieldOperation::Sub, ); let d_biguint = E::d_biguint(); let d_const = E::BaseField::to_limbs_field::(&d_biguint); self.dyy - .eval::(builder, &d_const, &self.yy.result, FpOperation::Mul); + .eval::(builder, &d_const, &self.yy.result, FieldOperation::Mul); self.v.eval::( builder, &[AB::Expr::one()].iter(), &self.dyy.result, - FpOperation::Add, + FieldOperation::Add, + ); + self.u_div_v.eval::( + builder, + &self.u.result, + &self.v.result, + FieldOperation::Div, ); - self.u_div_v - .eval::(builder, &self.u.result, &self.v.result, FpOperation::Div); self.x.eval::(builder, &self.u_div_v.result); self.neg_x.eval::( builder, &[AB::Expr::zero()].iter(), &self.x.multiplication.result, - FpOperation::Sub, + FieldOperation::Sub, ); for i in 0..NUM_WORDS_FIELD_ELEMENT { @@ -296,7 +300,7 @@ impl MachineAir for EdDecompressChip = row.as_mut_slice().borrow_mut(); let zero = BigUint::zero(); - cols.populate_fp_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 2c79794188..96d46a0559 100644 --- a/core/src/syscall/precompiles/k256/decompress.rs +++ b/core/src/syscall/precompiles/k256/decompress.rs @@ -6,9 +6,9 @@ use crate::cpu::MemoryReadRecord; use crate::cpu::MemoryWriteRecord; use crate::memory::MemoryReadCols; use crate::memory::MemoryReadWriteCols; -use crate::operations::field::fp_op::FpOpCols; -use crate::operations::field::fp_op::FpOperation; -use crate::operations::field::fp_sqrt::FpSqrtCols; +use crate::operations::field::field_op::FieldOpCols; +use crate::operations::field::field_op::FieldOperation; +use crate::operations::field::field_sqrt::FieldSqrtCols; use crate::runtime::ExecutionRecord; use crate::runtime::Syscall; use crate::syscall::precompiles::SyscallContext; @@ -144,11 +144,11 @@ pub struct K256DecompressCols { pub ptr: T, pub x_access: [MemoryReadCols; NUM_WORDS_FIELD_ELEMENT], pub y_access: [MemoryReadWriteCols; NUM_WORDS_FIELD_ELEMENT], - pub(crate) x_2: FpOpCols, - pub(crate) x_3: FpOpCols, - pub(crate) x_3_plus_b: FpOpCols, - pub(crate) y: FpSqrtCols, - pub(crate) neg_y: FpOpCols, + 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,29 +165,29 @@ impl K256DecompressCols { } let x = &BigUint::from_bytes_le(&event.x_bytes); - self.populate_fp_ops(x); + self.populate_field_ops(x); shard.field_events.append(&mut new_field_events); } - fn populate_fp_ops(&mut self, x: &BigUint) { + fn populate_field_ops(&mut self, x: &BigUint) { // Y = sqrt(x^3 + b) - let x_2 = self - .x_2 - .populate::(&x.clone(), &x.clone(), FpOperation::Mul); + let x_2 = + self.x_2 + .populate::(&x.clone(), &x.clone(), FieldOperation::Mul); let x_3 = self .x_3 - .populate::(&x_2, x, FpOperation::Mul); + .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, FpOperation::Add); + 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, FpOperation::Sub); + .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] }; @@ -208,12 +208,12 @@ impl K256DecompressCols { let x = limbs_from_prev_access(&self.x_access); self.x_2 - .eval::(builder, &x, &x, FpOperation::Mul); + .eval::(builder, &x, &x, FieldOperation::Mul); self.x_3.eval::( builder, &self.x_2.result, &x, - FpOperation::Mul, + FieldOperation::Mul, ); let b = Secp256k1Parameters::b_int(); let b_const = Secp256k1BaseField::to_limbs_field::(&b); @@ -221,7 +221,7 @@ impl K256DecompressCols { builder, &self.x_3.result, &b_const, - FpOperation::Add, + FieldOperation::Add, ); self.y .eval::(builder, &self.x_3_plus_b.result); @@ -229,7 +229,7 @@ impl K256DecompressCols { builder, &[AB::Expr::zero()].iter(), &self.y.multiplication.result, - FpOperation::Sub, + FieldOperation::Sub, ); // Constrain decomposition of least significant byte of Y into `y_least_bits` @@ -335,7 +335,7 @@ impl MachineAir for K256DecompressChip { .unwrap(); cols.x_access[i].access.value = Word(word_bytes); } - cols.populate_fp_ops(&dummy_value); + cols.populate_field_ops(&dummy_value); row }); diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs index 536b3a5884..d8f291d776 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_add.rs @@ -3,8 +3,8 @@ use crate::air::MachineAir; use crate::memory::MemoryCols; use crate::memory::MemoryReadCols; use crate::memory::MemoryWriteCols; -use crate::operations::field::fp_op::FpOpCols; -use crate::operations::field::fp_op::FpOperation; +use crate::operations::field::field_op::FieldOpCols; +use crate::operations::field::field_op::FieldOperation; use crate::operations::field::params::NUM_LIMBS; use crate::runtime::ExecutionRecord; use crate::runtime::Register; @@ -49,15 +49,15 @@ pub struct WeierstrassAddAssignCols { pub q_ptr_access: MemoryReadCols, pub p_access: [MemoryWriteCols; NUM_WORDS_EC_POINT], pub q_access: [MemoryReadCols; NUM_WORDS_EC_POINT], - pub(crate) slope_denominator: FpOpCols, - pub(crate) slope_numerator: FpOpCols, - pub(crate) slope: FpOpCols, - pub(crate) slope_squared: FpOpCols, - pub(crate) p_x_plus_q_x: FpOpCols, - pub(crate) x3_ins: FpOpCols, - pub(crate) p_x_minus_x: FpOpCols, - pub(crate) y3_ins: FpOpCols, - pub(crate) slope_times_p_x_minus_x: FpOpCols, + 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 +84,7 @@ impl WeierstrassAddAssignChip { } } - fn populate_fp_ops( + fn populate_field_ops( cols: &mut WeierstrassAddAssignCols, p_x: BigUint, p_y: BigUint, @@ -98,16 +98,16 @@ impl WeierstrassAddAssignChip { let slope = { let slope_numerator = cols.slope_numerator - .populate::(&q_y, &p_y, FpOperation::Sub); + .populate::(&q_y, &p_y, FieldOperation::Sub); let slope_denominator = cols.slope_denominator - .populate::(&q_x, &p_x, FpOperation::Sub); + .populate::(&q_x, &p_x, FieldOperation::Sub); cols.slope.populate::( &slope_numerator, &slope_denominator, - FpOperation::Div, + FieldOperation::Div, ) }; @@ -115,26 +115,29 @@ impl WeierstrassAddAssignChip { let x = { let slope_squared = cols.slope_squared - .populate::(&slope, &slope, FpOperation::Mul); + .populate::(&slope, &slope, FieldOperation::Mul); let p_x_plus_q_x = cols.p_x_plus_q_x - .populate::(&p_x, &q_x, FpOperation::Add); + .populate::(&p_x, &q_x, FieldOperation::Add); cols.x3_ins - .populate::(&slope_squared, &p_x_plus_q_x, FpOperation::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, FpOperation::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, - FpOperation::Mul, + FieldOperation::Mul, + ); + cols.y3_ins.populate::( + &slope_times_p_x_minus_x, + &p_y, + FieldOperation::Sub, ); - cols.y3_ins - .populate::(&slope_times_p_x_minus_x, &p_y, FpOperation::Sub); } } } @@ -179,7 +182,7 @@ impl MachineAir cols.p_ptr = F::from_canonical_u32(event.p_ptr); cols.q_ptr = F::from_canonical_u32(event.q_ptr); - Self::populate_fp_ops(cols, p_x, p_y, q_x, q_y); + 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 { @@ -199,7 +202,7 @@ impl MachineAir 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_fp_ops(cols, zero.clone(), zero.clone(), zero.clone(), zero); + Self::populate_field_ops(cols, zero.clone(), zero.clone(), zero.clone(), zero); row }); @@ -237,21 +240,21 @@ where builder, &q_y, &p_y, - FpOperation::Sub, + FieldOperation::Sub, ); row.slope_denominator.eval::( builder, &q_x, &p_x, - FpOperation::Sub, + FieldOperation::Sub, ); row.slope.eval::( builder, &row.slope_numerator.result, &row.slope_denominator.result, - FpOperation::Div, + FieldOperation::Div, ); row.slope.result @@ -263,17 +266,21 @@ where builder, &slope, &slope, - FpOperation::Mul, + FieldOperation::Mul, ); - row.p_x_plus_q_x - .eval::(builder, &p_x, &q_x, FpOperation::Add); + row.p_x_plus_q_x.eval::( + builder, + &p_x, + &q_x, + FieldOperation::Add, + ); row.x3_ins.eval::( builder, &row.slope_squared.result, &row.p_x_plus_q_x.result, - FpOperation::Sub, + FieldOperation::Sub, ); row.x3_ins.result @@ -282,20 +289,20 @@ where // y = slope * (p.x - x_3n) - q.y. { row.p_x_minus_x - .eval::(builder, &p_x, &x, FpOperation::Sub); + .eval::(builder, &p_x, &x, FieldOperation::Sub); row.slope_times_p_x_minus_x.eval::( builder, &slope, &row.p_x_minus_x.result, - FpOperation::Mul, + FieldOperation::Mul, ); row.y3_ins.eval::( builder, &row.slope_times_p_x_minus_x.result, &p_y, - FpOperation::Sub, + FieldOperation::Sub, ); } diff --git a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs index 2cd5f91b08..f8629458dc 100644 --- a/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs +++ b/core/src/syscall/precompiles/weierstrass/weierstrass_double.rs @@ -2,8 +2,8 @@ use crate::air::CurtaAirBuilder; use crate::air::MachineAir; use crate::memory::MemoryCols; use crate::memory::MemoryWriteCols; -use crate::operations::field::fp_op::FpOpCols; -use crate::operations::field::fp_op::FpOperation; +use crate::operations::field::field_op::FieldOpCols; +use crate::operations::field::field_op::FieldOperation; use crate::operations::field::params::NUM_LIMBS; use crate::runtime::ExecutionRecord; use crate::runtime::Syscall; @@ -45,17 +45,17 @@ pub struct WeierstrassDoubleAssignCols { pub clk: T, pub p_ptr: T, pub p_access: [MemoryWriteCols; NUM_WORDS_EC_POINT], - pub(crate) slope_denominator: FpOpCols, - pub(crate) slope_numerator: FpOpCols, - pub(crate) slope: FpOpCols, - pub(crate) p_x_squared: FpOpCols, - pub(crate) p_x_squared_times_3: FpOpCols, - pub(crate) slope_squared: FpOpCols, - pub(crate) p_x_plus_p_x: FpOpCols, - pub(crate) x3_ins: FpOpCols, - pub(crate) p_x_minus_x: FpOpCols, - pub(crate) y3_ins: FpOpCols, - pub(crate) slope_times_p_x_minus_x: FpOpCols, + 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)] @@ -82,7 +82,7 @@ impl WeierstrassDoubleAssignChip { } } - fn populate_fp_ops( + fn populate_field_ops( cols: &mut WeierstrassDoubleAssignCols, p_x: BigUint, p_y: BigUint, @@ -97,16 +97,16 @@ impl WeierstrassDoubleAssignChip { let slope_numerator = { let p_x_squared = cols.p_x_squared - .populate::(&p_x, &p_x, FpOperation::Mul); + .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), - FpOperation::Mul, + FieldOperation::Mul, ); cols.slope_numerator.populate::( &a, &p_x_squared_times_3, - FpOperation::Add, + FieldOperation::Add, ) }; @@ -114,13 +114,13 @@ impl WeierstrassDoubleAssignChip { let slope_denominator = cols.slope_denominator.populate::( &BigUint::from(2u32), &p_y, - FpOperation::Mul, + FieldOperation::Mul, ); cols.slope.populate::( &slope_numerator, &slope_denominator, - FpOperation::Div, + FieldOperation::Div, ) }; @@ -128,26 +128,29 @@ impl WeierstrassDoubleAssignChip { let x = { let slope_squared = cols.slope_squared - .populate::(&slope, &slope, FpOperation::Mul); + .populate::(&slope, &slope, FieldOperation::Mul); let p_x_plus_p_x = cols.p_x_plus_p_x - .populate::(&p_x, &p_x, FpOperation::Add); + .populate::(&p_x, &p_x, FieldOperation::Add); cols.x3_ins - .populate::(&slope_squared, &p_x_plus_p_x, FpOperation::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, FpOperation::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, - FpOperation::Mul, + FieldOperation::Mul, + ); + cols.y3_ins.populate::( + &slope_times_p_x_minus_x, + &p_y, + FieldOperation::Sub, ); - cols.y3_ins - .populate::(&slope_times_p_x_minus_x, &p_y, FpOperation::Sub); } } } @@ -188,7 +191,7 @@ impl MachineAir cols.clk = F::from_canonical_u32(event.clk); cols.p_ptr = F::from_canonical_u32(event.p_ptr); - Self::populate_fp_ops(cols, p_x, p_y); + Self::populate_field_ops(cols, p_x, p_y); // Populate the memory access columns. for i in 0..NUM_WORDS_EC_POINT { @@ -203,7 +206,7 @@ impl MachineAir 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_fp_ops(cols, zero.clone(), zero.clone()); + Self::populate_field_ops(cols, zero.clone(), zero.clone()); row }); @@ -243,21 +246,21 @@ where builder, &p_x, &p_x, - FpOperation::Mul, + FieldOperation::Mul, ); row.p_x_squared_times_3.eval::( builder, &row.p_x_squared.result, &limbs_from_biguint::(&BigUint::from(3u32)), - FpOperation::Mul, + FieldOperation::Mul, ); row.slope_numerator.eval::( builder, &a, &row.p_x_squared_times_3.result, - FpOperation::Add, + FieldOperation::Add, ); }; @@ -266,14 +269,14 @@ where builder, &limbs_from_biguint::(&BigUint::from(2u32)), &p_y, - FpOperation::Mul, + FieldOperation::Mul, ); row.slope.eval::( builder, &row.slope_numerator.result, &row.slope_denominator.result, - FpOperation::Div, + FieldOperation::Div, ); row.slope.result @@ -285,15 +288,19 @@ where builder, &slope, &slope, - FpOperation::Mul, + FieldOperation::Mul, + ); + row.p_x_plus_p_x.eval::( + builder, + &p_x, + &p_x, + FieldOperation::Add, ); - row.p_x_plus_p_x - .eval::(builder, &p_x, &p_x, FpOperation::Add); row.x3_ins.eval::( builder, &row.slope_squared.result, &row.p_x_plus_p_x.result, - FpOperation::Sub, + FieldOperation::Sub, ); row.x3_ins.result }; @@ -301,18 +308,18 @@ where // y = slope * (p.x - x) - p.y. { row.p_x_minus_x - .eval::(builder, &p_x, &x, FpOperation::Sub); + .eval::(builder, &p_x, &x, FieldOperation::Sub); row.slope_times_p_x_minus_x.eval::( builder, &slope, &row.p_x_minus_x.result, - FpOperation::Mul, + FieldOperation::Mul, ); row.y3_ins.eval::( builder, &row.slope_times_p_x_minus_x.result, &p_y, - FpOperation::Sub, + FieldOperation::Sub, ); }