Starknet 백서

Starknet

원문

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

```rust theme={null} impl_field!(M31, P); ``` Since we work over extensions of $\textsf{M31}$, it has the type alias `BaseField`, as follows: ```rust theme={null} pub type BaseField = M31; ``` # Extensions of Mersenne Prime Field This section describes two extensions of $\textsf{M31}$: *complex extension* and *quartic extension*. ## Complex Extension We construct the degree-2 extension of $\textsf{M31}$ denoted by $\textsf{CM31}$ using the polynomial $X^2 + 1$ which is irreducible over $\textsf{M31}$. $\textsf{CM31} = \textsf{M31}[X] / (X^2 + 1)$ This extension forms a field of size $P^2$, where elements can be represented as $(a, b)$ or $a + i \cdot b$ where $a, b \in \textsf{M31}$ and $i$ is the root of the polynomial $X^2 + 1$ i.e. $i^2 + 1 = 0$. This is implemented as follows: ```rust theme={null} pub struct CM31(pub M31, pub M31); ``` The order of the multiplicative group of $\textsf{CM31}$ is calculated as follows: $P^2 - 1 = (P-1) \cdot (P+1) = (2^{31}-2) \cdot (2^{31})$ As shown above, $2^{31} | P^2 - 1$ i.e. the multiplicative group of $\textsf{CM31}$ contains a subgroup of size that is a large power of two. This makes it suitable for instantiating STARKs. This subgroup is what we refer to as the *Circle group* (explored further in the next section). Similar to $\textsf{M31}$, the operations of $\textsf{CM31}$ are defined using the macros `impl_field!` and `impl_extension_field!` (which takes as argument the field $\textsf{CM31}$ and the field to be extended i.e. $\textsf{M31}$). These are implemented as follows: ```rust theme={null} impl_field!(CM31, P2); impl_extension_field!(CM31, M31); ``` where `P2` is the size of $\textsf{CM31}$ i.e. $P^2$. ## Quartic Extension For the soundness of the protocol, it is crucial that the verifier samples random challenges from a sufficiently large field to ensure that an adversary cannot guess or brute-force the challenges and generate a proof that passes verification without knowledge of the witness. If we use $P = 2^{31} -1$, then 31-bit random challenges are not sufficient to maintain the security of the protocol. To address this, the verifier draws random challenges from a degree-4 extension of $\textsf{M31}$ denoted by $\textsf{QM31}$, which is equivalent to degree-2 extension of $\textsf{CM31}$, denoted as $\textsf{QM31} = \textsf{CM31}[X]/(X^2 - 2 - i)$ Here the polynomial $(X^2 - 2 - i)$ is irreducible over $\textsf{CM31}$. The elements of $\textsf{QM31}$ can be represented as $(r, s)$ or $r + u \cdot s$ where $r, s \in \textsf{CM31}$ and $u$ is the root of the polynomial $X^2 - 2 - i$ i.e. $u^2 - 2 - i = 0$. This is implemented as follows: ```rust theme={null} pub struct QM31(pub CM31, pub CM31); pub type SecureField = QM31; ``` Since the verifier uses the field $\textsf{QM31}$ to sample random challenges it is given the type alias `SecureField`. Alternatively, the elements of $\textsf{QM31}$ can also be represented as four elements of $\textsf{M31}$ i.e. $((a, b), (c, d))$ or $(a + i \cdot b) + (c + i \cdot d) \cdot u$ where $a, b, c, d \in \textsf{M31}$. With four elements from $\textsf{M31}$, the challenge space consists of 124-bit values, offering a sufficiently large $2^{124}$ possibilities to sample a random challenge. Similar to $\textsf{CM31}$, the operations of $\textsf{QM31}$ are defined using the macros `impl_field!` and `impl_extension_field!`, implemented as follows: ```rust theme={null} impl_field!(QM31, P4); impl_extension_field!(QM31, CM31); ``` where `P4` is the size of $\textsf{QM31}$ i.e. $P^4$. In the next section, we will explore the circle group, which is used to instantiate the STARK protocol. # Introduction Source: https://docs.starknet.io/learn/S-two-book/how-it-works/pcs/index > This section presents the implementation of the polynomial commitment scheme in S-two, which is built on top of the FRI protocol described previously. Polynomial commitments are a core cryptographic primitive that enable a prover to commit to a polynomial and later reveal evaluations at specific points. This section is organized as follows: * [**Overview**](/learn/S-two-book/how-it-works/pcs/overview): Describes the polynomial commitment scheme of S-two. * [**PCS Prover**](/learn/S-two-book/how-it-works/pcs/prover): Details the implementation of the prover for the polynomial commitment scheme, including commitment and opening protocol. * [**PCS Verifier**](/learn/S-two-book/how-it-works/pcs/verifier): Describes the verifier implementation for checking commitments and evaluation proofs. # Technical Overview Source: https://docs.starknet.io/learn/S-two-book/how-it-works/pcs/overview In this section, we describe a polynomial commitment scheme using the FRI protocol covered in the previous section. ## Polynomial Commitment Scheme A polynomial commitment scheme (PCS) allows a prover to commit to a polynomial and later prove its evaluations at points chosen by the verifier. The verifier can then check that the evaluations are consistent with the committed polynomial. It consists of the following three algorithms: * $setup(d) \rightarrow pp$: Given an upper bound $d$ on the degree, it outputs public parameters $pp$ used to commit to polynomials of degree less than $d$. For STARKs, the public parameters include the hash functions used for the Merkle commitment scheme and the FRI protocol parameters. * $commit(pp, f) \rightarrow com_f$: Takes public parameters $pp$ and a polynomial $f$ of degree $< d$, and outputs a commitment $com_f$ to the polynomial. For STARKs, $com_f$ is the root of the Merkle tree which commits to the evaluations of the polynomial $f$ on the evaluation domain. * $open(\mathcal{P}, \mathcal{V}) \rightarrow 0/1$: An interactive protocol where the prover $\mathcal{P}$ convinces the verifier $\mathcal{V}$ that $f(u) = v$. The verifier outputs 1 (accept) or 0 (reject). For STARKs, this protocol is based on FRI. The verifier will ask the prover to open the polynomial $f$ (committed using the Merkle tree) at point $u$. The prover will send the opening $v$ and define the quotient: $$ q(x) = \frac{f(x) - v}{x - u} $$ If the prover sends the correct opening, then $q$ will be a polynomial of bounded degree. The prover then uses the FRI protocol to convince the verifier that $q$ is "close" to some polynomial with a pre-specified degree bound. For S-two, we have already described the $commit$ protocol, which evaluates the circle polynomial over a canonical coset and commits to those evaluations using a Merkle tree. The $open$ protocol follows the same idea as the univariate case discussed above, but it is slightly different, as described next. In S-two, instead of using the single-point opening as described above, we have two-point openings for the values at point $Q$ and its conjugate $\overline{Q}$. The protocol proceeds as follows: 1. The verifier first receives a Merkle commitment to the evaluations of the original polynomial $f$. 2. The verifier samples a circle point $Q \in C(\mathsf{QM31}) \setminus C(\mathsf{M31})$ and requests the evaluation $f(Q)$ from the prover. 3. The prover sends the purported value $V$, and then both prover and verifier engage in the FRI protocol on the quotient: $$ q(x, y) = \frac{f(x, y) - l_{Q,V}(x, y)}{v_Q(x, y)} $$ Here, $l_{Q,V}(x, y)$ is the linear polynomial interpolating $(Q, V)$ and $(\overline{Q}, \overline{V})$, while $v_Q(x, y)$ vanishes at $Q$ and its conjugate $\overline{Q}$. As in the univariate case, if $q(x, y)$ is "close" to a polynomial, then the verifier is convinced that the evaluation claim is correct, i.e., that $f(Q) = V$. In the above protocol, we are opening the polynomial at a single point $Q$. To open the polynomial at multiple points, we batch the quotients of each point using a random linear combination and then apply the FRI protocol to a single batched quotient. One key property of a polynomial commitment scheme is binding. Informally, the binding property states that once the prover commits to a polynomial, they cannot open some other polynomial which outputs a different evaluation. For STARKs, the binding property is closely related to out-of-domain sampling, which we will describe next. ## Out of Domain Sampling Out-of-domain sampling relates to the notion of "closeness" described in the [FRI section](/learn/S-two-book/how-it-works/circle-fri/overview#introduction). Informally, the FRI protocol tests whether a function provided by the prover is "close" to some bounded degree polynomial. There are two notions of "closeness": 1. **Unique Decoding Regime**: We operate in this regime if there is at most a single polynomial which is "close" to the function provided by the prover. If the function is "close" to a single polynomial, then we can infer that the function represents that *unique* polynomial. 2. **List Decoding Regime**: We operate in this regime if there is a list of polynomials which are "close" to the function provided by the prover. In this case, since the function can be "close" to a *list* of polynomials, we cannot be sure that it represents a unique polynomial. In practice, we are usually operate in the list decoding regime. So there can be multiple polynomials which are "close" to the function provided by the prover. This affects the binding property of the polynomial commitment scheme, since the function sent by the prover represents a list of polynomials rather than some unique polynomial. To bind the prover to a unique polynomial from the list, we ask the prover to open the polynomial at an out-of-domain point. This is also referred to as *Domain Extension for Eliminating Pretenders* (or the *DEEP method*). This is the informal motivation for out-of-domain sampling. For more details, please refer to ["A summary on the FRI low degree test"](https://eprint.iacr.org/2022/1216.pdf). As we have seen in the [Security Analysis section](/learn/S-two-book/how-it-works/circle-fri/overview#security-analysis), we can improve security by increasing the number of verifier queries. But this will lead to more prover work, because the prover will have to send a Merkle decommitment for each verifier query and also increase the proof size. We will now see a method to increase the security of our protocol without significantly increasing the prover's work. ## Proof of Work The key idea is that rather than increasing the number of verifier queries, we can increase the cost of generating a false proof by a malicious prover by using *proof of work* or *grinding*. We add an additional requirement to the FRI protocol: following all the commitments made by the prover, the prover must find a 64-bit nonce that, when hashed together with the state of the hash chain, results in a required number of leading zeros. The number of leading zeros defines a certain amount of work that the prover must perform before generating the randomness representing the queries. As a result, a malicious prover that attempts to generate favorable queries will need to repeat the grinding process every time a commitment is changed. On the other hand, an honest prover only needs to perform the grinding process once. This is similar to the grinding performed on many blockchains. The nonce found by the prover is sent to the verifier as part of the proof, and in turn the verifier checks its consistency with the state of the hash chain by running the hash function once. The required number of leading zeros is configured by the `pow_bits` parameter. This effectively reduces the computational power of the cheating prover while only slightly increasing the running time of the honest prover. This is because the honest prover needs to solve the proof-of-work once, while a cheating prover, during the long process of trying to find a false proof, would need to solve many different instances of the proof-of-work. # Polynomial Commitment Scheme Prover Source: https://docs.starknet.io/learn/S-two-book/how-it-works/pcs/prover In this section, we will see the implementation of the commitment scheme prover. We will start by looking at the building blocks. ## Commitment Tree Prover The `CommitmentTreeProver` struct represents the data for a single Merkle tree commitment. As we have seen in the [Merkle tree section](/learn/S-two-book/how-it-works/vcs/merkle_prover#merkle-prover), we can commit to multiple polynomials of different degrees in the same Merkle tree. It is implemented as follows: ```rust theme={null} pub struct CommitmentTreeProver<B: BackendForChannel<MC>, MC: MerkleChannel> { pub polynomials: ColumnVec<CirclePoly<B>>, pub evaluations: ColumnVec<CircleEvaluation<B, BaseField, BitReversedOrder>>, pub commitment: MerkleProver<B, MC::H>, } ``` Here, `pub type ColumnVec<T> = Vec<T>`. It contains the following fields: * `polynomials`: The set of polynomials committed in a single Merkle tree. * `evaluations`: The evaluations of these polynomials over their respective domains. * `commitment`: The `MerkleProver` struct as described in the [Merkle tree section](/learn/S-two-book/how-it-works/vcs/merkle_prover#merkleprover-structure). It is initialized as follows: ```rust theme={null} pub fn new( polynomials: ColumnVec<CirclePoly<B>>, log_blowup_factor: u32, channel: &mut MC::C, twiddles: &TwiddleTree<B>, ) -> Self { let span = span!(Level::INFO, "Extension").entered(); let evaluations = B::evaluate_polynomials(&polynomials, log_blowup_factor, twiddles); span.exit(); let _span = span!(Level::INFO, "Merkle").entered(); let tree = MerkleProver::commit(evaluations.iter().map(|eval| &eval.values).collect()); MC::mix_root(channel, tree.root()); CommitmentTreeProver { polynomials, evaluations, commitment: tree, } } ``` It proceeds as follows. First, given the `polynomials`, we evaluate them on the evaluation domain using circle FFT to compute `evaluations`. Then we commit to those evaluations using the `MerkleProver` struct. Finally, we create and output the `CommitmentTreeProver` struct. ## Commitment Scheme Prover The `CommitmentSchemeProver` struct is the key struct which maintains a vector of commitment trees. It implements functionalities to open the committed polynomials, compute quotients, and then apply the FRI protocol. It contains the following fields: ```rust theme={null} pub struct CommitmentSchemeProver<'a, B: BackendForChannel<MC>, MC: MerkleChannel> { pub trees: TreeVec<CommitmentTreeProver<B, MC>>, pub config: PcsConfig, twiddles: &'a TwiddleTree<B>, } ``` It contains the following fields: * `tree`: This contains a vector of commitment trees. Here, `pub struct TreeVec<T>(pub Vec<T>)`. * `config`: This is the `PcsConfig`, which contains the `fri_config` and `pow_bits`. ```rust theme={null} pub struct PcsConfig { pub pow_bits: u32, pub fri_config: FriConfig, } ``` The security of the polynomial commitment scheme is computed as: ```rust theme={null} pub const fn security_bits(&self) -> u32 { self.pow_bits + self.fri_config.security_bits() } ``` * `twiddles`: This contains [precomputed twiddle factors](/learn/S-two-book/how-it-works/circle-fft/twiddles#twiddle-tree). Now we will see some key functions defined on the `CommitmentSchemeProver` struct. ### Commit The `commit` function, given a batch of polynomials, computes the `CommitmentTreeProver` struct which commits to the input polynomials and then appends the `tree` struct to the vector of stored `trees`. ```rust theme={null} fn commit(&mut self, polynomials: ColumnVec<CirclePoly<B>>, channel: &mut MC::C) { let _span = span!(Level::INFO, "Commitment").entered(); let tree = CommitmentTreeProver::new( polynomials, self.config.fri_config.log_blowup_factor, channel, self.twiddles, ); self.trees.push(tree); } ``` ### Trace The `trace` function returns a `Trace` struct containing all polynomials and their evaluations corresponding to all the commitment trees. It is implemented as follows: ```rust theme={null} pub fn trace(&self) -> Trace<'_, B> { let polys = self.polynomials(); let evals = self.evaluations(); Trace { polys, evals } } ``` ### Commitment Tree Builder The `tree_builder` function outputs the `TreeBuilder` struct. ```rust theme={null} pub fn tree_builder(&mut self) -> TreeBuilder<'_, 'a, B, MC> { TreeBuilder { tree_index: self.trees.len(), commitment_scheme: self, polys: Vec::default(), } } ``` The `TreeBuilder` struct is a helper for aggregating polynomials and evaluations before committing them in a Merkle tree. It allows the prover to collect columns (polynomials) and then commit them together as a batch. It is implemented as follows: ```rust theme={null} pub struct TreeBuilder<'a, 'b, B: BackendForChannel<MC>, MC: MerkleChannel> { tree_index: usize, commitment_scheme: &'a mut CommitmentSchemeProver<'b, B, MC>, polys: ColumnVec<CirclePoly<B>>, } ``` ### Prove The `prove_values` function is central to the protocol, handling the opening of committed polynomials at specified sample points and integrating with the FRI protocol for low-degree testing. It is implemented as follows: ```rust theme={null} pub fn prove_values( self, sampled_points: TreeVec<ColumnVec<Vec<CirclePoint<SecureField>>>>, channel: &mut MC::C, ) -> CommitmentSchemeProof<MC::H> { // Evaluate polynomials on open points. let span = span!( Level::INFO, "Evaluate columns out of domain", class = "EvaluateOutOfDomain" ) .entered(); let samples = self .polynomials() .zip_cols(&sampled_points) .map_cols(|(poly, points)| { points .iter() .map(|&point| PointSample { point, value: poly.eval_at_point(point), }) .collect_vec() }); span.exit(); let sampled_values = samples .as_cols_ref() .map_cols(|x| x.iter().map(|o| o.value).collect()); channel.mix_felts(&sampled_values.clone().flatten_cols()); // Compute oods quotients for boundary constraints on the sampled points. let columns = self.evaluations().flatten(); let quotients = compute_fri_quotients( &columns, &samples.flatten(), channel.draw_secure_felt(), self.config.fri_config.log_blowup_factor, ); // Run FRI commitment phase on the oods quotients. let fri_prover = FriProver::<B, MC>::commit(channel, self.config.fri_config, &quotients, self.twiddles); // Proof of work. let span1 = span!(Level::INFO, "Grind", class = "Queries POW").entered(); let proof_of_work = B::grind(channel, self.config.pow_bits); span1.exit(); channel.mix_u64(proof_of_work); // FRI decommitment phase. let (fri_proof, query_positions_per_log_size) = fri_prover.decommit(channel); // Decommit the FRI queries on the merkle trees. let decommitment_results = self .trees .as_ref() .map(|tree| tree.decommit(&query_positions_per_log_size)); let queried_values = decommitment_results.as_ref().map(|(v, _)| v.clone()); let decommitments = decommitment_results.map(|(_, d)| d); CommitmentSchemeProof { commitments: self.roots(), sampled_values, decommitments, queried_values, proof_of_work, fri_proof, config: self.config, } } } ``` Here is a detailed breakdown: 1. **Evaluate Polynomials at Sample Points**: * For each committed polynomial and each sample point (including out-of-domain points and mask points which contain constraint offsets), the function evaluates the polynomials and collects the results in `samples`. * The `sampled_values` are mixed into the channel, ensuring they are bound to the proof and used for subsequent randomness generation. 2. **Compute FRI Quotients**: * The function computes FRI quotient polynomials using `compute_fri_quotients` to open the committed polynomials at sampled points in `samples`. This follows the same quotienting process as described in the [overview section](/learn/S-two-book/how-it-works/pcs/overview#polynomial-commitment-scheme). 3. **FRI Commitment Phase**: * The FRI protocol is run on the quotient polynomials, committing to their evaluations in Merkle trees and initializing the `fri_prover`. For more details, refer to the [FRI prover section](
프로젝트로 돌아가기