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.

294 lines
9.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package snark
  2. import (
  3. "fmt"
  4. "math/big"
  5. "os"
  6. "github.com/arnaucube/go-snark/bn128"
  7. "github.com/arnaucube/go-snark/circuitcompiler"
  8. "github.com/arnaucube/go-snark/fields"
  9. "github.com/arnaucube/go-snark/r1csqap"
  10. )
  11. // Setup is the data structure holding the Trusted Setup data. The Setup.Toxic sub struct must be destroyed after the GenerateTrustedSetup function is completed
  12. type Setup struct {
  13. Toxic struct {
  14. T *big.Int // trusted setup secret
  15. Ka *big.Int // prover
  16. Kb *big.Int // prover
  17. Kc *big.Int // prover
  18. Kbeta *big.Int
  19. Kgamma *big.Int
  20. RhoA *big.Int
  21. RhoB *big.Int
  22. RhoC *big.Int
  23. }
  24. // public
  25. G1T [][3]*big.Int // t encrypted in G1 curve
  26. G2T [][3][2]*big.Int // t encrypted in G2 curve
  27. Pk struct { // Proving Key pk:=(pkA, pkB, pkC, pkH)
  28. A [][3]*big.Int
  29. B [][3][2]*big.Int
  30. C [][3]*big.Int
  31. Kp [][3]*big.Int
  32. Ap [][3]*big.Int
  33. Bp [][3]*big.Int
  34. Cp [][3]*big.Int
  35. }
  36. Vk struct {
  37. Vka [3][2]*big.Int
  38. Vkb [3]*big.Int
  39. Vkc [3][2]*big.Int
  40. A [][3]*big.Int
  41. G1Kbg [3]*big.Int // g1 * Kbeta * Kgamma
  42. G2Kbg [3][2]*big.Int // g2 * Kbeta * Kgamma
  43. G2Kg [3][2]*big.Int // g2 * Kgamma
  44. Vkz [3][2]*big.Int
  45. }
  46. }
  47. // Proof contains the parameters to proof the zkSNARK
  48. type Proof struct {
  49. PiA [3]*big.Int
  50. PiAp [3]*big.Int
  51. PiB [3][2]*big.Int
  52. PiBp [3]*big.Int
  53. PiC [3]*big.Int
  54. PiCp [3]*big.Int
  55. PiH [3]*big.Int
  56. PiKp [3]*big.Int
  57. PublicSignals []*big.Int
  58. }
  59. type utils struct {
  60. Bn bn128.Bn128
  61. FqR fields.Fq
  62. PF r1csqap.PolynomialField
  63. }
  64. // Utils is the data structure holding the BN128, FqR Finite Field over R, PolynomialField, that will be used inside the snarks operations
  65. var Utils = prepareUtils()
  66. func prepareUtils() utils {
  67. bn, err := bn128.NewBn128()
  68. if err != nil {
  69. panic(err)
  70. }
  71. // new Finite Field
  72. fqR := fields.NewFq(bn.R)
  73. // new Polynomial Field
  74. pf := r1csqap.NewPolynomialField(fqR)
  75. return utils{
  76. Bn: bn,
  77. FqR: fqR,
  78. PF: pf,
  79. }
  80. }
  81. // GenerateTrustedSetup generates the Trusted Setup from a compiled Circuit. The Setup.Toxic sub data structure must be destroyed
  82. func GenerateTrustedSetup(witnessLength int, circuit circuitcompiler.Circuit, alphas, betas, gammas [][]*big.Int, zx []*big.Int) (Setup, error) {
  83. var setup Setup
  84. var err error
  85. // generate random t value
  86. setup.Toxic.T, err = Utils.FqR.Rand()
  87. if err != nil {
  88. return Setup{}, err
  89. }
  90. // k for calculating pi' and Vk
  91. setup.Toxic.Ka, err = Utils.FqR.Rand()
  92. if err != nil {
  93. return Setup{}, err
  94. }
  95. setup.Toxic.Kb, err = Utils.FqR.Rand()
  96. if err != nil {
  97. return Setup{}, err
  98. }
  99. setup.Toxic.Kc, err = Utils.FqR.Rand()
  100. if err != nil {
  101. return Setup{}, err
  102. }
  103. // generate Kβ (Kbeta) and Kγ (Kgamma)
  104. setup.Toxic.Kbeta, err = Utils.FqR.Rand()
  105. if err != nil {
  106. return Setup{}, err
  107. }
  108. setup.Toxic.Kgamma, err = Utils.FqR.Rand()
  109. if err != nil {
  110. return Setup{}, err
  111. }
  112. // generate ρ (Rho): ρA, ρB, ρC
  113. setup.Toxic.RhoA, err = Utils.FqR.Rand()
  114. if err != nil {
  115. return Setup{}, err
  116. }
  117. setup.Toxic.RhoB, err = Utils.FqR.Rand()
  118. if err != nil {
  119. return Setup{}, err
  120. }
  121. setup.Toxic.RhoC = Utils.FqR.Mul(setup.Toxic.RhoA, setup.Toxic.RhoB)
  122. // encrypt t values with curve generators
  123. var gt1 [][3]*big.Int
  124. var gt2 [][3][2]*big.Int
  125. for i := 0; i < witnessLength; i++ {
  126. tPow := Utils.FqR.Exp(setup.Toxic.T, big.NewInt(int64(i)))
  127. tEncr1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, tPow)
  128. gt1 = append(gt1, tEncr1)
  129. tEncr2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, tPow)
  130. gt2 = append(gt2, tEncr2)
  131. }
  132. // gt1: g1, g1*t, g1*t^2, g1*t^3, ...
  133. // gt2: g2, g2*t, g2*t^2, ...
  134. setup.G1T = gt1
  135. setup.G2T = gt2
  136. setup.Vk.Vka = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Ka)
  137. setup.Vk.Vkb = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, setup.Toxic.Kb)
  138. setup.Vk.Vkc = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kc)
  139. /*
  140. Verification keys:
  141. - Vk_betagamma1: setup.G1Kbg = g1 * Kbeta*Kgamma
  142. - Vk_betagamma2: setup.G2Kbg = g2 * Kbeta*Kgamma
  143. - Vk_gamma: setup.G2Kg = g2 * Kgamma
  144. */
  145. kbg := Utils.FqR.Mul(setup.Toxic.Kbeta, setup.Toxic.Kgamma)
  146. setup.Vk.G1Kbg = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kbg)
  147. setup.Vk.G2Kbg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, kbg)
  148. setup.Vk.G2Kg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kgamma)
  149. // for i := 0; i < circuit.NSignals; i++ {
  150. for i := 0; i < circuit.NVars; i++ {
  151. at := Utils.PF.Eval(alphas[i], setup.Toxic.T)
  152. a := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, at)
  153. setup.Pk.A = append(setup.Pk.A, a)
  154. if i <= circuit.NPublic {
  155. setup.Vk.A = append(setup.Vk.A, a)
  156. }
  157. bt := Utils.PF.Eval(betas[i], setup.Toxic.T)
  158. bg1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, bt)
  159. bg2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, bt)
  160. setup.Pk.B = append(setup.Pk.B, bg2)
  161. ct := Utils.PF.Eval(gammas[i], setup.Toxic.T)
  162. c := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, ct)
  163. setup.Pk.C = append(setup.Pk.C, c)
  164. kt := Utils.FqR.Add(Utils.FqR.Add(at, bt), ct)
  165. k := Utils.Bn.G1.Affine(Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt))
  166. ktest := Utils.Bn.G1.Affine(Utils.Bn.G1.Add(Utils.Bn.G1.Add(a, bg1), c))
  167. if !Utils.Bn.Fq2.Equal(k, ktest) {
  168. os.Exit(1)
  169. return setup, err
  170. }
  171. setup.Pk.Ap = append(setup.Pk.Ap, Utils.Bn.G1.MulScalar(a, setup.Toxic.Ka))
  172. setup.Pk.Bp = append(setup.Pk.Bp, Utils.Bn.G1.MulScalar(bg1, setup.Toxic.Kb))
  173. setup.Pk.Cp = append(setup.Pk.Cp, Utils.Bn.G1.MulScalar(c, setup.Toxic.Kc))
  174. k_ := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt)
  175. setup.Pk.Kp = append(setup.Pk.Kp, Utils.Bn.G1.MulScalar(k_, setup.Toxic.Kbeta))
  176. }
  177. setup.Vk.Vkz = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, Utils.PF.Eval(zx, setup.Toxic.T))
  178. return setup, nil
  179. }
  180. // GenerateProofs generates all the parameters to proof the zkSNARK from the Circuit, Setup and the Witness
  181. func GenerateProofs(circuit circuitcompiler.Circuit, setup Setup, hx []*big.Int, w []*big.Int) (Proof, error) {
  182. var proof Proof
  183. proof.PiA = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  184. proof.PiAp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  185. proof.PiB = Utils.Bn.Fq6.Zero()
  186. proof.PiBp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  187. proof.PiC = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  188. proof.PiCp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  189. proof.PiH = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  190. proof.PiKp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  191. for i := circuit.NPublic + 1; i < circuit.NVars; i++ {
  192. proof.PiA = Utils.Bn.G1.Add(proof.PiA, Utils.Bn.G1.MulScalar(setup.Pk.A[i], w[i]))
  193. proof.PiAp = Utils.Bn.G1.Add(proof.PiAp, Utils.Bn.G1.MulScalar(setup.Pk.Ap[i], w[i]))
  194. }
  195. for i := 0; i < circuit.NVars; i++ {
  196. proof.PiB = Utils.Bn.G2.Add(proof.PiB, Utils.Bn.G2.MulScalar(setup.Pk.B[i], w[i]))
  197. proof.PiBp = Utils.Bn.G1.Add(proof.PiBp, Utils.Bn.G1.MulScalar(setup.Pk.Bp[i], w[i]))
  198. proof.PiC = Utils.Bn.G1.Add(proof.PiC, Utils.Bn.G1.MulScalar(setup.Pk.C[i], w[i]))
  199. proof.PiCp = Utils.Bn.G1.Add(proof.PiCp, Utils.Bn.G1.MulScalar(setup.Pk.Cp[i], w[i]))
  200. proof.PiKp = Utils.Bn.G1.Add(proof.PiKp, Utils.Bn.G1.MulScalar(setup.Pk.Kp[i], w[i]))
  201. }
  202. for i := 0; i < len(hx); i++ {
  203. proof.PiH = Utils.Bn.G1.Add(proof.PiH, Utils.Bn.G1.MulScalar(setup.G1T[i], hx[i]))
  204. }
  205. proof.PublicSignals = w[1 : circuit.NPublic+1]
  206. return proof, nil
  207. }
  208. // VerifyProof verifies over the BN128 the Pairings of the Proof
  209. func VerifyProof(circuit circuitcompiler.Circuit, setup Setup, proof Proof) bool {
  210. // e(piA, Va) == e(piA', g2)
  211. pairingPiaVa := Utils.Bn.Pairing(proof.PiA, setup.Vk.Vka)
  212. pairingPiapG2 := Utils.Bn.Pairing(proof.PiAp, Utils.Bn.G2.G)
  213. if !Utils.Bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  214. return false
  215. }
  216. fmt.Println("✓ e(piA, Va) == e(piA', g2), valid knowledge commitment for A")
  217. // e(Vb, piB) == e(piB', g2)
  218. pairingVbPib := Utils.Bn.Pairing(setup.Vk.Vkb, proof.PiB)
  219. pairingPibpG2 := Utils.Bn.Pairing(proof.PiBp, Utils.Bn.G2.G)
  220. if !Utils.Bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  221. return false
  222. }
  223. fmt.Println("✓ e(Vb, piB) == e(piB', g2), valid knowledge commitment for B")
  224. // e(piC, Vc) == e(piC', g2)
  225. pairingPicVc := Utils.Bn.Pairing(proof.PiC, setup.Vk.Vkc)
  226. pairingPicpG2 := Utils.Bn.Pairing(proof.PiCp, Utils.Bn.G2.G)
  227. if !Utils.Bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  228. return false
  229. }
  230. fmt.Println("✓ e(piC, Vc) == e(piC', g2), valid knowledge commitment for C")
  231. // Vkx, to then calculate Vkx+piA
  232. vkxpia := setup.Vk.A[0]
  233. for i := 0; i < circuit.NPublic; i++ {
  234. vkxpia = Utils.Bn.G1.Add(vkxpia, Utils.Bn.G1.MulScalar(setup.Vk.A[i+1], proof.PublicSignals[i]))
  235. }
  236. // e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2)
  237. if !Utils.Bn.Fq12.Equal(
  238. Utils.Bn.Pairing(Utils.Bn.G1.Add(vkxpia, proof.PiA), proof.PiB),
  239. Utils.Bn.Fq12.Mul(
  240. Utils.Bn.Pairing(proof.PiH, setup.Vk.Vkz),
  241. Utils.Bn.Pairing(proof.PiC, Utils.Bn.G2.G))) {
  242. return false
  243. }
  244. fmt.Println("✓ e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2), QAP disibility checked")
  245. // e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB)
  246. // == e(piK, g2Kgamma)
  247. piApiC := Utils.Bn.G1.Add(Utils.Bn.G1.Add(vkxpia, proof.PiA), proof.PiC)
  248. pairingPiACG2Kbg := Utils.Bn.Pairing(piApiC, setup.Vk.G2Kbg)
  249. pairingG1KbgPiB := Utils.Bn.Pairing(setup.Vk.G1Kbg, proof.PiB)
  250. pairingL := Utils.Bn.Fq12.Mul(pairingPiACG2Kbg, pairingG1KbgPiB)
  251. pairingR := Utils.Bn.Pairing(proof.PiKp, setup.Vk.G2Kg)
  252. if !Utils.Bn.Fq12.Equal(pairingL, pairingR) {
  253. return false
  254. }
  255. fmt.Println("✓ e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB) == e(piK, g2Kgamma)")
  256. return true
  257. }