Skip to main content

kimchi/circuits/
domains.rs

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