Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line interface for riscV and pickles #2941

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion o1vm/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;

pub mod cannon;
pub mod riscv;

#[derive(Parser, Debug, Clone)]
#[command(
Expand All @@ -10,5 +11,7 @@ pub mod cannon;
)]
pub enum Commands {
#[command(subcommand)]
Cannon(cannon::Cannon),
Cannon(Box<cannon::Cannon>),
#[command(subcommand)]
RiscV(riscv::Command),
}
18 changes: 18 additions & 0 deletions o1vm/src/cli/riscv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use clap::{arg, Parser, Subcommand};

#[derive(Parser, Debug, Clone)]
pub struct Elf {
#[arg(
short = 'e',
long,
value_name = "ELF_PATH",
help = "input ELF file path for riscv"
)]
pub path: String,
}

#[derive(Subcommand, Clone, Debug)]
pub enum Command {
#[command(name = "elf")]
Run(Elf),
}
9 changes: 7 additions & 2 deletions o1vm/src/pickles/column_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ type Evals<F> = Evaluations<F, Radix2EvaluationDomain<F>>;
pub struct ColumnEnvironment<'a, F: FftField> {
/// The witness column polynomials. Includes relation columns and dynamic
/// selector columns.
pub witness: &'a WitnessColumns<Evals<F>, [Evals<F>; N_MIPS_SEL_COLS]>,
pub witness: &'a WitnessColumns<
Evals<F>,
[Evals<F>; N_MIPS_SEL_COLS],
SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE,
>,
/// The value `prod_{j != 1} (1 - ω^j)`, used for efficiently
/// computing the evaluations of the unnormalized Lagrange basis
/// polynomials.
Expand All @@ -47,7 +52,7 @@ pub fn get_all_columns() -> Vec<Column> {
cols
}

