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.

103 lines
2.7 KiB

  1. package gocircomprover
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  6. )
  7. type Proof struct {
  8. A *bn256.G1
  9. B *bn256.G2
  10. C *bn256.G1
  11. }
  12. type ProvingKey struct {
  13. A []*bn256.G1
  14. B2 []*bn256.G2
  15. B1 []*bn256.G1
  16. C []*bn256.G1
  17. NVars int
  18. NPublic int
  19. VkAlpha1 *bn256.G1
  20. VkDelta1 *bn256.G1
  21. VkBeta1 *bn256.G1
  22. VkBeta2 *bn256.G2
  23. VkDelta2 *bn256.G2
  24. HExps []*bn256.G1
  25. DomainSize int
  26. PolsA []map[int]*big.Int
  27. PolsB []map[int]*big.Int
  28. PolsC []map[int]*big.Int
  29. }
  30. type Witness []*big.Int
  31. var R, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  32. func RandBigInt() (*big.Int, error) {
  33. maxbits := R.BitLen()
  34. b := make([]byte, (maxbits/8)-1)
  35. _, err := rand.Read(b)
  36. if err != nil {
  37. return nil, err
  38. }
  39. r := new(big.Int).SetBytes(b)
  40. rq := new(big.Int).Mod(r, R)
  41. return rq, nil
  42. }
  43. func Prove(pk *ProvingKey, w Witness) (*Proof, []*big.Int, error) {
  44. var proof Proof
  45. r, err := RandBigInt()
  46. if err != nil {
  47. return nil, nil, err
  48. }
  49. s, err := RandBigInt()
  50. if err != nil {
  51. return nil, nil, err
  52. }
  53. proof.A = new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  54. proof.B = new(bn256.G2).ScalarBaseMult(big.NewInt(0))
  55. proof.C = new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  56. proofBG1 := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  57. for i := 0; i < pk.NVars; i++ {
  58. proof.A = new(bn256.G1).Add(proof.A, new(bn256.G1).ScalarMult(pk.A[i], w[i]))
  59. proof.B = new(bn256.G2).Add(proof.B, new(bn256.G2).ScalarMult(pk.B2[i], w[i]))
  60. proofBG1 = new(bn256.G1).Add(proofBG1, new(bn256.G1).ScalarMult(pk.B1[i], w[i]))
  61. }
  62. for i := pk.NPublic + 1; i < pk.NVars; i++ {
  63. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.C[i], w[i]))
  64. }
  65. proof.A = new(bn256.G1).Add(proof.A, pk.VkAlpha1)
  66. proof.A = new(bn256.G1).Add(proof.A, new(bn256.G1).ScalarMult(pk.VkDelta1, r))
  67. proof.B = new(bn256.G2).Add(proof.B, pk.VkBeta2)
  68. proof.B = new(bn256.G2).Add(proof.B, new(bn256.G2).ScalarMult(pk.VkDelta2, s))
  69. proofBG1 = new(bn256.G1).Add(proofBG1, pk.VkBeta1)
  70. proofBG1 = new(bn256.G1).Add(proofBG1, new(bn256.G1).ScalarMult(pk.VkDelta1, s))
  71. // TODO
  72. // h := calculateH(pk, w)
  73. h := []*big.Int{} // TMP
  74. for i := 0; i < len(h); i++ {
  75. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.HExps[i], h[i]))
  76. }
  77. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(proof.A, s))
  78. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(proofBG1, r))
  79. rsneg := new(big.Int).Mod(new(big.Int).Neg(new(big.Int).Mul(r, s)), R) // FAdd & FMul
  80. proof.C = new(bn256.G1).Add(proof.C, new(bn256.G1).ScalarMult(pk.VkDelta1, rsneg))
  81. pubSignals := w[1 : pk.NPublic+1]
  82. return &proof, pubSignals, nil
  83. }