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.

90 lines
2.2 KiB

  1. package externalVerif
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "github.com/arnaucube/go-snark/groth16"
  7. "github.com/arnaucube/go-snark/utils"
  8. )
  9. type CircomProof struct {
  10. PiA [3]string `json:"pi_a"`
  11. PiB [3][2]string `json:"pi_b"`
  12. PiC [3]string `json:"pi_c"`
  13. }
  14. type CircomVk struct {
  15. IC [][3]string `json:"IC"`
  16. Alpha1 [3]string `json:"vk_alfa_1"`
  17. Beta2 [3][2]string `json:"vk_beta_2"`
  18. Gamma2 [3][2]string `json:"vk_gamma_2"`
  19. Delta2 [3][2]string `json:"vk_delta_2"`
  20. AlphaBeta12 [2][3][2]string `json:"vk_alpfabeta_12"` // not really used, for the moment in go-snarks calculed in verification time
  21. }
  22. func VerifyFromCircom(vkPath, proofPath, publicSignalsPath string) (bool, error) {
  23. // open verification_key.json
  24. vkFile, err := ioutil.ReadFile(vkPath)
  25. if err != nil {
  26. return false, err
  27. }
  28. var circomVk CircomVk
  29. json.Unmarshal([]byte(string(vkFile)), &circomVk)
  30. if err != nil {
  31. return false, err
  32. }
  33. var strVk utils.GrothVkString
  34. strVk.IC = circomVk.IC
  35. strVk.G1.Alpha = circomVk.Alpha1
  36. strVk.G2.Beta = circomVk.Beta2
  37. strVk.G2.Gamma = circomVk.Gamma2
  38. strVk.G2.Delta = circomVk.Delta2
  39. vk, err := utils.GrothVkFromString(strVk)
  40. if err != nil {
  41. return false, err
  42. }
  43. fmt.Println("vk parsed:", vk)
  44. // open proof.json
  45. proofsFile, err := ioutil.ReadFile(proofPath)
  46. if err != nil {
  47. return false, err
  48. }
  49. var circomProof CircomProof
  50. json.Unmarshal([]byte(string(proofsFile)), &circomProof)
  51. if err != nil {
  52. return false, err
  53. }
  54. strProof := utils.GrothProofString{
  55. PiA: circomProof.PiA,
  56. PiB: circomProof.PiB,
  57. PiC: circomProof.PiC,
  58. }
  59. proof, err := utils.GrothProofFromString(strProof)
  60. if err != nil {
  61. return false, err
  62. }
  63. fmt.Println("proof parsed:", proof)
  64. // open public.json
  65. publicFile, err := ioutil.ReadFile(publicSignalsPath)
  66. if err != nil {
  67. return false, err
  68. }
  69. var publicStr []string
  70. json.Unmarshal([]byte(string(publicFile)), &publicStr)
  71. if err != nil {
  72. return false, err
  73. }
  74. publicSignals, err := utils.ArrayStringToBigInt(publicStr)
  75. if err != nil {
  76. return false, err
  77. }
  78. fmt.Println("publicSignals parsed:", publicSignals)
  79. verified := groth16.VerifyProof(vk, proof, publicSignals, true)
  80. return verified, nil
  81. }