Skip to content

Commit

Permalink
refactor: move prove_from_frames into Prover trait (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek authored Apr 12, 2024
1 parent 8d77847 commit a351957
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

1 comment on commit a351957

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Fibonacci GPU benchmark.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/lurk-rs/actions/runs/8660996885

Benchmark Results

LEM Fibonacci Prove - rc = 100

ref=8d77847a2a9733e665a43c4b3a099e39817c1e87 ref=a3519573ffee44b7f4ce45dc9c9a1a0b7a0fe428
num-100 1.51 s (✅ 1.00x) 1.47 s (✅ 1.02x faster)
num-200 2.87 s (✅ 1.00x) 2.81 s (✅ 1.02x faster)

LEM Fibonacci Prove - rc = 600

ref=8d77847a2a9733e665a43c4b3a099e39817c1e87 ref=a3519573ffee44b7f4ce45dc9c9a1a0b7a0fe428
num-100 1.86 s (✅ 1.00x) 1.87 s (✅ 1.01x slower)
num-200 3.08 s (✅ 1.00x) 3.05 s (✅ 1.01x faster)

Made with criterion-table

Please sign in to comment.