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.

39 lines
1.0 KiB

  1. package verifier
  2. import (
  3. "fmt"
  4. "math/big"
  5. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  6. "github.com/iden3/go-circom-prover-verifier/types"
  7. )
  8. // Vk is the Verification Key data structure
  9. type Vk struct {
  10. Alpha *bn256.G1
  11. Beta *bn256.G2
  12. Gamma *bn256.G2
  13. Delta *bn256.G2
  14. IC []*bn256.G1
  15. }
  16. // Verify verifies the Groth16 zkSNARK proof
  17. func Verify(vk *types.Vk, proof *types.Proof, inputs []*big.Int) bool {
  18. if len(inputs)+1 != len(vk.IC) {
  19. fmt.Println("len(inputs)+1 != len(vk.IC)")
  20. return false
  21. }
  22. vkX := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  23. for i := 0; i < len(inputs); i++ {
  24. // check input inside field
  25. if inputs[i].Cmp(types.R) != -1 {
  26. return false
  27. }
  28. vkX = new(bn256.G1).Add(vkX, new(bn256.G1).ScalarMult(vk.IC[i+1], inputs[i]))
  29. }
  30. vkX = new(bn256.G1).Add(vkX, vk.IC[0])
  31. g1 := []*bn256.G1{proof.A, new(bn256.G1).Neg(vk.Alpha), vkX.Neg(vkX), new(bn256.G1).Neg(proof.C)}
  32. g2 := []*bn256.G2{proof.B, vk.Beta, vk.Gamma, vk.Delta}
  33. return bn256.PairingCheck(g1, g2)
  34. }