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.

89 lines
2.1 KiB

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