forked from a16z/jolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.rs
143 lines (123 loc) · 4.8 KB
/
bench.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
use crate::host;
use crate::jolt::vm::rv32i_vm::{RV32IJoltVM, C, M};
use crate::jolt::vm::Jolt;
use ark_bn254::G1Projective;
use serde::Serialize;
#[derive(Debug, Copy, Clone, clap::ValueEnum)]
pub enum BenchType {
Fibonacci,
Sha2,
Sha3,
Sha2Chain,
}
#[allow(unreachable_patterns)] // good errors on new BenchTypes
pub fn benchmarks(
bench_type: BenchType,
_num_cycles: Option<usize>,
_memory_size: Option<usize>,
_bytecode_size: Option<usize>,
) -> Vec<(tracing::Span, Box<dyn FnOnce()>)> {
match bench_type {
BenchType::Sha2 => sha2(),
BenchType::Sha3 => sha3(),
BenchType::Sha2Chain => sha2chain(),
BenchType::Fibonacci => fibonacci(),
_ => panic!("BenchType does not have a mapping"),
}
}
fn fibonacci() -> Vec<(tracing::Span, Box<dyn FnOnce()>)> {
prove_example("fibonacci-guest", &9u32)
}
fn sha2() -> Vec<(tracing::Span, Box<dyn FnOnce()>)> {
prove_example("sha2-guest", &vec![5u8; 2048])
}
fn sha3() -> Vec<(tracing::Span, Box<dyn FnOnce()>)> {
prove_example("sha3-guest", &vec![5u8; 2048])
}
#[allow(dead_code)]
fn serialize_and_print_size(name: &str, item: &impl ark_serialize::CanonicalSerialize) {
use std::fs::File;
let mut file = File::create("temp_file").unwrap();
item.serialize_compressed(&mut file).unwrap();
let file_size_bytes = file.metadata().unwrap().len();
let file_size_kb = file_size_bytes as f64 / 1024.0;
let file_size_mb = file_size_kb / 1024.0;
println!("{:<30} : {:.3} MB", name, file_size_mb);
}
fn prove_example<T: Serialize>(
example_name: &str,
input: &T,
) -> Vec<(tracing::Span, Box<dyn FnOnce()>)> {
let mut tasks = Vec::new();
let mut program = host::Program::new(example_name);
program.set_input(input);
let task = move || {
let (bytecode, memory_init) = program.decode();
let (io_device, bytecode_trace, instruction_trace, memory_trace, circuit_flags) =
program.trace();
let preprocessing: crate::jolt::vm::JoltPreprocessing<
ark_ff::Fp<ark_ff::MontBackend<ark_bn254::FrConfig, 4>, 4>,
ark_ec::short_weierstrass::Projective<ark_bn254::g1::Config>,
> = RV32IJoltVM::preprocess(bytecode.clone(), memory_init, 1 << 20, 1 << 20, 1 << 22);
let (jolt_proof, jolt_commitments) = <RV32IJoltVM as Jolt<_, G1Projective, C, M>>::prove(
io_device,
bytecode_trace,
memory_trace,
instruction_trace,
circuit_flags,
preprocessing.clone(),
);
// println!("Proof sizing:");
// serialize_and_print_size("jolt_commitments", &jolt_commitments);
// serialize_and_print_size("jolt_proof", &jolt_proof);
// serialize_and_print_size(" jolt_proof.r1cs", &jolt_proof.r1cs);
// serialize_and_print_size(" jolt_proof.bytecode", &jolt_proof.bytecode);
// serialize_and_print_size(" jolt_proof.read_write_memory", &jolt_proof.read_write_memory);
// serialize_and_print_size(" jolt_proof.instruction_lookups", &jolt_proof.instruction_lookups);
let verification_result = RV32IJoltVM::verify(preprocessing, jolt_proof, jolt_commitments);
assert!(
verification_result.is_ok(),
"Verification failed with error: {:?}",
verification_result.err()
);
};
tasks.push((
tracing::info_span!("Example_E2E"),
Box::new(task) as Box<dyn FnOnce()>,
));
tasks
}
fn sha2chain() -> Vec<(tracing::Span, Box<dyn FnOnce()>)> {
let mut tasks = Vec::new();
let mut program = host::Program::new("sha2-chain-guest");
program.set_input(&[5u8; 32]);
program.set_input(&1024u32);
let task = move || {
let (bytecode, memory_init) = program.decode();
let (io_device, bytecode_trace, instruction_trace, memory_trace, circuit_flags) =
program.trace();
let preprocessing: crate::jolt::vm::JoltPreprocessing<
ark_ff::Fp<ark_ff::MontBackend<ark_bn254::FrConfig, 4>, 4>,
ark_ec::short_weierstrass::Projective<ark_bn254::g1::Config>,
> = RV32IJoltVM::preprocess(bytecode.clone(), memory_init, 1 << 20, 1 << 20, 1 << 22);
let (jolt_proof, jolt_commitments) = <RV32IJoltVM as Jolt<_, G1Projective, C, M>>::prove(
io_device,
bytecode_trace,
memory_trace,
instruction_trace,
circuit_flags,
preprocessing.clone(),
);
let verification_result = RV32IJoltVM::verify(preprocessing, jolt_proof, jolt_commitments);
assert!(
verification_result.is_ok(),
"Verification failed with error: {:?}",
verification_result.err()
);
};
tasks.push((
tracing::info_span!("Example_E2E"),
Box::new(task) as Box<dyn FnOnce()>,
));
tasks
}