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.

249 lines
5.6 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. //"fmt"
  12. )
  13. // Proof is the data structure of the Groth16 zkSNARK proof
  14. type Proof struct {
  15. A *bn256.G1
  16. B *bn256.G2
  17. C *bn256.G1
  18. }
  19. // Pk holds the data structure of the ProvingKey
  20. type Pk struct {
  21. A []*bn256.G1
  22. B2 []*bn256.G2
  23. B1 []*bn256.G1
  24. C []*bn256.G1
  25. NVars int
  26. NPublic int
  27. VkAlpha1 *bn256.G1
  28. VkDelta1 *bn256.G1
  29. VkBeta1 *bn256.G1
  30. VkBeta2 *bn256.G2
  31. VkDelta2 *bn256.G2
  32. HExps []*bn256.G1
  33. DomainSize int
  34. PolsA []map[int]*big.Int
  35. PolsB []map[int]*big.Int
  36. PolsC []map[int]*big.Int
  37. }
  38. // Witness contains the witness
  39. type Witness []*big.Int
  40. // Group Size
  41. const (
  42. GSIZE = 6
  43. )
  44. func randBigInt() (*big.Int, error) {
  45. maxbits := types.R.BitLen()
  46. b := make([]byte, (maxbits/8)-1)
  47. _, err := rand.Read(b)
  48. if err != nil {
  49. return nil, err
  50. }
  51. r := new(big.Int).SetBytes(b)
  52. rq := new(big.Int).Mod(r, types.R)
  53. return rq, nil
  54. }
  55. // GenerateProof generates the Groth16 zkSNARK proof
  56. func GenerateProof(pk *types.Pk, w types.Witness) (*types.Proof, []*big.Int, error) {
  57. var proof types.Proof
  58. r, err := randBigInt()
  59. if err != nil {
  60. return nil, nil, err
  61. }
  62. s, err := randBigInt()
  63. if err != nil {
  64. return nil, nil, err
  65. }
  66. // BEGIN PAR
  67. numcpu := runtime.NumCPU()
  68. proofA := arrayOfZeroesG1(numcpu)
  69. proofB := arrayOfZeroesG2(numcpu)
  70. proofC := arrayOfZeroesG1(numcpu)
  71. proofBG1 := arrayOfZeroesG1(numcpu)
  72. gsize := GSIZE
  73. var wg1 sync.WaitGroup
  74. wg1.Add(numcpu)
  75. for _cpu, _ranges := range ranges(pk.NVars, numcpu) {
  76. // split 1
  77. go func(cpu int, ranges [2]int) {
  78. proofA[cpu] = scalarMultNoDoubleG1(pk.A[ranges[0]:ranges[1]],
  79. w[ranges[0]:ranges[1]],
  80. proofA[cpu],
  81. gsize)
  82. proofB[cpu] = scalarMultNoDoubleG2(pk.B2[ranges[0]:ranges[1]],
  83. w[ranges[0]:ranges[1]],
  84. proofB[cpu],
  85. gsize)
  86. proofBG1[cpu] = scalarMultNoDoubleG1(pk.B1[ranges[0]:ranges[1]],
  87. w[ranges[0]:ranges[1]],
  88. proofBG1[cpu],
  89. gsize)
  90. minLim := pk.NPublic + 1
  91. if ranges[0] > pk.NPublic+1 {
  92. minLim = ranges[0]
  93. }
  94. if ranges[1] > pk.NPublic+1 {
  95. proofC[cpu] = scalarMultNoDoubleG1(pk.C[minLim:ranges[1]],
  96. w[minLim:ranges[1]],
  97. proofC[cpu],
  98. gsize)
  99. }
  100. wg1.Done()
  101. }(_cpu, _ranges)
  102. }
  103. wg1.Wait()
  104. // join 1
  105. for cpu := 1; cpu < numcpu; cpu++ {
  106. proofA[0].Add(proofA[0], proofA[cpu])
  107. proofB[0].Add(proofB[0], proofB[cpu])
  108. proofC[0].Add(proofC[0], proofC[cpu])
  109. proofBG1[0].Add(proofBG1[0], proofBG1[cpu])
  110. }
  111. proof.A = proofA[0]
  112. proof.B = proofB[0]
  113. proof.C = proofC[0]
  114. // END PAR
  115. h := calculateH(pk, w)
  116. proof.A.Add(proof.A, pk.VkAlpha1)
  117. proof.A.Add(proof.A, new(bn256.G1).ScalarMult(pk.VkDelta1, r))
  118. proof.B.Add(proof.B, pk.VkBeta2)
  119. proof.B.Add(proof.B, new(bn256.G2).ScalarMult(pk.VkDelta2, s))
  120. proofBG1[0].Add(proofBG1[0], pk.VkBeta1)
  121. proofBG1[0].Add(proofBG1[0], new(bn256.G1).ScalarMult(pk.VkDelta1, s))
  122. proofC = arrayOfZeroesG1(numcpu)
  123. var wg2 sync.WaitGroup
  124. wg2.Add(numcpu)
  125. for _cpu, _ranges := range ranges(len(h), numcpu) {
  126. // split 2
  127. go func(cpu int, ranges [2]int) {
  128. proofC[cpu] = scalarMultNoDoubleG1(pk.HExps[ranges[0]:ranges[1]],
  129. h[ranges[0]:ranges[1]],
  130. proofC[cpu],
  131. gsize)
  132. wg2.Done()
  133. }(_cpu, _ranges)
  134. }
  135. wg2.Wait()
  136. // join 2
  137. for cpu := 1; cpu < numcpu; cpu++ {
  138. proofC[0].Add(proofC[0], proofC[cpu])
  139. }
  140. proof.C.Add(proof.C, proofC[0])
  141. proof.C.Add(proof.C, new(bn256.G1).ScalarMult(proof.A, s))
  142. proof.C.Add(proof.C, new(bn256.G1).ScalarMult(proofBG1[0], r))
  143. rsneg := new(big.Int).Mod(new(big.Int).Neg(new(big.Int).Mul(r, s)), types.R)
  144. proof.C.Add(proof.C, new(bn256.G1).ScalarMult(pk.VkDelta1, rsneg))
  145. pubSignals := w[1 : pk.NPublic+1]
  146. return &proof, pubSignals, nil
  147. }
  148. func calculateH(pk *types.Pk, w types.Witness) []*big.Int {
  149. m := pk.DomainSize
  150. polAT := arrayOfZeroes(m)
  151. polBT := arrayOfZeroes(m)
  152. numcpu := runtime.NumCPU()
  153. var wg1 sync.WaitGroup
  154. wg1.Add(2)
  155. go func() {
  156. for i := 0; i < pk.NVars; i++ {
  157. for j := range pk.PolsA[i] {
  158. polAT[j] = fAdd(polAT[j], fMul(w[i], pk.PolsA[i][j]))
  159. }
  160. }
  161. wg1.Done()
  162. }()
  163. go func() {
  164. for i := 0; i < pk.NVars; i++ {
  165. for j := range pk.PolsB[i] {
  166. polBT[j] = fAdd(polBT[j], fMul(w[i], pk.PolsB[i][j]))
  167. }
  168. }
  169. wg1.Done()
  170. }()
  171. wg1.Wait()
  172. polATe := utils.BigIntArrayToElementArray(polAT)
  173. polBTe := utils.BigIntArrayToElementArray(polBT)
  174. polASe := ifft(polATe)
  175. polBSe := ifft(polBTe)
  176. r := int(math.Log2(float64(m))) + 1
  177. roots := newRootsT()
  178. roots.setRoots(r)
  179. var wg2 sync.WaitGroup
  180. wg2.Add(numcpu)
  181. for _cpu, _ranges := range ranges(len(polASe), numcpu) {
  182. go func(cpu int, ranges [2]int) {
  183. for i := ranges[0]; i < ranges[1]; i++ {
  184. polASe[i].Mul(polASe[i], roots.roots[r][i])
  185. polBSe[i].Mul(polBSe[i], roots.roots[r][i])
  186. }
  187. wg2.Done()
  188. }(_cpu, _ranges)
  189. }
  190. wg2.Wait()
  191. polATodd := fft(polASe)
  192. polBTodd := fft(polBSe)
  193. polABT := arrayOfZeroesE(len(polASe) * 2)
  194. var wg3 sync.WaitGroup
  195. wg3.Add(numcpu)
  196. for _cpu, _ranges := range ranges(len(polASe), numcpu) {
  197. go func(cpu int, ranges [2]int) {
  198. for i := ranges[0]; i < ranges[1]; i++ {
  199. polABT[2*i].Mul(polATe[i], polBTe[i])
  200. polABT[2*i+1].Mul(polATodd[i], polBTodd[i])
  201. }
  202. wg3.Done()
  203. }(_cpu, _ranges)
  204. }
  205. wg3.Wait()
  206. hSeFull := ifft(polABT)
  207. hSe := hSeFull[m:]
  208. return utils.ElementArrayToBigIntArray(hSe)
  209. }
  210. func ranges(n, parts int) [][2]int {
  211. s := make([][2]int, parts)
  212. p := float64(n) / float64(parts)
  213. for i := 0; i < parts; i++ {
  214. a, b := int(float64(i)*p), int(float64(i+1)*p)
  215. s[i] = [2]int{a, b}
  216. }
  217. return s
  218. }