bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 54쪽
functions:
```rust theme={null}
/// Evaluates the polynomial at all points in the domain.
pub fn evaluate(
&self,
domain: CircleDomain,
) -> CircleEvaluation<B, BaseField, BitReversedOrder> {
B::evaluate(self, domain, &B::precompute_twiddles(domain.half_coset))
}
/// Evaluates the polynomial at all points in the domain, using precomputed twiddles.
pub fn evaluate_with_twiddles(
&self,
domain: CircleDomain,
twiddles: &TwiddleTree<B>,
) -> CircleEvaluation<B, BaseField, BitReversedOrder> {
B::evaluate(self, domain, twiddles)
}
```
In the next section, we will see how the evaluations and polynomials are represented over the `SecureField`.
# Introduction
Source: https://docs.starknet.io/learn/S-two-book/how-it-works/circle-polynomials/index
> In STARK protocols, the computation trace is organized into columns and represented as polynomials. This section explains how S-two represents and manipulates polynomials over the circle, covering both point-value (evaluations) and coefficient representations.
This section is organized as follows:
* **[Columns](/learn/S-two-book/how-it-works/circle-polynomials/columns)**: How computation trace data is stored in columns, including both base field and secure field representations.
* **[Circle Evaluations and Polynomials](/learn/S-two-book/how-it-works/circle-polynomials/evals-and-poly)**: Point-value and coefficient representations of polynomials, their mathematical foundations, and conversion between forms.
* **[Secure Evaluations and Polynomials](/learn/S-two-book/how-it-works/circle-polynomials/secure-evals-and-poly)**: Extension to secure field polynomials using the quartic extension QM31, with implementation details.
# Secure Evaluations and Polynomials
Source: https://docs.starknet.io/learn/S-two-book/how-it-works/circle-polynomials/secure-evals-and-poly
# Secure Evaluations
Similar to `CircleEvaluation`, `SecureEvaluation` is a [point-value representation](/learn/S-two-book/how-it-works/circle-polynomials/evals-and-poly#point-value-representation) of a polynomial whose evaluations over the `CircleDomain` are from the `SecureField` (an alias for `QM31`). This is implemented as follows:
```rust theme={null}
pub struct SecureEvaluation<B: ColumnOps<BaseField>, EvalOrder> {
pub domain: CircleDomain,
pub values: SecureColumnByCoords<B>,
_eval_order: PhantomData<EvalOrder>,
}
```
As discussed in the [previous subsection](/learn/S-two-book/how-it-works/circle-polynomials/columns#secure-field-columns), each `SecureField` element is represented by four base field elements and stored in four consecutive columns. Thus the evaluations are represented as `SecureColumnByCoords`, as shown above.
Similar to `CircleEvaluation`, we can interpolate a `SecureCirclePoly` with coefficients from the `SecureField` as shown below:
```rust theme={null}
impl<B: PolyOps> SecureEvaluation<B, BitReversedOrder> {
/// Computes a minimal [`SecureCirclePoly`] that evaluates to the same values as this
/// evaluation, using precomputed twiddles.
pub fn interpolate_with_twiddles(self, twiddles: &TwiddleTree<B>) -> SecureCirclePoly<B> {
let domain = self.domain;
let cols = self.values.columns;
SecureCirclePoly(cols.map(|c| {
CircleEvaluation::<B, BaseField, BitReversedOrder>::new(domain, c)
.interpolate_with_twiddles(twiddles)
}))
}
}
```
# Secure Circle Polynomials
Similar to `CirclePoly`, `SecureCirclePoly` is a [coefficient representation](/learn/S-two-book/how-it-works/circle-polynomials/evals-and-poly#coefficient-representation) of a polynomial whose coefficients are from the `SecureField`. As discussed in the [earlier section](/learn/S-two-book/how-it-works/circle-polynomials/evals-and-poly#eq-circle-poly), we can define a circle polynomial as follows:
$p(x, y) = \sum_{j=0}^{2^n -1} v_j \cdot y^{j_0} \cdot x^{j_1} \cdot \pi(x)^{j_2} \cdot \pi^2(x)^{j_3} \cdots \pi^{n-2}(x)^{j_{n-1}}$
Here, $v_j$ are the coefficients from `SecureField` (i.e. $\mathsf{QM31}$). We can represent the coefficient $v_j$ using four elements from the `BaseField` (i.e. $\mathsf{M31}$) as follows:
$v_j = (a_j + i \cdot b_j) + (c_j + i \cdot d_j) \cdot u$
where $a_j, b_j, c_j, d_j \in \mathsf{M31}$. Substituting the above representation in the equation of $p(x, y)$ we get:
$p(x, y) = \sum_{j=0}^{2^n -1} \Big(a_j + i \cdot b_j + u \cdot c_j + i \cdot u \cdot d_j \Big) \cdot y^{j_0} \cdot x^{j_1} \cdot \pi(x)^{j_2} \cdot \pi^2(x)^{j_3} \cdots \pi^{n-2}(x)^{j_{n-1}}$
$p(x, y) = \sum_{j=0}^{2^n -1} a_j \cdot y^{j_0} \cdot x^{j_1} \cdot \pi(x)^{j_2} \cdots \pi^{n-2}(x)^{j_{n-1}} \\ + \\ i \cdot \sum_{j=0}^{2^n -1} b_j \cdot y^{j_0} \cdot x^{j_1} \cdot \pi(x)^{j_2} \cdots \pi^{n-2}(x)^{j_{n-1}} \\ + \\$
$\\ \\ u \cdot \sum_{j=0}^{2^n -1} c_j \cdot y^{j_0} \cdot x^{j_1} \cdot \pi(x)^{j_2} \cdots \pi^{n-2}(x)^{j_{n-1}} \\ + \\ iu \cdot \sum_{j=0}^{2^n -1} d_j \cdot y^{j_0} \cdot x^{j_1} \cdot \pi(x)^{j_2} \cdots \pi^{n-2}(x)^{j_{n-1}}$
Thus we can represent a `SecureCirclePoly` using four `CirclePoly`: $p_a(x, y), p_b(x, y), p_c(x, y)$ and $p_d(x, y)$ as follows:
$p(x, y) = p_a(x, y) + i \cdot p_b(x, y) + u \cdot p_c(x, y) + iu \cdot p_d(x, y)$
where $p_a(x,y)$ is a `CirclePoly` with coefficients $a_j \in \mathsf{M31}$, similarly for $p_b(x, y), p_c(x, y)$ and $p_d(x, y)$. This is implemented as follows:
```rust theme={null}
pub struct SecureCirclePoly<B: ColumnOps<BaseField>>(pub [CirclePoly<B>; SECURE_EXTENSION_DEGREE]);
```
Here, `SECURE_EXTENSION_DEGREE` is the degree of extension of the `SecureField`, which is 4.
Similar to `CirclePoly`, we can evaluate the `SecureCirclePoly` at points on the given `CircleDomain` which outputs the `SecureEvaluation`.
```rust theme={null}
pub fn evaluate_with_twiddles(
&self,
domain: CircleDomain,
twiddles: &TwiddleTree<B>,
) -> SecureEvaluation<B, BitReversedOrder> {
let polys = self.0.each_ref();
let columns = polys.map(|poly| poly.evaluate_with_twiddles(domain, twiddles).values);
SecureEvaluation::new(domain, SecureColumnByCoords { columns })
}
```
In the next section, we will see how the `interpolate` and `evaluate` functions convert between the two polynomial representations using Circle FFT. As you may have noticed, the twiddles are precomputed for efficiency, and we will explore this in the next section on Circle FFT.
# Introduction
Source: https://docs.starknet.io/learn/S-two-book/how-it-works/index
> This section provides an overview of the core components and protocols that make up S-two. It is intended for readers who want to understand the technical details and inner workings of the system implementation. Throughout this section, we describe the implementation as available in this [commit](https://github.com/starkware-libs/stwo/tree/0790eba46b8af5697083d84fb75bd34b08a0b31f).
The following topics are covered:
* [**Mersenne Primes**](/learn/S-two-book/how-it-works/mersenne-prime): Introduction to the Mersenne31 prime field used in S-two for efficient arithmetic.
* [**Circle Group**](/learn/S-two-book/how-it-works/circle-group): Explains the algebraic structure underlying FFT and polynomial operations.
* [**Circle Polynomials**](/learn/S-two-book/how-it-works/circle-polynomials/index): Details the representation and evaluation of polynomials in the circle group.
* [**Circle FFT**](/learn/S-two-book/how-it-works/circle-fft/index): Describes the fast Fourier transform algorithm adapted for the circle group.
* [**Vector Commitment Scheme**](/learn/S-two-book/how-it-works/vcs/index): Covers the use of Merkle trees for committing to vectors and enabling efficient proofs of inclusion.
* [**AIR to Composition Polynomial**](/learn/S-two-book/how-it-works/air/index): Shows how algebraic constraints are encoded as polynomials for proof generation.
* [**Circle FRI**](/learn/S-two-book/how-it-works/circle-fri/index): Explains the FRI protocol for low-degree testing of polynomials over the circle group.
* [**Polynomial Commitment Scheme**](/learn/S-two-book/how-it-works/pcs/index): Describes the protocol for committing to and opening polynomials with soundness guarantees.
* [**Proof Generation and Verification**](/learn/S-two-book/how-it-works/stark_proof/index): Walks through the process of generating and verifying a STARK proof in S-two.
* [**Lookups**](/learn/S-two-book/how-it-works/lookups): Discusses lookup arguments and their implementation in S-two for efficient constraint checking.
# Lookups
Source: https://docs.starknet.io/learn/S-two-book/how-it-works/lookups
Lookups are simply a way to connect one part of the table to another. When we "look up" a value, we are doing nothing more than creating a constraint that allows us to use that value in another part of the table without breaking soundness.
## Design
We will walk through four steps to incrementally build up the design of lookups.
### Step 1: Suppose we want to have two columns with the same values.
We can do this by creating two columns with the exact same values and adding a constraint over them: `col_1 - col_2 = 0`.
### Step 2: We want to check that the two columns have the same values but in a different order.
We can use the idea that two sets of values will have the same cumulative product if they are indeed permutations of each other. So we add new columns, `col_1_cumprod` for `col_1` and `col_2_cumprod` for `col_2`, which contain the running cumulative product of `col_1` and `col_2`, respectively. The new constraints will check that each of these new columns do indeed contain the cumulative product values and that their last values are the same. We can optimize this by creating just one new column that keeps a running cumulative product of the fraction `col_1 / col_2`.
### Step 3: We want to check that all values in `col_2` are in `col_1`, but each value appears an arbitrary number of times.
*(Note that this is a generalization of the second step in that for the second step, all values in `col_2` appear exactly once in `col_1`)*
Supporting this third step is actually pretty simple: when creating the running cumulative product, we need to raise each value in `col_1` to its multiplicity, or the number of times it appears in `col_2`. The rest of the constraints do not need to be changed.
### Step 4: We want to check that all values in `[col_2, col_3, ...]` are in `col_1` with arbitrary multiplicities
Finally, we want to create many more columns that contain values from `col_1`. Fortunately,
To support this, we can use the same idea as the third step: when creating the running cumulative product, we need to raise each value in `col_1` to the power of the number of times it appears in `[col_2, col_3, ...]`.
<Info>
**In summary, lookups support the following use-cases:**
1. Prove equality: we want to prove that the values of the first column are equal to the values of the second column.
2. Prove permutation: we want to prove that the values of the first column are a permutation of the values of the second column.
3. Prove permutation with multiplicities: we want to prove that each value of the first column appears a certain number of times over multiple columns.
</Info>
## Technique: LogUp
LogUp is a technique used to constrain lookups. It's a successor to [Plookup](https://eprint.iacr.org/2020/315), and is especially useful for proving permutation with multiplicities. Here, we'll briefly explain why this is the case.
Plookup and its variants use a technique called the Grand Product Check to prove permutation.
$\prod_{i=0}^{n-1} (X - a_i) = \prod_{i=0}^{n-1} (X - b_i)$
In the equation above, we can check that the set $\{a_0,...,a_{n-1}\}$ is a permutation of the set $\{b_0,...,b_{n-1}\}$ by setting $X$ to a random value provided by the verifier.
However, this becomes inefficient when we have multiplicities since we need to encode the multiplicities as powers of each lookup polynomial, and thus the degree of the polynomial increases linearly with the number of multiplicities.
$\prod_{i=0}^{n-1} (X - a_i) = \prod_{i=0}^{n-1} (X - b_i)^{m_i}$
On the other hand, LogUp uses the derivative of the Grand Product Check:
$\sum_{i=0}^{n-1} \frac{1}{X - a_i} = \sum_{i=0}^{n-1} \frac{m_i}{X - b_i}$
In this approach, each lookup polynomial is represented as a rational function with the multiplicity as the numerator. This transformation is significant because the degree of the polynomial remains constant regardless of the number of multiplicities, making LogUp more efficient for handling multiple lookups of the same value.
## Implementation
Let's walk through how LogUp is implemented in S-two using a simple example where we look up values from a preprocessed trace.
First, we create columns in the original trace, where all values are from the preprocessed trace $A$.
<div>
<img alt="" />
*Figure 1: Create original trace columns that look up values from a preprocessed trace*
</div>
Then, we add a multiplicity column to the original trace indicating the number of times each value in $A$ appears in the original trace.
<div>
<img alt="" />
*Figure 2: Add a multiplicity column*
</div>
Next, we create LogUp columns as part of the interaction trace, one for the preprocessed trace and the multiplicity column, and another for the batch of all lookups.
<div>
<img alt="" />
*Figure 3: Create LogUp columns*
</div>
To create a constraint over the LogUp columns, S-two modifies the LogUp columns to contain the cumulative sum of the fractions in each row. This results in columns that look like the following:
<div>
<img alt="" />
*Figure 4: Cumulative sum columns*
</div>
A constraint is created using the values of two rows of the cumulative sum LogUp column. For example, as in **Figure 5**, we can create a constraint by subtracting $row_1$ from $row_2$ and checking that it equals the LogUp fraction created using values $a_2$ and $m_2$:
$$
\frac{m_2}{X - a_2} = row\_2 - row\_1
$$
<div>
<img alt="" />
*Figure 5: Constraint over two rows*
</div>
However, this constraint actually does not hold for the first row since $\frac{m_1}{X - a_1} \neq row_1 - row_n$. A typical solution to this problem would be to disable this constraint for the first row and create a separate constraint that is enabled only on the first row.
But S-two solves this problem differently. First, before we accumulate the original LogUp column rows, we subtract each row by the average of the total sum of the rows. Only then do we accumulate each row. This way, the last row of the column will always equal zero, so we do not need to make an exception for the first row. The final constraint for the second row looks as follows:
$$
\frac{m_2}{X - a_2} = row\_2 - row\_1 + \text{avg}
$$
Where $\text{avg}$ is a witness value provided by the prover.
<div>
<img alt="" />
*Figure 6: Trick to not create a separate constraint for the first row*
</div>
The right column in **Figure 6** is the final form of the LogUp column that S-two commits to as part of the interaction trace.
## Batching
In general, batching multiple lookups together helps reduce the size of the proof as it reduces the number of LogUp columns, which means we can commit to fewer columns. However, we cannot batch an arbitrary number of fractions together because it increases the degree of the constraint polynomial.
More specifically, the degree of the constraint polynomial increases linearly with the number of fractions in the batch. Let's say we want to batch $k$ fractions together. This will create a constraint $\frac{1}{X - a_1} + \frac{1}{X - a_2} + ... + \frac{1}{X - a_k} = \text{sum} - \text{prev\_sum}$. Once we multiply both sides by the common denominator, we get a constraint of degree $k+1$ from the product $(X - a_1)\cdot \dots \cdot (X - a_k) \cdot \text{sum}$.
To illustrate the accounting for how many fractions we can batch together, we first need to understand how the degree of the composition polynomial is calculated in S-two. Here, the composition polynomial is the constraint polynomial divided by the quotient polynomial, where the quotient polynomial is the vanishing polynomial of the trace domain (i.e. evaluates to 0 over the trace domain). So let's say we have a degree-$m$ constraint on two columns: $a^m + b^m = 0$. We can define the composition polynomial as $\frac{a^m + b^m}{x^N - 1}$ where $N$ is the trace height. The degree of $a$ and $b$ is $N-1$ so, if the constraint holds, the degree of the composition polynomial is $m \cdot (N-1) - N = (m-1) \cdot N - m$. Thus, given that we already has $N$ evaluations, we can expand it by $m-1$ to convince the verifier that the constraint holds since $(m-1) \cdot N > (m-1) \cdot N - m$.
Now, coming back to the question of how many fractions we can batch together, we can see that we can batch up to exactly $k$ fractions if the rate of expansion is $k$.
# Mersenne Primes
Source: https://docs.starknet.io/learn/S-two-book/how-it-works/mersenne-prime
Proof systems typically rely on finite field operations, where efficient field arithmetic is crucial for optimizing proof generation. In STARK protocols, there is no direct dependency between the security level of the proof system and the field size. This allows the use of small fields with highly efficient arithmetic.
S-two uses a particular family of primes called *Mersenne primes*. A Mersenne prime is defined as a prime number that is one less than a power of two, expressed as $P = 2^k -1$. S-two uses the Mersenne prime $\textsf{M31}$ of size $P = 2^{31} - 1$, which is implemented as follows:
```rust theme={null}
pub struct M31(pub u32);
```
## Why $\textsf{M31}$?
The key advantage is extremely cheap modular reduction after a 31-bit multiplication. Consider computing $a \cdot b$, where $a, b \in \textsf{M31}$. This operation involves a 31-bit integer multiplication, producing a 62-bit intermediate result, which is then reduced modulo $P$.
Suppose $x = a \cdot b$ then we can decompose $x$ into two 31-bit values $b$ and $s$, such that $x = 2^{31} \cdot b + s$, as shown in the following figure.
<div>
<img alt="Product decomposition" />
<span>Figure 1: Product decomposition</span>
</div>
To perform modular reduction, we start with:
$x \equiv (2^{31} \cdot b + s) \quad mod \quad (2^{31} - 1)$
Substituting $2^{31} \equiv 1 \mod (2^{31} - 1)$ gives:
$x \equiv (b + s) \quad mod \quad (2^{31} - 1)$
Since $b$ and $s$ are both 31-bit values, they can be directly represented as field elements. Consequently, modular reduction is performed with a single field addition. This makes arithmetic over Mersenne primes exceptionally fast, making them an ideal choice for our STARK protocol. The above field multiplication is implemented as:
```rust theme={null}
fn mul(self, rhs: Self) -> Self::Output {
Self::reduce((self.0 as u64) * (rhs.0 as u64))
}
```
where `reduce` is a function which performs efficient reduction of the resulting number modulo $P$.
## Why We Need Extensions ?
We cannot instantiate our STARK protocols using $\textsf{M31}$ since it is not FFT-friendly field, meaning it does not contain a multiplicative subgroup of order that is a large power of two (commonly referred to as a *smooth* subgroup). The multiplicative group of $\textsf{M31}$ has the following order:
$P-1 = 2^{31}-2$
As shown above, the multiplicative group of $\textsf{M31}$ lacks a smooth subgroup of size that is a large power of two because there is no large power of two that divides $P-1$. In other words, there does not exist a sufficiently large $n$ such that $2^n|P - 1$. To make $\textsf{M31}$ compatible with STARKs, we will work over extensions of it.
## Field Operations
S-two avoids code duplication by providing two Rust macros, `impl_field!` and `impl_extension_field!`, for implementing field and extension field operations.
For example, field operations for $\textsf{M31}$ are implemented using the Rust macro `impl_field!`, which takes as argument the field $\textsf{M31}$ and the size of the field $P = 2^{31} - 1$, as follows:프로젝트로 돌아가기