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

MultiFrameTrait #642

Merged
merged 6 commits into from
Aug 30, 2023
Merged
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
8 changes: 6 additions & 2 deletions examples/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ use lurk::circuit::gadgets::pointer::AllocatedPtr;
#[cfg(not(target_arch = "wasm32"))]
use lurk::coprocessor::circom::non_wasm::CircomCoprocessor;

use lurk::circuit::circuit_frame::MultiFrame;
use lurk::eval::{empty_sym_env, lang::Lang};
use lurk::field::LurkField;
use lurk::proof::{nova::NovaProver, Prover};
use lurk::proof::{nova::NovaProver, MultiFrameTrait, Prover};
use lurk::ptr::Ptr;
use lurk::public_parameters::{public_params, public_params_default_dir};
use lurk::store::Store;
Expand Down Expand Up @@ -115,7 +116,10 @@ fn main() {
let expr = format!("({coproc_expr})");
let ptr = store.read(&expr).unwrap();

let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>>::new(REDUCTION_COUNT, lang.clone());
let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>, MultiFrame<'_, Fr, Sha256Coproc<Fr>>>::new(
REDUCTION_COUNT,
lang.clone(),
);
let lang_rc = Arc::new(lang);

println!("Setting up public parameters...");
Expand Down
6 changes: 5 additions & 1 deletion examples/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Instant;

use lurk::circuit::circuit_frame::MultiFrame;
use lurk::circuit::gadgets::constraints::alloc_equal;
use lurk::circuit::gadgets::data::{allocate_constant, GlobalAllocations};
use lurk::circuit::gadgets::pointer::{AllocatedContPtr, AllocatedPtr};
Expand Down Expand Up @@ -181,7 +182,10 @@ fn main() {

let cproc_call = store.list(&[cproc_sym_ptr]);

let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>>::new(REDUCTION_COUNT, lang);
let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>, MultiFrame<'_, Fr, Sha256Coproc<Fr>>>::new(
REDUCTION_COUNT,
lang,
);

println!("Setting up public parameters (rc = {REDUCTION_COUNT})...");

Expand Down
124 changes: 63 additions & 61 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::eval::{lang::Lang, Frame, Witness, IO};
use crate::expr::Thunk;
use crate::hash_witness::HashWitness;
use crate::lurk_sym_ptr;
use crate::proof::Provable;
use crate::proof::{MultiFrameTrait, Provable};
use crate::ptr::Ptr;
use crate::store::Store;
use crate::tag::{ContTag, ExprTag, Op1, Op2};
Expand Down Expand Up @@ -67,30 +67,34 @@ pub struct MultiFrame<'a, F: LurkField, C: Coprocessor<F>> {
pub count: usize,
}

