-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathkzg_benches.rs
More file actions
143 lines (130 loc) · 4.59 KB
/
Copy pathkzg_benches.rs
File metadata and controls
143 lines (130 loc) · 4.59 KB
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 std::path::PathBuf;
use c_kzg::*;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
use rand::{rngs::ThreadRng, Rng};
use std::sync::Arc;
fn generate_random_field_element(rng: &mut ThreadRng) -> Bytes32 {
let mut arr = [0u8; BYTES_PER_FIELD_ELEMENT];
rng.fill(&mut arr[..]);
arr[0] = 0;
arr.into()
}
fn generate_random_blob(rng: &mut ThreadRng) -> Blob {
let mut arr = [0u8; BYTES_PER_BLOB];
rng.fill(&mut arr[..]);
// Ensure that the blob is canonical by ensuring that
// each field element contained in the blob is < BLS_MODULUS
for i in 0..FIELD_ELEMENTS_PER_BLOB {
arr[i * BYTES_PER_FIELD_ELEMENT] = 0;
}
arr.into()
}
pub fn criterion_benchmark(c: &mut Criterion) {
let max_count: usize = 64;
let mut rng = rand::thread_rng();
let trusted_setup_file = PathBuf::from("../../src/trusted_setup.txt");
assert!(trusted_setup_file.exists());
let kzg_settings = Arc::new(KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap());
let blobs: Vec<Blob> = (0..max_count)
.map(|_| generate_random_blob(&mut rng))
.collect();
let commitments: Vec<Bytes48> = blobs
.iter()
.map(|blob| {
KzgCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings)
.unwrap()
.to_bytes()
})
.collect();
let proofs: Vec<Bytes48> = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KzgProof::compute_blob_kzg_proof(blob.clone(), *commitment, &kzg_settings)
.unwrap()
.to_bytes()
})
.collect();
let fields: Vec<Bytes32> = (0..max_count)
.map(|_| generate_random_field_element(&mut rng))
.collect();
c.bench_function("blob_to_kzg_commitment", |b| {
b.iter(|| {
KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap().clone(), &kzg_settings)
})
});
c.bench_function("compute_kzg_proof", |b| {
b.iter(|| {
KzgProof::compute_kzg_proof(
blobs.first().unwrap().clone(),
*fields.first().unwrap(),
&kzg_settings,
)
})
});
c.bench_function("compute_blob_kzg_proof", |b| {
b.iter(|| {
KzgProof::compute_blob_kzg_proof(
blobs.first().unwrap().clone(),
*commitments.first().unwrap(),
&kzg_settings,
)
})
});
c.bench_function("verify_kzg_proof", |b| {
b.iter(|| {
KzgProof::verify_kzg_proof(
*commitments.first().unwrap(),
*fields.first().unwrap(),
*fields.first().unwrap(),
*proofs.first().unwrap(),
&kzg_settings,
)
})
});
c.bench_function("verify_blob_kzg_proof", |b| {
b.iter(|| {
KzgProof::verify_blob_kzg_proof(
blobs.first().unwrap().clone(),
*commitments.first().unwrap(),
*proofs.first().unwrap(),
&kzg_settings,
)
})
});
let mut group = c.benchmark_group("verify_blob_kzg_proof_batch");
for count in [1, 2, 4, 8, 16, 32, 64] {
group.throughput(Throughput::Elements(count as u64));
group.bench_with_input(BenchmarkId::from_parameter(count), &count, |b, &count| {
b.iter_batched_ref(
|| {
let blobs_subset = blobs.clone().into_iter().take(count).collect::<Vec<Blob>>();
let commitments_subset = commitments
.clone()
.into_iter()
.take(count)
.collect::<Vec<Bytes48>>();
let proofs_subset = proofs
.clone()
.into_iter()
.take(count)
.collect::<Vec<Bytes48>>();
(blobs_subset, commitments_subset, proofs_subset)
},
|(blobs_subset, commitments_subset, proofs_subset)| {
KzgProof::verify_blob_kzg_proof_batch(
&blobs_subset,
&commitments_subset,
&proofs_subset,
&kzg_settings,
)
.unwrap();
},
BatchSize::LargeInput,
);
});
}
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);