Skip to content

Commit

Permalink
Merge pull request #1894 from o1-labs/dw/msm-serialization-rename-var
Browse files Browse the repository at this point in the history
MSM: rename LIMBS_NUM in N_LIMBS
  • Loading branch information
dannywillems authored Mar 5, 2024
2 parents 018eec0 + d2499d6 commit d8e8236
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
7 changes: 3 additions & 4 deletions msm/src/columns.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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),
Expand Down
42 changes: 21 additions & 21 deletions msm/src/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -58,17 +58,17 @@ use o1_utils::{field_helpers::FieldHelpers, foreign_field::ForeignElement};
pub type MSMExpr<F> = Expr<ConstantExpr<F>, 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<Fp, LIMBS_NUM> = ForeignElement::from_biguint(input_bi);
let ff_el: ForeignElement<Fp, N_LIMBS> = ForeignElement::from_biguint(input_bi);
ff_el.limbs
}

pub struct WitnessColumnsIndexer<T> {
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)]
Expand Down Expand Up @@ -99,11 +99,11 @@ impl MSMCircuitEnv<BN254G1Affine> {
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]);
}
}

Expand All @@ -116,7 +116,7 @@ impl MSMCircuitEnv<BN254G1Affine> {
/// Access exprs generated in the environment so far.
pub fn get_exprs_add(&self) -> Vec<MSMExpr<Fp>> {
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::<Operations<ConstantExprInner<Fp>>, Column>::Cell(Variable {
Expand All @@ -142,7 +142,7 @@ impl MSMCircuitEnv<BN254G1Affine> {
// TEST
pub fn get_exprs_mul(&self) -> Vec<MSMExpr<Fp>> {
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::<Operations<ConstantExprInner<Fp>>, Column>::Cell(Variable {
Expand Down Expand Up @@ -180,17 +180,17 @@ impl MSMCircuitEnv<BN254G1Affine> {
}

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<Fp> = 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,
Expand All @@ -201,18 +201,18 @@ impl MSMCircuitEnv<BN254G1Affine> {
}

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<Fp> = 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,
Expand Down
4 changes: 2 additions & 2 deletions msm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ark_bn254::Parameters>;
pub type BN254G1Affine = <BN254 as ark_ec::PairingEngine>::G1Affine;
Expand All @@ -41,7 +41,7 @@ pub type BN254G2Affine = <BN254 as ark_ec::PairingEngine>::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;
Expand Down
6 changes: 3 additions & 3 deletions msm/src/serialization/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,11 +37,11 @@ impl<F: Field> InterpreterEnv<F> for Env<F> {

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

Expand Down
8 changes: 4 additions & 4 deletions msm/src/serialization/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}
}

Expand Down
20 changes: 10 additions & 10 deletions msm/src/serialization/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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;

/// Environment for the serializer interpreter
pub struct Env<Fp> {
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],
Expand Down Expand Up @@ -39,7 +39,7 @@ impl<Fp: Field> InterpreterEnv<Fp> for Env<Fp> {

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 {
Expand All @@ -48,7 +48,7 @@ impl<Fp: Field> InterpreterEnv<Fp> for Env<Fp> {
}

fn get_column_for_msm_limb(j: usize) -> Self::Position {
assert!(j < LIMBS_NUM);
assert!(j < N_LIMBS);
Column::X(3 + j)
}

Expand Down Expand Up @@ -79,10 +79,10 @@ impl<Fp: Field> Env<Fp> {
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")
}
Expand All @@ -95,7 +95,7 @@ impl<Fp: Field> Env<Fp> {
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],
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d8e8236

Please sign in to comment.