Skip to content

Commit

Permalink
refactor: Replace MultiFrameTrait with C1LEM across codebase
Browse files Browse the repository at this point in the history
- Replaced the `MultiFrameTrait` with `C1LEM` across various functions in different files which made `circuit_cache_key` method and other related methods non-generic.
- Updated method calls reflecting changes of generic parameters to use `C1LEM` in place of `MultiFrameTrait` and also in cache handling methods in `supernova.rs` file.
- Removed `MultiFrame` usage in several benchmark methods - namely `end2end_benchmark`, `prove_benchmark`, `prove_compressed_benchmark`, `verify_benchmark`, and `verify_compressed_benchmark`.
- Updated `public_params` function and its related functions to work with changes of replacing `MultiFrameTrait` with `C1LEM`, this included adjusting the return type of `public_params` and modifications in `DiskCache` struct in `public_parameters/mod.rs` file.
- Examples and benchmarks using `public_params` function refactored to no longer require `MultiFrame` as a generic argument, this occurred in `examples/circom.rs`, `benches/fibonacci.rs`, and `benches/sha256.rs` files.
- `DiskCache` struct heavily refactored with the replacement of `MultiFrameTrait` with `C1LEM`, impacting `read` and `write` methods return types in `public_parameters/disk_cache.rs` file.
- The usage of `get_from_disk_cache_or_update_with` and `get_from_mem_cache_or_update_with` functions in `public_parameters/mem_cache.rs` file changed with the adoption of `C1LEM`.
- The `Instance` struct in the `src/public_parameters/instance.rs` was simplified and restructured to no longer depend on `MultiFrameTrait` and its associated methods and return types updated accordingly.
  • Loading branch information
