-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathfibonacci.rs
165 lines (144 loc) · 4.69 KB
/
fibonacci.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Duration};
use camino::Utf8Path;
use criterion::{
black_box, criterion_group, criterion_main, measurement, BatchSize, BenchmarkGroup,
BenchmarkId, Criterion, SamplingMode,
};
use pasta_curves::pallas;
use lurk::{
circuit::circuit_frame::MultiFrame,
eval::{
empty_sym_env,
lang::{Coproc, Lang},
},
field::LurkField,
proof::nova::NovaProver,
proof::Prover,
ptr::Ptr,
public_parameters::{
instance::{Instance, Kind},
public_params,
},
state::State,
store::Store,
};
const PUBLIC_PARAMS_PATH: &str = "/var/tmp/lurk_benches/public_params";
fn fib<F: LurkField>(store: &Store<F>, state: Rc<RefCell<State>>, _a: u64) -> Ptr<F> {
let program = r#"
(letrec ((next (lambda (a b) (next b (+ a b))))
(fib (next 0 1)))
(fib))
"#;
store.read_with_state(state, program).unwrap()
}
// The env output in the `fib_frame`th frame of the above, infinite Fibonacci computation will contain a binding of the
// nth Fibonacci number to `a`.
// means of computing it.]
fn fib_frame(n: usize) -> usize {
11 + 16 * n
}
// Set the limit so the last step will be filled exactly, since Lurk currently only pads terminal/error continuations.
fn fib_limit(n: usize, rc: usize) -> usize {
let frame = fib_frame(n);
rc * (frame / rc + usize::from(frame % rc != 0))
}
struct ProveParams {
fib_n: usize,
reduction_count: usize,
}
impl ProveParams {
fn name(&self) -> String {
let date = env!("VERGEN_GIT_COMMIT_DATE");
let sha = env!("VERGEN_GIT_SHA");
format!("{date}:{sha}:Fibonacci-rc={}", self.reduction_count)
}
}
fn fibo_prove<M: measurement::Measurement>(
prove_params: ProveParams,
c: &mut BenchmarkGroup<'_, M>,
state: Rc<RefCell<State>>,
) {
let ProveParams {
fib_n,
reduction_count,
} = prove_params;
let limit = fib_limit(fib_n, reduction_count);
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
// use cached public params
let instance = Instance::new(
reduction_count,
lang_rc.clone(),
true,
Kind::NovaPublicParams,
);
let pp =
public_params::<_, _, MultiFrame<'_, _, _>>(&instance, Utf8Path::new(PUBLIC_PARAMS_PATH))
.unwrap();
c.bench_with_input(
BenchmarkId::new(prove_params.name(), fib_n),
&prove_params,
|b, prove_params| {
let store = Store::default();
let env = empty_sym_env(&store);
let ptr = fib::<pasta_curves::Fq>(
&store,
state.clone(),
black_box(prove_params.fib_n as u64),
);
let prover = NovaProver::new(prove_params.reduction_count, lang_pallas.clone());
let frames = &prover
.get_evaluation_frames(ptr, env, &store, limit, lang_rc.clone())
.unwrap();
b.iter_batched(
|| (frames, lang_rc.clone()),
|(frames, lang_rc)| {
let result = prover.prove(&pp, frames, &store, &lang_rc);
let _ = black_box(result);
},
BatchSize::LargeInput,
)
},
);
}
fn fibonacci_prove(c: &mut Criterion) {
tracing::debug!("{:?}", &*lurk::config::CONFIG);
let reduction_counts = [100, 600, 700, 800, 900];
let batch_sizes = [100, 200];
let mut group: BenchmarkGroup<'_, _> = c.benchmark_group("Prove");
group.sampling_mode(SamplingMode::Flat); // This can take a *while*
group.sample_size(10);
let state = State::init_lurk_state().rccell();
for fib_n in batch_sizes.iter() {
for reduction_count in reduction_counts.iter() {
let prove_params = ProveParams {
fib_n: *fib_n,
reduction_count: *reduction_count,
};
fibo_prove(prove_params, &mut group, state.clone());
}
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "flamegraph")] {
criterion_group! {
name = benches;
config = Criterion::default()
.measurement_time(Duration::from_secs(120))
.sample_size(10)
.with_profiler(pprof::criterion::PProfProfiler::new(100, pprof::criterion::Output::Flamegraph(None)));
targets =
fibonacci_prove,
}
} else {
criterion_group! {
name = benches;
config = Criterion::default()
.measurement_time(Duration::from_secs(120))
.sample_size(10);
targets =
fibonacci_prove,
}
}
}
criterion_main!(benches);