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.

166 lines
4.2 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 TestGenerateProofs(t *testing.T) {
  12. z := []*big.Int{big.NewInt(int64(1))}
  13. for i := 1; i < 6; i++ {
  14. z = Utils.PF.Mul(
  15. z,
  16. []*big.Int{
  17. Utils.PF.F.Neg(
  18. big.NewInt(int64(i))),
  19. big.NewInt(int64(1)),
  20. })
  21. }
  22. fmt.Println(z)
  23. for i := 0; i < 7; i++ {
  24. fmt.Println(Utils.PF.Eval(z, big.NewInt(int64(i))))
  25. }
  26. z = []*big.Int{big.NewInt(int64(1))}
  27. for i := 1; i < 6; i++ {
  28. z = Utils.PF.Mul(
  29. z,
  30. []*big.Int{
  31. big.NewInt(int64(i)),
  32. big.NewInt(int64(1)),
  33. })
  34. }
  35. fmt.Println(z)
  36. z = []*big.Int{
  37. big.NewInt(int64(1)),
  38. big.NewInt(int64(
  39. -3)),
  40. }
  41. z = Utils.PF.Mul(
  42. z,
  43. []*big.Int{
  44. big.NewInt(int64(1)),
  45. big.NewInt(int64(3)),
  46. })
  47. fmt.Println(z)
  48. fmt.Println(Utils.PF.F.Neg(
  49. big.NewInt(int64(1))))
  50. fmt.Println(Utils.PF.F.Inverse(big.NewInt(int64(1))))
  51. }
  52. func TestNewProgramm(t *testing.T) {
  53. flat := `
  54. func main(a,b,c,d):
  55. e = a * b
  56. f = c * d
  57. g = e * f
  58. h = g / e
  59. i = h * 5
  60. out = g * i
  61. `
  62. parser := circuitcompiler.NewParser(strings.NewReader(flat))
  63. program, err := parser.Parse()
  64. if err != nil {
  65. panic(err)
  66. }
  67. fmt.Println("\n unreduced")
  68. fmt.Println(flat)
  69. program.BuildConstraintTrees()
  70. program.PrintContraintTrees()
  71. fmt.Println("\nReduced gates")
  72. //PrintTree(froots["mul"])
  73. gates := program.ReduceCombinedTree()
  74. for _, g := range gates {
  75. fmt.Println(g)
  76. }
  77. fmt.Println("generating R1CS")
  78. a, b, c := program.GenerateReducedR1CS(gates)
  79. fmt.Println(a)
  80. fmt.Println(b)
  81. fmt.Println(c)
  82. a1 := big.NewInt(int64(6))
  83. a2 := big.NewInt(int64(5))
  84. inputs := []*big.Int{a1, a2, a1, a2}
  85. w := program.CalculateWitness(inputs)
  86. fmt.Println("witness")
  87. fmt.Println(w)
  88. // R1CS to QAP
  89. alphas, betas, gammas, domain := Utils.PF.R1CSToQAP(a, b, c)
  90. fmt.Println("qap")
  91. fmt.Println("alphas", len(alphas))
  92. fmt.Println("alphas", alphas)
  93. fmt.Println("betas", len(betas))
  94. fmt.Println("gammas", len(gammas))
  95. fmt.Println("domain polynomial ", len(domain))
  96. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  97. fmt.Println("ax length", len(ax))
  98. fmt.Println("bx length", len(bx))
  99. fmt.Println("cx length", len(cx))
  100. fmt.Println("px length", len(px))
  101. hxQAP := Utils.PF.DivisorPolynomial(px, domain)
  102. fmt.Println("hx length", hxQAP)
  103. // hx==px/zx so px==hx*zx
  104. assert.Equal(t, px, Utils.PF.Mul(hxQAP, domain))
  105. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  106. abc := Utils.PF.Sub(Utils.PF.Mul(ax, bx), cx)
  107. assert.Equal(t, abc, px)
  108. hzQAP := Utils.PF.Mul(hxQAP, domain)
  109. assert.Equal(t, abc, hzQAP)
  110. div, rem := Utils.PF.Div(px, domain)
  111. assert.Equal(t, hxQAP, div) //not necessary
  112. assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(len(px)-len(domain)))
  113. //calculate trusted setup
  114. //setup, err := GenerateTrustedSetup(len(w),alphas, betas, gammas)
  115. //assert.Nil(t, err)
  116. //fmt.Println("\nt:", setup.Toxic.T)
  117. ////
  118. ////// 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
  119. //assert.Equal(t, domain, setup.Pk.Z)
  120. //
  121. //fmt.Println("hx pk.z", hxQAP)
  122. //hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  123. //fmt.Println("hx pk.z", hx)
  124. //// assert.Equal(t, hxQAP, hx)
  125. //assert.Equal(t, px, Utils.PF.Mul(hxQAP, zxQAP))
  126. //assert.Equal(t, px, Utils.PF.Mul(hx, setup.Pk.Z))
  127. //
  128. //assert.Equal(t, len(hx), len(px)-len(setup.Pk.Z)+1)
  129. //assert.Equal(t, len(hxQAP), len(px)-len(zxQAP)+1)
  130. //// fmt.Println("pk.Z", len(setup.Pk.Z))
  131. //// fmt.Println("zxQAP", len(zxQAP))
  132. //
  133. //// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  134. //proof, err := GenerateProofs(*circuit, setup, w, px)
  135. //assert.Nil(t, err)
  136. //
  137. //// fmt.Println("\n proofs:")
  138. //// fmt.Println(proof)
  139. //
  140. //// fmt.Println("public signals:", proof.PublicSignals)
  141. //fmt.Println("\nwitness", w)
  142. //// b1 := big.NewInt(int64(1))
  143. //b35 := big.NewInt(int64(35))
  144. //// publicSignals := []*big.Int{b1, b35}
  145. //publicSignals := []*big.Int{b35}
  146. //before := time.Now()
  147. //assert.True(t, VerifyProof(*circuit, setup, proof, publicSignals, true))
  148. //fmt.Println("verify proof time elapsed:", time.Since(before))
  149. }