huitseeker committed Jan 6, 2024
1 parent e9fc568 commit 3a19179
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 147 deletions.
11 changes: 5 additions & 6 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use lurk::{
field::LurkField,
lem::{
eval::{evaluate, evaluate_simple},
multiframe::MultiFrame,
pointers::Ptr,
store::Store,
},
Expand Down Expand Up @@ -74,7 +73,7 @@ fn end2end_benchmark(c: &mut Criterion) {
true,
Kind::NovaPublicParams,
);
let pp = public_parameters::public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_parameters::public_params(&instance).unwrap();

let size = (10, 0);
let benchmark_id = BenchmarkId::new("end2end_go_base_nova", format!("_{}_{}", size.0, size.1));
Expand Down Expand Up @@ -245,7 +244,7 @@ fn prove_benchmark(c: &mut Criterion) {
true,
Kind::NovaPublicParams,
);
let pp = public_parameters::public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_parameters::public_params(&instance).unwrap();

group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<Fq>(&store, state.clone(), s.0, s.1);
Expand Down Expand Up @@ -293,7 +292,7 @@ fn prove_compressed_benchmark(c: &mut Criterion) {
true,
Kind::NovaPublicParams,
);
let pp = public_parameters::public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_parameters::public_params(&instance).unwrap();

group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<Fq>(&store, state.clone(), s.0, s.1);
Expand Down Expand Up @@ -335,7 +334,7 @@ fn verify_benchmark(c: &mut Criterion) {
true,
Kind::NovaPublicParams,
);
let pp = public_parameters::public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_parameters::public_params(&instance).unwrap();

let sizes = [(10, 0)];
for size in sizes {
Expand Down Expand Up @@ -387,7 +386,7 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
true,
Kind::NovaPublicParams,
);
let pp = public_parameters::public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_parameters::public_params(&instance).unwrap();

let sizes = [(10, 0)];
for size in sizes {
Expand Down
4 changes: 2 additions & 2 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{sync::Arc, time::Duration};

use lurk::{
eval::lang::{Coproc, Lang},
lem::{eval::evaluate, multiframe::MultiFrame, store::Store},
lem::{eval::evaluate, store::Store},
proof::nova::NovaProver,
proof::Prover,
public_parameters::{
Expand Down Expand Up @@ -95,7 +95,7 @@ fn fibonacci_prove<M: measurement::Measurement>(
true,
Kind::NovaPublicParams,
);
let pp = public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_params(&instance).unwrap();

// Track the number of `Lurk frames / sec`
let rc = prove_params.reduction_count as u64;
Expand Down
9 changes: 4 additions & 5 deletions benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use lurk::{
field::LurkField,
lem::{
eval::{evaluate, make_cprocs_funcs_from_lang, make_eval_step_from_config, EvalConfig},
multiframe::MultiFrame,
pointers::Ptr,
store::Store,
},
Expand Down Expand Up @@ -112,13 +111,13 @@ fn sha256_ivc_prove<M: measurement::Measurement>(
let lurk_step = make_eval_step_from_config(&EvalConfig::new_ivc(&lang));

// use cached public params
let instance: Instance<'_, Fr, Sha256Coproc<Fr>, MultiFrame<'_, _, _>> = Instance::new(
let instance: Instance<'_, Fr, Sha256Coproc<Fr>> = Instance::new(
reduction_count,
lang_rc.clone(),
true,
Kind::NovaPublicParams,
);
let pp = public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_params(&instance).unwrap();

c.bench_with_input(
BenchmarkId::new(prove_params.name(), arity),
Expand Down Expand Up @@ -199,7 +198,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(
true,
Kind::NovaPublicParams,
);
let pp = public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_params(&instance).unwrap();

c.bench_with_input(
BenchmarkId::new(prove_params.name(), arity),
Expand Down Expand Up @@ -283,7 +282,7 @@ fn sha256_nivc_prove<M: measurement::Measurement>(
true,
Kind::SuperNovaAuxParams,
);
let pp = supernova_public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = supernova_public_params(&instance).unwrap();

c.bench_with_input(
BenchmarkId::new(prove_params.name(), arity),
Expand Down
3 changes: 1 addition & 2 deletions examples/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use std::time::Instant;

use lurk::circuit::gadgets::circom::CircomGadget;
use lurk::circuit::gadgets::pointer::AllocatedPtr;
use lurk::lem::multiframe::MultiFrame;

#[cfg(not(target_arch = "wasm32"))]
use lurk::coprocessor::circom::non_wasm::CircomCoprocessor;
Expand Down Expand Up @@ -116,7 +115,7 @@ fn main() {

let pp_start = Instant::now();
let instance = Instance::new(REDUCTION_COUNT, lang_rc, true, Kind::NovaPublicParams);
let pp = public_params::<_, _, MultiFrame<'_, _, _>>(&instance).unwrap();
let pp = public_params(&instance).unwrap();
let pp_end = pp_start.elapsed();

println!("Public parameters took {pp_end:?}");
Expand Down
3 changes: 1 addition & 2 deletions examples/sha256_nivc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use lurk::{
field::LurkField,
lem::{
eval::{evaluate, make_cprocs_funcs_from_lang, make_eval_step_from_config, EvalConfig},
multiframe::MultiFrame,
pointers::Ptr,
store::Store,
},
Expand Down Expand Up @@ -87,7 +86,7 @@ fn main() {
let pp_start = Instant::now();

let instance_primary = Instance::new(REDUCTION_COUNT, lang_rc, true, Kind::SuperNovaAuxParams);
let pp = supernova_public_params::<_, _, MultiFrame<'_, _, _>>(&instance_primary).unwrap();
let pp = supernova_public_params(&instance_primary).unwrap();

let pp_end = pp_start.elapsed();
println!("Running claim parameters took {:?}", pp_end);
Expand Down
9 changes: 2 additions & 7 deletions src/proof/nova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,12 @@ where
///
/// Note: For now, we use ad-hoc circuit cache keys.
/// See: [crate::public_parameters::instance]
pub fn circuit_cache_key<
'a,
F: CurveCycleEquipped,
C: Coprocessor<F> + 'a,
M: MultiFrameTrait<'a, F, C>,
>(
pub fn circuit_cache_key<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a>(
rc: usize,
lang: Arc<Lang<F, C>>,
) -> F {
let folding_config = Arc::new(FoldingConfig::new_ivc(lang, 2));
let circuit = M::blank(folding_config, 0);
let circuit = C1LEM::<'a, F, C>::blank(folding_config, 0);
F::from(rc as u64) * nova::circuit_digest::<F::E1, F::E2, _>(&circuit)
}

Expand Down
18 changes: 4 additions & 14 deletions src/proof/supernova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,7 @@ impl<F: LurkField, C: Coprocessor<F>> FoldingConfig<F, C> {
///
/// Note: For now, we use ad-hoc circuit cache keys.
/// See: [crate::public_parameters::instance]
pub fn circuit_cache_key<
'a,
F: CurveCycleEquipped,
C: Coprocessor<F> + 'a,
M: MultiFrameTrait<'a, F, C> + SuperStepCircuit<F> + NonUniformCircuit<E1<F>, E2<F>, M, C2<F>>,
>(
pub fn circuit_cache_key<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a>(
rc: usize,
lang: Arc<Lang<F, C>>,
circuit_index: usize,
Expand All @@ -346,20 +341,15 @@ where
<<E2<F> as Engine>::Scalar as PrimeField>::Repr: Abomonation,
{
let folding_config = Arc::new(FoldingConfig::new_nivc(lang, 2));
let circuit = M::blank(folding_config, 0);
let circuit = C1LEM::<'a, F, C>::blank(folding_config, 0);
let num_circuits = circuit.num_circuits();
let circuit = circuit.primary_circuit(circuit_index);
F::from(rc as u64) * supernova::circuit_digest::<F::E1, F::E2, _>(&circuit, num_circuits)
}

/// Collects all the cache keys of supernova instance. We need all of them to compute
/// a cache key for the digest of the [PublicParams] of the supernova instance.
pub fn circuit_cache_keys<
'a,
F: CurveCycleEquipped,
C: Coprocessor<F> + 'a,
M: MultiFrameTrait<'a, F, C> + SuperStepCircuit<F> + NonUniformCircuit<E1<F>, E2<F>, M, C2<F>>,
>(
pub fn circuit_cache_keys<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a>(
rc: usize,
lang: &Arc<Lang<F, C>>,
) -> CircuitDigests<E1<F>>
Expand All @@ -369,7 +359,7 @@ where
{
let num_circuits = lang.coprocessor_count() + 1;
let digests = (0..num_circuits)
.map(|circuit_index| circuit_cache_key::<F, C, M>(rc, lang.clone(), circuit_index))
.map(|circuit_index| circuit_cache_key::<F, C>(rc, lang.clone(), circuit_index))
.collect();
CircuitDigests::new(digests)
}
23 changes: 10 additions & 13 deletions src/public_parameters/disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use nova::traits::Engine;

use crate::config::lurk_config;
use crate::coprocessor::Coprocessor;
use crate::proof::nova::{CurveCycleEquipped, PublicParams, E1, E2};
use crate::proof::MultiFrameTrait;
use crate::proof::nova::{CurveCycleEquipped, PublicParams, C1LEM, E1, E2};
use crate::public_parameters::error::Error;

use super::instance::Instance;
Expand All @@ -20,18 +19,16 @@ pub(crate) fn public_params_dir() -> &'static Utf8PathBuf {
&lurk_config(None, None).public_params_dir
}

pub(crate) struct DiskCache<'a, F, C, M>
pub(crate) struct DiskCache<'a, F, C>
where
F: CurveCycleEquipped,
C: Coprocessor<F> + 'a,
M: MultiFrameTrait<'a, F, C>,
{
dir: Utf8PathBuf,
_t: PhantomData<(&'a (), F, C, M)>,
_t: PhantomData<(&'a (), F, C)>,
}

impl<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a, M: MultiFrameTrait<'a, F, C>>
DiskCache<'a, F, C, M>
impl<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a> DiskCache<'a, F, C>
where
// technical bounds that would disappear once associated_type_bounds stabilizes
<<E1<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
Expand All @@ -48,8 +45,8 @@ where

pub(crate) fn read(
&self,
instance: &Instance<'a, F, C, M>,
) -> Result<PublicParams<F, M>, Error> {
instance: &Instance<'a, F, C>,
) -> Result<PublicParams<F, C1LEM<'a, F, C>>, Error> {
let file = instance.open(&self.dir)?;
let reader = BufReader::new(file);
bincode::deserialize_from(reader)
Expand All @@ -58,7 +55,7 @@ where

pub(crate) fn read_bytes(
&self,
instance: &Instance<'a, F, C, M>,
instance: &Instance<'a, F, C>,
byte_sink: &mut Vec<u8>,
) -> Result<(), Error> {
let file = instance.open(&self.dir)?;
Expand All @@ -69,8 +66,8 @@ where

pub(crate) fn write(
&self,
instance: &Instance<'a, F, C, M>,
data: &PublicParams<F, M>,
instance: &Instance<'a, F, C>,
data: &PublicParams<F, C1LEM<'a, F, C>>,
) -> Result<(), Error> {
let file = instance.create(&self.dir)?;
let writer = BufWriter::new(&file);
Expand All @@ -80,7 +77,7 @@ where

pub(crate) fn write_abomonated<V: Abomonation>(
&self,
instance: &Instance<'a, F, C, M>,
instance: &Instance<'a, F, C>,
data: &V,
) -> Result<(), Error> {
let mut file = instance.create(&self.dir)?;
Expand Down
41 changes: 11 additions & 30 deletions src/public_parameters/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ use std::{
sync::Arc,
};

use ::nova::{
constants::NUM_HASH_BITS,
supernova::{NonUniformCircuit, StepCircuit as SuperStepCircuit},
traits::Engine,
};
use ::nova::{constants::NUM_HASH_BITS, traits::Engine};
use abomonation::Abomonation;
use camino::Utf8Path;
use serde::{Deserialize, Serialize};
Expand All @@ -55,10 +51,9 @@ use sha2::{Digest, Sha256};
use crate::{
coprocessor::Coprocessor,
eval::lang::Lang,
proof::MultiFrameTrait,
proof::{
nova::{self, CurveCycleEquipped, E1, E2},
supernova::{self, C2},
supernova::{self},
},
};

Expand All @@ -74,14 +69,13 @@ use crate::{
/// we derive the `num_circuits + 1` circuit param instances. This makes sure that we keep the SuperNova
/// instances as modular as possible, and reuse as much overlapping circuit params as possible.
#[derive(Debug)]
pub struct Instance<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a, M: MultiFrameTrait<'a, F, C>>
{
pub struct Instance<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a> {
pub rc: usize,
pub lang: Arc<Lang<F, C>>,
pub abomonated: bool,
pub cache_key: F,
pub kind: Kind,
pub _p: PhantomData<&'a M>,
pub _p: PhantomData<&'a ()>,
}

/// From [::nova], there are 3 "kinds" of public param objects that need to be cached.
Expand Down Expand Up @@ -112,13 +106,8 @@ pub struct Metadata {
}

impl Metadata {
fn from_instance<
'a,
F: CurveCycleEquipped,
C: Coprocessor<F> + 'a,
M: MultiFrameTrait<'a, F, C>,
>(
instance: &Instance<'a, F, C, M>,
fn from_instance<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'a>(
instance: &Instance<'a, F, C>,
) -> Self {
Metadata {
rc: instance.rc,
Expand All @@ -130,24 +119,17 @@ impl Metadata {
}
}

impl<
'a,
F: CurveCycleEquipped,
C: Coprocessor<F>,
M: MultiFrameTrait<'a, F, C> + SuperStepCircuit<F> + NonUniformCircuit<E1<F>, E2<F>, M, C2<F>>,
> Instance<'a, F, C, M>
impl<'a, F: CurveCycleEquipped, C: Coprocessor<F>> Instance<'a, F, C>
where
<<E1<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
<<E2<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
{
pub fn new(rc: usize, lang: Arc<Lang<F, C>>, abomonated: bool, kind: Kind) -> Self {
let cache_key = match kind {
Kind::NovaPublicParams => nova::circuit_cache_key::<'a, F, C, M>(rc, lang.clone()),
Kind::SuperNovaAuxParams => {
supernova::circuit_cache_keys::<F, C, M>(rc, &lang).digest()
}
Kind::NovaPublicParams => nova::circuit_cache_key::<'a, F, C>(rc, lang.clone()),
Kind::SuperNovaAuxParams => supernova::circuit_cache_keys::<F, C>(rc, &lang).digest(),
Kind::SuperNovaCircuitParams(circuit_index) => {
supernova::circuit_cache_key::<'a, F, C, M>(rc, lang.clone(), circuit_index)
supernova::circuit_cache_key::<'a, F, C>(rc, lang.clone(), circuit_index)
}
};
Instance {
Expand Down Expand Up @@ -193,8 +175,7 @@ where
}
}

impl<'a, F: CurveCycleEquipped, C: Coprocessor<F>, M: MultiFrameTrait<'a, F, C>>
Instance<'a, F, C, M>
impl<'a, F: CurveCycleEquipped, C: Coprocessor<F>> Instance<'a, F, C>
where
<<E1<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
<<E2<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
Expand Down
Loading

0 comments on commit 3a19179

Please sign in to comment.