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.

138 lines
3.6 KiB

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