Skip to content

Commit

Permalink
fast_registry
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubarov committed Jun 30, 2023
1 parent aa6b5dc commit fbc5ec2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 50 deletions.
9 changes: 3 additions & 6 deletions brakedown/benches/encode.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use p3_brakedown::{fast_height_14, BrakedownCode};
use p3_code::{CodeOrFamily, IdentityCode};
use p3_brakedown::fast_registry;
use p3_code::CodeOrFamily;
use p3_field::Field;
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::sparse::CsrMatrix;
use p3_mersenne_31::Mersenne31;
use rand::distributions::{Distribution, Standard};
use rand::thread_rng;
use std::any::type_name;

const BATCH_SIZE: usize = 1 << 12;
const A_ROW_WEIGHT: usize = 10;
const B_ROW_WEIGHT: usize = 20;

fn bench_encode(c: &mut Criterion) {
encode::<Mersenne31, 20>(c);
Expand All @@ -28,7 +25,7 @@ where
for n_log in [14] {
let n = 1 << n_log;

let code = fast_height_14();
let code = fast_registry();

let mut messages = RowMajorMatrix::rand(&mut rng, n, BATCH_SIZE);

Expand Down
1 change: 1 addition & 0 deletions brakedown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern crate alloc;

mod brakedown_code;
mod macros;
mod standard_fast;

pub use brakedown_code::*;
Expand Down
34 changes: 34 additions & 0 deletions brakedown/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
macro_rules! brakedown {
($a_width:literal, $a_height:literal, $a_density:literal,
$b_width:literal, $b_height:literal, $b_density:literal,
$inner_code:expr) => {{
let mut rng = ChaCha20Rng::seed_from_u64(0);
// TODO: Should actually by fixed column weight.
let a = CsrMatrix::<F>::rand_fixed_row_weight(&mut rng, $a_height, $a_width, $a_density);
let b = CsrMatrix::<F>::rand_fixed_row_weight(&mut rng, $b_height, $b_width, $b_density);
let inner_code = Box::new($inner_code);
BrakedownCode { a, b, inner_code }
}};
}

macro_rules! brakedown_to_rs {
($a_width:literal, $a_height:literal, $a_density:literal,
$b_width:literal, $b_height:literal, $b_density:literal) => {
brakedown!(
$a_width,
$a_height,
$a_density,
$b_width,
$b_height,
$b_density,
p3_reed_solomon::UndefinedReedSolomonCode::new(
p3_lde::NaiveUndefinedLDE,
$b_width,
$a_height
)
)
};
}

pub(crate) use brakedown;
pub(crate) use brakedown_to_rs;
57 changes: 17 additions & 40 deletions brakedown/src/standard_fast.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,32 @@
use crate::macros::{brakedown, brakedown_to_rs};
use crate::BrakedownCode;
use alloc::boxed::Box;
use p3_code::SystematicLinearCode;
use alloc::vec;
use p3_code::{LinearCodeFamily, SLCodeRegistry};
use p3_field::Field;
use p3_matrix::sparse::CsrMatrix;
use p3_matrix::MatrixRows;
use rand::distributions::{Distribution, Standard};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;

macro_rules! brakedown {
($a_width:literal, $a_height:literal, $a_density:literal,
$b_width:literal, $b_height:literal, $b_density:literal,
$inner_code:expr) => {{
let mut rng = ChaCha20Rng::seed_from_u64(0);
// TODO: Should actually by fixed column weight.
let a = CsrMatrix::<F>::rand_fixed_row_weight(&mut rng, $a_height, $a_width, $a_density);
let b = CsrMatrix::<F>::rand_fixed_row_weight(&mut rng, $b_height, $b_width, $b_density);
let inner_code = Box::new($inner_code);
BrakedownCode { a, b, inner_code }
}};
}

macro_rules! brakedown_to_rs {
($a_width:literal, $a_height:literal, $a_density:literal,
$b_width:literal, $b_height:literal, $b_density:literal) => {
brakedown!(
$a_width,
$a_height,
$a_density,
$b_width,
$b_height,
$b_density,
p3_reed_solomon::UndefinedReedSolomonCode::new(
p3_lde::NaiveUndefinedLDE,
$b_width,
$a_height
)
)
};
}

#[rustfmt::skip]
pub fn fast_height_14<F, In>() -> impl SystematicLinearCode<F, In>
pub fn fast_registry<F, In>() -> impl LinearCodeFamily<F, In>
where
F: Field,
Standard: Distribution<F>,
In: for<'a> MatrixRows<'a, F> + Sync,
{
// TODO: These numbers aren't 100% correct...
brakedown!(16384, 1967, 8, 2812, 4213, 20,
brakedown!(1967, 237, 9, 339, 506, 23,
brakedown!(237, 29, 11, 41, 61, 15,
brakedown_to_rs!(29, 4, 1, 4, 8, 1))))
#[rustfmt::skip]
let height_14 = brakedown!(16384, 1967, 8, 2810, 4211, 20,
brakedown!(1967, 237, 9, 338, 505, 23,
brakedown!(237, 29, 11, 41, 60, 15,
brakedown_to_rs!(29, 4, 0, 5, 7, 0))));

#[rustfmt::skip]
let height_16 = brakedown!(65536, 7865, 8, 11235, 16851, 19,
brakedown!(7865, 944, 8, 1348, 2022, 21,
brakedown!(944, 114, 9, 162, 242, 23,
brakedown_to_rs!(114, 14, 7, 20, 28, 11))));

SLCodeRegistry::new(vec![Box::new(height_14), Box::new(height_16)])
}
1 change: 1 addition & 0 deletions code/src/systematic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use p3_field::Field;
use p3_matrix::MatrixRows;

/// A systematic code, or a family thereof.
// TODO: Remove? Not really used.
pub trait SystematicCodeOrFamily<F: Field, In: for<'a> MatrixRows<'a, F>>:
CodeOrFamily<F, In>
{
Expand Down
2 changes: 1 addition & 1 deletion reed-solomon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
type Out = L::Out;

fn encode_batch(&self, messages: In) -> Self::Out {
self.lde.lde_batch(messages, self.n - self.k)
self.lde.lde_batch(messages, self.n)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rescue/src/rescue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
#[cfg(test)]
mod tests {
use itertools::Itertools;
use p3_field::{AbstractField, PrimeField};
use p3_field::AbstractField;
use p3_mersenne_31::Mersenne31;
use p3_symmetric::hasher::CryptographicHasher;
use p3_symmetric::permutation::CryptographicPermutation;
Expand Down
4 changes: 2 additions & 2 deletions rescue/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use sha3::{
Shake256,
};

/// Generate alpha, the smallest integer relatively prime to p − 1.
/// Generate alpha, the smallest integer relatively prime to `p − 1`.
pub(crate) fn get_alpha<F: PrimeField64>() -> u64 {
let p = F::ORDER_U64;

(3..p).find(|&a| a.gcd(p - 1) == 1).unwrap()
}

/// Given alpha, find its multiplicative inverse in Z/⟨p − 1⟩.
/// Given alpha, find its multiplicative inverse in `Z/⟨p − 1⟩`.
pub(crate) fn get_inverse<F: PrimeField64>(alpha: u64) -> u64 {
let p = F::ORDER_U64 as i64;
modinverse(alpha as i64, p - 1)
Expand Down

0 comments on commit fbc5ec2

Please sign in to comment.