impl<G> WitnessColumns<G, [G; N_MIPS_SEL_COLS]> {
impl<G> WitnessColumns<G, [G; N_MIPS_SEL_COLS], SCRATCH_SIZE, SCRATCH_SIZE_INVERSE> {
pub fn get_column(&self, col: &Column) -> Option<&G> {
match *col {
Column::Relation(i) => {
Expand Down
35 changes: 28 additions & 7 deletions o1vm/src/pickles/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ use mina_poseidon::{
use o1vm::{
cannon::{self, Start, State},
cli, elf_loader,
interpreters::mips::{
column::N_MIPS_REL_COLS,
constraints as mips_constraints,
witness::{self as mips_witness},
Instruction,
interpreters::{
mips::{
column::{
N_MIPS_REL_COLS, SCRATCH_SIZE as MIPS_SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE as MIPS_SCRATCH_SIZE_INVERSE,
},
constraints as mips_constraints,
witness::{self as mips_witness},
Instruction,
},
riscv32im::{witness::Env as RiscEnv, PAGE_SIZE},
},
pickles::{proof::ProofInputs, prover, verifier},
preimage_oracle::{NullPreImageOracle, PreImageOracle, PreImageOracleT},
Expand Down Expand Up @@ -78,7 +84,8 @@ pub fn cannon_main(args: cli::cannon::RunArgs) {

let constraints = mips_constraints::get_all_constraints::<Fp>();

let mut curr_proof_inputs: ProofInputs<Vesta> = ProofInputs::new(DOMAIN_SIZE);
let mut curr_proof_inputs: ProofInputs<Vesta, MIPS_SCRATCH_SIZE, MIPS_SCRATCH_SIZE_INVERSE> =
ProofInputs::new(DOMAIN_SIZE);
while !mips_wit_env.halt {
let _instr: Instruction = mips_wit_env.step(&configuration, meta, &start);
for (scratch, scratch_chunk) in mips_wit_env
Expand Down Expand Up @@ -113,6 +120,8 @@ pub fn cannon_main(args: cli::cannon::RunArgs) {
let proof = prover::prove::<
Vesta,
DefaultFqSponge<VestaParameters, PlonkSpongeConstantsKimchi>,
MIPS_SCRATCH_SIZE,
MIPS_SCRATCH_SIZE_INVERSE,
DefaultFrSponge<Fp, PlonkSpongeConstantsKimchi>,
_,
>(domain_fp, &srs, curr_proof_inputs, &constraints, &mut rng)
Expand Down Expand Up @@ -154,7 +163,7 @@ pub fn main() -> ExitCode {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let args = cli::Commands::parse();
match args {
cli::Commands::Cannon(args) => match args {
cli::Commands::Cannon(args) => match *args {
cli::cannon::Cannon::Run(args) => {
cannon_main(args);
}
Expand All @@ -165,6 +174,18 @@ pub fn main() -> ExitCode {
gen_state_json(args).expect("Error generating state.json");
}
},
cli::Commands::RiscV(args) => match args {
cli::riscv::Command::Run(args) => {
let path = Path::new(&args.path);
let state = elf_loader::parse_elf(elf_loader::Architecture::RiscV32, path)
.expect("Error parsing ELF file");

let mut witness = RiscEnv::<Fp>::create(PAGE_SIZE.try_into().unwrap(), state);
while !witness.halt {
witness.step();
}
}
},
}
ExitCode::SUCCESS
}
44 changes: 32 additions & 12 deletions o1vm/src/pickles/proof.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
use crate::interpreters::mips::column::{SCRATCH_SIZE, SCRATCH_SIZE_INVERSE};
use kimchi::{curve::KimchiCurve, proof::PointEvaluations};
use poly_commitment::{ipa::OpeningProof, PolyComm};

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

pub struct WitnessColumns<G, S> {
pub struct WitnessColumns<G, S, const SCRATCH_SIZE: usize, const SCRATCH_SIZE_INVERSE: usize> {
pub scratch: [G; SCRATCH_SIZE],
pub scratch_inverse: [G; SCRATCH_SIZE_INVERSE],
pub instruction_counter: G,
pub error: G,
pub selector: S,
}

pub struct ProofInputs<G: KimchiCurve> {
pub evaluations: WitnessColumns<Vec<G::ScalarField>, Vec<G::ScalarField>>,
pub struct ProofInputs<G: KimchiCurve, const SCRATCH_SIZE: usize, const SCRATCH_SIZE_INVERSE: usize>
{
pub evaluations: WitnessColumns<
Vec<G::ScalarField>,
Vec<G::ScalarField>,
SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE,
>,
}

impl<G: KimchiCurve> ProofInputs<G> {
impl<G: KimchiCurve> ProofInputs<G, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE> {
pub fn new(domain_size: usize) -> Self {
ProofInputs {
evaluations: WitnessColumns {
scratch: std::array::from_fn(|_| Vec::with_capacity(domain_size)),
scratch_inverse: std::array::from_fn(|_| Vec::with_capacity(domain_size)),
scratch: std::array::from_fn(|_| Vec::with_capacity(SCRATCH_SIZE)),
scratch_inverse: std::array::from_fn(|_| Vec::with_capacity(SCRATCH_SIZE_INVERSE)),
instruction_counter: Vec::with_capacity(domain_size),
error: Vec::with_capacity(domain_size),
selector: Vec::with_capacity(domain_size),
Expand All @@ -30,10 +35,25 @@ impl<G: KimchiCurve> ProofInputs<G> {
}

// FIXME: should we blind the commitment?
pub struct Proof<G: KimchiCurve> {
pub commitments: WitnessColumns<PolyComm<G>, [PolyComm<G>; N_MIPS_SEL_COLS]>,
pub zeta_evaluations: WitnessColumns<G::ScalarField, [G::ScalarField; N_MIPS_SEL_COLS]>,
pub zeta_omega_evaluations: WitnessColumns<G::ScalarField, [G::ScalarField; N_MIPS_SEL_COLS]>,
pub struct Proof<G: KimchiCurve, const INSTRUCTION_SET_SIZE: usize> {
pub commitments: WitnessColumns<
PolyComm<G>,
[PolyComm<G>; INSTRUCTION_SET_SIZE],
SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE,
>,
pub zeta_evaluations: WitnessColumns<
G::ScalarField,
[G::ScalarField; INSTRUCTION_SET_SIZE],
SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE,
>,
pub zeta_omega_evaluations: WitnessColumns<
G::ScalarField,
[G::ScalarField; INSTRUCTION_SET_SIZE],
SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE,
>,
pub quotient_commitment: PolyComm<G>,
pub quotient_evaluations: PointEvaluations<Vec<G::ScalarField>>,
/// IPA opening proof
Expand Down
39 changes: 31 additions & 8 deletions o1vm/src/pickles/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ use super::{
proof::{Proof, ProofInputs, WitnessColumns},
DEGREE_QUOTIENT_POLYNOMIAL,
};
use crate::{interpreters::mips::column::N_MIPS_SEL_COLS, E};
use crate::{
interpreters::mips::column::{
N_MIPS_SEL_COLS, SCRATCH_SIZE as MIPS_SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE as MIPS_SCRATCH_SIZE_INVERSE,
},
E,
};
use thiserror::Error;

/// Errors that can arise when creating a proof
Expand All @@ -55,15 +61,17 @@ pub enum ProverError {
pub fn prove<
G: KimchiCurve,
EFqSponge: FqSponge<G::BaseField, G, G::ScalarField> + Clone,
const SCRATCH_SIZE: usize,
const SCRATCH_SIZE_INVERSE: usize,
EFrSponge: FrSponge<G::ScalarField>,
RNG,
>(
domain: EvaluationDomains<G::ScalarField>,
srs: &SRS<G>,
inputs: ProofInputs<G>,
inputs: ProofInputs<G, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE>,
constraints: &[E<G::ScalarField>],
rng: &mut RNG,
) -> Result<Proof<G>, ProverError>
) -> Result<Proof<G, N_MIPS_SEL_COLS>, ProverError>
where
G::BaseField: PrimeField,
RNG: RngCore + CryptoRng,
Expand All @@ -82,6 +90,8 @@ where
let polys: WitnessColumns<
DensePolynomial<G::ScalarField>,
[DensePolynomial<G::ScalarField>; N_MIPS_SEL_COLS],
SCRATCH_SIZE,
SCRATCH_SIZE_INVERSE,
> = {
let WitnessColumns {
scratch,
Expand Down Expand Up @@ -130,7 +140,12 @@ where
};

debug!("Prover: committing to all columns, including the selectors");
let commitments: WitnessColumns<PolyComm<G>, [PolyComm<G>; N_MIPS_SEL_COLS]> = {
let commitments: WitnessColumns<
PolyComm<G>,
[PolyComm<G>; N_MIPS_SEL_COLS],
MIPS_SCRATCH_SIZE,
MIPS_SCRATCH_SIZE_INVERSE,
> = {
let WitnessColumns {
scratch,
scratch_inverse,
Expand Down Expand Up @@ -331,12 +346,20 @@ where
}
};
// All evaluations at ζ
let zeta_evaluations: WitnessColumns<G::ScalarField, [G::ScalarField; N_MIPS_SEL_COLS]> =
evals(&zeta);
let zeta_evaluations: WitnessColumns<
G::ScalarField,
[G::ScalarField; N_MIPS_SEL_COLS],
MIPS_SCRATCH_SIZE,
MIPS_SCRATCH_SIZE_INVERSE,
> = evals(&zeta);

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

let chunked_quotient = quotient_poly
.to_chunked_polynomial(DEGREE_QUOTIENT_POLYNOMIAL as usize, domain.d1.size as usize);
Expand Down
18 changes: 9 additions & 9 deletions o1vm/src/pickles/tests.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::time::Instant;

use super::{
super::interpreters::mips::column::SCRATCH_SIZE,
proof::{ProofInputs, WitnessColumns},
prover::prove,
};
use crate::{
interpreters::mips::{
column::SCRATCH_SIZE_INVERSE,
column::{SCRATCH_SIZE, SCRATCH_SIZE_INVERSE},
constraints as mips_constraints,
interpreter::{self, InterpreterEnv},
Instruction,
},
pickles::{verifier::verify, MAXIMUM_DEGREE_CONSTRAINTS, TOTAL_NUMBER_OF_CONSTRAINTS},
pickles::{
proof::{ProofInputs, WitnessColumns},
prover::prove,
verifier::verify,
MAXIMUM_DEGREE_CONSTRAINTS, TOTAL_NUMBER_OF_CONSTRAINTS,
},
};
use ark_ff::{Field, One, UniformRand, Zero};
use kimchi::circuits::{domains::EvaluationDomains, expr::Expr, gate::CurrOrNext};
Expand Down Expand Up @@ -63,7 +63,7 @@ fn zero_to_n_minus_one(n: usize) -> Vec<Fq> {
fn test_small_circuit() {
let domain = EvaluationDomains::<Fq>::create(8).unwrap();
let srs = SRS::create(8);
let proof_input = ProofInputs::<Pallas> {
let proof_input = ProofInputs::<Pallas, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE> {
evaluations: WitnessColumns {
scratch: std::array::from_fn(|_| zero_to_n_minus_one(8)),
scratch_inverse: std::array::from_fn(|_| (0..8).map(|_| Fq::zero()).collect()),
Expand All @@ -86,7 +86,7 @@ fn test_small_circuit() {
type BaseSponge = DefaultFqSponge<PallasParameters, PlonkSpongeConstantsKimchi>;
type ScalarSponge = DefaultFrSponge<Fq, PlonkSpongeConstantsKimchi>;

let proof = prove::<Pallas, BaseSponge, ScalarSponge, _>(
let proof = prove::<Pallas, BaseSponge, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE, ScalarSponge, _>(
domain,
&srs,
proof_input,
Expand Down
13 changes: 9 additions & 4 deletions o1vm/src/pickles/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ use super::{
column_env::get_all_columns,
proof::{Proof, WitnessColumns},
};
use crate::{interpreters::mips::column::N_MIPS_SEL_COLS, E};
use crate::{
interpreters::mips::column::{N_MIPS_SEL_COLS, SCRATCH_SIZE, SCRATCH_SIZE_INVERSE},
E,
};
use kimchi_msm::columns::Column;

type CommitmentColumns<G> = WitnessColumns<PolyComm<G>, [PolyComm<G>; N_MIPS_SEL_COLS]>;
type EvaluationColumns<F> = WitnessColumns<F, [F; N_MIPS_SEL_COLS]>;
type CommitmentColumns<G> =
WitnessColumns<PolyComm<G>, [PolyComm<G>; N_MIPS_SEL_COLS], SCRATCH_SIZE, SCRATCH_SIZE_INVERSE>;
type EvaluationColumns<F> =
WitnessColumns<F, [F; N_MIPS_SEL_COLS], SCRATCH_SIZE, SCRATCH_SIZE_INVERSE>;

struct ColumnEval<'a, G: AffineRepr> {
commitment: &'a CommitmentColumns<G>,
Expand Down Expand Up @@ -70,7 +75,7 @@ pub fn verify<
domain: EvaluationDomains<G::ScalarField>,
srs: &<OpeningProof<G> as OpenProof<G>>::SRS,
constraints: &[E<G::ScalarField>],
proof: &Proof<G>,
proof: &Proof<G, N_MIPS_SEL_COLS>,
) -> bool
where
<G as AffineRepr>::BaseField: PrimeField,
Expand Down
Loading