labrador/core/
crs.rs

1use crate::core::{challenge_set::ChallengeSet, env_params::EnvironmentParameters};
2use crate::ring::poly::PolyVector;
3
4pub struct PublicPrams {
5    // A \in R_q^(k*n)
6    pub matrix_a: Vec<PolyVector>,
7    // B_{ik} \in R_q^(k_1*k), for i \in [1,r] and k \in [0, t_1-1]
8    pub matrix_b: Vec<Vec<Vec<PolyVector>>>,
9    // C_{ijk} \in R_q^(k_2*1), for i \in [1,r], j \in [i, r], and k \in [0, t_2-1]
10    pub matrix_c: Vec<Vec<Vec<Vec<PolyVector>>>>,
11    // D_{ijk} \in R_q^(k_2*1), for i \in [1,r], j \in [i, r], and k \in [0, t_1-1]
12    pub matrix_d: Vec<Vec<Vec<Vec<PolyVector>>>>,
13}
14
15impl PublicPrams {
16    pub fn new(ep: &EnvironmentParameters) -> Self {
17        let d = ep.deg_bound_d;
18
19        let matrix_a = Self::challenge_matrix(ep.k, ep.n, d);
20
21        let matrix_b: Vec<Vec<Vec<PolyVector>>> = (0..ep.r)
22            .map(|_| {
23                (0..ep.t_1)
24                    .map(|_| Self::challenge_matrix(ep.k_1, ep.k, d))
25                    .collect()
26            })
27            .collect();
28
29        let matrix_c: Vec<Vec<Vec<Vec<PolyVector>>>> = (0..ep.r)
30            .map(|_| {
31                (0..ep.r)
32                    .map(|_| {
33                        (0..ep.t_2)
34                            .map(|_| Self::challenge_matrix(ep.k_2, 1, d))
35                            .collect()
36                    })
37                    .collect()
38            })
39            .collect();
40
41        let matrix_d: Vec<Vec<Vec<Vec<PolyVector>>>> = (0..ep.r)
42            .map(|_| {
43                (0..ep.r)
44                    .map(|_| {
45                        (0..ep.t_1)
46                            .map(|_| Self::challenge_matrix(ep.k_2, 1, d))
47                            .collect()
48                    })
49                    .collect()
50            })
51            .collect();
52
53        Self {
54            matrix_a,
55            matrix_b,
56            matrix_c,
57            matrix_d,
58        }
59    }
60
61    fn challenge_matrix(row: usize, col: usize, d: usize) -> Vec<PolyVector> {
62        (0..row)
63            .map(|_| {
64                (0..col)
65                    .map(|_| ChallengeSet::new(d).get_challenges().clone())
66                    .collect()
67            })
68            .collect()
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use std::time::Instant;
76
77    #[test]
78    fn test_crs() {
79        // set up example environment parameters, use default set for testing.
80        let ep_1 = EnvironmentParameters::default();
81
82        let total_start = Instant::now();
83        // generate the common reference string matrices A, B, C, D
84        let _pp = PublicPrams::new(&ep_1);
85
86        println!(
87            "Total time for PublicPrams::new: {:?}",
88            total_start.elapsed()
89        );
90    }
91}