Skip to content

Commit

Permalink
refactor: move prove_from_frames into Prover trait
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Apr 11, 2024
1 parent 8d77847 commit 606147c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
2 changes: 1 addition & 1 deletion benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use lurk::{
pointers::Ptr,
store::Store,
},
proof::{nova::NovaProver, RecursiveSNARKTrait},
proof::{nova::NovaProver, Prover, RecursiveSNARKTrait},
public_parameters::{
self,
instance::{Instance, Kind},
Expand Down
1 change: 1 addition & 0 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use lurk::{
lang::{Coproc, Lang},
lem::{eval::evaluate, store::Store},
proof::nova::NovaProver,
proof::Prover,
public_parameters::{
instance::{Instance, Kind},
public_params,
Expand Down
2 changes: 1 addition & 1 deletion benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use lurk::{
pointers::Ptr,
store::Store,
},
proof::{nova::NovaProver, supernova::SuperNovaProver, RecursiveSNARKTrait},
proof::{nova::NovaProver, supernova::SuperNovaProver, Prover, RecursiveSNARKTrait},
public_parameters::{
instance::{Instance, Kind},
public_params, supernova_public_params,
Expand Down
1 change: 1 addition & 0 deletions examples/tp_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use lurk::{
lang::{Coproc, Lang},
lem::{eval::evaluate, store::Store},
proof::nova::{public_params, NovaProver, PublicParams},
proof::Prover,
};
use num_traits::ToPrimitive;
use statrs::statistics::Statistics;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
proof::{
nova::{CurveCycleEquipped, Dual, NovaProver},
supernova::SuperNovaProver,
RecursiveSNARKTrait,
Prover, RecursiveSNARKTrait,
},
public_parameters::{instance::Instance, public_params, supernova_public_params},
state::{State, StateRcCell},
Expand Down
29 changes: 28 additions & 1 deletion src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl FoldingMode {
}

/// A trait for a prover that works with a field `F`.
pub trait Prover<F: CurveCycleEquipped> {
pub trait Prover<F: CurveCycleEquipped, C: Coprocessor<F>> {
/// Associated type for a frame-like datatype
type Frame: FrameLike<Ptr, FrameIO = Vec<Ptr>> + Send;

Expand Down Expand Up @@ -215,4 +215,31 @@ pub trait Prover<F: CurveCycleEquipped> {
let unfull_multiframe_frame_count = raw_iterations % rc;
full_multiframe_count + usize::from(unfull_multiframe_frame_count != 0)
}

/// Generate a proof from a sequence of frames
fn prove_from_frames(
&self,
pp: &Self::PublicParams,
frames: &[crate::lem::interpreter::Frame],
store: &Arc<Store<F>>,
init: Option<
<Self::RecursiveSNARK as RecursiveSNARKTrait<F, Self::Frame>>::BaseRecursiveSNARK,
>,
) -> Result<(Self::RecursiveSNARK, Vec<F>, Vec<F>, usize), ProofError> {
let folding_config = self
.folding_mode()
.folding_config(self.lang().clone(), self.reduction_count());
let steps = Self::from_frames(frames, store, &folding_config.into());
self.prove(pp, steps, store, init)
}

/// Returns the `Lang` wrapped with `Arc` for cheap cloning
fn lang(&self) -> &Arc<Lang<F, C>>;

/// Converts input into Self::Frames according to the rules of the Prover
fn from_frames(
frames: &[crate::lem::interpreter::Frame],
store: &Arc<Store<F>>,
folding_config: &Arc<FoldingConfig<F, C>>,
) -> Vec<Self::Frame>;
}
36 changes: 14 additions & 22 deletions src/proof/nova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,30 +392,9 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> NovaProver<F, C> {
folding_mode: FoldingMode::IVC,
}
}

/// Generate a proof from a sequence of frames
pub fn prove_from_frames(
&self,
pp: &PublicParams<F>,
frames: &[Frame],
store: &Arc<Store<F>>,
init: Option<RecursiveSNARK<E1<F>>>,
) -> Result<(Proof<F, C1LEM<F, C>>, Vec<F>, Vec<F>, usize), ProofError> {
let folding_config = self
.folding_mode()
.folding_config(self.lang().clone(), self.reduction_count());
let steps = C1LEM::<F, C>::from_frames(frames, store, &folding_config.into());
self.prove(pp, steps, store, init)
}

#[inline]
/// Returns the `Lang` wrapped with `Arc` for cheap cloning
pub fn lang(&self) -> &Arc<Lang<F, C>> {
&self.lang
}
}

impl<F: CurveCycleEquipped, C: Coprocessor<F>> Prover<F> for NovaProver<F, C> {
impl<F: CurveCycleEquipped, C: Coprocessor<F>> Prover<F, C> for NovaProver<F, C> {
type Frame = C1LEM<F, C>;
type PublicParams = PublicParams<F>;
type RecursiveSNARK = Proof<F, C1LEM<F, C>>;
Expand Down Expand Up @@ -444,4 +423,17 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> Prover<F> for NovaProver<F, C> {
C1LEM::<F, C>::build_frames(expr, env, store, limit, &eval_config, ch_terminal)?;
self.prove_from_frames(pp, &frames, store, None)
}

fn from_frames(
frames: &[Frame],
store: &Arc<Store<F>>,
folding_config: &Arc<FoldingConfig<F, C>>,
) -> Vec<Self::Frame> {
C1LEM::<F, C>::from_frames(frames, store, folding_config)
}

#[inline]
fn lang(&self) -> &Arc<Lang<F, C>> {
&self.lang
}
}
21 changes: 14 additions & 7 deletions src/proof/supernova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,6 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> SuperNovaProver<F, C> {
let steps = C1LEM::<F, C>::from_frames(frames, store, &folding_config.into());
self.prove(pp, steps, store, init)
}

#[inline]
/// Returns the `Lang` wrapped with `Arc` for cheap cloning
pub fn lang(&self) -> &Arc<Lang<F, C>> {
&self.lang
}
}

impl<F: CurveCycleEquipped, C: Coprocessor<F>> RecursiveSNARKTrait<F, C1LEM<F, C>>
Expand Down Expand Up @@ -323,7 +317,7 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> RecursiveSNARKTrait<F, C1LEM<F, C
}
}

impl<F: CurveCycleEquipped, C: Coprocessor<F>> Prover<F> for SuperNovaProver<F, C> {
impl<F: CurveCycleEquipped, C: Coprocessor<F>> Prover<F, C> for SuperNovaProver<F, C> {
type Frame = C1LEM<F, C>;
type PublicParams = PublicParams<F>;
type RecursiveSNARK = Proof<F, C1LEM<F, C>>;
Expand Down Expand Up @@ -352,6 +346,19 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> Prover<F> for SuperNovaProver<F,
C1LEM::<F, C>::build_frames(expr, env, store, limit, &eval_config, ch_terminal)?;
self.prove_from_frames(pp, &frames, store, None)
}

fn from_frames(
frames: &[Frame],
store: &Arc<Store<F>>,
folding_config: &Arc<FoldingConfig<F, C>>,
) -> Vec<Self::Frame> {
C1LEM::<F, C>::from_frames(frames, store, folding_config)
}

#[inline]
fn lang(&self) -> &Arc<Lang<F, C>> {
&self.lang
}
}

#[derive(Clone, Debug)]
Expand Down

0 comments on commit 606147c

Please sign in to comment.