Skip to content

Commit

Permalink
Refactor: remove useless component from evaluation functions
Browse files Browse the repository at this point in the history
* Since #887, the number of iterations on evaluation is precisely the number of frames

* Remove `iterations` component from evaluation functions that return the frames

Note: this component was being ignored in most places already
  • Loading branch information
arthurpaulino committed Dec 21, 2023
1 parent 5e3ed46 commit eacd076
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 40 deletions.
10 changes: 5 additions & 5 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn end2end_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
b.iter(|| {
let ptr = go_base::<Fq>(&store, state.clone(), s.0, s.1);
let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let _result = prover.prove(&pp, &frames, &store).unwrap();
})
});
Expand Down Expand Up @@ -251,7 +251,7 @@ fn prove_benchmark(c: &mut Criterion) {
let ptr = go_base::<Fq>(&store, state.clone(), s.0, s.1);
let prover: NovaProver<'_, Fq, Coproc<Fq>, MultiFrame<'_, Fq, Coproc<Fq>>> =
NovaProver::new(reduction_count, lang_pallas_rc.clone());
let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();

b.iter(|| {
let result = prover.prove(&pp, &frames, &store).unwrap();
Expand Down Expand Up @@ -298,7 +298,7 @@ fn prove_compressed_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<Fq>(&store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas_rc.clone());
let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();

b.iter(|| {
let (proof, _, _, _) = prover.prove(&pp, &frames, &store).unwrap();
Expand Down Expand Up @@ -344,7 +344,7 @@ fn verify_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base(&store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas_rc.clone());
let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let (proof, z0, zi, _num_steps) = prover.prove(&pp, &frames, &store).unwrap();

b.iter_batched(
Expand Down Expand Up @@ -396,7 +396,7 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base(&store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas_rc.clone());
let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let (proof, z0, zi, _num_steps) = prover.prove(&pp, &frames, &store).unwrap();

let compressed_proof = proof.compress(&pp).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ fn fibonacci_prove<M: measurement::Measurement>(

let frames =
&evaluate::<pasta_curves::Fq, Coproc<pasta_curves::Fq>>(None, ptr, &store, limit)
.unwrap()
.0;
.unwrap();

b.iter_batched(
|| frames,
Expand Down
12 changes: 3 additions & 9 deletions benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ fn sha256_ivc_prove<M: measurement::Measurement>(

let prover = NovaProver::new(prove_params.reduction_count, lang_rc.clone());

let frames = &evaluate(Some((&lurk_step, &lang)), ptr, store, limit)
.unwrap()
.0;
let frames = &evaluate(Some((&lurk_step, &lang)), ptr, store, limit).unwrap();

b.iter_batched(
|| frames,
Expand Down Expand Up @@ -217,9 +215,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(

let prover = NovaProver::new(prove_params.reduction_count, lang_rc.clone());

let frames = &evaluate(Some((&lurk_step, &lang)), ptr, store, limit)
.unwrap()
.0;
let frames = &evaluate(Some((&lurk_step, &lang)), ptr, store, limit).unwrap();

b.iter_batched(
|| frames,
Expand Down Expand Up @@ -302,9 +298,7 @@ fn sha256_nivc_prove<M: measurement::Measurement>(

let prover = SuperNovaProver::new(prove_params.reduction_count, lang_rc.clone());

let frames = &evaluate(Some((&lurk_step, &lang)), ptr, store, limit)
.unwrap()
.0;
let frames = &evaluate(Some((&lurk_step, &lang)), ptr, store, limit).unwrap();

b.iter_batched(
|| frames,
Expand Down
2 changes: 1 addition & 1 deletion benches/synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn synthesize<M: measurement::Measurement>(
let store = Store::default();
let fib_n = (reduction_count / 3) as u64; // Heuristic, since one fib is 35 iterations.
let ptr = fib::<pasta_curves::Fq>(&store, state.clone(), black_box(fib_n));
let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, ptr, &store, limit).unwrap();

let folding_config =
Arc::new(FoldingConfig::new_ivc(lang_rc.clone(), *reduction_count));
Expand Down
2 changes: 1 addition & 1 deletion examples/sha256_nivc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() {
let lang_rc = Arc::new(lang.clone());

let lurk_step = make_eval_step_from_config(&EvalConfig::new_nivc(&lang));
let (frames, _) = evaluate(Some((&lurk_step, &lang)), call, store, 1000).unwrap();
let frames = evaluate(Some((&lurk_step, &lang)), call, store, 1000).unwrap();

let supernova_prover = SuperNovaProver::<Fr, Sha256Coproc<Fr>, MultiFrame<'_, _, _>>::new(
REDUCTION_COUNT,
Expand Down
4 changes: 3 additions & 1 deletion src/cli/repl/meta_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ impl MetaCmd<F> {

Self::post_verify_check(repl, post_verify)?;

let (frames, iterations) = evaluate_with_env_and_cont::<F, Coproc<F>>(
let frames = evaluate_with_env_and_cont::<F, Coproc<F>>(
None,
cek_io[0],
cek_io[1],
Expand All @@ -1055,6 +1055,8 @@ impl MetaCmd<F> {
repl.limit,
)?;

let iterations = frames.len();

{
// making sure the output matches expectation before proving
let res = &frames.last().expect("frames can't be empty").output;
Expand Down
9 changes: 7 additions & 2 deletions src/cli/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,14 @@ impl Repl<F> {
}

fn eval_expr_and_memoize(&mut self, expr_ptr: Ptr) -> Result<(Vec<Ptr>, usize)> {
let (frames, iterations) =
let frames =
evaluate_with_env::<F, Coproc<F>>(None, expr_ptr, self.env, &self.store, self.limit)?;
let output = frames[frames.len() - 1].output.clone();
let iterations = frames.len();
let output = frames
.last()
.expect("evaluation should return at least one frame")
.output
.clone();
self.evaluation = Some(Evaluation { frames, iterations });
Ok((output, iterations))
}
Expand Down
10 changes: 5 additions & 5 deletions src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn build_frames<
limit: usize,
lang: &Lang<F, C>,
log_fmt: LogFmt,
) -> Result<(Vec<Frame>, usize)> {
) -> Result<Vec<Frame>> {
let mut pc = 0;
let mut frames = vec![];
let mut iterations = 0;
Expand All @@ -113,7 +113,7 @@ fn build_frames<
}
pc = get_pc(&expr, store, lang);
}
Ok((frames, iterations))
Ok(frames)
}

/// Faster version of `build_frames` that doesn't accumulate frames
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn evaluate_with_env_and_cont<F: LurkField, C: Coprocessor<F>>(
cont: Ptr,
store: &Store<F>,
limit: usize,
) -> Result<(Vec<Frame>, usize)> {
) -> Result<Vec<Frame>> {
let state = initial_lurk_state();
let log_fmt = |i: usize, inp: &[Ptr], emit: &[Ptr], store: &Store<F>| {
let mut out = format!(
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn evaluate_with_env<F: LurkField, C: Coprocessor<F>>(
env: Ptr,
store: &Store<F>,
limit: usize,
) -> Result<(Vec<Frame>, usize)> {
) -> Result<Vec<Frame>> {
evaluate_with_env_and_cont(func_lang, expr, env, store.cont_outermost(), store, limit)
}

Expand All @@ -196,7 +196,7 @@ pub fn evaluate<F: LurkField, C: Coprocessor<F>>(
expr: Ptr,
store: &Store<F>,
limit: usize,
) -> Result<(Vec<Frame>, usize)> {
) -> Result<Vec<Frame>> {
evaluate_with_env_and_cont(
func_lang,
expr,
Expand Down
17 changes: 4 additions & 13 deletions src/lem/multiframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,8 @@ impl<'a, F: LurkField, C: Coprocessor<F> + 'a> MultiFrameTrait<'a, F, C> for Mul
) -> Result<Vec<Self::EvalFrame>, ProofError> {
let cont = store.cont_outermost();
let lurk_step = make_eval_step_from_config(ec);
match evaluate_with_env_and_cont(
Some((&lurk_step, ec.lang())),
expr,
env,
cont,
store,
limit,
) {
Ok((frames, _)) => Ok(frames),
Err(e) => Err(ProofError::Reduction(ReductionError::Misc(e.to_string()))),
}
evaluate_with_env_and_cont(Some((&lurk_step, ec.lang())), expr, env, cont, store, limit)
.map_err(|e| ProofError::Reduction(ReductionError::Misc(e.to_string())))
}

fn significant_frame_count(frames: &[Self::EvalFrame]) -> usize {
Expand Down Expand Up @@ -988,7 +979,7 @@ mod tests {

let expr = store.read_with_default_state("(if t (+ 5 5) 6)").unwrap();

let (frames, _) = evaluate::<Fq, Coproc<Fq>>(None, expr, &store, 10).unwrap();
let frames = evaluate::<Fq, Coproc<Fq>>(None, expr, &store, 10).unwrap();

let sequential_slots_witnesses =
generate_slots_witnesses(&store, &frames, num_slots_per_frame, false);
Expand Down Expand Up @@ -1095,7 +1086,7 @@ mod tests {
let expr = store.read_with_default_state("(+ 1 2)").unwrap();

let lang = Arc::new(Lang::<Fq, Coproc<Fq>>::new());
let (mut frames, _) = evaluate::<Fq, Coproc<Fq>>(None, expr, &store, 1).unwrap();
let mut frames = evaluate::<Fq, Coproc<Fq>>(None, expr, &store, 1).unwrap();
assert_eq!(frames.len(), 1);

let mut frame = frames.pop().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/lem/tests/nivc_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test_nivc_steps() {
// 9^2 + 8 = 89
let expr = store.read_with_default_state("(cproc-dumb 9 8)").unwrap();

let (frames, _) = evaluate(Some((&lurk_step, &lang)), expr, &store, 10).unwrap();
let frames = evaluate(Some((&lurk_step, &lang)), expr, &store, 10).unwrap();

// Iteration 1: evaluate first argument
// Iteration 2: evaluate second argument
Expand Down

0 comments on commit eacd076

Please sign in to comment.