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