snarkjs/src/verifier.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-09-05 04:56:49 +02:00
/*
2018-09-10 11:53:09 +02:00
Copyright 2018 0kims association.
2018-09-05 04:56:49 +02:00
2018-10-21 19:41:44 +02:00
This file is part of snarkjs.
2018-09-05 04:56:49 +02:00
2018-10-21 19:41:44 +02:00
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
2018-09-10 11:53:09 +02:00
any later version.
2018-09-05 04:56:49 +02:00
2018-10-21 19:41:44 +02:00
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
2018-09-10 11:53:09 +02:00
more details.
2018-09-05 04:56:49 +02:00
You should have received a copy of the GNU General Public License along with
2018-10-21 19:41:44 +02:00
snarkjs. If not, see <https://www.gnu.org/licenses/>.
2018-09-05 04:56:49 +02:00
*/
2018-09-09 14:07:28 +02:00
const BN128 = require("./bn128.js");
2018-08-09 15:31:16 +02:00
2018-08-25 00:16:12 +02:00
const bn128 = new BN128();
const G1 = bn128.G1;
const G2 = bn128.G2;
2018-08-09 15:31:16 +02:00
2018-08-09 08:16:34 +02:00
module.exports = function isValid(vk_verifier, proof, publicSignals) {
2018-08-25 00:16:12 +02:00
let full_pi_a = vk_verifier.A[0];
2018-08-09 15:31:16 +02:00
for (let s= 0; s< vk_verifier.nPublic; s++) {
2018-08-25 00:16:12 +02:00
full_pi_a = G1.add( full_pi_a, G1.mulScalar( vk_verifier.A[s+1], publicSignals[s]));
2018-08-09 15:31:16 +02:00
}
2018-08-25 00:16:12 +02:00
full_pi_a = G1.add( full_pi_a, proof.pi_a);
2018-08-09 18:59:39 +02:00
2018-08-25 00:16:12 +02:00
if (! bn128.F12.equals(
bn128.pairing( proof.pi_a , vk_verifier.vk_a ),
bn128.pairing( proof.pi_ap , G2.g )))
2018-08-09 15:31:16 +02:00
return false;
2018-08-25 00:16:12 +02:00
if (! bn128.F12.equals(
bn128.pairing( vk_verifier.vk_b, proof.pi_b ),
bn128.pairing( proof.pi_bp , G2.g )))
2018-08-09 15:31:16 +02:00
return false;
2018-08-25 00:16:12 +02:00
if (! bn128.F12.equals(
bn128.pairing( proof.pi_c , vk_verifier.vk_c ),
bn128.pairing( proof.pi_cp , G2.g )))
2018-08-09 15:31:16 +02:00
return false;
if (! bn128.F12.equals(
bn128.F12.mul(
bn128.pairing( G1.add(full_pi_a, proof.pi_c) , vk_verifier.vk_gb_2 ),
bn128.pairing( vk_verifier.vk_gb_1 , proof.pi_b ),
),
bn128.pairing( proof.pi_kp , vk_verifier.vk_g )))
return false;
2018-08-25 00:16:12 +02:00
if (! bn128.F12.equals(
bn128.pairing( full_pi_a , proof.pi_b ),
bn128.F12.mul(
bn128.pairing( proof.pi_h , vk_verifier.vk_z ),
bn128.pairing( proof.pi_c , G2.g ),
)))
2018-08-09 15:31:16 +02:00
return false;
return true;
};