Skip to content

Commit

Permalink
chore: add benchmarks for erasure encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed May 28, 2024
1 parent 0a3ba20 commit 9cf1f6b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions erasure_codes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ repository = { workspace = true }
[dependencies]
bls12_381 = { workspace = true }
polynomial = { workspace = true }

[dev-dependencies]
criterion = "0.5.1"
rand = "0.8.4"

[[bench]]
name = "benchmark"
harness = false
58 changes: 58 additions & 0 deletions erasure_codes/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::ops::Range;

use bls12_381::Scalar;
use crate_crypto_internal_peerdas_erasure_codes::{reed_solomon::Erasures, ReedSolomon};
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn bench_erasure_code_decoding_4096_8192(c: &mut Criterion) {
const POLYNOMIAL_LEN: usize = 4096;

let rs = ReedSolomon::new(POLYNOMIAL_LEN, 2);
let extended_poly_len = rs.extended_polynomial_length();

let mut encoded_polynomial = Vec::with_capacity(extended_poly_len);
for i in 0..extended_poly_len {
encoded_polynomial.push(black_box(-Scalar::from(i as u64 + 1)));
}

fn generate_unique_random_numbers(range: Range<usize>, n: usize) -> Vec<usize> {
use rand::prelude::SliceRandom;
let mut numbers: Vec<_> = range.into_iter().collect();
numbers.shuffle(&mut rand::thread_rng());
numbers.into_iter().take(n).collect()
}

let cell_size = 64;
let num_cells = extended_poly_len / cell_size;

let missing_cells = generate_unique_random_numbers(0..cell_size, num_cells / 2);

// Zero out the values in the polynomial that correspond to the cell_id
for cell in &missing_cells {
for i in 0..cell_size {
encoded_polynomial[*cell as usize * cell_size + i] = Scalar::from(0);
}
}

c.bench_function(
&format!(
"computing decoding: EXT_SIZE {}, MISSING_CELLS {}",
extended_poly_len,
num_cells / 2
),
|b| {
b.iter(|| {
rs.recover_polynomial_codeword(
encoded_polynomial.clone(),
Erasures::Cells {
cell_size,
cells: missing_cells.clone(),
},
)
})
},
);
}

criterion_group!(benches, bench_erasure_code_decoding_4096_8192);
criterion_main!(benches);

0 comments on commit 9cf1f6b

Please sign in to comment.