Skip to content

Commit

Permalink
Merge pull request #2740 from o1-labs/dw/simplify-types-o1vm
Browse files Browse the repository at this point in the history
o1vm: simplify types in proof and prover
  • Loading branch information
dannywillems authored Nov 1, 2024
2 parents 99eb8ff + fd0e8b8 commit df2415a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
4 changes: 2 additions & 2 deletions o1vm/src/pickles/proof.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use kimchi::{curve::KimchiCurve, proof::PointEvaluations};
use poly_commitment::{ipa::OpeningProof, PolyComm};

use crate::interpreters::mips::column::N_MIPS_SEL_COLS;
use crate::interpreters::mips::{column::N_MIPS_SEL_COLS, witness::SCRATCH_SIZE};

pub struct WitnessColumns<G, S> {
pub scratch: [G; crate::interpreters::mips::witness::SCRATCH_SIZE],
pub scratch: [G; SCRATCH_SIZE],
pub instruction_counter: G,
pub error: G,
pub selector: S,
Expand Down
45 changes: 20 additions & 25 deletions o1vm/src/pickles/prover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::array;

use ark_ec::{AffineRepr, Group};
use ark_ff::{One, PrimeField, Zero};
use ark_poly::{univariate::DensePolynomial, Evaluations, Polynomial, Radix2EvaluationDomain as D};
use kimchi::{
Expand Down Expand Up @@ -65,7 +64,7 @@ pub fn prove<
rng: &mut RNG,
) -> Result<Proof<G>, ProverError>
where
<G as AffineRepr>::BaseField: PrimeField,
G::BaseField: PrimeField,
RNG: RngCore + CryptoRng,
{
let num_chunks = 1;
Expand All @@ -76,11 +75,13 @@ where
////////////////////////////////////////////////////////////////////////////
// Round 1: Creating and absorbing column commitments
////////////////////////////////////////////////////////////////////////////
type F<G> = DensePolynomial<<<G as AffineRepr>::Group as Group>::ScalarField>;

debug!("Prover: interpolating all columns, including the selectors");
let ProofInputs { evaluations } = inputs;
let polys: WitnessColumns<F<G>, [F<G>; N_MIPS_SEL_COLS]> = {
let polys: WitnessColumns<
DensePolynomial<G::ScalarField>,
[DensePolynomial<G::ScalarField>; N_MIPS_SEL_COLS],
> = {
let WitnessColumns {
scratch,
instruction_counter,
Expand All @@ -91,18 +92,17 @@ where
let domain_size = domain.d1.size as usize;

// Build the selectors
let selector: [Vec<<<G as AffineRepr>::Group as Group>::ScalarField>; N_MIPS_SEL_COLS] =
array::from_fn(|i| {
let mut s_i = Vec::with_capacity(domain_size);
for s in &selector {
s_i.push(if G::ScalarField::from(i as u64) == *s {
G::ScalarField::one()
} else {
G::ScalarField::zero()
})
}
s_i
});
let selector: [Vec<G::ScalarField>; N_MIPS_SEL_COLS] = array::from_fn(|i| {
let mut s_i = Vec::with_capacity(domain_size);
for s in &selector {
s_i.push(if G::ScalarField::from(i as u64) == *s {
G::ScalarField::one()
} else {
G::ScalarField::zero()
})
}
s_i
});

let eval_col = |evals: Vec<G::ScalarField>| {
Evaluations::<G::ScalarField, D<G::ScalarField>>::from_vec_and_domain(evals, domain.d1)
Expand Down Expand Up @@ -254,7 +254,6 @@ where
// We compute the polynomial t(X) by dividing the constraints polynomial
// by the vanishing polynomial, i.e. Z_H(X).
let (quotient, rem) = expr_evaluation_interpolated
// FIXME: Should this be d8?
.divide_by_vanishing_poly(domain.d1)
.unwrap_or_else(fail_final_q_division);
// As the constraints must be verified on H, the rest of the division
Expand Down Expand Up @@ -307,16 +306,12 @@ where
}
};
// All evaluations at ζ
let zeta_evaluations: WitnessColumns<
<<G as AffineRepr>::Group as Group>::ScalarField,
[<<G as AffineRepr>::Group as Group>::ScalarField; N_MIPS_SEL_COLS],
> = evals(&zeta);
let zeta_evaluations: WitnessColumns<G::ScalarField, [G::ScalarField; N_MIPS_SEL_COLS]> =
evals(&zeta);

// All evaluations at ζω
let zeta_omega_evaluations: WitnessColumns<
<<G as AffineRepr>::Group as Group>::ScalarField,
[<<G as AffineRepr>::Group as Group>::ScalarField; N_MIPS_SEL_COLS],
> = evals(&zeta_omega);
let zeta_omega_evaluations: WitnessColumns<G::ScalarField, [G::ScalarField; N_MIPS_SEL_COLS]> =
evals(&zeta_omega);

let chunked_quotient = quotient_poly
.to_chunked_polynomial(DEGREE_QUOTIENT_POLYNOMIAL as usize, domain.d1.size as usize);
Expand Down
5 changes: 3 additions & 2 deletions o1vm/src/pickles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use mina_poseidon::{
use o1_utils::tests::make_test_rng;
use poly_commitment::SRS;
use strum::{EnumCount, IntoEnumIterator};

#[test]
fn test_regression_constraints_with_selectors() {
let constraints = {
Expand Down Expand Up @@ -75,6 +76,7 @@ fn test_regression_selectors_for_instructions() {
fn zero_to_n_minus_one(n: usize) -> Vec<Fq> {
(0..n).map(|i| Fq::from((i) as u64)).collect()
}

#[test]
fn test_small_circuit() {
let domain = EvaluationDomains::<Fq>::create(8).unwrap();
Expand Down Expand Up @@ -111,8 +113,7 @@ fn test_small_circuit() {
.unwrap();

let instant_before_verification = Instant::now();
let verif =
verify::<Pallas, BaseSponge, ScalarSponge>(domain, &srs, &vec![expr.clone()], &proof);
let verif = verify::<Pallas, BaseSponge, ScalarSponge>(domain, &srs, &[expr.clone()], &proof);
let instant_after_verification = Instant::now();
debug!(
"Verification took: {} ms",
Expand Down
20 changes: 8 additions & 12 deletions o1vm/src/pickles/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ark_ec::{AffineRepr, Group};
use ark_ec::AffineRepr;
use ark_ff::{Field, One, PrimeField, Zero};
use rand::thread_rng;

Expand Down Expand Up @@ -31,23 +31,20 @@ use crate::{interpreters::mips::column::N_MIPS_SEL_COLS, E};
use kimchi_msm::columns::Column;

type CommitmentColumns<G> = WitnessColumns<PolyComm<G>, [PolyComm<G>; N_MIPS_SEL_COLS]>;
type EvaluationColumns<G> = WitnessColumns<
<<G as AffineRepr>::Group as Group>::ScalarField,
[<<G as AffineRepr>::Group as Group>::ScalarField; N_MIPS_SEL_COLS],
>;
type EvaluationColumns<F> = WitnessColumns<F, [F; N_MIPS_SEL_COLS]>;

struct ColumnEval<'a, G: AffineRepr> {
commitment: &'a CommitmentColumns<G>,
zeta_eval: &'a EvaluationColumns<G>,
zeta_omega_eval: &'a EvaluationColumns<G>,
zeta_eval: &'a EvaluationColumns<G::ScalarField>,
zeta_omega_eval: &'a EvaluationColumns<G::ScalarField>,
}

impl<G: AffineRepr> ColumnEvaluations<<G as AffineRepr>::ScalarField> for ColumnEval<'_, G> {
impl<G: AffineRepr> ColumnEvaluations<G::ScalarField> for ColumnEval<'_, G> {
type Column = Column;
fn evaluate(
&self,
col: Self::Column,
) -> Result<PointEvaluations<<G as AffineRepr>::ScalarField>, ExprError<Self::Column>> {
) -> Result<PointEvaluations<G::ScalarField>, ExprError<Self::Column>> {
let ColumnEval {
commitment: _,
zeta_eval,
Expand All @@ -72,8 +69,7 @@ pub fn verify<
>(
domain: EvaluationDomains<G::ScalarField>,
srs: &<OpeningProof<G> as OpenProof<G>>::SRS,
//FIXME: change vec to array
constraints: &Vec<E<G::ScalarField>>,
constraints: &[E<G::ScalarField>],
proof: &Proof<G>,
) -> bool
where
Expand Down Expand Up @@ -180,7 +176,7 @@ where
};

let combined_expr =
Expr::combine_constraints(0..(constraints.len() as u32), constraints.clone());
Expr::combine_constraints(0..(constraints.len() as u32), constraints.to_vec());

let numerator_zeta = PolishToken::evaluate(
combined_expr.to_polish().as_slice(),
Expand Down

0 comments on commit df2415a

Please sign in to comment.