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.

77 lines
1.6 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. "github.com/urfave/cli"
  7. "github.com/arnaucube/go-snark/circuit"
  8. "github.com/arnaucube/go-snark/fields"
  9. "github.com/arnaucube/go-snark/proof"
  10. )
  11. func test(context *cli.Context) error {
  12. // load circuit
  13. cir := &circuit.Circuit{}
  14. if err := loadFromFile(compiledFileName, cir); err != nil {
  15. return err
  16. }
  17. // load inputs
  18. var inputs circuit.Inputs
  19. if err := loadFromFile(privateFileName, &inputs.Private); err != nil {
  20. return err
  21. }
  22. if err := loadFromFile(publicFileName, &inputs.Public); err != nil {
  23. return err
  24. }
  25. // calculate witness
  26. w, err := cir.CalculateWitness(inputs.Private, inputs.Public)
  27. if err != nil {
  28. return err
  29. }
  30. // R1CS to QAP
  31. alphas, betas, gammas, zx := proof.R1CSToQAP(
  32. cir.R1CS.A,
  33. cir.R1CS.B,
  34. cir.R1CS.C,
  35. )
  36. // px == ax * bx - cx
  37. ax, bx, cx, px := proof.Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  38. // hx == px / zx
  39. hx := proof.Utils.PF.DivisorPolynomial(px, zx)
  40. if !fields.BigArraysEqual(px, proof.Utils.PF.Mul(hx, zx)) {
  41. return fmt.Errorf("px != hx * zx")
  42. }
  43. // ax * bx - cx == px
  44. abc := proof.Utils.PF.Sub(proof.Utils.PF.Mul(ax, bx), cx)
  45. if !fields.BigArraysEqual(abc, px) {
  46. return fmt.Errorf("ax * bx - cx != px")
  47. }
  48. // hx * zx == ax * bx - cx
  49. hz := proof.Utils.PF.Mul(hx, zx)
  50. if !fields.BigArraysEqual(hz, abc) {
  51. return fmt.Errorf("hx * zx != ax * bx - cx")
  52. }
  53. // dx == px / zx + rx
  54. dx, rx := proof.Utils.PF.Div(px, zx)
  55. if !fields.BigArraysEqual(dx, hx) {
  56. return fmt.Errorf("dx != hx")
  57. }
  58. for _, r := range rx {
  59. if !bytes.Equal(r.Bytes(), big.NewInt(int64(0)).Bytes()) {
  60. return fmt.Errorf("rx != 0")
  61. }
  62. }
  63. return nil
  64. }