From db691a00d1e0fc0cba144b368556ec6c63e32845 Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Thu, 14 Nov 2024 18:03:14 +0100 Subject: [PATCH 1/9] add fec benchmark --- Cargo.toml | 4 ++++ examples/fec.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 examples/fec.rs diff --git a/Cargo.toml b/Cargo.toml index 3fed51b..fe2526e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,3 +62,7 @@ required-features = ["aplonk"] [[example]] name = "fri" required-features = ["fri"] + +[[example]] +name = "fec" +required-features = ["fri"] \ No newline at end of file diff --git a/examples/fec.rs b/examples/fec.rs new file mode 100644 index 0000000..bf87c68 --- /dev/null +++ b/examples/fec.rs @@ -0,0 +1,54 @@ +use ark_poly::univariate::DensePolynomial; +use fri::algorithms::Sha3_512; +use fri_test_utils::Fq; +use rand::{Rng, RngCore, SeedableRng}; +use komodo::algebra::linalg; +use komodo::fec; + +fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { + // Randomly drop some shards until k are left + while shards.len() > k { + let i = rng.gen_range(0..shards.len()); + shards.remove(i); + } +} + +fn main() { + let mut start; + + let mut rng = rand::rngs::StdRng::seed_from_u64(0xCAFE); + + let mut bytes = vec![0u8; 32 * 1024]; + rng.fill_bytes(&mut bytes); + + let (k,n) = (1024,2048); + let matrix = linalg::Matrix::random(k, n, &mut rng); + + start = std::time::Instant::now(); + let mut shards = fec::encode::<Fq>(&bytes, &matrix).unwrap(); + println!("Encoding took: {:?}", start.elapsed()); + + start = std::time::Instant::now(); + let evaluations = komodo::fri::evaluate::<Fq>(&bytes, k, n); + + let evals = evaluations.clone(); + let shards2 = komodo::fri::encode::<Fq>(&bytes, evals, k); + println!("FFT Encoding took: {:?}", start.elapsed()); + + random_loss(&mut shards, k, &mut rng); + + start = std::time::Instant::now(); + let recovered = fec::decode::<Fq>(shards).unwrap(); + println!("Decoding took: {:?}", start.elapsed()); + + + let mut blocks = komodo::fri::prove::<2, Fq, Sha3_512, DensePolynomial<Fq>>(evaluations, shards2, 2, 2, 1).unwrap(); + + random_loss(&mut blocks, k, &mut rng); + + start = std::time::Instant::now(); + komodo::fri::decode::<Fq, Sha3_512>(blocks, n); + println!("FRI Decoding took: {:?}", start.elapsed()); + + assert_eq!(bytes, recovered); +} \ No newline at end of file -- GitLab From 1fb554e12d90c87337c7b24d6b2d8e1f0de4a642 Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Fri, 15 Nov 2024 15:35:54 +0100 Subject: [PATCH 2/9] add time module to use macros in examples --- examples/time.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/time.rs diff --git a/examples/time.rs b/examples/time.rs new file mode 100644 index 0000000..c761835 --- /dev/null +++ b/examples/time.rs @@ -0,0 +1,55 @@ + +/// measure the time it takes to apply a function on a set of arguments and returns the result of +/// the call +/// +/// ```rust +/// fn add(a: i32, b: i32) { a + b } +/// let (res, time) = timeit!(add, 1, 2); +/// ``` +/// will be the same as +/// ```rust +/// fn add(a: i32, b: i32) { a + b } +/// let (res, time) = { +/// let start = Instant::now(); +/// let res = add(1, 2); +/// let time = start.elapsed(); +/// (res, time) +/// }; +/// ``` +#[macro_export] +macro_rules! timeit { + ($func:expr, $( $args:expr ),*) => {{ + let start = Instant::now(); + let res = $func( $( $args ),* ); + let time = start.elapsed(); + (res, time) + }}; +} + +/// same as [`timeit`] but prints a name and the time at the end directly +/// +/// ```rust +/// fn add(a: i32, b: i32) { a + b } +/// let res = timeit_and_print!("addition", add, 1, 2); +/// ``` +/// will be the same as +/// ```rust +/// fn add(a: i32, b: i32) { a + b } +/// let res = { +/// print!("addition: "); +/// let start = Instant::now(); +/// let res = add(1, 2); +/// let time = start.elapsed(); +/// println!("{}", time.as_nanos()); +/// res +/// }; +/// ``` +#[macro_export] +macro_rules! timeit_and_print { + ($name: expr, $func:expr, $( $args:expr ),*) => {{ + print!("{}: ", $name); + let (res, time) = timeit!($func, $($args),*); + println!("{}", time.as_nanos()); + res + }}; +} -- GitLab From e57c3928881eca2de5f2c03abf4b2228dcd8a25a Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Fri, 15 Nov 2024 15:36:21 +0100 Subject: [PATCH 3/9] add an example to measure coding time for FEC and FFT (fri) --- examples/fec.rs | 114 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 86 insertions(+), 28 deletions(-) diff --git a/examples/fec.rs b/examples/fec.rs index bf87c68..c47aaf5 100644 --- a/examples/fec.rs +++ b/examples/fec.rs @@ -1,9 +1,17 @@ use ark_poly::univariate::DensePolynomial; -use fri::algorithms::Sha3_512; -use fri_test_utils::Fq; +use clap::{arg, Parser, ValueEnum}; +use dragoonfri_test_utils::Fq; +use dragoonfri::algorithms::Sha3_512; use rand::{Rng, RngCore, SeedableRng}; use komodo::algebra::linalg; -use komodo::fec; +use komodo::{fec, fri}; + +use std::time::Instant; +use ark_ff::PrimeField; +use komodo::fec::Shard; + +#[macro_use] +mod time; fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { // Randomly drop some shards until k are left @@ -13,42 +21,92 @@ fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { } } -fn main() { - let mut start; +#[derive(ValueEnum, Debug, Clone)] +enum Coding { + Matrix, + FFT, +} + +#[derive(ValueEnum, Debug, Clone)] +enum FiniteField { + FP128, +} - let mut rng = rand::rngs::StdRng::seed_from_u64(0xCAFE); - let mut bytes = vec![0u8; 32 * 1024]; - rng.fill_bytes(&mut bytes); +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(short, long)] + data_size: usize, + + #[arg(long, default_value = "1234")] + seed: u64, + + #[arg(short)] + k: usize, + + #[arg(short)] + n: usize, - let (k,n) = (1024,2048); - let matrix = linalg::Matrix::random(k, n, &mut rng); + #[arg(long, default_value = "fp128")] + finite_field: FiniteField, + + #[arg(long, default_value = "matrix")] + coding: Coding, +} + +fn encode_fft<F: PrimeField>(bytes: &[u8], k: usize, n: usize) -> Vec<Shard<F>> { + let evaluations = fri::evaluate::<F>(&bytes, k, n); + fri::encode::<F>(&bytes, evaluations, k) +} + +fn run<F: PrimeField>( + bytes: &[u8], + k: usize, + n: usize, + seed: u64, + coding: Coding +) { + let mut rng = rand::rngs::StdRng::seed_from_u64(seed); - start = std::time::Instant::now(); - let mut shards = fec::encode::<Fq>(&bytes, &matrix).unwrap(); - println!("Encoding took: {:?}", start.elapsed()); - start = std::time::Instant::now(); - let evaluations = komodo::fri::evaluate::<Fq>(&bytes, k, n); + match coding { + Coding::Matrix => { + let matrix = linalg::Matrix::random(k, n, &mut rng); + let mut shards = timeit_and_print!("encoding", fec::encode,&bytes, &matrix).unwrap(); + random_loss(&mut shards, k, &mut rng); + let recovered = timeit_and_print!("decoding", fec::decode::<F>, shards).unwrap(); + assert_eq!(bytes, recovered); + }, + Coding::FFT => { + assert_eq!(n.count_ones(), 1, "n must be a power of 2"); + assert_eq!(k.count_ones(), 1, "k must be a power of 2"); + let shards = timeit_and_print!("encoding", + encode_fft::<F>, + &bytes, k, n); - let evals = evaluations.clone(); - let shards2 = komodo::fri::encode::<Fq>(&bytes, evals, k); - println!("FFT Encoding took: {:?}", start.elapsed()); + let evaluations = fri::evaluate::<F>(&bytes, k, n); + let mut blocks = fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1).unwrap(); - random_loss(&mut shards, k, &mut rng); + random_loss(&mut blocks, k, &mut rng); - start = std::time::Instant::now(); - let recovered = fec::decode::<Fq>(shards).unwrap(); - println!("Decoding took: {:?}", start.elapsed()); + let recovered = timeit_and_print!("decoding", fri::decode::<F, Sha3_512>, blocks, n); + assert_eq!(bytes, recovered, "decoded data does not match original data"); + } + } +} - let mut blocks = komodo::fri::prove::<2, Fq, Sha3_512, DensePolynomial<Fq>>(evaluations, shards2, 2, 2, 1).unwrap(); +fn main() { + let args = Args::parse(); - random_loss(&mut blocks, k, &mut rng); + let mut rng = rand::rngs::StdRng::seed_from_u64(args.seed); - start = std::time::Instant::now(); - komodo::fri::decode::<Fq, Sha3_512>(blocks, n); - println!("FRI Decoding took: {:?}", start.elapsed()); + let mut bytes = vec![0u8; args.data_size]; + rng.fill_bytes(&mut bytes); - assert_eq!(bytes, recovered); + let (k,n) = (args.k, args.n); + match args.finite_field { + FiniteField::FP128 => run::<Fq>(&bytes, k, n, args.seed, args.coding), + } } \ No newline at end of file -- GitLab From 56e530db103f8ce2d4a7a49dd91f0209a8067a5f Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Wed, 20 Nov 2024 14:41:42 +0100 Subject: [PATCH 4/9] add feature fri for komodo dependency for benchmarks --- benchmarks/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/benchmarks/Cargo.toml b/benchmarks/Cargo.toml index 5b980d2..2b866b2 100644 --- a/benchmarks/Cargo.toml +++ b/benchmarks/Cargo.toml @@ -20,6 +20,7 @@ ark-secp256r1 = "0.4.0" ark-std = "0.4.0" ark-vesta = "0.4.0" clap = { version = "4.5.4", features = ["derive"] } -komodo = { path = ".." } +komodo = { path = "..", features = ["fri"] } plnk = { git = "https://gitlab.isae-supaero.fr/a.stevan/plnk", tag = "0.7.0", version = "0.7.0" } rand = "0.8.5" +dragoonfri = { version = "0.1.0"} -- GitLab From fb17e93f91e552ae11e7ee9eff6bbb2bafb847d5 Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Wed, 20 Nov 2024 14:42:02 +0100 Subject: [PATCH 5/9] add the fft encoding method (from fri) --- benchmarks/src/bin/fec.rs | 117 +++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 28 deletions(-) diff --git a/benchmarks/src/bin/fec.rs b/benchmarks/src/bin/fec.rs index 173438d..cdcf73d 100644 --- a/benchmarks/src/bin/fec.rs +++ b/benchmarks/src/bin/fec.rs @@ -1,15 +1,25 @@ // see `examples/benches/README.md` use ark_ff::PrimeField; - +use ark_poly::univariate::DensePolynomial; use clap::{arg, command, Parser, ValueEnum}; -use komodo::{algebra::linalg::Matrix, fec}; +use dragoonfri::algorithms::Sha3_512; +use komodo::{algebra::linalg::Matrix, fec, fri}; use plnk::Bencher; use rand::{rngs::ThreadRng, thread_rng, Rng, RngCore}; +use benchmarks::fields::Fq128; fn random_bytes(n: usize, rng: &mut ThreadRng) -> Vec<u8> { (0..n).map(|_| rng.gen::<u8>()).collect() } +fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { + // Randomly drop some shards until k are left + while shards.len() > k { + let i = rng.gen_range(0..shards.len()); + shards.remove(i); + } +} + fn build_encoding_mat<F: PrimeField>( k: usize, n: usize, @@ -23,49 +33,92 @@ fn build_encoding_mat<F: PrimeField>( .map(|i| F::from_le_bytes_mod_order(&i.to_le_bytes())) .collect(); Matrix::vandermonde_unchecked(&points, k) - } + }, + _ => panic!("FFT encoding is not supported for matrix encoding"), } } fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, encoding: &Encoding) { let mut rng = thread_rng(); - let encoding_mat = build_encoding_mat(k, n, encoding, &mut rng); - - plnk::bench( - b, - &format!( - r#"{{"bytes": {}, "step": "encode", "k": {}, "n": {}}}"#, - nb_bytes, k, n - ), - || { + match encoding { + Encoding::FFT => { + assert_eq!(n.count_ones(), 1, "n must be a power of 2"); + assert_eq!(k.count_ones(), 1, "k must be a power of 2"); let bytes = random_bytes(nb_bytes, &mut rng); + let mut shards: Vec<fec::Shard<F>> = vec![]; + plnk::bench( + b, + &format!( + r#"{{"bytes": {}, "step": "encode", "k": {}, "n": {}}}"#, + nb_bytes, k, n + ), + || { + plnk::timeit(|| { + let evaluations = fri::evaluate::<F>(&bytes, k, n); + shards = fri::encode::<F>(&bytes, evaluations, k) + }) + } + ); + + let evaluations = fri::evaluate::<F>(&bytes, k, n); + let mut blocks = fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1).unwrap(); + + random_loss(&mut blocks, k, &mut rng); + + plnk::bench( + b, + &format!( + r#"{{"bytes": {}, "step": "decode", "k": {}, "n": {}}}"#, + nb_bytes, k, n + ), + || { + plnk::timeit(|| { + fri::decode::<F, Sha3_512>(blocks.clone(), n); + }) + }, + ); + } + _ => { + let encoding_mat = build_encoding_mat(k, n, encoding, &mut rng); + + plnk::bench( + b, + &format!( + r#"{{"bytes": {}, "step": "encode", "k": {}, "n": {}}}"#, + nb_bytes, k, n + ), + || { + let bytes = random_bytes(nb_bytes, &mut rng); - plnk::timeit(|| fec::encode::<F>(&bytes, &encoding_mat).unwrap()) - }, - ); + plnk::timeit(|| fec::encode::<F>(&bytes, &encoding_mat).unwrap()) + }, + ); - let encoding_mat = build_encoding_mat(k, k, encoding, &mut rng); + let encoding_mat = build_encoding_mat(k, k, encoding, &mut rng); - plnk::bench( - b, - &format!( - r#"{{"bytes": {}, "step": "decode", "k": {}, "n": {}}}"#, - nb_bytes, k, n - ), - || { - let bytes = random_bytes(nb_bytes, &mut rng); - let shards = fec::encode::<F>(&bytes, &encoding_mat).unwrap(); + plnk::bench( + b, + &format!( + r#"{{"bytes": {}, "step": "decode", "k": {}, "n": {}}}"#, + nb_bytes, k, n + ), + || { + let bytes = random_bytes(nb_bytes, &mut rng); + let shards = fec::encode::<F>(&bytes, &encoding_mat).unwrap(); - plnk::timeit(|| fec::decode::<F>(shards.clone()).unwrap()) - }, - ); + plnk::timeit(|| fec::decode::<F>(shards.clone()).unwrap()) + }, + ); + } + } } #[derive(ValueEnum, Clone)] enum Encoding { Vandermonde, Random, + FFT, } #[derive(ValueEnum, Clone, Hash, PartialEq, Eq)] @@ -73,6 +126,7 @@ enum Curve { BLS12381, BN254, Pallas, + FP128 } #[derive(Parser)] @@ -124,6 +178,13 @@ fn main() { cli.n, &cli.encoding, ), + Curve::FP128 => template::<Fq128>( + &b.with_name("FP128"), + n, + cli.k, + cli.n, + &cli.encoding, + ), } } } -- GitLab From 7adb63ff3e7bb0fdcf4f65ee1ba6cf47eef6e561 Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Wed, 20 Nov 2024 14:57:05 +0100 Subject: [PATCH 6/9] add method field in log --- benchmarks/src/bin/fec.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/benchmarks/src/bin/fec.rs b/benchmarks/src/bin/fec.rs index cdcf73d..3603be0 100644 --- a/benchmarks/src/bin/fec.rs +++ b/benchmarks/src/bin/fec.rs @@ -50,7 +50,7 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc plnk::bench( b, &format!( - r#"{{"bytes": {}, "step": "encode", "k": {}, "n": {}}}"#, + r#"{{"bytes": {}, "step": "encode", "method": "fft", "k": {}, "n": {}}}"#, nb_bytes, k, n ), || { @@ -69,7 +69,7 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc plnk::bench( b, &format!( - r#"{{"bytes": {}, "step": "decode", "k": {}, "n": {}}}"#, + r#"{{"bytes": {}, "step": "decode", "method":"fft" "k": {}, "n": {}}}"#, nb_bytes, k, n ), || { @@ -85,7 +85,7 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc plnk::bench( b, &format!( - r#"{{"bytes": {}, "step": "encode", "k": {}, "n": {}}}"#, + r#"{{"bytes": {}, "step": "encode", "method": "matrix", "k": {}, "n": {}}}"#, nb_bytes, k, n ), || { @@ -100,7 +100,7 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc plnk::bench( b, &format!( - r#"{{"bytes": {}, "step": "decode", "k": {}, "n": {}}}"#, + r#"{{"bytes": {}, "step": "decode", "method":"matrix", "k": {}, "n": {}}}"#, nb_bytes, k, n ), || { -- GitLab From 814b4c5a0de3577335009c29a0feb64950782694 Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Wed, 20 Nov 2024 16:09:41 +0100 Subject: [PATCH 7/9] format code --- benchmarks/src/bin/fec.rs | 38 ++++++++++++++++++-------------------- examples/fec.rs | 38 ++++++++++++++++---------------------- examples/time.rs | 1 - 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/benchmarks/src/bin/fec.rs b/benchmarks/src/bin/fec.rs index 3603be0..433a9d6 100644 --- a/benchmarks/src/bin/fec.rs +++ b/benchmarks/src/bin/fec.rs @@ -1,12 +1,12 @@ // see `examples/benches/README.md` use ark_ff::PrimeField; use ark_poly::univariate::DensePolynomial; +use benchmarks::fields::Fq128; use clap::{arg, command, Parser, ValueEnum}; use dragoonfri::algorithms::Sha3_512; use komodo::{algebra::linalg::Matrix, fec, fri}; use plnk::Bencher; use rand::{rngs::ThreadRng, thread_rng, Rng, RngCore}; -use benchmarks::fields::Fq128; fn random_bytes(n: usize, rng: &mut ThreadRng) -> Vec<u8> { (0..n).map(|_| rng.gen::<u8>()).collect() @@ -33,7 +33,7 @@ fn build_encoding_mat<F: PrimeField>( .map(|i| F::from_le_bytes_mod_order(&i.to_le_bytes())) .collect(); Matrix::vandermonde_unchecked(&points, k) - }, + } _ => panic!("FFT encoding is not supported for matrix encoding"), } } @@ -50,27 +50,29 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc plnk::bench( b, &format!( - r#"{{"bytes": {}, "step": "encode", "method": "fft", "k": {}, "n": {}}}"#, - nb_bytes, k, n + r#"{{"bytes": {}, "step": "encode", "method": "fft", "k": {}, "n": {}}}"#, + nb_bytes, k, n ), || { - plnk::timeit(|| { - let evaluations = fri::evaluate::<F>(&bytes, k, n); - shards = fri::encode::<F>(&bytes, evaluations, k) - }) - } + plnk::timeit(|| { + let evaluations = fri::evaluate::<F>(&bytes, k, n); + shards = fri::encode::<F>(&bytes, evaluations, k) + }) + }, ); let evaluations = fri::evaluate::<F>(&bytes, k, n); - let mut blocks = fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1).unwrap(); + let mut blocks = + fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1) + .unwrap(); random_loss(&mut blocks, k, &mut rng); plnk::bench( b, &format!( - r#"{{"bytes": {}, "step": "decode", "method":"fft" "k": {}, "n": {}}}"#, - nb_bytes, k, n + r#"{{"bytes": {}, "step": "decode", "method":"fft" "k": {}, "n": {}}}"#, + nb_bytes, k, n ), || { plnk::timeit(|| { @@ -126,7 +128,7 @@ enum Curve { BLS12381, BN254, Pallas, - FP128 + FP128, } #[derive(Parser)] @@ -178,13 +180,9 @@ fn main() { cli.n, &cli.encoding, ), - Curve::FP128 => template::<Fq128>( - &b.with_name("FP128"), - n, - cli.k, - cli.n, - &cli.encoding, - ), + Curve::FP128 => { + template::<Fq128>(&b.with_name("FP128"), n, cli.k, cli.n, &cli.encoding) + } } } } diff --git a/examples/fec.rs b/examples/fec.rs index c47aaf5..713888f 100644 --- a/examples/fec.rs +++ b/examples/fec.rs @@ -1,14 +1,14 @@ use ark_poly::univariate::DensePolynomial; use clap::{arg, Parser, ValueEnum}; -use dragoonfri_test_utils::Fq; use dragoonfri::algorithms::Sha3_512; -use rand::{Rng, RngCore, SeedableRng}; +use dragoonfri_test_utils::Fq; use komodo::algebra::linalg; use komodo::{fec, fri}; +use rand::{Rng, RngCore, SeedableRng}; -use std::time::Instant; use ark_ff::PrimeField; use komodo::fec::Shard; +use std::time::Instant; #[macro_use] mod time; @@ -32,7 +32,6 @@ enum FiniteField { FP128, } - #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { @@ -60,43 +59,38 @@ fn encode_fft<F: PrimeField>(bytes: &[u8], k: usize, n: usize) -> Vec<Shard<F>> fri::encode::<F>(&bytes, evaluations, k) } -fn run<F: PrimeField>( - bytes: &[u8], - k: usize, - n: usize, - seed: u64, - coding: Coding -) { +fn run<F: PrimeField>(bytes: &[u8], k: usize, n: usize, seed: u64, coding: Coding) { let mut rng = rand::rngs::StdRng::seed_from_u64(seed); - match coding { Coding::Matrix => { let matrix = linalg::Matrix::random(k, n, &mut rng); - let mut shards = timeit_and_print!("encoding", fec::encode,&bytes, &matrix).unwrap(); + let mut shards = timeit_and_print!("encoding", fec::encode, &bytes, &matrix).unwrap(); random_loss(&mut shards, k, &mut rng); let recovered = timeit_and_print!("decoding", fec::decode::<F>, shards).unwrap(); assert_eq!(bytes, recovered); - }, + } Coding::FFT => { assert_eq!(n.count_ones(), 1, "n must be a power of 2"); assert_eq!(k.count_ones(), 1, "k must be a power of 2"); - let shards = timeit_and_print!("encoding", - encode_fft::<F>, - &bytes, k, n); + let shards = timeit_and_print!("encoding", encode_fft::<F>, &bytes, k, n); let evaluations = fri::evaluate::<F>(&bytes, k, n); - let mut blocks = fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1).unwrap(); + let mut blocks = + fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1) + .unwrap(); random_loss(&mut blocks, k, &mut rng); let recovered = timeit_and_print!("decoding", fri::decode::<F, Sha3_512>, blocks, n); - assert_eq!(bytes, recovered, "decoded data does not match original data"); + assert_eq!( + bytes, recovered, + "decoded data does not match original data" + ); } } } - fn main() { let args = Args::parse(); @@ -105,8 +99,8 @@ fn main() { let mut bytes = vec![0u8; args.data_size]; rng.fill_bytes(&mut bytes); - let (k,n) = (args.k, args.n); + let (k, n) = (args.k, args.n); match args.finite_field { FiniteField::FP128 => run::<Fq>(&bytes, k, n, args.seed, args.coding), } -} \ No newline at end of file +} diff --git a/examples/time.rs b/examples/time.rs index c761835..db9e627 100644 --- a/examples/time.rs +++ b/examples/time.rs @@ -1,4 +1,3 @@ - /// measure the time it takes to apply a function on a set of arguments and returns the result of /// the call /// -- GitLab From 089e530cbbfd8422f915c00154c17f20df4cf0d8 Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Wed, 20 Nov 2024 16:32:34 +0100 Subject: [PATCH 8/9] refactor utils module, and correct enum names --- benchmarks/src/bin/fec.rs | 4 ++-- examples/fec.rs | 17 +++++++++-------- examples/{ => utils}/time.rs | 0 3 files changed, 11 insertions(+), 10 deletions(-) rename examples/{ => utils}/time.rs (100%) diff --git a/benchmarks/src/bin/fec.rs b/benchmarks/src/bin/fec.rs index 433a9d6..d738306 100644 --- a/benchmarks/src/bin/fec.rs +++ b/benchmarks/src/bin/fec.rs @@ -42,7 +42,7 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc let mut rng = thread_rng(); match encoding { - Encoding::FFT => { + Encoding::Fft => { assert_eq!(n.count_ones(), 1, "n must be a power of 2"); assert_eq!(k.count_ones(), 1, "k must be a power of 2"); let bytes = random_bytes(nb_bytes, &mut rng); @@ -120,7 +120,7 @@ fn template<F: PrimeField>(b: &Bencher, nb_bytes: usize, k: usize, n: usize, enc enum Encoding { Vandermonde, Random, - FFT, + Fft, } #[derive(ValueEnum, Clone, Hash, PartialEq, Eq)] diff --git a/examples/fec.rs b/examples/fec.rs index 713888f..3d6c1ff 100644 --- a/examples/fec.rs +++ b/examples/fec.rs @@ -10,9 +10,10 @@ use ark_ff::PrimeField; use komodo::fec::Shard; use std::time::Instant; -#[macro_use] +#[path = "utils/time.rs"] mod time; + fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { // Randomly drop some shards until k are left while shards.len() > k { @@ -24,7 +25,7 @@ fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { #[derive(ValueEnum, Debug, Clone)] enum Coding { Matrix, - FFT, + Fft, } #[derive(ValueEnum, Debug, Clone)] @@ -55,8 +56,8 @@ struct Args { } fn encode_fft<F: PrimeField>(bytes: &[u8], k: usize, n: usize) -> Vec<Shard<F>> { - let evaluations = fri::evaluate::<F>(&bytes, k, n); - fri::encode::<F>(&bytes, evaluations, k) + let evaluations = fri::evaluate::<F>(bytes, k, n); + fri::encode::<F>(bytes, evaluations, k) } fn run<F: PrimeField>(bytes: &[u8], k: usize, n: usize, seed: u64, coding: Coding) { @@ -65,17 +66,17 @@ fn run<F: PrimeField>(bytes: &[u8], k: usize, n: usize, seed: u64, coding: Codin match coding { Coding::Matrix => { let matrix = linalg::Matrix::random(k, n, &mut rng); - let mut shards = timeit_and_print!("encoding", fec::encode, &bytes, &matrix).unwrap(); + let mut shards = timeit_and_print!("encoding", fec::encode, bytes, &matrix).unwrap(); random_loss(&mut shards, k, &mut rng); let recovered = timeit_and_print!("decoding", fec::decode::<F>, shards).unwrap(); assert_eq!(bytes, recovered); } - Coding::FFT => { + Coding::Fft => { assert_eq!(n.count_ones(), 1, "n must be a power of 2"); assert_eq!(k.count_ones(), 1, "k must be a power of 2"); - let shards = timeit_and_print!("encoding", encode_fft::<F>, &bytes, k, n); + let shards = timeit_and_print!("encoding", encode_fft::<F>, bytes, k, n); - let evaluations = fri::evaluate::<F>(&bytes, k, n); + let evaluations = fri::evaluate::<F>(bytes, k, n); let mut blocks = fri::prove::<2, F, Sha3_512, DensePolynomial<F>>(evaluations, shards, 2, 2, 1) .unwrap(); diff --git a/examples/time.rs b/examples/utils/time.rs similarity index 100% rename from examples/time.rs rename to examples/utils/time.rs -- GitLab From 5529579a356fb2824133c3ece64b1e7b6eb2726d Mon Sep 17 00:00:00 2001 From: "j.detchart" <jonathan.detchart@isae-supaero.fr> Date: Wed, 20 Nov 2024 16:33:07 +0100 Subject: [PATCH 9/9] format code --- examples/fec.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/fec.rs b/examples/fec.rs index 3d6c1ff..ab49d8a 100644 --- a/examples/fec.rs +++ b/examples/fec.rs @@ -13,7 +13,6 @@ use std::time::Instant; #[path = "utils/time.rs"] mod time; - fn random_loss<T>(shards: &mut Vec<T>, k: usize, rng: &mut impl Rng) { // Randomly drop some shards until k are left while shards.len() > k { -- GitLab