You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
16 KiB

  1. # Implementing the powers of tau contributor
  2. *2023-01-02*
  3. In the following notes we go over the ideas behind the *Powers of Tau* ceremony, how participants contribute and how the contributions are verified.
  4. The powers of tau are used in schemes such as KZG Commitments, and by consequence in the Plonk-KZG and in Verkle Trees use cases, which the later ones are in the Ethereum roadmap, but probably the ceremony output will be used in many other applications.
  5. The ceremony is named also as *Trusted Setup Ceremony*, and *SRS* (Structured Reference String) in Plonk and Sonic papers.
  6. Specifically these notes focus on the powers of tau ceremony that is [being prepared in Ethereum](https://github.com/ethereum/kzg-ceremony). For learning about the specifics of the powers of tau reasoning, I've used the following two documents:
  7. - [KZG10-Ceremony-audit-report.pdf, by SECBIT Labs](https://github.com/ethereum/kzg-ceremony/blob/main/KZG10-Ceremony-audit-report.pdf)
  8. - [*Why and how zkSNARKs work*, by Maksym Petkus](https://arxiv.org/abs/1906.07221v1)
  9. Additionally, [this article by Vitalik Buterin](https://vitalik.ca/general/2022/03/14/trustedsetup.html) explains the ideas behind trusted setups.
  10. The contents of these notes are distributed as follows:
  11. <br><span style="margin-right:40px;"></span>
  12. <a style="color:gray;" href="#the-main idea">The main idea</a>
  13. <br><span style="margin-right:40px;"></span>
  14. <a style="color:gray;" href="#preliminaries">Preliminaries</a>
  15. <br><span style="margin-right:40px;"></span>
  16. <a style="color:gray;" href="#participant-contribution">Participant contribution</a>
  17. <br><span style="margin-right:40px;"></span>
  18. <a style="color:gray;" href="#proof-of correct computation">Proof of correct computation</a>
  19. <br><span style="margin-right:40px;"></span>
  20. <a style="color:gray;" href="#contribution-verification">Contribution verification</a>
  21. <br><span style="margin-right:40px;"></span>
  22. <a style="color:gray;" href="#full-flow">Full flow</a>
  23. <br><span style="margin-right:40px;"></span>
  24. <a style="color:gray;" href="#conclusions">Conclusions</a>
  25. Across these notes some snippets of Sage code are provided, the full Sage code can be found [here](https://github.com/arnaucube/math/blob/master/powersoftau.sage), and also a [full implementation in Go](https://github.com/arnaucube/eth-kzg-ceremony-alt) which also contains a CLI to participate in the upcomming Ethereum KZG-Ceremony.
  26. ### The main idea
  27. The main idea is that we want to compute $\tau^0, \tau^1, \tau^2, \ldots, \tau^{n-1}$, where $\tau$ should be unknown. In order to achieve this, we use some technics to combine different $\tau$s from different contributors, in a way where we obtain the powers of a tau which is a combination of the different secret taus ($\tau_{p_i}$) of the different participants.
  28. The $\tau_{p_i}$ should be secret for each participant, furthermore, it should be generated from some source of randomness and destroyed at the end.
  29. As we've mentioned, we want to come up with the powers of tau where tau is a combination of the taus from all the participants, in a way where as long as there is at least one honest participant, which does not share their part of the final tau, the combined-tau can not be recovered and the ceremony is assumed to be safe.
  30. This is important, because if somebody has knowledge of the tau value, could generate fake proofs. As an example [this article by Kobi Gurkan](https://medium.com/qed-it/how-toxic-is-the-waste-in-a-zksnark-trusted-setup-9b250d59bdb4) shows how fake BCTV14a proofs can be generated if tau is known.
  31. This is relevant in the context of [KZG Commitments](https://arnaucube.com/blog/kzg-commitments.html), specially in the roadmap of Ethereum.
  32. Ethereum will run the Ceremony which will be used at least in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844). That's why EF's is encouraging people to participate in various ways, as the Ceremony is considered safe as long as there is at least one honest participant, with the idea that if you participate, assuming that you consider yourself honest, you can consider the Ceremony safe.
  33. The output of the ceremony will be used in the Ethereum VerkleTrees, which uses KZG Commitments, but also it will be useful for Plonk-KZG proofs.
  34. So the best way to trust the output of the ceremony is by becoming a participant yourself!, Furhermore, by participating with your own code ^^
  35. ### Preliminaries
  36. **Pairing**<br>
  37. We will try to focus only in the concepts that we strictly need for the implementation, so we will treat Pairings as a black box with some simplified properties.
  38. For our use case, we use asymmetric bilinear pairings. We can see an asymmetric bilinear pairing as a function that maps an element from the group $\mathbb{G_1}$ and another one from $\mathbb{G_2}$ into an el element of the group $\mathbb{G_T}$.
  39. $$e: \mathbb{G_1} \times \mathbb{G_2} \longrightarrow \mathbb{G_T}$$
  40. For our use case, we'll focus only on one property, *bilinearity*.
  41. Let $a, b \in \mathbb{F_q}$, $\mathbb{G}_1 = \langle G \rangle$ and $\mathbb{G}_2 = \langle H \rangle~$ (In other words, $G$ is the generator of $\mathbb{G}_1$, and $H$ is the generator of $\mathbb{G}_2$).
  42. Then, the bilinearity property tells us that
  43. $$e(a \cdot G,~b \cdot H) = e((a \cdot b) \cdot G,~H) = e(G,~(a \cdot b) \cdot H)$$
  44. For better readability, we will use the following notation to represent the scalar point multiplication:
  45. $$[x]_1 = x \cdot G \in \mathbb{G}_1\newline
  46. [x]_2 = x \cdot H \in \mathbb{G}_2$$
  47. So, our previous bilinearity example would look like
  48. $$e([a]_1, [b]_2) = e([a \cdot b]_1, [1]_2) = e([1]_1, [a \cdot b]_2)$$
  49. In our practical use case, we use the [BLS12-381](https://hackmd.io/@benjaminion/bls12-381) pairing, where $\mathbb{G}_1, \mathbb{G}_2$ are elliptic curve groups, thus the points $G, H$ are elliptic curve points.
  50. **Code**<br>
  51. For the code examples we'll use [SageMath](https://www.sagemath.org), but the code would look quite similar in Python, and the same logic can be implemented in any language.
  52. ### Participant contribution
  53. In the context of SRS (Structured Reference String), the powers of tau consist of an array containing the scalar multiplication of the generator point by each power of tau, eg. $\tau^0 \cdot G,~\tau^1 \cdot G,~\tau^2 \cdot G,~\tau^3 \cdot G \ldots$
  54. For our case, we'll generate $n$ powers of tau on $\mathbb{G}_1$, and $m$ on $\mathbb{G}_2$. We will denote the combination of both $\mathbb{G}_1$ and $\mathbb{G}_2$ powers of tau by SRS.
  55. Each participant will obtain the previous-participant generated SRS, which is formed by
  56. $$\{ [\tau^0]_1, [\tau^1]_1, [\tau^2]_2, \ldots, [\tau^{n-1}]_1 \},\\
  57. \{ [\tau^0]_2, [\tau^1]_2, [\tau^2]_2, \ldots, [\tau^{m-1}]_2 \}$$
  58. for simplicity, we will represent it as
  59. $$\{~[~\tau^i]_1~\}^{n-1}_{i=0},~\{~[~\tau^i]_2~\}^{m-1}_{j=0}$$
  60. Each participant generates their secret random tau $\tau_p$, which to avoid confusion we will denote as $p \in \mathbb{F}_r$. From it, the participant can compute $[p]_2 \in \mathbb{G}_2$.
  61. Then the participant proceeds to update the reference string (SRS), by multiplying each element by $p^i$:
  62. $$\{~ p^i \cdot [~ \tau^i]_1~\}^{n-1}_{i=0},~\{~ p^i \cdot [~\tau^i]_2~\}^{m-1}_{j=0}$$
  63. which, by the properties of scalar multiplication of a point, is equivalent to
  64. $$\{~ [~ (p \cdot \tau)^i]_1~\}^{n-1}_{i=0},~\{~ [~ (p \cdot \tau)^i]_2~\}^{m-1}_{j=0}$$
  65. and we denote $p \cdot \tau$ as $\tau'$, so we can write the previous expression as
  66. $$\{~[~\tau'^i]_1~\}^{n-1}_{i=0},~\{~[~\tau'^i]_2~\}^{m-1}_{j=0}$$
  67. which is
  68. $$\{ [(\tau \cdot p)^0]_1, [(\tau \cdot p)^1]_1, [(\tau \cdot p)^2]_2, \ldots, [(\tau \cdot p)^{n-1}]_1 \},\\
  69. \{ [(\tau \cdot p)^0]_2, [(\tau \cdot p)^1]_2, [(\tau \cdot p)^2]_2, \ldots, [(\tau \cdot p)^{m-1}]_2 \}$$
  70. Notice that the contributor does not know the previous $\tau$, but can obtain $(\tau \cdot p)^i$.
  71. <br><br>
  72. We can express the previous logic as follows:
  73. ```python
  74. def compute_contribution(new_tau, prev_srs):
  75. g1s = [None] * len(prev_srs[0])
  76. g2s = [None] * len(prev_srs[1])
  77. srs = [g1s, g2s]
  78. Q = e.r
  79. # compute [τ'⁰]₁, [τ'¹]₁, [τ'²]₁, ..., [τ'ⁿ⁻¹]₁, where n = len(prev_srs.G1s)
  80. for i in range(0, len(prev_srs[0])):
  81. srs[0][i] = (new_tau^i) * prev_srs[0][i]
  82. # compute [τ'⁰]₂, [τ'¹]₂, [τ'²]₂, ..., [τ'ⁿ⁻¹]₂, where n = len(prev_srs.G2s)
  83. for i in range(0, len(prev_srs[1])):
  84. srs[1][i] = (new_tau^i) * prev_srs[1][i]
  85. return srs
  86. ```
  87. ### Proof of correct computation
  88. The participant will compute the proof, which consists of
  89. $$\pi = ([\tau']_1,~[p]_2)$$
  90. which is equivalent to $\pi = (\tau' \cdot G,~p \cdot H) = ((p \cdot \tau) \cdot G,~p \cdot H)$
  91. In the next section we will see how this is used to verify the contribution correct computation.
  92. <br>
  93. We implement this logic as follows:
  94. ```python
  95. def generate_proof(tau, prev_srs, new_srs):
  96. # g_1^{tau'} = g_1^{p * tau} = SRS_G1s[1] * p
  97. g1_ptau = prev_srs[0][1] * tau
  98. # g_2^{p}
  99. g2_p = tau * e.G2
  100. return [g1_ptau, g2_p]
  101. ```
  102. ### Contribution verification
  103. This is the interesting part. We want to check that the new SRS is well computed from the previous SRS together with the new tau. We want to avoid accepting contributions that do not follow the powers of tau structure, or that contain empty elements or that do not derive from the previous SRS.
  104. Following the powers of tau structure means that
  105. $$\tau'^{i+1} = \tau' \cdot \tau'^i ~~\forall~i \in [1, n-1]$$
  106. And the relation between the new SRS and the previous SRS is
  107. $$\tau' = \tau \cdot p$$
  108. where $\tau$ is the secret element from the previous SRS and $p$ is the secret one from the new SRS, resulting in the combined $\tau'$.
  109. Now, we're interested into how we can check these relations in the context of the SRS elements. A new contribution can be verified as follows:
  110. 1. Check that elements of the new SRS are non-empty, non-zero and in the correct prime order subgroups.
  111. 2. Get the proof ($\pi = ([\tau']_1, [p]_2)$), and confirm that the 1st element is equal to the element in position 1 $[\tau'^1]_1$ of the new SRS (which corresponds to $\tau'$ powered to $1$).
  112. 3. Check that $\tau'$ (the new SRS) is correctly related to $\tau$ (the previous SRS).
  113. To check this, we rely on the pairing properties and we use the proof value generated by the contributor, which consists of $[p]_2$
  114. $$e([\tau]_1, [p]_2) \stackrel{?}{=} e([\tau']_1, [1]_2)$$
  115. We can see how this holds, as $\tau' = p \cdot \tau$, and by the bilinear property of the pairing (see [Preliminearies section](#preliminaries)) we have $e(a \cdot G, b \cdot H) = e((a \cdot b) \cdot G, H)$, which in our notation is represented as
  116. $$e([a]_1, [b]_2) = e([a \cdot b]_1, [1]_2)$$
  117. by applying this to our case we can see that:
  118. $$e([\tau]_1, [p]_2) = e([\tau \cdot p]_1, [1]_2) = e([\tau']_1, [1]_2)$$
  119. 4. Check if the new SRS follows the powers of tau structure:
  120. $$e([\tau'^i]_1, [\tau']_2) \stackrel{?}{=} e([\tau'^{i+1}]_1, [1]_2) ~~ \forall ~i \in [1, n-1]$$
  121. $$e([\tau']_1, [\tau'^j]_2) \stackrel{?}{=} e([1]_1, [\tau'^{j+1}]_2) ~~ \forall ~i \in [1, m-1]$$
  122. Which, again, by the bilinarity property, we can see that
  123. $$e([\tau'^i]_1, [\tau']_2) = e([\tau'^{i+1}]_1, [1]_2)$$
  124. is enforcing that $\tau'^i \cdot \tau'^1 = \tau'^{i+1}$.
  125. And the same for
  126. $$e([\tau']_1, [\tau'^j]_2) = e([1]_1, [\tau'^{j+1}]_2)$$
  127. which checks that $\tau'^1 \cdot \tau'^j = \tau'^{j+1}$.
  128. With this check, we're ensuring that each power of tau of the SRS is consistent with the previous one.
  129. <br><br>
  130. The complete verification function would look like
  131. ```python
  132. def verify(prev_srs, new_srs, proof):
  133. # 1. check that elements of the newSRS are valid points
  134. for i in range(0, len(new_srs[0])-1):
  135. assert new_srs[0][i] != None
  136. assert new_srs[0][i] != e.E1(0)
  137. assert new_srs[0][i] in e.E1
  138. for i in range(0, len(new_srs[1])-1):
  139. assert new_srs[1][i] != None
  140. assert new_srs[1][i] != e.E1(0)
  141. assert new_srs[1][i] in e.E2
  142. # 2. check proof.G1PTau == newSRS.G1Powers[1]
  143. assert proof[0] == new_srs[0][1]
  144. # 3. check newSRS.G1s[1] (g₁^τ'), is correctly related to prev_srs.G1s[1] (g₁^τ)
  145. # e([τ]₁, [p]₂) == e([τ']₁, [1]₂)
  146. assert e.pair(prev_srs[0][1], proof[1]) == e.pair(new_srs[0][1], e.G2)
  147. # 4. check newSRS following the powers of tau structure
  148. # i) e([τ'ⁱ]₁, [τ']₂) == e([τ'ⁱ⁺¹]₁, [1]₂), for i ∈ [1, n−1]
  149. for i in range(0, len(new_srs[0])-1):
  150. assert e.pair(new_srs[0][i], new_srs[1][1]) == e.pair(new_srs[0][i+1], e.G2)
  151. # ii) e([τ']₁, [τ'ʲ]₂) == e([1]₁, [τ'ʲ⁺¹]₂), for j ∈ [1, m−1]
  152. for i in range(0, len(new_srs[1])-1):
  153. assert e.pair(new_srs[0][1], new_srs[1][i]) == e.pair(e.G1, new_srs[1][i+1])
  154. ```
  155. ### Full flow
  156. Now that we have the three methods (*compute_contribution, generate_proof, verify*), it's a matter of using them.
  157. First let's add a couple of helpers to our code:
  158. ```python
  159. # here we import the BLS12-381 pairing machinery
  160. load("bls12-381.sage") # file from https://github.com/arnaucube/math/blob/master/bls12-381.sage
  161. e = Pairing()
  162. # this method generates a SRS filled by the generator points of G1 & G2
  163. def new_empty_SRS(nG1, nG2):
  164. g1s = [None] * nG1
  165. g2s = [None] * nG2
  166. for i in range(0, nG1):
  167. g1s[i] = e.G1
  168. for i in range(0, nG2):
  169. g2s[i] = e.G2
  170. return [g1s, g2s]
  171. ```
  172. Let's do the flow:
  173. ```python
  174. # generate a fake previous-SRS
  175. (prev_srs) = new_empty_SRS(5, 3)
  176. # set a random value to be used for our tau
  177. random = 12345 # this would be a random value
  178. tau = e.F1(random)
  179. # compute our contribution, from the previous SRS and our tau
  180. new_srs = compute_contribution(tau, prev_srs)
  181. # generate a proof of correct computation of our contribution
  182. proof = generate_proof(tau, prev_srs, new_srs)
  183. # verify the proof for the given prev_srs and the new_srs
  184. verify(prev_srs, new_srs, proof)
  185. ```
  186. ## Conclusions
  187. As we've seen, we can prove and verify the correct computation of the new SRS with few operations.
  188. From here the next thing would be to implement the [interaction with the Sequencer](https://github.com/ethereum/kzg-ceremony-specs/blob/master/apiSpec/sequencerApi.yml), in order to authenticate the participant, fetch the previous SRS, and send the new SRS and proof.
  189. You can find the Sage example that we've been using [here](https://github.com/arnaucube/math/blob/master/powersoftau.sage), and a full Go implementation here: https://github.com/arnaucube/eth-kzg-ceremony-alt (which uses [Kilic's BLS12-381 implementation](https://github.com/kilic/bls12-381), and includes also the interaction with the Sequencer and a CLI).
  190. There is also the EF's rust implementation [small-powers-of-tau](https://github.com/crate-crypto/small-powers-of-tau) which has been audited. The same logic can be implemented in the language of your choice.
  191. <br><br>
  192. <a href="https://github.com/arnaucube/eth-kzg-ceremony-alt" target="_blank" title="https://github.com/arnaucube/eth-kzg-ceremony-alt">
  193. <img src="img/posts/powersoftau/kzg-ceremony-screenshot.png" style="width:100%;" />
  194. <span style="font-size:80%; color:gray;font-style:italic;">Screenshot of the eth-kzg-ceremony-alt CLI after contributing to the testnet ceremony.</a>
  195. </a>
  196. Another interesting piece is the *randomness generation*. In our example, for simplicity, tau was hardcoded in the code, but in the real world case we would use some source of randomness to generate our secret value. Here you can get [as creative as you can](https://www.youtube.com/watch?v=I4cDAqeEmpU), and probably we will see some cool approaches for the Ethereum-KZG-Ceremony.
  197. This ceremony output is quite important, as it will be used in many applications once available. It will be used in the Ethereum protocol for the KZG Commitments on EIP-4844, but also on many other applications (some of them unrelated to Ethereum!). For example, any (up to the degree upperbound) Plonk-KZG deployment could reuse this ceremony to avoid needing to compute their own.
  198. So it's important to gather a notable amount of participants in the ceremony, and probably participate to it by yourself! (as if you're contributing you know that at least there has been one honest contribution (you)).
  199. You can find more info on the Ethereum's KZG Ceremony here: https://ceremony.ethereum.org (which includes also a nice contributor webapp to participate from the browser).