Skip to content

Commit

Permalink
Chore: parallel steps witness generation for NIVC
Browse files Browse the repository at this point in the history
* MultiFrames with PC=0 have their witnesses cached just like in the IVC pipeline

* MultiFrames with PC!=0 have their witnesses cached in parallel due to limited size
  (agnostic to RC) and lack of internal parallelism
  • Loading branch information
arthurpaulino committed Jan 3, 2024
1 parent f53cff4 commit ff2af20
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/lem/multiframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,10 @@ impl<'a, F: LurkField, C: Coprocessor<F> + 'a> MultiFrameTrait<'a, F, C> for Mul
.skip_while(|f| f.input == f.output && stop_cond(&f.output))
.count()
}

fn program_counter(&self) -> usize {
self.pc
}
}

impl<'a, F: LurkField, C: Coprocessor<F>> Circuit<F> for MultiFrame<'a, F, C> {
Expand Down
3 changes: 3 additions & 0 deletions src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub trait MultiFrameTrait<'a, F: LurkField, C: Coprocessor<F> + 'a>:
store: &'a Self::Store,
folding_config: Arc<FoldingConfig<F, C>>,
) -> Vec<Self>;

/// The program counter (circuit index)
fn program_counter(&self) -> usize;
}

/// Represents a sequential Constraint System for a given proof.
Expand Down
2 changes: 0 additions & 2 deletions src/proof/nova.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(non_snake_case)]

use abomonation::Abomonation;
use bellpepper_core::{num::AllocatedNum, ConstraintSystem};
use ff::PrimeField;
Expand Down
128 changes: 101 additions & 27 deletions src/proof/supernova.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(non_snake_case)]

use abomonation::Abomonation;
use ff::PrimeField;
use nova::{
Expand All @@ -15,11 +13,17 @@ use nova::{
Engine,
},
};
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{marker::PhantomData, ops::Index, sync::Arc};
use std::{
marker::PhantomData,
ops::Index,
sync::{Arc, Mutex},
};
use tracing::info;

use crate::{
config::lurk_config,
coprocessor::Coprocessor,
error::ProofError,
eval::lang::Lang,
Expand Down Expand Up @@ -199,7 +203,7 @@ where
pp: &PublicParams<F, M>,
z0: &[F],
steps: Vec<M>,
_store: &'a <M>::Store,
store: &'a <M>::Store,
_reduction_count: usize,
_lang: Arc<Lang<F, C>>,
) -> Result<Self, ProofError> {
Expand All @@ -208,29 +212,99 @@ where
let z0_primary = z0;
let z0_secondary = Self::z0_secondary();

for (i, step) in steps.iter().enumerate() {
info!("prove_recursively, step {i}");

let mut recursive_snark = recursive_snark_option.clone().unwrap_or_else(|| {
info!("RecursiveSnark::new {i}");
RecursiveSNARK::new(
&pp.pp,
step,
step,
&step.secondary_circuit(),
z0_primary,
&z0_secondary,
)
.unwrap()
});

info!("prove_step {i}");

recursive_snark
.prove_step(&pp.pp, step, &step.secondary_circuit())
.unwrap();

recursive_snark_option = Some(recursive_snark);
if lurk_config(None, None)
.perf
.parallelism
.recursive_steps
.is_parallel()
{
let cc = steps
.into_iter()
.map(|mf| (mf.program_counter() == 0, Mutex::new(mf)))
.collect::<Vec<_>>();

crossbeam::thread::scope(|s| {
s.spawn(|_| {
// Skip the very first circuit's witness, so `prove_step` can begin immediately.
// That circuit's witness will not be cached and will just be computed on-demand.

// There are many MultiFrames with PC = 0 and they have several inner frames, with proper internal
// paralellism for witness generation, so we do it like on Nova's pipeline
cc.iter()
.skip(1)
.filter(|(is_zero_pc, _)| *is_zero_pc)
.for_each(|(_, mf)| {
mf.lock()
.unwrap()
.cache_witness(store)
.expect("witness caching failed");
});

// There shouldn't be too many MultiFrames with PC != 0 and they only have one inner frame, without
// internal parallelism for witness generation, so we can generate their witnesses in parallel
cc.par_iter()
.skip(1)
.filter(|(is_zero_pc, _)| !*is_zero_pc)
.for_each(|(_, mf)| {
mf.lock()
.unwrap()
.cache_witness(store)
.expect("witness caching failed");
});
});

for (i, (_, step)) in cc.iter().enumerate() {
let step = step.lock().unwrap();
info!("prove_recursively, step {i}");

let mut recursive_snark = recursive_snark_option.clone().unwrap_or_else(|| {
info!("RecursiveSnark::new {i}");
RecursiveSNARK::new(
&pp.pp,
&*step,
&step,
&step.secondary_circuit(),
z0_primary,
&z0_secondary,
)
.unwrap()
});

info!("prove_step {i}");

recursive_snark
.prove_step(&pp.pp, &step, &step.secondary_circuit())
.unwrap();

recursive_snark_option = Some(recursive_snark);
}
})
.unwrap()
} else {
for (i, step) in steps.iter().enumerate() {
info!("prove_recursively, step {i}");

let mut recursive_snark = recursive_snark_option.clone().unwrap_or_else(|| {
info!("RecursiveSnark::new {i}");
RecursiveSNARK::new(
&pp.pp,
step,
step,
&step.secondary_circuit(),
z0_primary,
&z0_secondary,
)
.unwrap()
});

info!("prove_step {i}");

recursive_snark
.prove_step(&pp.pp, step, &step.secondary_circuit())
.unwrap();

recursive_snark_option = Some(recursive_snark);
}
}

// This probably should be made unnecessary.
Expand Down

0 comments on commit ff2af20

Please sign in to comment.