Skip to content

Commit

Permalink
Merge pull request #2907 from o1-labs/o1vm/extract-get-all-constraint…
Browse files Browse the repository at this point in the history
…s-from-main

o1vm/pickles: simplifying main file by extracting all constraints
  • Loading branch information
martyall authored Jan 2, 2025
2 parents c6dc6de + 146b83d commit 097aa30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
23 changes: 22 additions & 1 deletion o1vm/src/interpreters/mips/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
MIPS_LENGTH_BYTES_OFF, MIPS_NUM_BYTES_READ_OFF, MIPS_PREIMAGE_BYTES_OFF,
MIPS_PREIMAGE_CHUNK_OFF, MIPS_PREIMAGE_KEY, N_MIPS_REL_COLS,
},
interpreter::InterpreterEnv,
interpreter::{interpret_instruction, InterpreterEnv},
Instruction,
},
lookups::{Lookup, LookupTableIDs},
Expand All @@ -19,6 +19,7 @@ use kimchi::circuits::{
};
use kimchi_msm::columns::ColumnIndexer as _;
use std::array;
use strum::IntoEnumIterator;

use super::column::N_MIPS_SEL_COLS;

Expand Down Expand Up @@ -673,3 +674,23 @@ impl<Fp: Field> Env<Fp> {
self.lookups.clone()
}
}

pub fn get_all_constraints<Fp: Field>() -> Vec<E<Fp>> {
let mut mips_con_env = Env::<Fp>::default();
let mut constraints = Instruction::iter()
.flat_map(|instr_typ| instr_typ.into_iter())
.fold(vec![], |mut acc, instr| {
interpret_instruction(&mut mips_con_env, instr);
let selector = mips_con_env.get_selector();
let constraints_with_selector: Vec<E<Fp>> = mips_con_env
.get_constraints()
.into_iter()
.map(|c| selector.clone() * c)
.collect();
acc.extend(constraints_with_selector);
mips_con_env.reset();
acc
});
constraints.extend(mips_con_env.get_selector_constraints());
constraints
}
26 changes: 2 additions & 24 deletions o1vm/src/pickles/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ark_ff::UniformRand;
use clap::Parser;
use kimchi::circuits::domains::EvaluationDomains;
use kimchi_msm::expr::E;
use log::debug;
use mina_curves::pasta::{Fp, Vesta, VestaParameters};
use mina_poseidon::{
Expand All @@ -14,7 +13,6 @@ use o1vm::{
interpreters::mips::{
column::N_MIPS_REL_COLS,
constraints as mips_constraints,
interpreter::{self, InterpreterEnv},
witness::{self as mips_witness},
Instruction,
},
Expand All @@ -24,7 +22,6 @@ use o1vm::{
};
use poly_commitment::{ipa::SRS, SRS as _};
use std::{fs::File, io::BufReader, path::Path, process::ExitCode, time::Instant};
use strum::IntoEnumIterator;

pub const DOMAIN_SIZE: usize = 1 << 15;

Expand Down Expand Up @@ -64,25 +61,7 @@ pub fn cannon_main(args: cli::cannon::RunArgs) {
let mut mips_wit_env =
mips_witness::Env::<Fp, PreImageOracle>::create(cannon::PAGE_SIZE as usize, state, po);

let constraints = {
let mut mips_con_env = mips_constraints::Env::<Fp>::default();
let mut constraints = Instruction::iter()
.flat_map(|instr_typ| instr_typ.into_iter())
.fold(vec![], |mut acc, instr| {
interpreter::interpret_instruction(&mut mips_con_env, instr);
let selector = mips_con_env.get_selector();
let constraints_with_selector: Vec<E<Fp>> = mips_con_env
.get_constraints()
.into_iter()
.map(|c| selector.clone() * c)
.collect();
acc.extend(constraints_with_selector);
mips_con_env.reset();
acc
});
constraints.extend(mips_con_env.get_selector_constraints());
constraints
};
let constraints = mips_constraints::get_all_constraints::<Fp>();

let mut curr_proof_inputs: ProofInputs<Vesta> = ProofInputs::new(DOMAIN_SIZE);
while !mips_wit_env.halt {
Expand Down Expand Up @@ -114,7 +93,6 @@ pub fn cannon_main(args: cli::cannon::RunArgs) {
.push(Fp::from((mips_wit_env.selector - N_MIPS_REL_COLS) as u64));

if curr_proof_inputs.evaluations.instruction_counter.len() == DOMAIN_SIZE {
// FIXME
let start_iteration = Instant::now();
debug!("Limit of {DOMAIN_SIZE} reached. We make a proof, verify it (for testing) and start with a new chunk");
let proof = prover::prove::<
Expand All @@ -124,7 +102,7 @@ pub fn cannon_main(args: cli::cannon::RunArgs) {
_,
>(domain_fp, &srs, curr_proof_inputs, &constraints, &mut rng)
.unwrap();
// FIXME: check that the proof is correct. This is for testing purposes.
// Check that the proof is correct. This is for testing purposes.
// Leaving like this for now.
debug!(
"Proof generated in {elapsed} μs",
Expand Down

0 comments on commit 097aa30

Please sign in to comment.