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.

118 lines
3.3 KiB

  1. package snark
  2. import (
  3. "fmt"
  4. "github.com/mottla/go-snark/circuitcompiler"
  5. "github.com/stretchr/testify/assert"
  6. "math/big"
  7. "strings"
  8. "testing"
  9. )
  10. func TestNewProgramm(t *testing.T) {
  11. flat := `
  12. func main(a,b,c,d):
  13. e = a + b
  14. f = c * d
  15. out = e * f
  16. `
  17. parser := circuitcompiler.NewParser(strings.NewReader(flat))
  18. program, err := parser.Parse()
  19. if err != nil {
  20. panic(err)
  21. }
  22. fmt.Println("\n unreduced")
  23. fmt.Println(flat)
  24. program.BuildConstraintTrees()
  25. program.PrintContraintTrees()
  26. fmt.Println("\nReduced gates")
  27. //PrintTree(froots["mul"])
  28. gates := program.ReduceCombinedTree()
  29. for _, g := range gates {
  30. fmt.Println(g)
  31. }
  32. fmt.Println("generating R1CS")
  33. a, b, c := program.GenerateReducedR1CS(gates)
  34. fmt.Println(a)
  35. fmt.Println(b)
  36. fmt.Println(c)
  37. a1 := big.NewInt(int64(6))
  38. a2 := big.NewInt(int64(5))
  39. inputs := []*big.Int{a1, a2, a1, a2}
  40. w := program.CalculateWitness(inputs)
  41. fmt.Println("witness")
  42. fmt.Println(w)
  43. // R1CS to QAP
  44. alphas, betas, gammas, zxQAP := Utils.PF.R1CSToQAP(a, b, c)
  45. fmt.Println("qap")
  46. fmt.Println("alphas", len(alphas))
  47. fmt.Println("alphas", alphas)
  48. fmt.Println("betas", len(betas))
  49. fmt.Println("gammas", len(gammas))
  50. fmt.Println("zx length", len(zxQAP))
  51. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  52. fmt.Println("ax length", len(ax))
  53. fmt.Println("bx length", len(bx))
  54. fmt.Println("cx length", len(cx))
  55. fmt.Println("px length", len(px))
  56. hxQAP := Utils.PF.DivisorPolynomial(px, zxQAP)
  57. fmt.Println("hx length", len(hxQAP))
  58. // hx==px/zx so px==hx*zx
  59. assert.Equal(t, px, Utils.PF.Mul(hxQAP, zxQAP))
  60. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  61. abc := Utils.PF.Sub(Utils.PF.Mul(ax, bx), cx)
  62. assert.Equal(t, abc, px)
  63. hzQAP := Utils.PF.Mul(hxQAP, zxQAP)
  64. assert.Equal(t, abc, hzQAP)
  65. //div, rem := Utils.PF.Div(px, zxQAP)
  66. //assert.Equal(t, hxQAP, div)
  67. //assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
  68. // calculate trusted setup
  69. //setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  70. //assert.Nil(t, err)
  71. //fmt.Println("\nt:", setup.Toxic.T)
  72. //
  73. //// zx and setup.Pk.Z should be the same (currently not, the correct one is the calculation used inside GenerateTrustedSetup function), the calculation is repeated. TODO avoid repeating calculation
  74. //// assert.Equal(t, zxQAP, setup.Pk.Z)
  75. //
  76. //fmt.Println("hx pk.z", hxQAP)
  77. //hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  78. //fmt.Println("hx pk.z", hx)
  79. //// assert.Equal(t, hxQAP, hx)
  80. //assert.Equal(t, px, Utils.PF.Mul(hxQAP, zxQAP))
  81. //assert.Equal(t, px, Utils.PF.Mul(hx, setup.Pk.Z))
  82. //
  83. //assert.Equal(t, len(hx), len(px)-len(setup.Pk.Z)+1)
  84. //assert.Equal(t, len(hxQAP), len(px)-len(zxQAP)+1)
  85. //// fmt.Println("pk.Z", len(setup.Pk.Z))
  86. //// fmt.Println("zxQAP", len(zxQAP))
  87. //
  88. //// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  89. //proof, err := GenerateProofs(*circuit, setup, w, px)
  90. //assert.Nil(t, err)
  91. //
  92. //// fmt.Println("\n proofs:")
  93. //// fmt.Println(proof)
  94. //
  95. //// fmt.Println("public signals:", proof.PublicSignals)
  96. //fmt.Println("\nwitness", w)
  97. //// b1 := big.NewInt(int64(1))
  98. //b35 := big.NewInt(int64(35))
  99. //// publicSignals := []*big.Int{b1, b35}
  100. //publicSignals := []*big.Int{b35}
  101. //before := time.Now()
  102. //assert.True(t, VerifyProof(*circuit, setup, proof, publicSignals, true))
  103. //fmt.Println("verify proof time elapsed:", time.Since(before))
  104. }