kimchi/circuits/
domains.rs

1//! This module describes the evaluation domains that can be used by the
2//! polynomials.
3
4use ark_ff::FftField;
5use ark_poly::{EvaluationDomain, Radix2EvaluationDomain};
6use serde::{Deserialize, Serialize};
7use serde_with::serde_as;
8
9use crate::error::DomainCreationError;
10
11/// The different multiplicaive domain sizes that can be used by the polynomials.
12/// We do support up to 8 times the size of the original domain for now.
13#[derive(Clone, Copy, Debug, PartialEq)]
14pub enum Domain {
15    D1 = 1,
16    D2 = 2,
17    D4 = 4,
18    D8 = 8,
19}
20
21#[serde_as]
22#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
23pub struct EvaluationDomains<F: FftField> {
24    #[serde_as(as = "o1_utils::serialization::SerdeAs")]
25    pub d1: Radix2EvaluationDomain<F>, // size n
26    #[serde_as(as = "o1_utils::serialization::SerdeAs")]
27    pub d2: Radix2EvaluationDomain<F>, // size 2n
28    #[serde_as(as = "o1_utils::serialization::SerdeAs")]
29    pub d4: Radix2EvaluationDomain<F>, // size 4n
30    #[serde_as(as = "o1_utils::serialization::SerdeAs")]
31    pub d8: Radix2EvaluationDomain<F>, // size 8n
32}
33
34impl<F: FftField> EvaluationDomains<F> {
35    /// Creates 4 evaluation domains `d1` (of size `n`), `d2` (of size `2n`),
36    /// `d4` (of size `4n`), and `d8` (of size `8n`). If generator of `d8` is
37    /// `g`, the generator of `d4` is `g^2`, the generator of `d2` is `g^4`, and
38    /// the generator of `d1` is `g^8`.
39    pub fn create(n: usize) -> Result<Self, DomainCreationError> {
40        let n = Radix2EvaluationDomain::<F>::compute_size_of_domain(n)
41            .ok_or(DomainCreationError::DomainSizeFailed(n))?;
42
43        let d1 = Radix2EvaluationDomain::<F>::new(n).ok_or(
44            DomainCreationError::DomainConstructionFailed("d1".to_string(), n),
45        )?;
46
47        // we also create domains of larger sizes
48        // to efficiently operate on polynomials in evaluation form.
49        // (in evaluation form, the domain needs to grow as the degree of a
50        // polynomial grows)
51        let d2 = Radix2EvaluationDomain::<F>::new(2 * n).ok_or(
52            DomainCreationError::DomainConstructionFailed("d2".to_string(), 2 * n),
53        )?;
54        let d4 = Radix2EvaluationDomain::<F>::new(4 * n).ok_or(
55            DomainCreationError::DomainConstructionFailed("d4".to_string(), 4 * n),
56        )?;
57        let d8 = Radix2EvaluationDomain::<F>::new(8 * n).ok_or(
58            DomainCreationError::DomainConstructionFailed("d8".to_string(), 8 * n),
59        )?;
60
61        // ensure the relationship between the three domains in case the
62        // library's behavior changes
63        assert_eq!(d2.group_gen.square(), d1.group_gen);
64        assert_eq!(d4.group_gen.square(), d2.group_gen);
65        assert_eq!(d8.group_gen.square(), d4.group_gen);
66
67        Ok(EvaluationDomains { d1, d2, d4, d8 })
68    }
69}