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.9 KiB

  1. package snark
  2. import (
  3. "fmt"
  4. "github.com/arnaucube/go-snark/circuitcompiler"
  5. "github.com/arnaucube/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. def main(a,b,c,d):
  14. e = a * b
  15. f = c * d
  16. g = e * f
  17. h = g / e
  18. i = h * 5
  19. out = g * i
  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.PrintContraintTrees()
  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. r1cs := program.GenerateReducedR1CS(gates)
  38. //[[0 1 0 0 0 0 0 0 0 0] [0 0 0 1 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1 0 0]]
  39. //[[0 0 1 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 5 0]]
  40. //[[0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 0 0 1]]
  41. a1 := big.NewInt(int64(6))
  42. a2 := big.NewInt(int64(5))
  43. inputs := []*big.Int{a1, a2, a1, a2}
  44. w := circuitcompiler.CalculateWitness(inputs, r1cs)
  45. r1csReordered, wReordered := RelocateOutput(program.GlobalInputCount(), r1cs, w)
  46. a, b, c := r1csReordered.A, r1csReordered.B, r1csReordered.C
  47. fmt.Println(a)
  48. fmt.Println(b)
  49. fmt.Println(c)
  50. // R1CS to QAP
  51. alphas, betas, gammas, domain := Utils.PF.R1CSToQAP(a, b, c)
  52. fmt.Println("QAP array lengths")
  53. fmt.Println("alphas", len(alphas))
  54. fmt.Println("betas", len(betas))
  55. fmt.Println("gammas", len(gammas))
  56. fmt.Println("domain polynomial ", len(domain))
  57. ax, bx, cx, px := Utils.PF.CombinePolynomials(wReordered, alphas, betas, gammas)
  58. fmt.Println("ax length", len(ax))
  59. fmt.Println("bx length", len(bx))
  60. fmt.Println("cx length", len(cx))
  61. fmt.Println("px length", len(px))
  62. hxQAP := Utils.PF.DivisorPolynomial(px, domain)
  63. fmt.Println("hx length", hxQAP)
  64. // hx==px/zx so px==hx*zx
  65. assert.Equal(t, px, Utils.PF.Mul(hxQAP, domain))
  66. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  67. abc := Utils.PF.Sub(Utils.PF.Mul(ax, bx), cx)
  68. assert.Equal(t, abc, px)
  69. div, rem := Utils.PF.Div(px, domain)
  70. assert.Equal(t, hxQAP, div) //not necessary, since DivisorPolynomial is Div, just discarding 'rem'
  71. assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(len(px)-len(domain)))
  72. //calculate trusted setup
  73. setup, err := GenerateTrustedSetup(len(w), 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, domain, setup.Pk.Z)
  79. fmt.Println("hx pk.z", hxQAP)
  80. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  81. fmt.Println("hx pk.z", hx)
  82. // assert.Equal(t, hxQAP, hx)
  83. assert.Equal(t, px, Utils.PF.Mul(hxQAP, domain))
  84. assert.Equal(t, px, Utils.PF.Mul(hx, setup.Pk.Z))
  85. assert.Equal(t, len(hx), len(px)-len(setup.Pk.Z)+1)
  86. assert.Equal(t, len(hxQAP), len(px)-len(domain)+1)
  87. // fmt.Println("pk.Z", len(setup.Pk.Z))
  88. // fmt.Println("zxQAP", len(zxQAP))
  89. // piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  90. proof, err := GenerateProofs(setup, program.GlobalInputCount(), wReordered, px)
  91. assert.Nil(t, err)
  92. // fmt.Println("\n proofs:")
  93. // fmt.Println(proof)
  94. // fmt.Println("public signals:", proof.PublicSignals)
  95. fmt.Println("\nwitness", w)
  96. fmt.Println("\nwitness Reordered ", wReordered)
  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(setup, proof, wReordered[:program.GlobalInputCount()], true))
  103. //fmt.Println("verify proof time elapsed:", time.Since(before))
  104. }