Skip to main content

p3_commit/
periodic.rs

1//! Periodic column evaluation support.
2//!
3//! Periodic columns are columns whose values repeat with a period that divides the trace length.
4//! This module provides the `PeriodicEvaluator` trait for evaluating periodic polynomials
5//! in a domain-agnostic way (supporting both two-adic and circle STARKs).
6//!
7//! ## Power-of-Two Requirement
8//!
9//! **All period lengths must be powers of two.** This is because:
10//! - The trace domain is a multiplicative/additive group of order `n` (a power of 2)
11//! - The periodic subdomain must be a subgroup of order `p`
12//! - For `p` to divide `n` as group orders, `p` must also be a power of 2
13//!
14//! ## Mathematical Background
15//!
16//! A periodic column with period `p` and trace length `n` repeats every `p` rows:
17//! `col[i] = col[i + p]` for all `i`.
18//!
19//! **The problem**: We have a polynomial `P` of degree `n-1` over the trace domain `H`,
20//! but it only takes `p` distinct values. Can we work with a smaller polynomial instead?
21//!
22//! **Key observation**: We want `P(ω^i) = P(ω^{i+p})` for all `i`. So we need a map
23//! `π: H → ?` that identifies points `p` apart: `π(ω^i) = π(ω^{i+p})`, i.e., `π` must
24//! be constant on cosets of the subgroup `⟨ω^p⟩` of order `n/p`.
25//!
26//! **Finding π**: For cyclic groups, raising to the power `k` gives a homomorphism with
27//! kernel of size `k`. Since we need `ker(π) = ⟨ω^p⟩` of order `n/p`, we set `π(x) = x^(n/p)`.
28//! Indeed, `π(ω^{i+p}) = ω^{(i+p)·n/p} = ω^{i·n/p} · ω^n = π(ω^i)` since `ω^n = 1`.
29//!
30//! **Where π lands**: The image of `π` is `H_p = {1, ω^(n/p), ω^(2n/p), ...}`, a subgroup
31//! of order `p`. Now we can factor `P = Q ∘ π` where `Q: H_p → F` is a degree `p-1`
32//! polynomial interpolating the `p` periodic values.
33//!
34//! **Group-theoretic view**: `π: H → H_p` is a surjective homomorphism with kernel of
35//! order `n/p`. By the first isomorphism theorem, `H/ker(π) ≅ H_p`. The periodic column
36//! is constant on cosets of `ker(π)`, so it factors through `π`.
37//!
38//! **For Circle STARKs**: The same idea applies with `π(P) = (n/p)·P` (repeated doubling)
39//! instead of exponentiation.
40//!
41//! **Evaluating at an out-of-domain point `ζ`**:
42//! 1. Compute `π(ζ)` to get a point in `H_p`
43//! 2. Evaluate `Q(π(ζ))` using Lagrange interpolation over `H_p`
44//!
45//! ## Memory-Efficient Storage
46//!
47//! Instead of materializing the full LDE-sized table (which would be wasteful for small periods),
48//! we store only `max_period × blowup` rows in a [`PeriodicLdeTable`]. All periodic columns are
49//! padded to the maximum period, creating a rectangular matrix that can be efficiently accessed
50//! with modular indexing in the constraint evaluation hot loop.
51
52use alloc::vec::Vec;
53
54use p3_field::{ExtensionField, Field};
55use p3_matrix::dense::RowMajorMatrix;
56
57use crate::PolynomialSpace;
58
59/// Compact storage for periodic column values on the LDE domain.
60///
61/// Instead of materializing the full LDE-sized table, stores only `extended_height` rows
62/// (where `extended_height = max_period × blowup`) and uses modular indexing to access values.
63///
64/// All periodic columns are padded to the maximum period before extrapolation, creating a
65/// rectangular matrix for cache-friendly row-wise access.
66///
67/// # Invariants
68///
69/// - All periods must be powers of 2 (see module-level documentation)
70/// - Height is always `max_period × blowup` (both powers of 2, so height is power of 2)
71#[derive(Clone, Debug)]
72pub struct PeriodicLdeTable<F> {
73    /// Values in row-major form: height = extended_height, width = num_columns.
74    /// Empty if there are no periodic columns.
75    values: RowMajorMatrix<F>,
76}
77
78impl<F: Clone + Send + Sync> PeriodicLdeTable<F> {
79    /// Create a new periodic LDE table from extrapolated values.
80    ///
81    /// The matrix should have height = `max_period × blowup` and width = `num_periodic_columns`.
82    pub const fn new(values: RowMajorMatrix<F>) -> Self {
83        Self { values }
84    }
85
86    /// Create an empty table (for AIRs without periodic columns).
87    pub fn empty() -> Self {
88        Self {
89            values: RowMajorMatrix::new(Vec::new(), 0),
90        }
91    }
92
93    /// Returns true if there are no periodic columns.
94    pub const fn is_empty(&self) -> bool {
95        self.values.values.is_empty()
96    }
97
98    /// Number of periodic columns.
99    pub const fn width(&self) -> usize {
100        self.values.width
101    }
102
103    /// Height of the compact table (max_period × blowup).
104    pub const fn height(&self) -> usize {
105        match self.values.values.len().checked_div(self.values.width) {
106            Some(h) => h,
107            None => 0,
108        }
109    }
110
111    /// Get all periodic column values for a given LDE index using modular indexing.
112    ///
113    /// Returns a slice of length `width()` containing the value of each periodic column.
114    #[inline]
115    pub fn get_row(&self, lde_idx: usize) -> &[F] {
116        let height = self.height();
117        debug_assert!(height > 0, "cannot index into empty periodic table");
118        let row_idx = lde_idx % height;
119        let start = row_idx * self.values.width;
120        let end = start + self.values.width;
121        &self.values.values[start..end]
122    }
123
124    /// Get a specific periodic column value for a given LDE index.
125    #[inline]
126    pub fn get(&self, lde_idx: usize, col_idx: usize) -> &F {
127        let height = self.height();
128        debug_assert!(height > 0, "cannot index into empty periodic table");
129        let row_idx = lde_idx % height;
130        &self.values.values[row_idx * self.values.width + col_idx]
131    }
132}
133
134/// Evaluates periodic polynomials for a given domain system.
135///
136/// Periodic columns are defined by their values over one period. This trait
137/// handles interpolation and evaluation, abstracting over the domain-specific
138/// math (two-adic multiplicative groups vs circle groups).
139///
140/// # Power-of-Two Requirement
141///
142/// **All period lengths must be powers of two.** This ensures the periodic subdomain
143/// is a valid subgroup of the trace domain. See module-level documentation for details.
144///
145/// # Type Parameters
146/// - `F`: The base field type
147/// - `D`: The polynomial space / domain type
148pub trait PeriodicEvaluator<F: Field, D: PolynomialSpace<Val = F>> {
149    /// Evaluate all periodic columns on the LDE domain, returning a compact table.
150    ///
151    /// This is used by the prover to compute periodic column values on the
152    /// low-degree extension domain for constraint evaluation.
153    ///
154    /// The returned table stores only `max_period × blowup` rows. All columns are
155    /// padded to the maximum period before extrapolation, creating a rectangular
156    /// matrix for efficient row-wise access with modular indexing.
157    ///
158    /// # Arguments
159    /// * `periodic_table` - Slice of periodic columns, each containing one period of values.
160    ///   The length of each inner `Vec` is the period of that column (must be a power of 2).
161    /// * `trace_domain` - The original trace domain
162    /// * `lde_domain` - The low-degree extension domain
163    ///
164    /// # Returns
165    /// A [`PeriodicLdeTable`] with height = `max_period × blowup` and width = number of columns.
166    fn eval_on_lde(
167        periodic_table: &[Vec<F>],
168        trace_domain: &D,
169        lde_domain: &D,
170    ) -> PeriodicLdeTable<F>;
171
172    /// Evaluate all periodic columns at a single point (for verification).
173    ///
174    /// This is used by the verifier to compute periodic column values at
175    /// query points during constraint verification.
176    ///
177    /// # Arguments
178    /// * `periodic_table` - Slice of periodic columns. Each column's length (period)
179    ///   must be a power of 2.
180    /// * `trace_domain` - The original trace domain
181    /// * `point` - The query point (in extension field)
182    ///
183    /// # Returns
184    /// `Vec<EF>` containing the evaluation of each periodic column at `point`
185    fn eval_at_point<EF: ExtensionField<F>>(
186        periodic_table: &[Vec<F>],
187        trace_domain: &D,
188        point: EF,
189    ) -> Vec<EF>;
190}
191
192/// Unit type implements `PeriodicEvaluator` as a no-op.
193///
194/// This is used internally by `prove` and `verify` for AIRs without periodic columns.
195/// Panics if any periodic columns are present.
196impl<F: Field, D: PolynomialSpace<Val = F>> PeriodicEvaluator<F, D> for () {
197    fn eval_on_lde(
198        periodic_table: &[Vec<F>],
199        _trace_domain: &D,
200        _lde_domain: &D,
201    ) -> PeriodicLdeTable<F> {
202        assert!(
203            periodic_table.is_empty(),
204            "AIR has periodic columns but no PeriodicEvaluator was specified. \
205             Use prove_with_periodic or verify_with_periodic with TwoAdicPeriodicEvaluator \
206             or CirclePeriodicEvaluator."
207        );
208        PeriodicLdeTable::empty()
209    }
210
211    fn eval_at_point<EF: ExtensionField<F>>(
212        periodic_table: &[Vec<F>],
213        _trace_domain: &D,
214        _point: EF,
215    ) -> Vec<EF> {
216        assert!(
217            periodic_table.is_empty(),
218            "AIR has periodic columns but no PeriodicEvaluator was specified. \
219             Use prove_with_periodic or verify_with_periodic with TwoAdicPeriodicEvaluator \
220             or CirclePeriodicEvaluator."
221        );
222        Vec::new()
223    }
224}