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.

133 lines
3.4 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. type Proof struct {
  9. A *bn256.G1
  10. B *bn256.G2
  11. C *bn256.G1
  12. }
  13. type ProvingKey struct {
  14. A []*bn256.G1
  15. B2 []*bn256.G2
  16. B1 []*bn256.G1
  17. C []*bn256.G1
  18. NVars int
  19. NPublic int
  20. VkAlpha1 *bn256.G1
  21. VkDelta1 *bn256.G1
  22. VkBeta1 *bn256.G1
  23. VkBeta2 *bn256.G2
  24. VkDelta2 *bn256.G2
  25. HExps []*bn256.G1
  26. DomainSize int
  27. PolsA []map[int]*big.Int
  28. PolsB []map[int]*big.Int
  29. PolsC []map[int]*big.Int
  30. }
  31. type Witness []*big.Int
  32. var R, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  33. func RandBigInt() (*big.Int, error) {
  34. maxbits := R.BitLen()
  35. b := make([]byte, (maxbits/8)-1)
  36. _, err := rand.Read(b)
  37. if err != nil {
  38. return nil, err
  39. }
  40. r := new(big.Int).SetBytes(b)
  41. rq := new(big.Int).Mod(r, R)
  42. return rq, nil
  43. }
  44. func Prove(pk *ProvingKey, w Witness) (*Proof, []*big.Int, error) {
  45. var proof Proof
  46. r, err := RandBigInt()
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. s, err := RandBigInt()
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. proof.A = new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  55. proof.B = new(bn256.G2).ScalarBaseMult(big.NewInt(0))
  56. proof.C = new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  57. proofBG1 := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  58. for i := 0; i < pk.NVars; i++ {
  59. proof.A = new(bn256.G1).Add(proof.A, new(bn256.G1).ScalarMult(pk.A[i], w[i]))
  60. proof.B = new(bn256.G2).Add(proof.B, new(bn256.G2).ScalarMult(pk.B2[i], w[i]))
  61. proofBG1 = new(bn256.G1).Add(proofBG1, new(bn256.G1).ScalarMult(pk.B1[i], w[i]))
  62. }
  63. for i := pk.NPublic + 1; i < pk.NVars; i++ {
  64. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.C[i], w[i]))
  65. }
  66. proof.A = new(bn256.G1).Add(proof.A, pk.VkAlpha1)
  67. proof.A = new(bn256.G1).Add(proof.A, new(bn256.G1).ScalarMult(pk.VkDelta1, r))
  68. proof.B = new(bn256.G2).Add(proof.B, pk.VkBeta2)
  69. proof.B = new(bn256.G2).Add(proof.B, new(bn256.G2).ScalarMult(pk.VkDelta2, s))
  70. proofBG1 = new(bn256.G1).Add(proofBG1, pk.VkBeta1)
  71. proofBG1 = new(bn256.G1).Add(proofBG1, new(bn256.G1).ScalarMult(pk.VkDelta1, s))
  72. // TODO
  73. // h := calculateH(pk, w)
  74. h := []*big.Int{} // TMP
  75. for i := 0; i < len(h); i++ {
  76. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.HExps[i], h[i]))
  77. }
  78. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(proof.A, s))
  79. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(proofBG1, r))
  80. rsneg := new(big.Int).Mod(new(big.Int).Neg(new(big.Int).Mul(r, s)), R) // FAdd & FMul
  81. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.VkDelta1, rsneg))
  82. pubSignals := w[1 : pk.NPublic+1]
  83. return &proof, pubSignals, nil
  84. }
  85. func calculateH(pk *ProvingKey, w Witness) []*big.Int {
  86. m := pk.DomainSize
  87. polAT := arrayOfZeroes(m)
  88. polBT := arrayOfZeroes(m)
  89. polCT := arrayOfZeroes(m)
  90. for i := 0; i < pk.NVars; i++ {
  91. for j, _ := range pk.PolsA[i] {
  92. polAT[j] = FAdd(polAT[j], FMul(w[i], pk.PolsA[i][j]))
  93. fmt.Println(polAT[j])
  94. }
  95. for j, _ := range pk.PolsB[i] {
  96. polBT[j] = FAdd(polBT[j], FMul(w[i], pk.PolsB[i][j]))
  97. }
  98. for j, _ := range pk.PolsC[i] {
  99. polCT[j] = FAdd(polCT[j], FMul(w[i], pk.PolsC[i][j]))
  100. }
  101. }
  102. polAS := ifft(polAT)
  103. polBS := ifft(polBT)
  104. polABS := PolynomialMul(polAS, polBS)
  105. polCS := ifft(polCT)
  106. polABCS := PolynomialSub(polABS, polCS)
  107. hS := polABCS[m:]
  108. return hS
  109. }