Starknet 백서

Starknet

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 53쪽

unction $g_1$ into $g_{1,0}$ and $g_{1,1}$. Then fold all their evaluations as follows: $$ g_2(x) = g_{1,0}(x) + \lambda_2 \cdot g_{1, 1}(x) + \lambda_2^2 \cdot (h_{2,0}(x) + \lambda_2 \cdot h_{2,1}(x)) $$ This is the final round, so the prover will send $g_2(x) \in F^{I_2}$ to the verifier in plain. Let us describe the protocol for the general case. In each round $i = \{0, \dots, r\}$, the prover has previous evaluations $g_{i-1} \in F^{I_{i-1}}$ (for round $i = 0$ we take $g_{-1} = 0$). The verifier sends $\lambda_i \in F$ and the prover commits to $g_i \in F^{I_i}$, defined as follows: $$ g_i = g_{i-1,0} + \lambda_i \cdot g_{i-1,1} + \lambda_i^2 \cdot (h_{i,0} + \lambda_i \cdot h_{i,1}) $$ where $g_{i-1,0}, g_{i-1,1} \in F^{I_i}$ are the decomposition of $g_{i-1} \in F^{I_{i-1}}$ and $h_{i,0}, h_{i,1} \in F^{I_i}$ are the decompositions of $h_i \in F^{H_i}$. The prover gives the verifier oracle access to $g_i$, and in the final round $g_r$ is sent in plain. 2. *Query Phase*: In the query phase, the verifier checks that the prover computed the folding correctly using the oracles sent by the prover. It consists of $s \geq 1$ rounds. In each round, the verifier samples $P_0 = (x_0,y_0) \in H_0$, and queries the oracles for their values to spot-check the folding identities along the projection trace $P_i = (x_i,y_i) = \pi^i(x_0,y_0)$. That is, for all $i = \{0, 1, 2\}$ the verifier checks that: $$ \begin{aligned} g_i(x_i) &= \frac{g_{i-1}(x_{i-1}) + g_{i-1}(-x_{i-1})}{2} + \lambda_i \cdot \frac{g_{i-1}(x_{i-1}) - g_{i-1}(-x_{i-1})}{2 \cdot x_{i-1}} \\ &\quad + \lambda_i^2 \cdot \left( \frac{h_i(x_i,y_i) + h_i(x_i,-y_i)}{2} + \lambda_i \cdot \frac{h_i(x_i,y_i) - h_i(x_i,-y_i)}{2 \cdot y_i} \right) \end{aligned} $$ If in each of the query rounds all spot-checks hold, and if $g_2 \in F^{I_2}$ is "close" to the polynomial space $\mathcal{L}_0$, then the verifier accepts. Otherwise, it rejects. ## Security Analysis In this section, we analyze the security of the FRI protocol at a high level. There are roughly two ways a prover can cheat: * The functions $h_{i} \in F^{H_i}$ are "far" from the polynomial space $L'_{3-i}(F)$ but somehow the prover gets lucky and ends up with $g_2 \in \mathcal{L}_0$. The [proximity gaps paper](https://eprint.iacr.org/2020/654.pdf) analyzes that this happens with negligible probability. That is, if even one of the functions $h_{i} \in F^{H_i}$ is "far" from the polynomial space $L'_{3-i}(F)$, then the function $g_2$ will be "far" from $\mathcal{L}_0$ with high probability. * Now suppose $h_{i} \in F^{H_i}$ are "far" from the polynomial space $L'_{3-i}(F)$, then the function $g_2$ will be "far" from $\mathcal{L}_0$, but to cheat the verifier, the prover cheats in the folding rounds and sends some valid $g_2 \in \mathcal{L}_0$. Now the verifier must ensure that the prover has performed the folding correctly in each round using the oracles sent by the prover. This is exactly what the verifier checks in the query phase. From the "Toy Problem Conjecture" ([ethSTARK, Section 5.9](https://eprint.iacr.org/2021/582.pdf)), each query done by the verifier gives $\beta$ (i.e. log of blowup $B$) bits of security. Thus, for $s$ queries, the security is $s \cdot \beta$ bits. # Circle Group Source: https://docs.starknet.io/learn/S-two-book/how-it-works/circle-group As discussed in the previous section, Mersenne prime field $\textsf{M31}$ lacks a smooth subgroup whose order is a large power of two. This property makes such fields unsuitable for instantiating STARK protocols. To address this, we consider extensions of $\textsf{M31}$ that have smooth subgroups, which are suitable for performing FFTs and implementing the FRI protocol. For a field extension $F$ of $\textsf{M31}$, we define the *circle curve* $C(F)$ as the set of points $(x, y) \in F^2$ satisfying the relation: $x^2 + y^2 = 1$ In S-two implementation, a point on the circle is defined as follows: ```rust theme={null} pub struct CirclePoint<F> { pub x: F, pub y: F, } ``` The set $C(F)$ forms a cyclic group under the operation defined by: $(x,y) + (x', y') = (xx' - yy', xy' + x'y)$ Here, the group is defined additively, which differs from the multiplicative notation used in the <a href="https://eprint.iacr.org/2024/278">Circle STARKs paper</a>. In this documentation, we adopt the additive notation for consistency with the implementation. The above group operation is implemented as: ```rust theme={null} fn add(self, rhs: Self) -> Self::Output { let x = self.x.clone() * rhs.x.clone() - self.y.clone() * rhs.y.clone(); let y = self.x * rhs.y + self.y * rhs.x; Self { x, y } } ``` The identity element in this group is $(1, 0)$, implemented as: ```rust theme={null} pub fn zero() -> Self { Self { x: F::one(), y: F::zero(), } } ``` Negation in the circle group corresponds to the conjugation map $J$, defined by: $J(x, y) = (x, -y)$ This is same as complex conjugation in complex numbers. In S-two, the conjugate of a `CirclePoint` is computed as: ```rust theme={null} pub fn conjugate(&self) -> CirclePoint<F> { Self { x: self.x.clone(), y: -self.y.clone(), } } ``` The total number of points in the circle group $C(F)$ is given by $|F| + 1$. Specifically, for $C(\textsf{M31})$, the number of points is $P + 1$, which, as discussed earlier, is a large power of two and can thus be used in STARK protocol instantiations. This result is proven in *Lemma 1* of the <a href="https://eprint.iacr.org/2024/278">Circle STARKs paper</a>. In S-two implementation, the generator $g$ of the group $C(\textsf{M31})$ is defined as: $g = (2, 1268011823)$ Subgroups of $C(\textsf{M31})$ of size $2^n$ can be generated using the following function: ```rust theme={null} pub fn subgroup_gen(n: u32) -> CirclePoint<F> { assert!(n <= M31_CIRCLE_LOG_ORDER); // M31_CIRCLE_LOG_ORDER = 31 let s = 1 << (M31_CIRCLE_LOG_ORDER - n); M31_CIRCLE_GEN.mul(s) // M31_CIRCLE_GEN = g = (2, 1268011823) } ``` To generate a subgroup $\langle g_n \rangle$ of size $2^n$, the function computes $2^{31 - n} \cdot g$, i.e. it applies the group law to the generator $g$ with itself $2^{31 - n}$ times, as shown below: ```rust theme={null} pub fn mul(&self, mut scalar: u128) -> CirclePoint<F> { let mut res = Self::zero(); let mut cur = self.clone(); while scalar > 0 { if scalar & 1 == 1 { res = res + cur.clone(); } cur = cur.double(); scalar >>= 1; } res } ``` Hence, the point $2^{31-n} \cdot g$ serves as a generator of a subgroup $\langle g_n \rangle$ of order $2^n$. ## Circle Domain In a STARK protocol, the computation trace is interpolated as a low-degree polynomial over a domain using FFT. For Circle STARKs, this domain consists of points on the circle curve and is referred to as the *circle domain*. The circle domain $D_n$ of size $2^n$ is constructed as the union of two disjoint cosets: $D_n = q + \langle g_{n-1} \rangle \cup -q + \langle g_{n-1} \rangle$ Here, $\langle g_{n-1} \rangle$ is a subgroup of size $2^{n-1}$, and $q$ is the coset offset such that $q \neq -q$. This union is also called the *twin-coset*. The second coset in the union can be viewed as the negation (or conjugation) of the first: $J(q + \langle g_{n-1} \rangle) = -q + \langle g_{n-1} \rangle$ Therefore, it suffices to store only the half coset $q + \langle g_{n-1} \rangle$, and generate the full domain via its conjugates. The circle domain is defined in S-two as: ```rust theme={null} pub struct CircleDomain { pub half_coset: Coset, } ``` The following figure shows a circle domain of size 8. It is constructed from the half coset $q + \langle g_2 \rangle$ of size 4 (shown as red points) and its negation $-q + \langle g_2 \rangle$ (shown as blue points). <div> <img alt="" /> *Figure 1: Circle Domain of size 8* </div> To iterate over all points in the circle domain, we can iterate over the half coset and its conjugates: ```rust theme={null} pub fn iter(&self) -> CircleDomainIterator { self.half_coset .iter() .chain(self.half_coset.conjugate().iter()) } ``` ## Canonic Coset For a specific choice of offset $q$, the twin-coset $D_n$ becomes a coset of a larger subgroup. In particular, if $q$ is a generator of a subgroup of order $2^{n+1}$, then: $D_n = q + \langle g_n \rangle = q + \langle g_{n-1} \rangle \cup -q + \langle g_{n-1} \rangle$ This result is proven in *Proposition 1* of the <a href="https://eprint.iacr.org/2024/278">Circle STARKs paper</a>. Such domains are called *standard position coset*, or are referred to as *canonic cosets*. They are implemented as follows: ```rust theme={null} pub struct CanonicCoset { pub coset: Coset, } ``` Here, `CanonicCoset` represents the full coset $q + \langle g_n \rangle$, while `CircleDomain` is represented with its half coset $q + \langle g_{n-1} \rangle$. Thus to compute the `CircleDomain` from the `CanonicCoset`, first calculate the half coset $q + \langle g_{n-1} \rangle$, which will be used to initialize the `CircleDomain` as shown below: ```rust theme={null} pub fn circle_domain(&self) -> CircleDomain { CircleDomain::new(self.half_coset()) } ``` The following figure shows a canonic coset of size 8. It is constructed from the coset $\langle g_3 \rangle$ of size 8 followed by an offset by $q$, where $q$ is the generator of subgroup $\langle g_4 \rangle$. <div> <img alt="" /> *Figure 2: Canonic Coset of size 8* </div> We can verify whether a given `CircleDomain` is *canonic* by checking the step size of the half coset against the initial coset offset. In the `CircleDomain` implementation, only the *half coset* $q + \langle g_{n-1} \rangle$ is explicitly stored. If `CircleDomain` is canonic, $q$ must be a generator of the subgroup $\langle g_{n+1} \rangle$, which has order $2^{n+1}$ i.e. $q = 2^{31 - (n+1)} \cdot g$. Recall that the generator of the subgroup $\langle g_{n-1} \rangle$ is $2^{31 - (n-1)} \cdot g$. Thus, the step size between consecutive elements in the half coset is $2^{31 - (n-1)} \cdot g$, and the initial point is $q = 2^{31 - (n+1)} \cdot g$. Therefore, the ratio between the step size and the initial coset offset is: $\frac{2^{31 - (n-1)}}{2^{31 - (n+1)}} = 2^2 = 4$ This means that in a canonic coset, the step size is exactly four times the initial coset offset. This condition is used to check whether a `CircleDomain` is canonic, as shown below: ```rust theme={null} pub fn is_canonic(&self) -> bool { self.half_coset.initial_index * 4 == self.half_coset.step_size } ``` In the next section, we will dive into polynomials defined over the circle. # Columns Source: https://docs.starknet.io/learn/S-two-book/how-it-works/circle-polynomials/columns In S-two, the computation trace is represented using multiple columns, each containing elements from the Mersenne prime field $\mathsf{M31}$. The columns are defined via the `Column<T>` trait, where `T` is typically `BaseField` (an alias for `M31`). ```rust theme={null} pub trait Column<T>: Clone + Debug + FromIterator<T> { /// Creates a new column of zeros with the given length. fn zeros(len: usize) -> Self; /// Creates a new column of uninitialized values with the given length. /// # Safety /// The caller must ensure that the column is populated before being used. unsafe fn uninitialized(len: usize) -> Self; /// Returns a cpu vector of the column. fn to_cpu(&self) -> Vec<T>; /// Returns the length of the column. fn len(&self) -> usize; /// Returns true if the column is empty. fn is_empty(&self) -> bool { self.len() == 0 } /// Retrieves the element at the given index. fn at(&self, index: usize) -> T; /// Sets the element at the given index. fn set(&mut self, index: usize, value: T); } ``` The operations over a column such as bit reversal of elements is provided using the `ColumnOps<T>` trait, which also implements the type alias `Col<B, T>` to conveniently represent a column. ```rust theme={null} pub trait ColumnOps<T> { type Column: Column<T>; fn bit_reverse_column(column: &mut Self::Column); } pub type Col<B, T> = <B as ColumnOps<T>>::Column; ``` <Callout type="info"> S-two defines a `Backend` trait, with two main implementations: `CpuBackend` and `SimdBackend`. The `SimdBackend` offers optimized routines for hardware supporting SIMD instructions, while `CpuBackend` provides a straightforward reference implementation. Each backend implements the `ColumnOps` trait. Here and in the following sections, we will describe the trait implementations for the `CpuBackend`. </Callout> The `ColumnOps<T>` trait is implemented for the `CpuBackend` as follows: ```rust theme={null} impl<T: Debug + Clone + Default> ColumnOps<T> for CpuBackend { type Column = Vec<T>; fn bit_reverse_column(column: &mut Self::Column) { bit_reverse(column) } } ``` Here, `bit_reverse` performs a naive bit-reversal permutation on the `column`. ## Secure Field Columns An element of the secure field (`SecureField` = `QM31`) cannot be stored in a single `BaseField` column because it is a quartic extension of `M31`. Instead, each secure field element is represented by four base field coordinates and stored in four consecutive columns. ```rust theme={null} pub struct SecureColumnByCoords<B: ColumnOps<BaseField>> { pub columns: [Col<B, BaseField>; SECURE_EXTENSION_DEGREE], } ``` Here, `SECURE_EXTENSION_DEGREE` is the extension degree of `QM31` i.e. 4. You can think of each row of the 4 columns containing a single element of the `SecureField`. Thus accessing an element by index reconstructs it from its base field coordinates, implemented as follows: ```rust theme={null} pub fn at(&self, index: usize) -> SecureField { SecureField::from_m31_array(std::array::from_fn(|i| self.columns[i].at(index))) } ``` Now that we know how columns are represented, we can explore their use in storing evaluations over the circle domain and in interpolating polynomials. # Circle Evaluations and Polynomials Source: https://docs.starknet.io/learn/S-two-book/how-it-works/circle-polynomials/evals-and-poly A polynomial can be represented in two main ways: 1. **Point-value representation**: as evaluations over a domain 2. **Coefficient representation**: as coefficients with respect to some basis The conversion from point-value representation to coefficient representation is called *interpolation* and the conversion from coefficient representation to point-value representation is called *evaluation*. Both these conversions are based on fast Fourier transform (FFT), which will be covered in the upcoming sections. In this subsection, we will look at the implementations of both these representations in S-two and the respective functions defined on them to interpolate and evaluate polynomials. ## Point-value representation In S-two, the evaluations of a polynomial over a `CircleDomain` are stored using the struct `CircleEvaluation`, implemented as follows: ```rust theme={null} pub struct CircleEvaluation<B: ColumnOps<F>, F: ExtensionOf<BaseField>, EvalOrder = NaturalOrder> { pub domain: CircleDomain, pub values: Col<B, F>, _eval_order: PhantomData<EvalOrder>, } ``` Here, the domain (i.e. `CircleDomain`) and the evaluations (i.e. `values`) have the same ordering (i.e. `EvalOrder`) which can either be `NaturalOrder` or `BitReversedOrder`. Now given the evaluations over a domain as `CircleEvaluation` we can interpolate a circle polynomial using the following functions: ```rust theme={null} impl<B: PolyOps> CircleEvaluation<B, BaseField, BitReversedOrder> { /// Computes a minimal [CirclePoly] that evaluates to the same values as this evaluation. pub fn interpolate(self) -> CirclePoly<B> { let coset = self.domain.half_coset; B::interpolate(self, &B::precompute_twiddles(coset)) } /// Computes a minimal [CirclePoly] that evaluates to the same values as this evaluation, using /// precomputed twiddles. pub fn interpolate_with_twiddles(self, twiddles: &TwiddleTree<B>) -> CirclePoly<B> { B::interpolate(self, twiddles) } } ``` Here, `PolyOps` is a trait that defines operations on `BaseField` polynomials, such as interpolation and evaluation. Now before looking into the representation of the circle polynomial (i.e. `CirclePoly`), let us first look into some theory on polynomials over the circle. ## Polynomials over the circle Let $F$ be an extension of the Mersenne prime field $\mathsf{M31}$. Then $F[x, y]$ represents the ring of bivariate polynomials with coefficients in $F$. The *circle polynomials* are bivariate polynomials over $F$ quotient by the ideal $\left(x^2 + y^2 - 1\right)$. The space of these bivariate polynomials over the circle curve of total degree $\leq N/2$ is denoted by $L_N(F)$. $L_N(F) = F\left[x, y\right] / \left(x^2 + y^2 - 1\right)$ For any polynomial $p(x, y) \in L_N(F)$, we can substitute all the higher degree terms of $y$ with the equation of circle $y^2 = 1 - x^2$ and reduce $p(x, y)$ to the form: $p(x, y) = p_0(x) + y \cdot p_1(x),$ The above form is called the *canonical representation* of any polynomial $p(x, y) \in L_N(F)$. Since $p(x, y)$ is of total degree $\leq N/2$, $p_0$ has degree $\leq N/2$ and $p_1$ has degree $\leq N/2 - 1$. Thus an alternate representation of the space $L_N(F)$ is as follows: $L_N(F) = F[x]^{ \leq N/2} + y \cdot F[x]^{ \leq N/2 - 1}$ From the above representation we can see that the space $L_N(F)$ is spanned by the following monomial basis: $1, x, \ldots, x^{N/2}, y, y \cdot x, \ldots, y \cdot x^{N/2 - 1}$ The dimension of $L_N(F)$ is the number of monomial basis elements, which is $N + 1$. As a developer, you can usually ignore most of these details, since concepts like polynomial space and basis do not explicitly appear in the S-two implementation. However, they do arise implicitly in the Circle FFT algorithm, as we will see in the next section. Now let us describe the coefficient representation and how the circle polynomials are implemented in S-two. ## Coefficient representation Given the evaluations over a domain (i.e. `CircleEvaluation`) we can compute the coefficients of a polynomial (i.e. `CirclePoly`) with respect to some basis using the `interpolate` function. The coefficients are stored as follows: ```rust theme={null} pub struct CirclePoly<B: ColumnOps<BaseField>> { /// Coefficients of the polynomial in the FFT basis. /// Note: These are not the coefficients of the polynomial in the standard /// monomial basis. The FFT basis is a tensor product of the twiddles: /// y, x, pi(x), pi^2(x), ..., pi^{log_size-2}(x). /// pi(x) := 2x^2 - 1. pub coeffs: Col<B, BaseField>, /// The number of coefficients stored as `log2(len(coeffs))`. log_size: u32, } ``` The `interpolate` function computes the coefficients with respect to the following basis: $b^{(n)}_j(x, y) := 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}}$ where * $\pi$ is the squaring map on the $x$-coordinate i.e. $\pi(x) = 2x^2 - 1$ and $\pi^i$ is applying the squaring map $i$ times, for example $\pi^2(x) = \pi(\pi(x))$ * $n$ is log of the size of the `CircleDomain` * $0 \leq j \leq 2^n - 1$ and $(j_0, \ldots, j_{n-1}) \in \{0, 1\}^n$ is the binary representation of $j$, i.e., $j = j_0 + j_1 \cdot 2 + \cdots + j_{n-1} \cdot 2^{n-1}$ Thus the `interpolate` function computes coefficients $c_j$ for polynomials $p(x, y)$ of the form: <span /> $p(x, y) = \sum_{j=0}^{2^n-1} c_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}}$ This polynomial form and its underlying basis are implicit in the Circle FFT construction, as we will see in the next section. However, as discussed earlier, you can largely ignore these details and simply view a polynomial as a set of evaluations over a domain, with its coefficients computed via the FFT algorithm. Now given the coefficient representation as `CirclePoly`, we can evaluate the polynomial over a given domain (i.e. compute the `CircleEvaluation`) using the following
프로젝트로 돌아가기