|
1 | 1 | use bls12_381::Scalar;
|
2 | 2 | use criterion::{criterion_group, criterion_main, Criterion};
|
3 |
| -use eip7594::{prover::ProverContext, trusted_setup}; |
| 3 | +use eip7594::{ |
| 4 | + constants::{BYTES_PER_BLOB, CELLS_PER_EXT_BLOB}, |
| 5 | + prover::ProverContext, |
| 6 | + trusted_setup, Cell, KZGCommitment, KZGProof, VerifierContext, |
| 7 | +}; |
4 | 8 |
|
5 |
| -/// This is here for reference, same as the above `bench_compute_proof_without_fk20`. |
6 |
| -pub fn bench_compute_cells_and_kzg_proofs(c: &mut Criterion) { |
7 |
| - const POLYNOMIAL_LEN: usize = 4096; |
8 |
| - |
9 |
| - let trusted_setup = trusted_setup::TrustedSetup::default(); |
10 |
| - let prover_context = ProverContext::with_num_threads(&trusted_setup, 100); |
| 9 | +const POLYNOMIAL_LEN: usize = 4096; |
11 | 10 |
|
12 |
| - let polynomial_4096: Vec<_> = (0..POLYNOMIAL_LEN) |
| 11 | +fn dummy_blob() -> [u8; BYTES_PER_BLOB] { |
| 12 | + let polynomial: Vec<_> = (0..POLYNOMIAL_LEN) |
13 | 13 | .map(|i| -Scalar::from(i as u64))
|
14 | 14 | .collect();
|
15 |
| - |
16 |
| - let blob: Vec<_> = polynomial_4096 |
| 15 | + let blob: Vec<_> = polynomial |
17 | 16 | .into_iter()
|
18 | 17 | .flat_map(|scalar| scalar.to_bytes_be())
|
19 | 18 | .collect();
|
20 |
| - let blob = &blob.try_into().unwrap(); |
| 19 | + blob.try_into().unwrap() |
| 20 | +} |
| 21 | + |
| 22 | +fn dummy_commitment_cells_and_proofs() -> ( |
| 23 | + KZGCommitment, |
| 24 | + ([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), |
| 25 | +) { |
| 26 | + let ctx = ProverContext::default(); |
| 27 | + let blob = dummy_blob(); |
| 28 | + |
| 29 | + let commitment = ctx.blob_to_kzg_commitment(&blob).unwrap(); |
| 30 | + (commitment, ctx.compute_cells_and_kzg_proofs(&blob).unwrap()) |
| 31 | +} |
| 32 | + |
| 33 | +const THREAD_COUNTS: [usize; 5] = [1, 4, 8, 16, 32]; |
| 34 | + |
| 35 | +pub fn bench_compute_cells_and_kzg_proofs(c: &mut Criterion) { |
| 36 | + let trusted_setup = trusted_setup::TrustedSetup::default(); |
| 37 | + |
| 38 | + let blob = dummy_blob(); |
| 39 | + |
| 40 | + for num_threads in THREAD_COUNTS { |
| 41 | + let prover_context = ProverContext::with_num_threads(&trusted_setup, num_threads); |
| 42 | + c.bench_function( |
| 43 | + &format!( |
| 44 | + "computing cells_and_kzg_proofs - NUM_THREADS: {}", |
| 45 | + num_threads |
| 46 | + ), |
| 47 | + |b| b.iter(|| prover_context.compute_cells_and_kzg_proofs(&blob)), |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub fn bench_recover_cells_and_compute_kzg_proofs(c: &mut Criterion) { |
| 53 | + let trusted_setup = trusted_setup::TrustedSetup::default(); |
| 54 | + |
| 55 | + let (_, (cells, proofs)) = dummy_commitment_cells_and_proofs(); |
| 56 | + let cell_ids: Vec<u64> = (0..cells.len()).map(|x| x as u64).collect(); |
| 57 | + |
| 58 | + // Worse case is when half of the cells are missing |
| 59 | + let half_cell_ids = &cell_ids[..CELLS_PER_EXT_BLOB / 2]; |
| 60 | + let half_cells = &cells[..CELLS_PER_EXT_BLOB / 2]; |
| 61 | + let half_cells = half_cells |
| 62 | + .into_iter() |
| 63 | + .map(|cell| cell.as_ref()) |
| 64 | + .collect::<Vec<_>>(); |
| 65 | + let half_proofs = &proofs[..CELLS_PER_EXT_BLOB / 2]; |
| 66 | + let half_proofs = half_proofs.into_iter().collect::<Vec<_>>(); |
| 67 | + |
| 68 | + for num_threads in THREAD_COUNTS { |
| 69 | + let prover_context = ProverContext::with_num_threads(&trusted_setup, num_threads); |
| 70 | + c.bench_function( |
| 71 | + &format!( |
| 72 | + "worse-case recover_cells_and_compute_proofs - NUM_THREADS: {}", |
| 73 | + num_threads |
| 74 | + ), |
| 75 | + |b| { |
| 76 | + b.iter(|| { |
| 77 | + prover_context.recover_cells_and_proofs( |
| 78 | + half_cell_ids.to_vec(), |
| 79 | + half_cells.to_vec(), |
| 80 | + half_proofs.to_vec(), |
| 81 | + ) |
| 82 | + }) |
| 83 | + }, |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +pub fn bench_verify_cell_kzg_proofs(c: &mut Criterion) { |
| 89 | + let trusted_setup = trusted_setup::TrustedSetup::default(); |
| 90 | + |
| 91 | + let (commitment, (cells, proofs)) = dummy_commitment_cells_and_proofs(); |
21 | 92 |
|
22 |
| - c.bench_function("computing cells_and_kzg_proofs", |b| { |
23 |
| - b.iter(|| prover_context.compute_cells_and_kzg_proofs(blob)) |
24 |
| - }); |
| 93 | + for num_threads in THREAD_COUNTS { |
| 94 | + let verifier_context = VerifierContext::with_num_threads(&trusted_setup, num_threads); |
| 95 | + c.bench_function( |
| 96 | + &format!("verify_cell_kzg_proof - NUM_THREADS: {}", num_threads), |
| 97 | + |b| { |
| 98 | + b.iter(|| { |
| 99 | + verifier_context.verify_cell_kzg_proof(&commitment, 0, &cells[0], &proofs[0]) |
| 100 | + }) |
| 101 | + }, |
| 102 | + ); |
| 103 | + } |
25 | 104 | }
|
26 | 105 |
|
27 |
| -criterion_group!(benches, bench_compute_cells_and_kzg_proofs); |
| 106 | +criterion_group!( |
| 107 | + benches, |
| 108 | + bench_compute_cells_and_kzg_proofs, |
| 109 | + bench_recover_cells_and_compute_kzg_proofs, |
| 110 | + bench_verify_cell_kzg_proofs |
| 111 | +); |
28 | 112 | criterion_main!(benches);
|
0 commit comments