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.

38 lines
979 B

  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. func Verify(vk *types.Vk, proof *types.Proof, inputs []*big.Int) bool {
  17. if len(inputs)+1 != len(vk.IC) {
  18. fmt.Println("len(inputs)+1 != len(vk.IC)")
  19. return false
  20. }
  21. vkX := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
  22. for i := 0; i < len(inputs); i++ {
  23. // check input inside field
  24. if inputs[i].Cmp(types.R) != -1 {
  25. return false
  26. }
  27. vkX = new(bn256.G1).Add(vkX, new(bn256.G1).ScalarMult(vk.IC[i+1], inputs[i]))
  28. }
  29. vkX = new(bn256.G1).Add(vkX, vk.IC[0])
  30. g1 := []*bn256.G1{proof.A, new(bn256.G1).Neg(vk.Alpha), vkX.Neg(vkX), new(bn256.G1).Neg(proof.C)}
  31. g2 := []*bn256.G2{proof.B, vk.Beta, vk.Gamma, vk.Delta}
  32. return bn256.PairingCheck(g1, g2)
  33. }