ark_poly_commit/linear_codes/
ligero.rs

1use crate::{
2    linear_codes::{utils::calculate_t, LigeroPCParams, LinCodeParametersInfo},
3    utils::ceil_div,
4    PCCommitterKey, PCUniversalParams, PCVerifierKey,
5};
6use ark_crypto_primitives::{
7    crh::{CRHScheme, TwoToOneCRHScheme},
8    merkle_tree::{Config, LeafParam, TwoToOneParam},
9};
10use ark_ff::PrimeField;
11use ark_std::{log2, marker::PhantomData};
12#[cfg(all(not(feature = "std"), target_arch = "aarch64"))]
13use num_traits::Float;
14
15impl<F, C, H> LigeroPCParams<F, C, H>
16where
17    F: PrimeField,
18    C: Config,
19    H: CRHScheme,
20{
21    /// Create new UniversalParams
22    pub fn new(
23        sec_param: usize,
24        rho_inv: usize,
25        check_well_formedness: bool,
26        leaf_hash_param: LeafParam<C>,
27        two_to_one_hash_param: TwoToOneParam<C>,
28        col_hash_params: H::Parameters,
29    ) -> Self {
30        Self {
31            _field: PhantomData,
32            sec_param,
33            rho_inv,
34            check_well_formedness,
35            leaf_hash_param,
36            two_to_one_hash_param,
37            col_hash_params,
38        }
39    }
40}
41
42impl<F, C, H> PCUniversalParams for LigeroPCParams<F, C, H>
43where
44    F: PrimeField,
45    C: Config,
46    H: CRHScheme,
47{
48    fn max_degree(&self) -> usize {
49        if F::TWO_ADICITY < self.rho_inv as u32 {
50            0
51        } else if (F::TWO_ADICITY - self.rho_inv as u32) * 2 < 64 {
52            2_usize.pow((F::TWO_ADICITY - self.rho_inv as u32) * 2)
53        } else {
54            usize::MAX
55        }
56    }
57}
58
59impl<F, C, H> PCCommitterKey for LigeroPCParams<F, C, H>
60where
61    F: PrimeField,
62    C: Config,
63    H: CRHScheme,
64{
65    fn max_degree(&self) -> usize {
66        if (F::TWO_ADICITY - self.rho_inv as u32) * 2 < 64 {
67            2_usize.pow((F::TWO_ADICITY - self.rho_inv as u32) * 2)
68        } else {
69            usize::MAX
70        }
71    }
72
73    fn supported_degree(&self) -> usize {
74        <LigeroPCParams<F, C, H> as PCCommitterKey>::max_degree(self)
75    }
76}
77
78impl<F, C, H> PCVerifierKey for LigeroPCParams<F, C, H>
79where
80    F: PrimeField,
81    C: Config,
82    H: CRHScheme,
83{
84    fn max_degree(&self) -> usize {
85        if (F::TWO_ADICITY - self.rho_inv as u32) * 2 < 64 {
86            2_usize.pow((F::TWO_ADICITY - self.rho_inv as u32) * 2)
87        } else {
88            usize::MAX
89        }
90    }
91
92    fn supported_degree(&self) -> usize {
93        <LigeroPCParams<F, C, H> as PCVerifierKey>::max_degree(self)
94    }
95}
96
97impl<F, C, H> LinCodeParametersInfo<C, H> for LigeroPCParams<F, C, H>
98where
99    F: PrimeField,
100    C: Config,
101    H: CRHScheme,
102{
103    fn check_well_formedness(&self) -> bool {
104        self.check_well_formedness
105    }
106
107    fn distance(&self) -> (usize, usize) {
108        (self.rho_inv - 1, self.rho_inv)
109    }
110
111    fn sec_param(&self) -> usize {
112        self.sec_param
113    }
114
115    /// Compute the a suitable (for instance, FFT-friendly over F) matrix with at least poly_len entries.
116    /// The return pair (n, m) corresponds to the dimensions n x m.
117    /// FIXME: Maybe, there should be some checks for making sure the extended row can have an FFT.
118    fn compute_dimensions(&self, poly_len: usize) -> (usize, usize) {
119        assert_eq!(
120            (poly_len as f64) as usize,
121            poly_len,
122            "n cannot be converted to f64: aborting"
123        );
124        let t = calculate_t::<F>(self.sec_param(), self.distance(), poly_len).unwrap();
125        let n = 1 << log2((ceil_div(2 * poly_len, t) as f64).sqrt().ceil() as usize);
126        let m = ceil_div(poly_len, n);
127        (n, m)
128    }
129
130    fn leaf_hash_param(&self) -> &<<C as Config>::LeafHash as CRHScheme>::Parameters {
131        &self.leaf_hash_param
132    }
133
134    fn two_to_one_hash_param(
135        &self,
136    ) -> &<<C as Config>::TwoToOneHash as TwoToOneCRHScheme>::Parameters {
137        &self.two_to_one_hash_param
138    }
139
140    fn col_hash_params(&self) -> &<H as CRHScheme>::Parameters {
141        &self.col_hash_params
142    }
143}