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.

102 lines
2.8 KiB

  1. package kzg
  2. import (
  3. "fmt"
  4. "math/big"
  5. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  6. )
  7. // TrustedSetup also named Reference String
  8. type TrustedSetup struct {
  9. Tau1 []*bn256.G1
  10. Tau2 []*bn256.G2
  11. }
  12. // NewTrustedSetup returns a new trusted setup. This step should be done in a
  13. // secure & distributed way
  14. func NewTrustedSetup(p []*big.Int) (*TrustedSetup, error) {
  15. // compute random s
  16. s, err := randBigInt()
  17. if err != nil {
  18. return nil, err
  19. }
  20. // Notation: [x]₁=xG ∈ 𝔾₁, [x]₂=xH ∈ 𝔾₂
  21. // τ₁: [x₀]₁, [x₁]₁, [x₂]₁, ..., [x n₋₁]₁
  22. // τ₂: [x₀]₂, [x₁]₂, [x₂]₂, ..., [x n₋₁]₂
  23. // sPow := make([]*big.Int, len(p))
  24. tauG1 := make([]*bn256.G1, len(p))
  25. tauG2 := make([]*bn256.G2, len(p))
  26. for i := 0; i < len(p); i++ {
  27. sPow := fExp(s, big.NewInt(int64(i)))
  28. tauG1[i] = new(bn256.G1).ScalarBaseMult(sPow)
  29. tauG2[i] = new(bn256.G2).ScalarBaseMult(sPow)
  30. }
  31. return &TrustedSetup{tauG1, tauG2}, nil
  32. }
  33. // Commit generates the commitment to the polynomial p(x)
  34. func Commit(ts *TrustedSetup, p []*big.Int) *bn256.G1 {
  35. c := evaluateG1(ts, p)
  36. return c
  37. }
  38. func evaluateG1(ts *TrustedSetup, p []*big.Int) *bn256.G1 {
  39. c := new(bn256.G1).ScalarMult(ts.Tau1[0], p[0])
  40. for i := 1; i < len(p); i++ {
  41. sp := new(bn256.G1).ScalarMult(ts.Tau1[i], p[i])
  42. c = new(bn256.G1).Add(c, sp)
  43. }
  44. return c
  45. }
  46. //nolint:deadcode,unused
  47. func evaluateG2(ts *TrustedSetup, p []*big.Int) *bn256.G2 {
  48. c := new(bn256.G2).ScalarMult(ts.Tau2[0], p[0])
  49. for i := 1; i < len(p); i++ {
  50. sp := new(bn256.G2).ScalarMult(ts.Tau2[i], p[i])
  51. c = new(bn256.G2).Add(c, sp)
  52. }
  53. return c
  54. }
  55. // EvaluationProof generates the evaluation proof
  56. func EvaluationProof(ts *TrustedSetup, p []*big.Int, z, y *big.Int) (*bn256.G1, error) {
  57. n := polynomialSub(p, []*big.Int{y}) // p-y
  58. // n := p // we can omit y (p(z))
  59. d := []*big.Int{fNeg(z), big.NewInt(1)} // x-z
  60. q, rem := polynomialDiv(n, d)
  61. if compareBigIntArray(rem, arrayOfZeroes(len(rem))) {
  62. return nil,
  63. fmt.Errorf("remainder should be 0, instead is %d", rem)
  64. }
  65. fmt.Println("q(x):", PolynomialToString(q)) // TMP DBG
  66. // proof: e = [q(s)]₁
  67. e := evaluateG1(ts, q)
  68. return e, nil
  69. }
  70. // Verify computes the KZG commitment verification
  71. func Verify(ts *TrustedSetup, c, proof *bn256.G1, z, y *big.Int) bool {
  72. s2 := ts.Tau2[1] // [s]₂ = sG ∈ 𝔾₂ = Tau2[1]
  73. zG2Neg := new(bn256.G2).Neg(
  74. new(bn256.G2).ScalarBaseMult(z)) // [z]₂ = zG ∈ 𝔾₂
  75. // [s]₂ - [z]₂
  76. sz := new(bn256.G2).Add(s2, zG2Neg)
  77. yG1Neg := new(bn256.G1).Neg(
  78. new(bn256.G1).ScalarBaseMult(y)) // [y]₁ = yG ∈ 𝔾₁
  79. // c - [y]₁
  80. cy := new(bn256.G1).Add(c, yG1Neg)
  81. h := new(bn256.G2).ScalarBaseMult(big.NewInt(1)) // H ∈ 𝔾₂
  82. // e(proof, [s]₂ - [z]₂) == e(c - [y]₁, H)
  83. e1 := bn256.Pair(proof, sz)
  84. e2 := bn256.Pair(cy, h)
  85. return e1.String() == e2.String()
  86. }