Skip to content

Commit

Permalink
[#1793] Start implementing FFAInterpreterEnv for FFA WitnessBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
volhovm committed Mar 5, 2024
1 parent fec07b2 commit 3ce8795
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
19 changes: 19 additions & 0 deletions msm/src/ffa/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ impl ColumnIndexer for FFAColumnIndexer {
}
}
}

impl FFAColumnIndexer {
pub fn column_to_ix(col: Column) -> Self {
let Column::X(pos) = col;
let upper_bound = |i: usize| (i + 1) * N_LIMBS;
let pos_map = |i: usize| pos - upper_bound(i - 1);
return if pos < upper_bound(0) {
FFAColumnIndexer::A(pos_map(0))
} else if pos < upper_bound(1) {
FFAColumnIndexer::B(pos_map(1))
} else if pos < upper_bound(2) {
FFAColumnIndexer::C(pos_map(2))
} else if pos < upper_bound(3) {
FFAColumnIndexer::D(pos_map(3))
} else {
panic!("column_to_ix: Invalid column index")
};
}
}
8 changes: 3 additions & 5 deletions msm/src/ffa/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ 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,
};
use kimchi_msm::{BaseSponge, Ff1, Fp, OpeningProof, ScalarSponge, BN254, DOMAIN_SIZE};

pub fn generate_random_msm_witness() -> FFAWitnessBuilder<BN254G1Affine> {
let mut circuit_env = FFAWitnessBuilder::<BN254G1Affine>::empty();
pub fn generate_random_msm_witness() -> FFAWitnessBuilder<Fp> {
let mut circuit_env = FFAWitnessBuilder::<Fp>::empty();
let mut rng = thread_rng();

let row_num = DOMAIN_SIZE;
Expand Down
44 changes: 39 additions & 5 deletions msm/src/ffa/witness.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use ark_ff::PrimeField;
use ark_ff::Zero;
use num_bigint::BigUint;

use crate::{
ffa::columns::FFA_N_COLUMNS,
columns::Column,
ffa::{
columns::{FFAColumnIndexer, FFA_N_COLUMNS},
interpreter::FFAInterpreterEnv,
},
lookups::LookupTableIDs,
proof::ProofInputs,
witness::Witness,
{BN254G1Affine, Ff1, Fp, N_LIMBS},
};
use kimchi::curve::KimchiCurve;
use o1_utils::{field_helpers::FieldHelpers, foreign_field::ForeignElement};

// TODO use more foreign_field.rs with from/to bigint conversion
Expand All @@ -20,19 +24,49 @@ fn limb_decompose(input: &Ff1) -> [Fp; N_LIMBS] {

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

impl WitnessBuilder<BN254G1Affine> {
impl<F: PrimeField> FFAInterpreterEnv<F> for WitnessBuilder<F> {
type Position = Column;

type Variable = F;

fn add_constraint(&mut self, cst: Self::Variable) {
assert_eq!(cst, F::zero());
}

fn constant(value: F) -> Self::Variable {
value
}

fn copy(&mut self, x: &Self::Variable, position: Self::Position) -> Self::Variable {
self.write_column(position, *x);
*x
}
}

impl<F: PrimeField> WitnessBuilder<F> {
pub fn write_column(&mut self, column: Column, _value: F) {
match FFAColumnIndexer::column_to_ix(column) {
FFAColumnIndexer::A(_i) => unimplemented!(),
FFAColumnIndexer::B(_i) => unimplemented!(),
FFAColumnIndexer::C(_i) => unimplemented!(),
FFAColumnIndexer::D(_i) => unimplemented!(),
}
}

pub fn empty() -> Self {
WitnessBuilder {
witness_raw: vec![],
}
}
}

impl WitnessBuilder<Fp> {
/// 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`).
Expand Down

0 comments on commit 3ce8795

Please sign in to comment.