impl<'a, F: LurkField, C: Coprocessor<F>> CircuitFrame<'a, F, C> {
pub fn blank() -> Self {
Self {
store: None,
input: None,
output: None,
witness: None,
_p: Default::default(),
}
impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrameTrait<'a, F, C> for MultiFrame<'a, F, C> {
type Store = Store<F>;
type Ptr = Ptr<F>;
type Frame = Frame<IO<F>, Witness<F>, C>;
type CircuitFrame = CircuitFrame<'a, F, C>;
type GlobalAllocation = GlobalAllocations<F>;
type AllocatedIO = AllocatedIO<F>;

fn precedes(&self, maybe_next: &Self) -> bool {
self.output == maybe_next.input
}

pub fn from_frame(frame: &Frame<IO<F>, Witness<F>, C>, store: &'a Store<F>) -> Self {
CircuitFrame {
store: Some(store),
input: Some(frame.input),
output: Some(frame.output),
witness: Some(frame.witness),
_p: Default::default(),
fn synthesize_frames<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
store: &Store<F>,
input: Self::AllocatedIO,
frames: &[Self::CircuitFrame],
g: &GlobalAllocations<F>,
) -> Self::AllocatedIO {
if cs.is_witness_generator() && CONFIG.parallelism.synthesis.is_parallel() {
self.synthesize_frames_parallel(cs, store, input, frames, g)
} else {
self.synthesize_frames_sequential(cs, store, input, frames, None, g)
}
}
}

impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
pub fn blank(count: usize, lang: Arc<Lang<F, C>>) -> Self {
fn blank(count: usize, lang: Arc<Lang<F, C>>) -> Self {
Self {
store: None,
lang: Some(lang),
Expand All @@ -102,13 +106,9 @@ impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
}
}

pub fn get_store(&self) -> &Store<F> {
self.store.expect("store missing")
}

pub fn from_frames(
fn from_frames(
count: usize,
frames: &[Frame<IO<F>, Witness<F>, C>],
frames: &[Self::Frame],
store: &'a Store<F>,
lang: Arc<Lang<F, C>>,
) -> Vec<Self> {
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
}

/// Make a dummy `MultiFrame`, duplicating `self`'s final `CircuitFrame`.
pub(crate) fn make_dummy(
fn make_dummy(
count: usize,
circuit_frame: Option<CircuitFrame<'a, F, C>>,
store: &'a Store<F>,
Expand All @@ -179,37 +179,45 @@ impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
count,
}
}
}

pub fn synthesize_frames<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
store: &Store<F>,
input_expr: AllocatedPtr<F>,
input_env: AllocatedPtr<F>,
input_cont: AllocatedContPtr<F>,
frames: &[CircuitFrame<'_, F, C>],
g: &GlobalAllocations<F>,
) -> (AllocatedPtr<F>, AllocatedPtr<F>, AllocatedContPtr<F>) {
if cs.is_witness_generator() && CONFIG.parallelism.synthesis.is_parallel() {
self.synthesize_frames_parallel(cs, store, input_expr, input_env, input_cont, frames, g)
} else {
self.synthesize_frames_sequential(
cs, store, input_expr, input_env, input_cont, frames, None, g,
)
impl<'a, F: LurkField, C: Coprocessor<F>> CircuitFrame<'a, F, C> {
pub fn blank() -> Self {
Self {
store: None,
input: None,
output: None,
witness: None,
_p: Default::default(),
}
}

pub fn from_frame(frame: &Frame<IO<F>, Witness<F>, C>, store: &'a Store<F>) -> Self {
CircuitFrame {
store: Some(store),
input: Some(frame.input),
output: Some(frame.output),
witness: Some(frame.witness),
_p: Default::default(),
}
}
}

impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
pub fn get_store(&self) -> &Store<F> {
self.store.expect("store missing")
}

pub fn synthesize_frames_sequential<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
store: &Store<F>,
input_expr: AllocatedPtr<F>,
input_env: AllocatedPtr<F>,
input_cont: AllocatedContPtr<F>,
input: AllocatedIO<F>,
frames: &[CircuitFrame<'_, F, C>],
cons_and_cont_witnesses: Option<Vec<(ConsCircuitWitness<F>, ContCircuitWitness<F>)>>,
g: &GlobalAllocations<F>,
) -> (AllocatedPtr<F>, AllocatedPtr<F>, AllocatedContPtr<F>) {
let (input_expr, input_env, input_cont) = input;
let mut hash_circuit_witness_cache = HashMap::new();

let acc = (input_expr, input_env, input_cont);
Expand Down Expand Up @@ -287,14 +295,13 @@ impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
&self,
cs: &mut CS,
store: &Store<F>,
input_expr: AllocatedPtr<F>,
input_env: AllocatedPtr<F>,
input_cont: AllocatedContPtr<F>,
input: AllocatedIO<F>,
frames: &[CircuitFrame<'_, F, C>],
g: &GlobalAllocations<F>,
) -> (AllocatedPtr<F>, AllocatedPtr<F>, AllocatedContPtr<F>) {
assert!(cs.is_witness_generator());
assert!(CONFIG.parallelism.synthesis.is_parallel());
let (input_expr, input_env, input_cont) = input;

// TODO: this probably belongs in config, perhaps per-Flow.
const MIN_CHUNK_SIZE: usize = 10;
Expand Down Expand Up @@ -372,9 +379,7 @@ impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
let output = self.synthesize_frames_sequential(
&mut cs,
store,
input_expr,
input_env,
input_cont,
(input_expr, input_env, input_cont),
chunk,
Some(cons_and_cont_witnesses),
g,
Expand Down Expand Up @@ -403,12 +408,6 @@ impl<F: LurkField, C: Coprocessor<F>> CircuitFrame<'_, F, C> {
}
}

impl<F: LurkField, C: Coprocessor<F>> MultiFrame<'_, F, C> {
pub fn precedes(&self, maybe_next: &Self) -> bool {
self.output == maybe_next.input
}
}

impl<
F: LurkField, // W: Copy + Sync,
C: Coprocessor<F>,
Expand Down Expand Up @@ -566,7 +565,7 @@ impl<F: LurkField, C: Coprocessor<F>> Circuit<F> for MultiFrame<'_, F, C> {
let g = GlobalAllocations::new(&mut cs.namespace(|| "global_allocations"), store)?;

let (new_expr, new_env, new_cont) =
self.synthesize_frames(cs, store, input_expr, input_env, input_cont, frames, &g);
self.synthesize_frames(cs, store, (input_expr, input_env, input_cont), frames, &g);

output_expr.enforce_equal(
&mut cs.namespace(|| "outer output expr is correct"),
Expand Down Expand Up @@ -5432,13 +5431,16 @@ mod tests {
let lang = Arc::new(raw_lang.clone());
let (_, witness) = input.reduce(&mut store, &lang).unwrap();

let public_params = Groth16Prover::<Bls12, Coproc<Fr>, Fr>::create_groth_params(
let public_params = Groth16Prover::<Bls12, Coproc<Fr>, Fr, MultiFrame<'_, Fr, Coproc<Fr>>>::create_groth_params(
DEFAULT_REDUCTION_COUNT,
lang.clone(),
)
.unwrap();
let groth_prover =
Groth16Prover::<Bls12, Coproc<Fr>, Fr>::new(DEFAULT_REDUCTION_COUNT, raw_lang);
Groth16Prover::<Bls12, Coproc<Fr>, Fr, MultiFrame<'_, Fr, Coproc<Fr>>>::new(
DEFAULT_REDUCTION_COUNT,
raw_lang,
);
let groth_params = &public_params.0;

let vk = &groth_params.vk;
Expand Down
28 changes: 15 additions & 13 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod field_data;
mod lurk_proof;
pub mod paths;
mod repl;
pub mod repl_lem;
// pub mod repl_lem;

use anyhow::{bail, Context, Result};
use camino::Utf8PathBuf;
Expand All @@ -27,7 +27,7 @@ use crate::{
use crate::cli::{
paths::set_lurk_dirs,
repl::{validate_non_zero, Repl},
repl_lem::ReplLEM,
// repl_lem::ReplLEM,
};

use self::backend::Backend;
Expand Down Expand Up @@ -380,11 +380,12 @@ impl ReplCli {
macro_rules! repl {
( $rc:expr, $limit:expr, $field:path, $backend:expr ) => {{
if self.lem {
let mut repl = ReplLEM::<$field>::new(None, $rc, $limit, $backend);
if let Some(lurk_file) = &self.load {
repl.load_file(lurk_file)?;
}
repl.start()
unimplemented!("LEM");
// let mut repl = ReplLEM::<$field>::new(None, $rc, $limit, $backend);
// if let Some(lurk_file) = &self.load {
// repl.load_file(lurk_file)?;
// }
// repl.start()
} else {
let mut repl = new_repl!(self, $rc, $limit, $field, $backend);
if let Some(lurk_file) = &self.load {
Expand Down Expand Up @@ -436,12 +437,13 @@ impl LoadCli {
macro_rules! load {
( $rc:expr, $limit:expr, $field:path, $backend:expr ) => {{
if self.lem {
let mut repl = ReplLEM::<$field>::new(None, $rc, $limit, $backend);
repl.load_file(&self.lurk_file)?;
if self.prove {
repl.prove_last_frames()?;
}
Ok(())
unimplemented!("LEM");
// let mut repl = ReplLEM::<$field>::new(None, $rc, $limit, $backend);
// repl.load_file(&self.lurk_file)?;
// if self.prove {
// repl.prove_last_frames()?;
// }
// Ok(())
} else {
let mut repl = new_repl!(self, $rc, $limit, $field, $backend);
repl.load_file(&self.lurk_file)?;
Expand Down
7 changes: 6 additions & 1 deletion src/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::{
};

use crate::{
circuit::MultiFrame,
cli::paths::{proof_path, public_params_dir},
eval::{
lang::{Coproc, Lang},
Expand Down Expand Up @@ -200,7 +201,11 @@ impl Repl<F> {
let pp =
public_params(self.rc, true, self.lang.clone(), &public_params_dir())?;

let prover = NovaProver::new(self.rc, (*self.lang).clone());
let prover =
NovaProver::<'_, F, Coproc<F>, MultiFrame<'_, F, Coproc<F>>>::new(
self.rc,
(*self.lang).clone(),
);

info!("Proving");
let (proof, public_inputs, public_outputs, num_steps) =
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl_lem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
package::{Package, SymbolRef},
parser,
proof::{
nova_lem::{public_params, NovaProver},
nova::{public_params, NovaProver},
Prover,
},
state::State,
Expand Down
Loading