dragoonfri_test_utils/lib.rs
1//! Code shared between tests and benches
2
3use ark_ff::{Fp128, MontBackend, MontConfig};
4use rand::{
5 distributions::{Distribution, Standard},
6 thread_rng, Rng,
7};
8
9pub const NUMBER_OF_POLYNOMIALS: usize = 10;
10pub const POLY_COEFFS_LEN: usize = 4096;
11pub const BLOWUP_FACTOR: usize = 4;
12pub const NUM_QUERIES: usize = 32;
13
14pub const DOMAIN_SIZE: usize = (POLY_COEFFS_LEN * BLOWUP_FACTOR).next_power_of_two();
15
16/// Matches [`winter_math::fields::f128::BaseElement`]
17#[derive(MontConfig)]
18#[modulus = "340282366920938463463374557953744961537"]
19#[generator = "3"]
20pub struct Fp128Config;
21/// A prime, fft-friendly field isomorph to [`winter_math::fields::f128::BaseElement`](https://github.com/facebook/winterfell/blob/9f21cf426cae080f8871ec2043573ce5652dad72/math/src/field/f128/mod.rs#L40)
22pub type Fq = Fp128<MontBackend<Fp128Config, 2>>;
23
24/// Sample a _random file_ pre-arranged in an $m \ times k$ matrix of elements of $\mathbb{F}$
25///
26/// > **Note**
27/// >
28/// > - $k$ is the FEC parameter
29/// > - $m$ is the number of polynomials which is related to $k$ and the size of the possibly
30/// > padded file by
31/// > $$s = k \times m \ times F$$
32/// > where $s$ is the size of the padded file and $F$ is the size, in bytes, of an element of
33/// > $\mathbb{F}$
34pub fn random_file<F: Clone>(k: usize, m: usize) -> Vec<Vec<F>>
35where
36 Standard: Distribution<F>,
37{
38 let nb_items = k * m;
39 let mut rng = thread_rng();
40 (0..nb_items)
41 .map(|_| rng.gen())
42 .collect::<Vec<_>>()
43 .chunks_exact(k)
44 .map(<[F]>::to_vec)
45 .collect()
46}
47
48/// allows to run a code snippet with a "_varying_" constant
49///
50/// this is especially useful in tests for functions that require a constant value.
51///
52/// ## example
53/// the following macro invocation
54/// ```rust
55/// # use dragoonfri_test_utils::do_for_multiple_folding_factors;
56/// do_for_multiple_folding_factors!(FACTOR = 2, 4, 8, 16 => {
57/// // some arbitrary code involving the `FACTOR` constant
58/// });
59/// ```
60/// will be expanded to
61/// ```compile_fail
62/// const FACTOR: usize = 2;
63/// // the code
64/// const FACTOR: usize = 4;
65/// // the code
66/// const FACTOR: usize = 8;
67/// // the code
68/// const FACTOR: usize = 16;
69/// // the code
70/// ```
71#[macro_export]
72macro_rules! do_for_multiple_folding_factors {
73 ($factor: ident = $($factors: literal),* => $action: block) => {
74 {
75 $({
76 const $factor: usize = $factors;
77 $action;
78 })*
79 }
80 };
81}