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.

109 lines
2.7 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "strings"
  7. "time"
  8. "github.com/arnaucube/go-snark"
  9. "github.com/arnaucube/go-snark/circuitcompiler"
  10. )
  11. func main() {
  12. // circuit function
  13. // y = x^5 + 2*x + 6
  14. code := `
  15. func exp5(private a):
  16. b = a * a
  17. c = a * b
  18. d = a * c
  19. e = a * d
  20. return e
  21. func main(private s0, public s1):
  22. s2 = exp5(s0)
  23. s3 = s0 * 2
  24. s4 = s3 + s2
  25. s5 = s4 + 6
  26. equals(s1, s5)
  27. out = 1 * 1
  28. `
  29. fmt.Print("\ncode of the circuit:")
  30. fmt.Println(code)
  31. // parse the code
  32. parser := circuitcompiler.NewParser(strings.NewReader(code))
  33. circuit, err := parser.Parse()
  34. if err != nil {
  35. panic(err)
  36. }
  37. fmt.Println("\ncircuit data:", circuit)
  38. circuitJson, _ := json.Marshal(circuit)
  39. fmt.Println("circuit:", string(circuitJson))
  40. b8 := big.NewInt(int64(8))
  41. privateInputs := []*big.Int{b8}
  42. b32790 := big.NewInt(int64(32790))
  43. publicSignals := []*big.Int{b32790}
  44. // wittness
  45. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  46. if err != nil {
  47. panic(err)
  48. }
  49. // code to R1CS
  50. fmt.Println("\ngenerating R1CS from code")
  51. a, b, c := circuit.GenerateR1CS()
  52. fmt.Println("\nR1CS:")
  53. fmt.Println("a:", a)
  54. fmt.Println("b:", b)
  55. fmt.Println("c:", c)
  56. // R1CS to QAP
  57. // TODO zxQAP is not used and is an old impl, TODO remove
  58. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  59. fmt.Println("qap")
  60. fmt.Println(alphas)
  61. fmt.Println(betas)
  62. fmt.Println(gammas)
  63. _, _, _, px := snark.Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  64. // calculate trusted setup
  65. setup, err := snark.GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  66. if err != nil {
  67. panic(err)
  68. }
  69. fmt.Println("\nt:", setup.Toxic.T)
  70. // 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
  71. proof, err := snark.GenerateProofs(*circuit, setup.Pk, w, px)
  72. if err != nil {
  73. panic(err)
  74. }
  75. fmt.Println("\n proofs:")
  76. fmt.Println(proof)
  77. // fmt.Println("public signals:", proof.PublicSignals)
  78. fmt.Println("\nsignals:", circuit.Signals)
  79. fmt.Println("witness:", w)
  80. b32790Verif := big.NewInt(int64(32790))
  81. publicSignalsVerif := []*big.Int{b32790Verif}
  82. before := time.Now()
  83. if !snark.VerifyProof(setup.Vk, proof, publicSignalsVerif, true) {
  84. fmt.Println("Verification not passed")
  85. }
  86. fmt.Println("verify proof time elapsed:", time.Since(before))
  87. // check that with another public input the verification returns false
  88. bOtherWrongPublic := big.NewInt(int64(34))
  89. wrongPublicSignalsVerif := []*big.Int{bOtherWrongPublic}
  90. if snark.VerifyProof(setup.Vk, proof, wrongPublicSignalsVerif, true) {
  91. fmt.Println("Verification should not have passed")
  92. }
  93. }