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.

134 lines
3.6 KiB

  1. package gocircomprover
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  6. )
  7. // Proof is the data structure of the Groth16 zkSNARK proof
  8. type Proof struct {
  9. A *bn256.G1
  10. B *bn256.G2
  11. C *bn256.G1
  12. }
  13. // ProvingKey holds the data structure of the provingKey
  14. type ProvingKey struct {
  15. A []*bn256.G1
  16. B2 []*bn256.G2
  17. B1 []*bn256.G1
  18. C []*bn256.G1
  19. NVars int
  20. NPublic int
  21. VkAlpha1 *bn256.G1
  22. VkDelta1 *bn256.G1
  23. VkBeta1 *bn256.G1
  24. VkBeta2 *bn256.G2
  25. VkDelta2 *bn256.G2
  26. HExps []*bn256.G1
  27. DomainSize int
  28. PolsA []map[int]*big.Int
  29. PolsB []map[int]*big.Int
  30. PolsC []map[int]*big.Int
  31. }
  32. // Witness contains the witness
  33. type Witness []*big.Int
  34. // R is the mod of the finite field
  35. var R, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  36. func randBigInt() (*big.Int, error) {
  37. maxbits := R.BitLen()
  38. b := make([]byte, (maxbits/8)-1)
  39. _, err := rand.Read(b)
  40. if err != nil {
  41. return nil, err
  42. }
  43. r := new(big.Int).SetBytes(b)
  44. rq := new(big.Int).Mod(r, R)
  45. return rq, nil
  46. }
  47. // GenerateProof generates the Groth16 zkSNARK proof
  48. func GenerateProof(pk *ProvingKey, w Witness) (*Proof, []*big.Int, error) {
  49. var proof Proof
  50. r, err := randBigInt()
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. s, err := randBigInt()
  55. if err != nil {
  56. return nil, nil, err
  57. }
  58. proof.A = new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  59. proof.B = new(bn256.G2).ScalarBaseMult(big.NewInt(0))
  60. proof.C = new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  61. proofBG1 := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  62. for i := 0; i < pk.NVars; i++ {
  63. proof.A = new(bn256.G1).Add(proof.A, new(bn256.G1).ScalarMult(pk.A[i], w[i]))
  64. proof.B = new(bn256.G2).Add(proof.B, new(bn256.G2).ScalarMult(pk.B2[i], w[i]))
  65. proofBG1 = new(bn256.G1).Add(proofBG1, new(bn256.G1).ScalarMult(pk.B1[i], w[i]))
  66. }
  67. for i := pk.NPublic + 1; i < pk.NVars; i++ {
  68. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.C[i], w[i]))
  69. }
  70. proof.A = new(bn256.G1).Add(proof.A, pk.VkAlpha1)
  71. proof.A = new(bn256.G1).Add(proof.A, new(bn256.G1).ScalarMult(pk.VkDelta1, r))
  72. proof.B = new(bn256.G2).Add(proof.B, pk.VkBeta2)
  73. proof.B = new(bn256.G2).Add(proof.B, new(bn256.G2).ScalarMult(pk.VkDelta2, s))
  74. proofBG1 = new(bn256.G1).Add(proofBG1, pk.VkBeta1)
  75. proofBG1 = new(bn256.G1).Add(proofBG1, new(bn256.G1).ScalarMult(pk.VkDelta1, s))
  76. h := calculateH(pk, w)
  77. for i := 0; i < len(h); i++ {
  78. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.HExps[i], h[i]))
  79. }
  80. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(proof.A, s))
  81. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(proofBG1, r))
  82. rsneg := new(big.Int).Mod(new(big.Int).Neg(new(big.Int).Mul(r, s)), R) // fAdd & fMul
  83. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.VkDelta1, rsneg))
  84. pubSignals := w[1 : pk.NPublic+1]
  85. return &proof, pubSignals, nil
  86. }
  87. func calculateH(pk *ProvingKey, w Witness) []*big.Int {
  88. m := pk.DomainSize
  89. polAT := arrayOfZeroes(m)
  90. polBT := arrayOfZeroes(m)
  91. polCT := arrayOfZeroes(m)
  92. for i := 0; i < pk.NVars; i++ {
  93. for j := range pk.PolsA[i] {
  94. polAT[j] = fAdd(polAT[j], fMul(w[i], pk.PolsA[i][j]))
  95. }
  96. for j := range pk.PolsB[i] {
  97. polBT[j] = fAdd(polBT[j], fMul(w[i], pk.PolsB[i][j]))
  98. }
  99. for j := range pk.PolsC[i] {
  100. polCT[j] = fAdd(polCT[j], fMul(w[i], pk.PolsC[i][j]))
  101. }
  102. }
  103. polAS := ifft(polAT)
  104. polBS := ifft(polBT)
  105. polABS := polynomialMul(polAS, polBS)
  106. polCS := ifft(polCT)
  107. polABCS := polynomialSub(polABS, polCS)
  108. hS := polABCS[m:]
  109. return hS
  110. }