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.

165 lines
4.1 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. def 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. r1cs := program.GenerateReducedR1CS(gates)
  79. a, b, c := r1cs.A, r1cs.B, r1cs.C
  80. fmt.Println(a)
  81. fmt.Println(b)
  82. fmt.Println(c)
  83. a1 := big.NewInt(int64(6))
  84. a2 := big.NewInt(int64(5))
  85. inputs := []*big.Int{a1, a2, a1, a2}
  86. w := circuitcompiler.CalculateWitness(inputs, r1cs)
  87. fmt.Println("witness")
  88. fmt.Println(w)
  89. // R1CS to QAP
  90. alphas, betas, gammas, domain := Utils.PF.R1CSToQAP(a, b, c)
  91. fmt.Println("qap")
  92. fmt.Println("alphas", len(alphas))
  93. fmt.Println("alphas", alphas)
  94. fmt.Println("betas", len(betas))
  95. fmt.Println("gammas", len(gammas))
  96. fmt.Println("domain polynomial ", len(domain))
  97. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  98. fmt.Println("ax length", len(ax))
  99. fmt.Println("bx length", len(bx))
  100. fmt.Println("cx length", len(cx))
  101. fmt.Println("px length", len(px))
  102. hxQAP := Utils.PF.DivisorPolynomial(px, domain)
  103. fmt.Println("hx length", hxQAP)
  104. // hx==px/zx so px==hx*zx
  105. assert.Equal(t, px, Utils.PF.Mul(hxQAP, domain))
  106. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  107. abc := Utils.PF.Sub(Utils.PF.Mul(ax, bx), cx)
  108. assert.Equal(t, abc, px)
  109. div, rem := Utils.PF.Div(px, domain)
  110. assert.Equal(t, hxQAP, div) //not necessary, since DivisorPolynomial is Div, just discarding 'rem'
  111. assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(len(px)-len(domain)))
  112. //calculate trusted setup
  113. setup, err := GenerateTrustedSetup(len(w), alphas, betas, gammas)
  114. assert.Nil(t, err)
  115. fmt.Println("\nt:", setup.Toxic.T)
  116. //
  117. //// 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
  118. //assert.Equal(t, domain, setup.Pk.Z)
  119. fmt.Println("hx pk.z", hxQAP)
  120. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  121. fmt.Println("hx pk.z", hx)
  122. // assert.Equal(t, hxQAP, hx)
  123. assert.Equal(t, px, Utils.PF.Mul(hxQAP, domain))
  124. assert.Equal(t, px, Utils.PF.Mul(hx, setup.Pk.Z))
  125. assert.Equal(t, len(hx), len(px)-len(setup.Pk.Z)+1)
  126. assert.Equal(t, len(hxQAP), len(px)-len(domain)+1)
  127. // fmt.Println("pk.Z", len(setup.Pk.Z))
  128. // fmt.Println("zxQAP", len(zxQAP))
  129. // piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  130. proof, err := GenerateProofs(setup, 5, w, px)
  131. assert.Nil(t, err)
  132. // fmt.Println("\n proofs:")
  133. // fmt.Println(proof)
  134. // fmt.Println("public signals:", proof.PublicSignals)
  135. fmt.Println("\nwitness", w)
  136. // b1 := big.NewInt(int64(1))
  137. //b35 := big.NewInt(int64(35))
  138. //// publicSignals := []*big.Int{b1, b35}
  139. //publicSignals := []*big.Int{b35}
  140. //before := time.Now()
  141. assert.True(t, VerifyProof(setup, proof, w[:5], true))
  142. //fmt.Println("verify proof time elapsed:", time.Since(before))
  143. }