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.

87 lines
2.2 KiB

  1. package snark
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. "github.com/arnaucube/go-snark/bn128"
  7. "github.com/arnaucube/go-snark/circuitcompiler"
  8. "github.com/arnaucube/go-snark/fields"
  9. "github.com/arnaucube/go-snark/r1csqap"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestZk(t *testing.T) {
  13. bn, err := bn128.NewBn128()
  14. assert.Nil(t, err)
  15. // new Finite Field
  16. fqR := fields.NewFq(bn.R)
  17. // new Polynomial Field
  18. pf := r1csqap.NewPolynomialField(fqR)
  19. b0 := big.NewInt(int64(0))
  20. b1 := big.NewInt(int64(1))
  21. b3 := big.NewInt(int64(3))
  22. b5 := big.NewInt(int64(5))
  23. b9 := big.NewInt(int64(9))
  24. b27 := big.NewInt(int64(27))
  25. b30 := big.NewInt(int64(30))
  26. b35 := big.NewInt(int64(35))
  27. a := [][]*big.Int{
  28. []*big.Int{b0, b1, b0, b0, b0, b0},
  29. []*big.Int{b0, b0, b0, b1, b0, b0},
  30. []*big.Int{b0, b1, b0, b0, b1, b0},
  31. []*big.Int{b5, b0, b0, b0, b0, b1},
  32. }
  33. b := [][]*big.Int{
  34. []*big.Int{b0, b1, b0, b0, b0, b0},
  35. []*big.Int{b0, b1, b0, b0, b0, b0},
  36. []*big.Int{b1, b0, b0, b0, b0, b0},
  37. []*big.Int{b1, b0, b0, b0, b0, b0},
  38. }
  39. c := [][]*big.Int{
  40. []*big.Int{b0, b0, b0, b1, b0, b0},
  41. []*big.Int{b0, b0, b0, b0, b1, b0},
  42. []*big.Int{b0, b0, b0, b0, b0, b1},
  43. []*big.Int{b0, b0, b1, b0, b0, b0},
  44. }
  45. alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
  46. // wittness = 1, 3, 35, 9, 27, 30
  47. w := []*big.Int{b1, b3, b35, b9, b27, b30}
  48. circuit := circuitcompiler.Circuit{
  49. NVars: 6,
  50. NPublic: 0,
  51. NSignals: len(w),
  52. }
  53. ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
  54. hx := pf.DivisorPolinomial(px, zx)
  55. // hx==px/zx so px==hx*zx
  56. assert.Equal(t, px, pf.Mul(hx, zx))
  57. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  58. abc := pf.Sub(pf.Mul(ax, bx), cx)
  59. assert.Equal(t, abc, px)
  60. hz := pf.Mul(hx, zx)
  61. assert.Equal(t, abc, hz)
  62. div, rem := pf.Div(px, zx)
  63. assert.Equal(t, hx, div)
  64. assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
  65. // calculate trusted setup
  66. setup, err := GenerateTrustedSetup(bn, fqR, pf, len(w), circuit, alphas, betas, gammas, zx)
  67. assert.Nil(t, err)
  68. fmt.Println("t", setup.Toxic.T)
  69. // piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  70. proof, err := GenerateProofs(bn, fqR, circuit, setup, hx, w)
  71. assert.Nil(t, err)
  72. assert.True(t, VerifyProof(bn, circuit, setup, proof))
  73. }