diff --git a/msm/src/columns.rs b/msm/src/columns.rs index 4a5a5b54d3..6132417eba 100644 --- a/msm/src/columns.rs +++ b/msm/src/columns.rs @@ -1,7 +1,6 @@ +use crate::N_LIMBS; use kimchi::circuits::expr::{Domain, GenericColumn}; -use crate::LIMBS_NUM; - // @volhovm: maybe this needs to be a trait /// Describe a generic indexed variable X_{i}. #[derive(PartialEq, Eq, Clone, Copy, Debug)] @@ -37,8 +36,8 @@ pub enum MSMColumnIndexer { impl ColumnIndexer for MSMColumnIndexer { fn ix_to_column(self) -> Column { let to_column_inner = |offset, i| { - assert!(i < LIMBS_NUM); - Column::X(LIMBS_NUM * offset + i) + assert!(i < N_LIMBS); + Column::X(N_LIMBS * offset + i) }; match self { MSMColumnIndexer::A(i) => to_column_inner(0, i), diff --git a/msm/src/constraint.rs b/msm/src/constraint.rs index f995e0defc..2a8d024a3c 100644 --- a/msm/src/constraint.rs +++ b/msm/src/constraint.rs @@ -7,7 +7,7 @@ use crate::{ lookups::LookupTableIDs, proof::ProofInputs, witness::Witness, - {BN254G1Affine, Ff1, Fp, LIMBS_NUM, MSM_FFADD_N_COLUMNS}, + {BN254G1Affine, Ff1, Fp, MSM_FFADD_N_COLUMNS, N_LIMBS}, }; use kimchi::{ circuits::{ @@ -58,17 +58,17 @@ use o1_utils::{field_helpers::FieldHelpers, foreign_field::ForeignElement}; pub type MSMExpr = Expr, Column>; // TODO use more foreign_field.rs with from/to bigint conversion -fn limb_decompose(input: &Ff1) -> [Fp; LIMBS_NUM] { +fn limb_decompose(input: &Ff1) -> [Fp; N_LIMBS] { let input_bi: BigUint = FieldHelpers::to_biguint(input); - let ff_el: ForeignElement = ForeignElement::from_biguint(input_bi); + let ff_el: ForeignElement = ForeignElement::from_biguint(input_bi); ff_el.limbs } pub struct WitnessColumnsIndexer { - pub(crate) a: [T; LIMBS_NUM], - pub(crate) b: [T; LIMBS_NUM], - pub(crate) c: [T; LIMBS_NUM], - pub(crate) d: [T; LIMBS_NUM], + pub(crate) a: [T; N_LIMBS], + pub(crate) b: [T; N_LIMBS], + pub(crate) c: [T; N_LIMBS], + pub(crate) d: [T; N_LIMBS], } #[allow(dead_code)] @@ -99,11 +99,11 @@ impl MSMCircuitEnv { c: wc_c, d: wc_d, } = wc; - for i in 0..LIMBS_NUM { + for i in 0..N_LIMBS { cols[i].push(wc_a[i]); - cols[LIMBS_NUM + i].push(wc_b[i]); - cols[2 * LIMBS_NUM + i].push(wc_c[i]); - cols[3 * LIMBS_NUM + i].push(wc_d[i]); + cols[N_LIMBS + i].push(wc_b[i]); + cols[2 * N_LIMBS + i].push(wc_c[i]); + cols[3 * N_LIMBS + i].push(wc_d[i]); } } @@ -116,7 +116,7 @@ impl MSMCircuitEnv { /// Access exprs generated in the environment so far. pub fn get_exprs_add(&self) -> Vec> { let mut limb_exprs: Vec<_> = vec![]; - for i in 0..LIMBS_NUM { + for i in 0..N_LIMBS { let limb_constraint = { let a_i = MSMExpr::Atom( ExprInner::>, Column>::Cell(Variable { @@ -142,7 +142,7 @@ impl MSMCircuitEnv { // TEST pub fn get_exprs_mul(&self) -> Vec> { let mut limb_exprs: Vec<_> = vec![]; - for i in 0..LIMBS_NUM { + for i in 0..N_LIMBS { let limb_constraint = { let a_i = MSMExpr::Atom( ExprInner::>, Column>::Cell(Variable { @@ -180,17 +180,17 @@ impl MSMCircuitEnv { } pub fn add_test_addition(&mut self, a: Ff1, b: Ff1) { - let a_limbs: [Fp; LIMBS_NUM] = limb_decompose(&a); - let b_limbs: [Fp; LIMBS_NUM] = limb_decompose(&b); + let a_limbs: [Fp; N_LIMBS] = limb_decompose(&a); + let b_limbs: [Fp; N_LIMBS] = limb_decompose(&b); let c_limbs_vec: Vec = a_limbs .iter() .zip(b_limbs.iter()) .map(|(ai, bi)| *ai + *bi) .collect(); - let c_limbs: [Fp; LIMBS_NUM] = c_limbs_vec + let c_limbs: [Fp; N_LIMBS] = c_limbs_vec .try_into() .unwrap_or_else(|_| panic!("Length mismatch")); - let d_limbs: [Fp; LIMBS_NUM] = [Zero::zero(); LIMBS_NUM]; + let d_limbs: [Fp; N_LIMBS] = [Zero::zero(); N_LIMBS]; self.witness_raw.push(WitnessColumnsIndexer { a: a_limbs, @@ -201,18 +201,18 @@ impl MSMCircuitEnv { } pub fn add_test_multiplication(&mut self, a: Ff1, b: Ff1) { - let a_limbs: [Fp; LIMBS_NUM] = limb_decompose(&a); - let b_limbs: [Fp; LIMBS_NUM] = limb_decompose(&b); + let a_limbs: [Fp; N_LIMBS] = limb_decompose(&a); + let b_limbs: [Fp; N_LIMBS] = limb_decompose(&b); let d_limbs_vec: Vec = a_limbs .iter() .zip(b_limbs.iter()) .map(|(ai, bi)| *ai * *bi) .collect(); - let d_limbs: [Fp; LIMBS_NUM] = d_limbs_vec + let d_limbs: [Fp; N_LIMBS] = d_limbs_vec .try_into() .unwrap_or_else(|_| panic!("Length mismatch")); - let c_limbs: [Fp; LIMBS_NUM] = [Zero::zero(); LIMBS_NUM]; + let c_limbs: [Fp; N_LIMBS] = [Zero::zero(); N_LIMBS]; self.witness_raw.push(WitnessColumnsIndexer { a: a_limbs, diff --git a/msm/src/lib.rs b/msm/src/lib.rs index 7b80aec5f6..61b39482a9 100644 --- a/msm/src/lib.rs +++ b/msm/src/lib.rs @@ -32,7 +32,7 @@ pub const LIMB_BITSIZE: usize = 15; /// Number of limbs representing one foreign field element (either /// [`Ff1`] or [`Ff2`]). -pub const LIMBS_NUM: usize = 17; +pub const N_LIMBS: usize = 17; pub type BN254 = ark_ec::bn::Bn; pub type BN254G1Affine = ::G1Affine; @@ -41,7 +41,7 @@ pub type BN254G2Affine = ::G2Affine; /// Number of columns /// FIXME: we must move it into the subdirectory of the /// foreign field addition circuit -pub const MSM_FFADD_N_COLUMNS: usize = 4 * LIMBS_NUM; +pub const MSM_FFADD_N_COLUMNS: usize = 4 * N_LIMBS; /// The native field we are working with. pub type Fp = ark_bn254::Fr; diff --git a/msm/src/serialization/constraints.rs b/msm/src/serialization/constraints.rs index 1b9079cea2..764dbf13e1 100644 --- a/msm/src/serialization/constraints.rs +++ b/msm/src/serialization/constraints.rs @@ -4,7 +4,7 @@ use kimchi::circuits::{ gate::CurrOrNext, }; -use crate::{columns::Column, serialization::N_INTERMEDIATE_LIMBS, LIMBS_NUM}; +use crate::{columns::Column, serialization::N_INTERMEDIATE_LIMBS, N_LIMBS}; use super::interpreter::InterpreterEnv; @@ -37,11 +37,11 @@ impl InterpreterEnv for Env { fn get_column_for_intermediate_limb(j: usize) -> Self::Position { assert!(j < N_INTERMEDIATE_LIMBS); - Column::X(3 + LIMBS_NUM + j) + Column::X(3 + N_LIMBS + j) } fn get_column_for_msm_limb(j: usize) -> Self::Position { - assert!(j < LIMBS_NUM); + assert!(j < N_LIMBS); Column::X(3 + j) } diff --git a/msm/src/serialization/main.rs b/msm/src/serialization/main.rs index d4ea352fe8..c4e14079fc 100644 --- a/msm/src/serialization/main.rs +++ b/msm/src/serialization/main.rs @@ -10,9 +10,9 @@ use kimchi_msm::proof::ProofInputs; use kimchi_msm::prover::prove; use kimchi_msm::serialization::interpreter::deserialize_field_element; use kimchi_msm::verifier::verify; -use kimchi_msm::{BaseSponge, Fp, OpeningProof, ScalarSponge, BN254, DOMAIN_SIZE, LIMBS_NUM}; +use kimchi_msm::{BaseSponge, Fp, OpeningProof, ScalarSponge, BN254, DOMAIN_SIZE, N_LIMBS}; -const SERIALIZATION_N_COLUMNS: usize = 3 + N_INTERMEDIATE_LIMBS + LIMBS_NUM; +const SERIALIZATION_N_COLUMNS: usize = 3 + N_INTERMEDIATE_LIMBS + N_LIMBS; pub fn main() { // FIXME: use a proper RNG @@ -35,11 +35,11 @@ pub fn main() { for i in 0..3 { witness.cols[i].push(env.current_kimchi_limbs[i]); } - for i in 0..LIMBS_NUM { + for i in 0..N_LIMBS { witness.cols[3 + i].push(env.msm_limbs[i]); } for i in 0..N_INTERMEDIATE_LIMBS { - witness.cols[3 + LIMBS_NUM + i].push(env.intermediate_limbs[i]); + witness.cols[3 + N_LIMBS + i].push(env.intermediate_limbs[i]); } } diff --git a/msm/src/serialization/witness.rs b/msm/src/serialization/witness.rs index 5e1c64f707..a3651291a8 100644 --- a/msm/src/serialization/witness.rs +++ b/msm/src/serialization/witness.rs @@ -3,7 +3,7 @@ use o1_utils::FieldHelpers; use crate::columns::Column; use crate::serialization::interpreter::InterpreterEnv; -use crate::LIMBS_NUM; +use crate::N_LIMBS; use super::N_INTERMEDIATE_LIMBS; @@ -11,7 +11,7 @@ use super::N_INTERMEDIATE_LIMBS; pub struct Env { pub current_kimchi_limbs: [Fp; 3], /// The LIMB_NUM limbs that is used to encode a field element for the MSM - pub msm_limbs: [Fp; LIMBS_NUM], + pub msm_limbs: [Fp; N_LIMBS], /// Used for the decomposition in base 4 of the last limb of the foreign /// field Kimchi gate pub intermediate_limbs: [Fp; N_INTERMEDIATE_LIMBS], @@ -39,7 +39,7 @@ impl InterpreterEnv for Env { fn get_column_for_intermediate_limb(j: usize) -> Self::Position { assert!(j < N_INTERMEDIATE_LIMBS); - Column::X(3 + LIMBS_NUM + j) + Column::X(3 + N_LIMBS + j) } fn copy(&mut self, x: &Self::Variable, position: Self::Position) -> Self::Variable { @@ -48,7 +48,7 @@ impl InterpreterEnv for Env { } fn get_column_for_msm_limb(j: usize) -> Self::Position { - assert!(j < LIMBS_NUM); + assert!(j < N_LIMBS); Column::X(3 + j) } @@ -79,10 +79,10 @@ impl Env { Column::X(i) => { if i < 3 { self.current_kimchi_limbs[i] = value - } else if i < 3 + LIMBS_NUM { + } else if i < 3 + N_LIMBS { self.msm_limbs[i - 3] = value; - } else if i < 3 + LIMBS_NUM + N_INTERMEDIATE_LIMBS { - self.intermediate_limbs[i - 3 - LIMBS_NUM] = value; + } else if i < 3 + N_LIMBS + N_INTERMEDIATE_LIMBS { + self.intermediate_limbs[i - 3 - N_LIMBS] = value; } else { panic!("Invalid column index") } @@ -95,7 +95,7 @@ impl Env { pub fn create() -> Self { Self { current_kimchi_limbs: [Fp::zero(); 3], - msm_limbs: [Fp::zero(); LIMBS_NUM], + msm_limbs: [Fp::zero(); N_LIMBS], intermediate_limbs: [Fp::zero(); N_INTERMEDIATE_LIMBS], } } @@ -106,7 +106,7 @@ mod tests { use std::str::FromStr; use crate::serialization::N_INTERMEDIATE_LIMBS; - use crate::{LIMBS_NUM, LIMB_BITSIZE}; + use crate::{LIMB_BITSIZE, N_LIMBS}; use super::Env; use crate::serialization::interpreter::deserialize_field_element; @@ -179,7 +179,7 @@ mod tests { } // Checking msm limbs - for i in 0..LIMBS_NUM { + for i in 0..N_LIMBS { let le_bits: &[bool] = &bits .clone() .into_iter()