Skip to content

Commit

Permalink
[#1793] Get rid of unnecessary WitnessColumnIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
volhovm committed Mar 5, 2024
1 parent 2c04f85 commit 4184873
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 46 deletions.
3 changes: 3 additions & 0 deletions msm/src/ffa/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::columns::ColumnIndexer;

use crate::LIMBS_NUM;

/// Number of columns in the FFA circuits.
pub const FFA_N_COLUMNS: usize = 4 * LIMBS_NUM;

#[derive(Clone, Copy, Debug, PartialEq)]
/// Column indexer for MSM columns
pub enum FFAColumnIndexer {
Expand Down
59 changes: 22 additions & 37 deletions msm/src/ffa/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use num_bigint::BigUint;
use crate::{
columns::{Column, ColumnIndexer},
expr::MSMExpr,
ffa::columns::FFAColumnIndexer,
ffa::columns::{FFAColumnIndexer, FFA_N_COLUMNS},
lookups::LookupTableIDs,
proof::ProofInputs,
witness::Witness,
{BN254G1Affine, Ff1, Fp, LIMBS_NUM, MSM_FFADD_N_COLUMNS},
{BN254G1Affine, Ff1, Fp, LIMBS_NUM},
};
use kimchi::{
circuits::{
Expand All @@ -30,19 +30,12 @@ fn limb_decompose(input: &Ff1) -> [Fp; LIMBS_NUM] {
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],
}

#[allow(dead_code)]
/// Builder environment for a native group `G`.
pub struct MSMCircuitEnv<G: KimchiCurve> {
/// Aggregated witness, in raw form. For accessing [`Witness`], see the
/// `get_witness` method.
witness_raw: Vec<WitnessColumnsIndexer<G::ScalarField>>,
witness_raw: Vec<Witness<FFA_N_COLUMNS, G::ScalarField>>,
}

impl MSMCircuitEnv<BN254G1Affine> {
Expand All @@ -55,21 +48,13 @@ impl MSMCircuitEnv<BN254G1Affine> {
/// Each WitnessColumn stands for both one row and multirow. This
/// function converts from a vector of one-row instantiation to a
/// single multi-row form (which is a `Witness`).
pub fn get_witness(&self) -> ProofInputs<MSM_FFADD_N_COLUMNS, BN254G1Affine, LookupTableIDs> {
let mut cols: [Vec<Fp>; MSM_FFADD_N_COLUMNS] = std::array::from_fn(|_| vec![]);

for wc in &self.witness_raw {
let WitnessColumnsIndexer {
a: wc_a,
b: wc_b,
c: wc_c,
d: wc_d,
} = wc;
for i in 0..LIMBS_NUM {
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]);
pub fn get_witness(&self) -> ProofInputs<FFA_N_COLUMNS, BN254G1Affine, LookupTableIDs> {
let mut cols: [Vec<Fp>; FFA_N_COLUMNS] = std::array::from_fn(|_| vec![]);

for w in &self.witness_raw {
let Witness { cols: witness_row } = w;
for i in 0..4 * LIMBS_NUM {
cols[i].push(witness_row[i]);
}
}

Expand Down Expand Up @@ -158,12 +143,12 @@ impl MSMCircuitEnv<BN254G1Affine> {
.unwrap_or_else(|_| panic!("Length mismatch"));
let d_limbs: [Fp; LIMBS_NUM] = [Zero::zero(); LIMBS_NUM];

self.witness_raw.push(WitnessColumnsIndexer {
a: a_limbs,
b: b_limbs,
c: c_limbs,
d: d_limbs,
});
let witness_row: [Fp; 4 * LIMBS_NUM] = [a_limbs, b_limbs, c_limbs, d_limbs]
.concat()
.try_into()
.unwrap();

self.witness_raw.push(Witness { cols: witness_row });
}

pub fn add_test_multiplication(&mut self, a: Ff1, b: Ff1) {
Expand All @@ -180,11 +165,11 @@ impl MSMCircuitEnv<BN254G1Affine> {

let c_limbs: [Fp; LIMBS_NUM] = [Zero::zero(); LIMBS_NUM];

self.witness_raw.push(WitnessColumnsIndexer {
a: a_limbs,
b: b_limbs,
c: c_limbs,
d: d_limbs,
});
let witness_row: [Fp; 4 * LIMBS_NUM] = [a_limbs, b_limbs, c_limbs, d_limbs]
.concat()
.try_into()
.unwrap();

self.witness_raw.push(Witness { cols: witness_row });
}
}
5 changes: 0 additions & 5 deletions msm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ pub type BN254 = ark_ec::bn::Bn<ark_bn254::Parameters>;
pub type BN254G1Affine = <BN254 as ark_ec::PairingEngine>::G1Affine;
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;

/// The native field we are working with.
pub type Fp = ark_bn254::Fr;

Expand Down
7 changes: 3 additions & 4 deletions msm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use kimchi::circuits::domains::EvaluationDomains;
use poly_commitment::pairing_proof::PairingSRS;

use kimchi_msm::columns::Column;
use kimchi_msm::ffa::constraint::MSMCircuitEnv;
use kimchi_msm::ffa::{columns::FFA_N_COLUMNS, constraint::MSMCircuitEnv};
use kimchi_msm::lookups::LookupTableIDs;
use kimchi_msm::precomputed_srs::get_bn254_srs;
use kimchi_msm::prover::prove;
use kimchi_msm::verifier::verify;
use kimchi_msm::{
BN254G1Affine, BaseSponge, Ff1, Fp, OpeningProof, ScalarSponge, BN254, DOMAIN_SIZE,
MSM_FFADD_N_COLUMNS,
};

pub fn generate_random_msm_witness() -> MSMCircuitEnv<BN254G1Affine> {
Expand Down Expand Up @@ -54,12 +53,12 @@ pub fn main() {
ScalarSponge,
Column,
_,
MSM_FFADD_N_COLUMNS,
FFA_N_COLUMNS,
LookupTableIDs,
>(domain, &srs, &constraint_exprs, proof_inputs, &mut rng);

println!("Verifying the proof");
let verifies = verify::<_, OpeningProof, BaseSponge, ScalarSponge, MSM_FFADD_N_COLUMNS>(
let verifies = verify::<_, OpeningProof, BaseSponge, ScalarSponge, FFA_N_COLUMNS>(
domain,
&srs,
&constraint_exprs,
Expand Down

0 comments on commit 4184873

Please sign in to comment.