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.

104 lines
2.3 KiB

  1. package kzg
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestSimpleFlow(t *testing.T) {
  8. // p(x) = x^3 + x + 5
  9. p := []*big.Int{
  10. big.NewInt(5),
  11. big.NewInt(1), // x^1
  12. big.NewInt(0), // x^2
  13. big.NewInt(1), // x^3
  14. }
  15. assert.Equal(t, "x³ + x¹ + 5", PolynomialToString(p))
  16. // TrustedSetup
  17. ts, err := NewTrustedSetup(len(p))
  18. assert.Nil(t, err)
  19. // Commit
  20. c := Commit(ts, p)
  21. // p(z)=y --> p(3)=35
  22. z := big.NewInt(3)
  23. y := big.NewInt(35)
  24. // z & y: to prove an evaluation p(z)=y
  25. proof, err := EvaluationProof(ts, p, z, y)
  26. assert.Nil(t, err)
  27. v := Verify(ts, c, proof, z, y)
  28. assert.True(t, v)
  29. v = Verify(ts, c, proof, big.NewInt(4), y)
  30. assert.False(t, v)
  31. }
  32. func TestBatchProof(t *testing.T) {
  33. // p(x) = x^3 + x + 5
  34. p := []*big.Int{
  35. big.NewInt(5),
  36. big.NewInt(1), // x^1
  37. big.NewInt(0), // x^2
  38. big.NewInt(1), // x^3
  39. big.NewInt(10), // x^4
  40. }
  41. assert.Equal(t, "10x⁴ + x³ + x¹ + 5", PolynomialToString(p))
  42. // TrustedSetup
  43. ts, err := NewTrustedSetup(len(p))
  44. assert.Nil(t, err)
  45. // Commit
  46. c := Commit(ts, p)
  47. // 1st point: p(z)=y --> p(3)=35
  48. z0 := big.NewInt(3)
  49. y0 := polynomialEval(p, z0)
  50. // 2nd point: p(10)=1015
  51. z1 := big.NewInt(10)
  52. y1 := polynomialEval(p, z1)
  53. // 3nd point: p(256)=16777477
  54. z2 := big.NewInt(256)
  55. y2 := polynomialEval(p, z2)
  56. zs := []*big.Int{z0, z1, z2}
  57. ys := []*big.Int{y0, y1, y2}
  58. // prove an evaluation of the multiple z_i & y_i
  59. proof, err := EvaluationBatchProof(ts, p, zs, ys)
  60. assert.Nil(t, err)
  61. // batch proof verification
  62. v := VerifyBatchProof(ts, c, proof, zs, ys)
  63. assert.True(t, v)
  64. // changing order of the points to be verified
  65. zs[0], zs[1], zs[2] = zs[1], zs[2], zs[0]
  66. ys[0], ys[1], ys[2] = ys[1], ys[2], ys[0]
  67. v = VerifyBatchProof(ts, c, proof, zs, ys)
  68. assert.True(t, v)
  69. // change a value of zs and check that verification fails
  70. zs[0] = big.NewInt(2)
  71. v = VerifyBatchProof(ts, c, proof, zs, ys)
  72. assert.False(t, v)
  73. // using a value that is not in the evaluation proof should generate a
  74. // proof that will not correctly be verified
  75. zs = []*big.Int{z0, z1, z2}
  76. ys = []*big.Int{y0, y1, y2}
  77. proof, err = EvaluationBatchProof(ts, p, zs, ys)
  78. assert.Nil(t, err)
  79. zs[2] = big.NewInt(2500)
  80. ys[2] = polynomialEval(p, zs[2])
  81. v = VerifyBatchProof(ts, c, proof, zs, ys)
  82. assert.False(t, v)
  83. }