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.

125 lines
3.4 KiB

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