polynomials_ecc/
domain.rs

1use bls12_381::Scalar;
2
3// The generator of the field to compute the primitive root of unity
4const FIELD_GEN: usize = 7;
5
6/// The power-of-2-sized domain to evaluate / interpolate
7pub struct Domain(Vec<Scalar>);
8
9impl Domain {
10    /// Returns a new domain given the exponent of a power of 2
11    pub fn new(e: u32) -> Domain {
12        let n = 2usize.pow(e);
13        let mut roots = vec![Scalar::zero(); n];
14
15        // r = k ยท n + 1
16        let k_bytes = (-Scalar::one() * Scalar::from(n as u64).invert().unwrap()).to_bytes();
17        let mut k = [0u64; 4];
18
19        for i in 0..4 {
20            k[i] = u64::from_le_bytes(k_bytes[i * 8..i * 8 + 8].try_into().unwrap());
21        }
22
23        // w = g^k
24        let w = Scalar::from(FIELD_GEN as u64).pow(&k);
25
26        roots[0] = Scalar::one();
27        for i in 1..n {
28            roots[i] = roots[i - 1] * w;
29        }
30
31        Domain(roots)
32    }
33
34    /// Returns the roots of unity
35    pub fn roots(&self) -> Vec<Scalar> {
36        self.0.clone()
37    }
38
39    /// Returns the length of the domain
40    pub fn len(&self) -> usize {
41        self.0.len()
42    }
43
44    /// Returns true if the domain is empty
45    pub fn is_empty(&self) -> bool {
46        self.0.len() == 0
47    }
48}