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.

71 lines
2.2 KiB

5 years ago
5 years ago
  1. /*
  2. Copyright 2018 0kims association
  3. This file is part of zksnark javascript library.
  4. zksnark javascript library is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. zksnark javascript library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. const BN128 = require("./BN128.js");
  16. const bn128 = new BN128();
  17. const G1 = bn128.G1;
  18. const G2 = bn128.G2;
  19. const pairing = bn128.pairing;
  20. module.exports = function isValid(vk_verifier, proof, publicSignals) {
  21. let full_pi_a = vk_verifier.A[0];
  22. for (let s= 0; s< vk_verifier.nPublic; s++) {
  23. full_pi_a = G1.add( full_pi_a, G1.mulScalar( vk_verifier.A[s+1], publicSignals[s]));
  24. }
  25. full_pi_a = G1.add( full_pi_a, proof.pi_a);
  26. if (! bn128.F12.equals(
  27. bn128.pairing( proof.pi_a , vk_verifier.vk_a ),
  28. bn128.pairing( proof.pi_ap , G2.g )))
  29. return false;
  30. if (! bn128.F12.equals(
  31. bn128.pairing( vk_verifier.vk_b, proof.pi_b ),
  32. bn128.pairing( proof.pi_bp , G2.g )))
  33. return false;
  34. if (! bn128.F12.equals(
  35. bn128.pairing( proof.pi_c , vk_verifier.vk_c ),
  36. bn128.pairing( proof.pi_cp , G2.g )))
  37. return false;
  38. if (! bn128.F12.equals(
  39. bn128.pairing( full_pi_a , proof.pi_b ),
  40. bn128.F12.mul(
  41. bn128.pairing( proof.pi_h , vk_verifier.vk_z ),
  42. bn128.pairing( proof.pi_c , G2.g ),
  43. )))
  44. return false;
  45. if (! bn128.F12.equals(
  46. bn128.F12.mul(
  47. bn128.pairing( G1.add(full_pi_a, proof.pi_c) , vk_verifier.vk_gb_2 ),
  48. bn128.pairing( vk_verifier.vk_gb_1 , proof.pi_b ),
  49. ),
  50. bn128.pairing( proof.pi_kp , vk_verifier.vk_g )))
  51. return false;
  52. return true;
  53. };