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.

201 lines
4.9 KiB

  1. package prover
  2. import (
  3. "crypto/rand"
  4. "math"
  5. "math/big"
  6. "runtime"
  7. "sync"
  8. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  9. "github.com/iden3/go-circom-prover-verifier/types"
  10. "github.com/iden3/go-iden3-crypto/utils"
  11. )
  12. // Proof is the data structure of the Groth16 zkSNARK proof
  13. type Proof struct {
  14. A *bn256.G1
  15. B *bn256.G2
  16. C *bn256.G1
  17. }
  18. // Pk holds the data structure of the ProvingKey
  19. type Pk struct {
  20. A []*bn256.G1
  21. B2 []*bn256.G2
  22. B1 []*bn256.G1
  23. C []*bn256.G1
  24. NVars int
  25. NPublic int
  26. VkAlpha1 *bn256.G1
  27. VkDelta1 *bn256.G1
  28. VkBeta1 *bn256.G1
  29. VkBeta2 *bn256.G2
  30. VkDelta2 *bn256.G2
  31. HExps []*bn256.G1
  32. DomainSize int
  33. PolsA []map[int]*big.Int
  34. PolsB []map[int]*big.Int
  35. PolsC []map[int]*big.Int
  36. }
  37. // Witness contains the witness
  38. type Witness []*big.Int
  39. func randBigInt() (*big.Int, error) {
  40. maxbits := types.R.BitLen()
  41. b := make([]byte, (maxbits/8)-1)
  42. _, err := rand.Read(b)
  43. if err != nil {
  44. return nil, err
  45. }
  46. r := new(big.Int).SetBytes(b)
  47. rq := new(big.Int).Mod(r, types.R)
  48. return rq, nil
  49. }
  50. // GenerateProof generates the Groth16 zkSNARK proof
  51. func GenerateProof(pk *types.Pk, w types.Witness) (*types.Proof, []*big.Int, error) {
  52. var proof types.Proof
  53. r, err := randBigInt()
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. s, err := randBigInt()
  58. if err != nil {
  59. return nil, nil, err
  60. }
  61. numcpu := runtime.NumCPU()
  62. proofA := arrayOfZeroesG1(numcpu)
  63. proofB := arrayOfZeroesG2(numcpu)
  64. proofC := arrayOfZeroesG1(numcpu)
  65. proofBG1 := arrayOfZeroesG1(numcpu)
  66. var wg1 sync.WaitGroup
  67. wg1.Add(numcpu)
  68. for _cpu, _ranges := range ranges(pk.NVars, numcpu) {
  69. // split 1
  70. go func(cpu int, ranges [2]int) {
  71. tmpG1 := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  72. tmpG2 := new(bn256.G2).ScalarBaseMult(big.NewInt(0))
  73. for i := ranges[0]; i < ranges[1]; i++ {
  74. proofA[cpu].Add(proofA[cpu], tmpG1.ScalarMult(pk.A[i], w[i]))
  75. proofB[cpu].Add(proofB[cpu], tmpG2.ScalarMult(pk.B2[i], w[i]))
  76. proofBG1[cpu].Add(proofBG1[cpu], tmpG1.ScalarMult(pk.B1[i], w[i]))
  77. if i >= pk.NPublic+1 {
  78. proofC[cpu].Add(proofC[cpu], tmpG1.ScalarMult(pk.C[i], w[i]))
  79. }
  80. }
  81. wg1.Done()
  82. }(_cpu, _ranges)
  83. }
  84. wg1.Wait()
  85. // join 1
  86. for cpu := 1; cpu < numcpu; cpu++ {
  87. proofA[0].Add(proofA[0], proofA[cpu])
  88. proofB[0].Add(proofB[0], proofB[cpu])
  89. proofC[0].Add(proofC[0], proofC[cpu])
  90. proofBG1[0].Add(proofBG1[0], proofBG1[cpu])
  91. }
  92. proof.A = proofA[0]
  93. proof.B = proofB[0]
  94. proof.C = proofC[0]
  95. h := calculateH(pk, w)
  96. println("len(h)", len(h))
  97. proof.A.Add(proof.A, pk.VkAlpha1)
  98. proof.A.Add(proof.A, new(bn256.G1).ScalarMult(pk.VkDelta1, r))
  99. proof.B.Add(proof.B, pk.VkBeta2)
  100. proof.B.Add(proof.B, new(bn256.G2).ScalarMult(pk.VkDelta2, s))
  101. proofBG1[0].Add(proofBG1[0], pk.VkBeta1)
  102. proofBG1[0].Add(proofBG1[0], new(bn256.G1).ScalarMult(pk.VkDelta1, s))
  103. proofC = arrayOfZeroesG1(numcpu)
  104. var wg2 sync.WaitGroup
  105. wg2.Add(numcpu)
  106. for _cpu, _ranges := range ranges(len(h), numcpu) {
  107. // split 2
  108. go func(cpu int, ranges [2]int) {
  109. tmpG1 := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  110. for i := ranges[0]; i < ranges[1]; i++ {
  111. proofC[cpu].Add(proofC[cpu], tmpG1.ScalarMult(pk.HExps[i], h[i]))
  112. }
  113. wg2.Done()
  114. }(_cpu, _ranges)
  115. }
  116. wg2.Wait()
  117. // join 2
  118. for cpu := 1; cpu < numcpu; cpu++ {
  119. proofC[0].Add(proofC[0], proofC[cpu])
  120. }
  121. proof.C.Add(proof.C, proofC[0])
  122. proof.C.Add(proof.C, new(bn256.G1).ScalarMult(proof.A, s))
  123. proof.C.Add(proof.C, new(bn256.G1).ScalarMult(proofBG1[0], r))
  124. rsneg := new(big.Int).Mod(new(big.Int).Neg(new(big.Int).Mul(r, s)), types.R) // fAdd & fMul
  125. proof.C.Add(proof.C, new(bn256.G1).ScalarMult(pk.VkDelta1, rsneg))
  126. pubSignals := w[1 : pk.NPublic+1]
  127. return &proof, pubSignals, nil
  128. }
  129. func calculateH(pk *types.Pk, w types.Witness) []*big.Int {
  130. m := pk.DomainSize
  131. polAT := arrayOfZeroes(m)
  132. polBT := arrayOfZeroes(m)
  133. for i := 0; i < pk.NVars; i++ {
  134. for j := range pk.PolsA[i] {
  135. polAT[j] = fAdd(polAT[j], fMul(w[i], pk.PolsA[i][j]))
  136. }
  137. for j := range pk.PolsB[i] {
  138. polBT[j] = fAdd(polBT[j], fMul(w[i], pk.PolsB[i][j]))
  139. }
  140. }
  141. polATe := utils.BigIntArrayToElementArray(polAT)
  142. polBTe := utils.BigIntArrayToElementArray(polBT)
  143. polASe := ifft(polATe)
  144. polBSe := ifft(polBTe)
  145. r := int(math.Log2(float64(m))) + 1
  146. roots := newRootsT()
  147. roots.setRoots(r)
  148. for i := 0; i < len(polASe); i++ {
  149. polASe[i].Mul(polASe[i], roots.roots[r][i])
  150. polBSe[i].Mul(polBSe[i], roots.roots[r][i])
  151. }
  152. polATodd := fft(polASe)
  153. polBTodd := fft(polBSe)
  154. polABT := arrayOfZeroesE(len(polASe) * 2)
  155. for i := 0; i < len(polASe); i++ {
  156. polABT[2*i].Mul(polATe[i], polBTe[i])
  157. polABT[2*i+1].Mul(polATodd[i], polBTodd[i])
  158. }
  159. hSeFull := ifft(polABT)
  160. hSe := hSeFull[m:]
  161. return utils.ElementArrayToBigIntArray(hSe)
  162. }
  163. func ranges(n, parts int) [][2]int {
  164. s := make([][2]int, parts)
  165. p := float64(n) / float64(parts)
  166. for i := 0; i < parts; i++ {
  167. a, b := int(float64(i)*p), int(float64(i+1)*p)
  168. s[i] = [2]int{a, b}
  169. }
  170. return s
  171. }