Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

snarkjs

Package Overview
Dependencies
Maintainers
1
Versions
136
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snarkjs - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6

src/prover_groth.js

214

cli.js

@@ -62,2 +62,8 @@ #!/usr/bin/env node

--protocol [original|groth]
Defines withc variant of snark you want to use
Default: original
calculate witness command

@@ -148,2 +154,3 @@ =========================

generate solidity verifier command

@@ -168,2 +175,3 @@ ==================================

generate call parameters

@@ -175,7 +183,7 @@ ========================

Outputs into the console the raw parameters to be used in 'verifyProof'
method of the solidity verifier.
method of the solidity verifier function.
-p or --proof
Input filenam with the zero knowlage proof you want to use
Input filename with the zero knowlage proof you want to use

@@ -240,3 +248,4 @@ Default: proof.json

const publicName = (argv.public) ? argv.public : "public.json";
const verifierName = (argv.public) ? argv.public : "verifier.sol";
const verifierName = (argv.verifier) ? argv.verifier : "verifier.sol";
const protocol = (argv.protocol) ? argv.protocol : "original";

@@ -270,4 +279,6 @@ function p256(n) {

const cir = new zkSnark.Circuit(cirDef);
const setup = zkSnark.setup(cir);
if (!zkSnark[protocol]) throw new Error("Invalid protocol");
const setup = zkSnark[protocol].setup(cir);
fs.writeFileSync(provingKeyName, JSON.stringify(stringifyBigInts(setup.vk_proof), null, 1), "utf-8");

@@ -289,3 +300,5 @@ fs.writeFileSync(verificationKeyName, JSON.stringify(stringifyBigInts(setup.vk_verifier), null, 1), "utf-8");

const {proof, publicSignals} = zkSnark.genProof(provingKey, witness);
const protocol = provingKey.protocol;
if (!zkSnark[protocol]) throw new Error("Invalid protocol");
const {proof, publicSignals} = zkSnark[protocol].genProof(provingKey, witness);

@@ -299,4 +312,8 @@ fs.writeFileSync(proofName, JSON.stringify(stringifyBigInts(proof), null, 1), "utf-8");

const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8")));
const isValid = zkSnark.isValid(verificationKey, proof, public);
const protocol = verificationKey.protocol;
if (!zkSnark[protocol]) throw new Error("Invalid protocol");
const isValid = zkSnark[protocol].isValid(verificationKey, proof, public);
if (isValid) {

@@ -312,56 +329,13 @@ console.log("OK");

const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8")));
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier.sol"), "utf-8");
const vka_str = `[${verificationKey.vk_a[0][1].toString()},`+
`${verificationKey.vk_a[0][0].toString()}], `+
`[${verificationKey.vk_a[1][1].toString()},` +
`${verificationKey.vk_a[1][0].toString()}]`;
template = template.replace("<%vk_a%>", vka_str);
const vkb_str = `${verificationKey.vk_b[0].toString()},`+
`${verificationKey.vk_b[1].toString()}`;
template = template.replace("<%vk_b%>", vkb_str);
const vkc_str = `[${verificationKey.vk_c[0][1].toString()},`+
`${verificationKey.vk_c[0][0].toString()}], `+
`[${verificationKey.vk_c[1][1].toString()},` +
`${verificationKey.vk_c[1][0].toString()}]`;
template = template.replace("<%vk_c%>", vkc_str);
const vkg_str = `[${verificationKey.vk_g[0][1].toString()},`+
`${verificationKey.vk_g[0][0].toString()}], `+
`[${verificationKey.vk_g[1][1].toString()},` +
`${verificationKey.vk_g[1][0].toString()}]`;
template = template.replace("<%vk_g%>", vkg_str);
const vkgb1_str = `${verificationKey.vk_gb_1[0].toString()},`+
`${verificationKey.vk_gb_1[1].toString()}`;
template = template.replace("<%vk_gb1%>", vkgb1_str);
const vkgb2_str = `[${verificationKey.vk_gb_2[0][1].toString()},`+
`${verificationKey.vk_gb_2[0][0].toString()}], `+
`[${verificationKey.vk_gb_2[1][1].toString()},` +
`${verificationKey.vk_gb_2[1][0].toString()}]`;
template = template.replace("<%vk_gb2%>", vkgb2_str);
const vkz_str = `[${verificationKey.vk_z[0][1].toString()},`+
`${verificationKey.vk_z[0][0].toString()}], `+
`[${verificationKey.vk_z[1][1].toString()},` +
`${verificationKey.vk_z[1][0].toString()}]`;
template = template.replace("<%vk_z%>", vkz_str);
// The points
template = template.replace("<%vk_input_length%>", (verificationKey.A.length-1).toString());
template = template.replace("<%vk_ic_length%>", verificationKey.A.length.toString());
let vi = "";
for (let i=0; i<verificationKey.A.length; i++) {
if (vi != "") vi = vi + " ";
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.A[i][0].toString()},`+
`${verificationKey.A[i][1].toString()});\n`;
let verifierCode;
if (verificationKey.protocol == "original") {
verifierCode = generateVerifier_original(verificationKey);
} else if (verificationKey.protocol == "groth") {
verifierCode = generateVerifier_groth(verificationKey);
} else {
throw new Error("InvalidProof");
}
template = template.replace("<%vk_ic_pts%>", vi);
fs.writeFileSync(verifierName, template, "utf-8");
fs.writeFileSync(verifierName, verifierCode, "utf-8");
process.exit(0);

@@ -380,11 +354,21 @@

const S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
`[${p256(proof.pi_ap[0])}, ${p256(proof.pi_ap[1])}],` +
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
`[${p256(proof.pi_bp[0])}, ${p256(proof.pi_bp[1])}],` +
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
`[${p256(proof.pi_cp[0])}, ${p256(proof.pi_cp[1])}],` +
`[${p256(proof.pi_h[0])}, ${p256(proof.pi_h[1])}],` +
`[${p256(proof.pi_kp[0])}, ${p256(proof.pi_kp[1])}],` +
`[${inputs}]` ;
let S;
if ((typeof proof.protocol === "undefined") || (proof.protocol == "original")) {
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
`[${p256(proof.pi_ap[0])}, ${p256(proof.pi_ap[1])}],` +
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
`[${p256(proof.pi_bp[0])}, ${p256(proof.pi_bp[1])}],` +
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
`[${p256(proof.pi_cp[0])}, ${p256(proof.pi_cp[1])}],` +
`[${p256(proof.pi_h[0])}, ${p256(proof.pi_h[1])}],` +
`[${p256(proof.pi_kp[0])}, ${p256(proof.pi_kp[1])}],` +
`[${inputs}]`;
} else if (proof.protocol == "groth") {
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
`[${inputs}]`;
} else {
throw new Error("InvalidProof");
}

@@ -397,2 +381,3 @@ console.log(S);

} catch(err) {
console.log(err.stack);
console.log("ERROR: " + err);

@@ -403,4 +388,101 @@ process.exit(1);

function generateVerifier_original(verificationKey) {
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_original.sol"), "utf-8");
const vka_str = `[${verificationKey.vk_a[0][1].toString()},`+
`${verificationKey.vk_a[0][0].toString()}], `+
`[${verificationKey.vk_a[1][1].toString()},` +
`${verificationKey.vk_a[1][0].toString()}]`;
template = template.replace("<%vk_a%>", vka_str);
const vkb_str = `${verificationKey.vk_b[0].toString()},`+
`${verificationKey.vk_b[1].toString()}`;
template = template.replace("<%vk_b%>", vkb_str);
const vkc_str = `[${verificationKey.vk_c[0][1].toString()},`+
`${verificationKey.vk_c[0][0].toString()}], `+
`[${verificationKey.vk_c[1][1].toString()},` +
`${verificationKey.vk_c[1][0].toString()}]`;
template = template.replace("<%vk_c%>", vkc_str);
const vkg_str = `[${verificationKey.vk_g[0][1].toString()},`+
`${verificationKey.vk_g[0][0].toString()}], `+
`[${verificationKey.vk_g[1][1].toString()},` +
`${verificationKey.vk_g[1][0].toString()}]`;
template = template.replace("<%vk_g%>", vkg_str);
const vkgb1_str = `${verificationKey.vk_gb_1[0].toString()},`+
`${verificationKey.vk_gb_1[1].toString()}`;
template = template.replace("<%vk_gb1%>", vkgb1_str);
const vkgb2_str = `[${verificationKey.vk_gb_2[0][1].toString()},`+
`${verificationKey.vk_gb_2[0][0].toString()}], `+
`[${verificationKey.vk_gb_2[1][1].toString()},` +
`${verificationKey.vk_gb_2[1][0].toString()}]`;
template = template.replace("<%vk_gb2%>", vkgb2_str);
const vkz_str = `[${verificationKey.vk_z[0][1].toString()},`+
`${verificationKey.vk_z[0][0].toString()}], `+
`[${verificationKey.vk_z[1][1].toString()},` +
`${verificationKey.vk_z[1][0].toString()}]`;
template = template.replace("<%vk_z%>", vkz_str);
// The points
template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
let vi = "";
for (let i=0; i<verificationKey.IC.length; i++) {
if (vi != "") vi = vi + " ";
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
`${verificationKey.IC[i][1].toString()});\n`;
}
template = template.replace("<%vk_ic_pts%>", vi);
return template;
}
function generateVerifier_groth(verificationKey) {
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_groth.sol"), "utf-8");
const vkalfa1_str = `${verificationKey.vk_alfa_1[0].toString()},`+
`${verificationKey.vk_alfa_1[1].toString()}`;
template = template.replace("<%vk_alfa1%>", vkalfa1_str);
const vkbeta2_str = `[${verificationKey.vk_beta_2[0][1].toString()},`+
`${verificationKey.vk_beta_2[0][0].toString()}], `+
`[${verificationKey.vk_beta_2[1][1].toString()},` +
`${verificationKey.vk_beta_2[1][0].toString()}]`;
template = template.replace("<%vk_beta2%>", vkbeta2_str);
const vkgamma2_str = `[${verificationKey.vk_gamma_2[0][1].toString()},`+
`${verificationKey.vk_gamma_2[0][0].toString()}], `+
`[${verificationKey.vk_gamma_2[1][1].toString()},` +
`${verificationKey.vk_gamma_2[1][0].toString()}]`;
template = template.replace("<%vk_gamma2%>", vkgamma2_str);
const vkdelta2_str = `[${verificationKey.vk_delta_2[0][1].toString()},`+
`${verificationKey.vk_delta_2[0][0].toString()}], `+
`[${verificationKey.vk_delta_2[1][1].toString()},` +
`${verificationKey.vk_delta_2[1][0].toString()}]`;
template = template.replace("<%vk_delta2%>", vkdelta2_str);
// The points
template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
let vi = "";
for (let i=0; i<verificationKey.IC.length; i++) {
if (vi != "") vi = vi + " ";
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
`${verificationKey.IC[i][1].toString()});\n`;
}
template = template.replace("<%vk_ic_pts%>", vi);
return template;
}

@@ -21,5 +21,16 @@ /*

exports.Circuit = require("./src/circuit.js");
exports.setup = require("./src/setup.js");
exports.genProof = require("./src/prover.js");
exports.isValid = require("./src/verifier.js");
exports.original = {
setup: require("./src/setup_original.js"),
genProof: require("./src/prover_original.js"),
isValid: require("./src/verifier_original.js")
};
exports.groth = {
setup: require("./src/setup_groth.js"),
genProof: require("./src/prover_groth.js"),
isValid: require("./src/verifier_groth.js")
};
exports.bigInt = require("./src/bigint.js");
exports.ZqField = require("./src/zqfield.js");
const Bn128 = require("./src/bn128.js");
exports.bn128 = new Bn128();
{
"name": "snarkjs",
"version": "0.1.5",
"version": "0.1.6",
"description": "zkSNARKs implementation in JavaScript",

@@ -5,0 +5,0 @@ "main": "index.js",

# snarkjs: JavaScript implementation of zkSNARKs.
This is a JavaScript implementation of zkSNARK schemes.
This is a JavaScript implementation of zkSNARK schemes. It allows the original 8points protocol
and the Groth Protocol (3 point only and 3 pairings)

@@ -5,0 +6,0 @@ This library allows to do the trusted setup, generate proofs and verify the proofs.

@@ -55,2 +55,3 @@ /*

this.F12 = new F2Field(this.F6, this.nonResidueF6);
this.Fr = new F1Field(this.r);
const self = this;

@@ -70,3 +71,3 @@ this.F12._mulByNonResidue = function(a) {

if (this.loopCount.isNegative()) {
this.loopCount = this.neg();
this.loopCount = this.loopCount.neg();
this.loopCountNeg = true;

@@ -73,0 +74,0 @@ } else {

@@ -131,3 +131,3 @@ /*

if (!isNaN(name)) return Number(name);
throw new Error("Invalid signal identifier: ", name);
throw new Error("Invalid signal identifier: "+ name);
}

@@ -134,0 +134,0 @@

@@ -26,4 +26,2 @@ /*

const pairing = bn128.pairing;
module.exports = function isValid(vk_verifier, proof, publicSignals) {

@@ -30,0 +28,0 @@

@@ -27,5 +27,6 @@ /*

constructor(q) {
this.q = q;
this.q = bigInt(q);
this.zero = bigInt.zero;
this.one = bigInt.one;
this.minusone = this.q.sub(this.one);
this.add = bigInt.genAdd();

@@ -43,2 +44,20 @@ this.double = bigInt.genDouble();

this.twoinv = this.inverse(this.two);
const e = this.minusone.shr(this.one);
this.nqr = this.two;
let r = this.exp(this.nqr, e);
while (!r.equals(this.minusone)) {
this.nqr = this.nqr.add(this.one);
r = this.exp(this.nqr, e);
}
this.s = this.zero;
this.t = this.minusone;
while (!this.t.isOdd()) {
this.s = this.s.add(this.one);
this.t = this.t.shr(this.one);
}
this.nqr_to_t = this.exp(this.nqr, this.t);
}

@@ -76,2 +95,43 @@

}
sqrt(n) {
n = this.affine(n);
if (n.equals(this.zero)) return this.zero;
// Test that have solution
const res = this.exp(n, this.minusone.shr(this.one));
if (!res.equals(this.one)) return null;
let m = parseInt(this.s);
let c = this.nqr_to_t;
let t = this.exp(n, this.t);
let r = this.exp(n, this.add(this.t, this.one).shr(this.one) );
while (!t.equals(this.one)) {
let sq = this.square(t);
let i = 1;
while (!sq.equals(this.one)) {
i++;
sq = this.square(sq);
}
// b = c ^ m-i-1
let b = c;
for (let j=0; j< m-i-1; j ++) b = this.square(b);
m = i;
c = this.square(b);
t = this.mul(t, c);
r = this.mul(r, b);
}
if (r.greater(this.q.shr(this.one))) {
r = this.neg(r);
}
return r;
}
}

@@ -78,0 +138,0 @@

@@ -6,13 +6,13 @@ /*

zksnark JavaScript library 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)
zksnark JavaScript library 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)
any later version.
zksnark JavaScript library 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
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
You should have received a copy of the GNU General Public License along with
zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.

@@ -47,2 +47,24 @@ */

});
it("Should compute sqrts", () => {
const bn128 = new BN128();
const F = new F1Field(bn128.r);
const a = bigInt("4");
let b = F.sqrt(a);
assert(F.equals(bigInt(0), F.sqrt(bigInt("0"))));
assert(F.equals(b, bigInt("2")));
assert(F.sqrt(F.nqr) === null);
});
it("Should compute sqrt of 100 random numbers", () => {
const bn128 = new BN128();
const F = new F1Field(bn128.r);
for (let j=0;j<100; j++) {
let a = F.random();
let s = F.sqrt(a);
if (s != null) {
assert(F.equals(F.square(s), a));
}
}
});
});

@@ -49,0 +71,0 @@

@@ -26,3 +26,3 @@ /*

const Circuit = require("../src/circuit.js");
const zkSnark = require("../index.js");
const zkSnark = require("../index.js").original;
const BN128 = require("../src/bn128.js");

@@ -42,6 +42,3 @@ const PolField = require("../src/polfield.js");

describe("zkSnark", () => {
describe("zkSnark original", () => {
it("Load a circuit, create trusted setup, create a proof and validate it", () => {

@@ -48,0 +45,0 @@

@@ -1,1 +0,1 @@

{"nPublic":2,"A":[["13748780897548213951275281002596580813562717675655399375007549036110641136021","191470769863817069897387201389601788324536690033187060846183983079098357394","1"],["18950485977844447902223302204376167458696138040252686570435320938365864348502","7271813516566066459381958879561228755848812321761332703504031940443193860951","1"],["10550502146506181421156845871338656937604707762177820631954232892783886482938","21242116051056802188468939122405035309672553177789742050267311430394049717054","1"]],"vk_a":[["3648994822673106647455305244073563060200556471519763966341475644443204011541","19190015192797538035156514697167432375350886603409288416207360861952319261026"],["19912375136853529904499116381486024931886647702841458927760031833692938494923","7571101790086228463314286168379655212520805710327619853340541433526915960099"],["1","0"]],"vk_b":["10310887787081964577086957678647751381175396134873750255854004838446037014291","16002673809262616992952363436700888148785514822471217759714188814309317465080","1"],"vk_c":[["6160108760190506073804555639033841543328310342176679447310878073768168329940","12370096448271566220904474347814530659706679423163203247294098158176124902591"],["4706988174442652261148543353687214573443297804403418205251952930962669528199","3546107194049375164253573714498474040316692798649722107846476327644607583072"],["1","0"]],"vk_gb_1":["7146696309986681281110057575452850985532610669098681308295432109836239411953","16686123413410528449772051830348980913895858679850292529110189221785284368789","1"],"vk_gb_2":[["18953991732646709983341064534549536299218349659392861886236367876404227375356","1071540992240725849073673180902567457862664009887584586695766822861438847863"],["20870421036314350569737683283619505426680530028797392571771354832217410362185","9848505931878091491817866253146722443798097790010800493474936101706576832542"],["1","0"]],"vk_g":[["918130074516815543852789857283473367724249001127655869824094873426529782559","12374520330065818718765523336075006974954164429863062496190757782266518553924"],["15136410217911142493142327756483366574433556210485010398268543896801775084341","91578221478298064914132335114027313829587197089740823987464475984348362086"],["1","0"]],"vk_z":[["8444006341203690846248826436908513653949166405412766828583891245391665907179","19744368555025039818988622435429063512843759021805799665260531402900174284945"],["1525609944586286127212978014095291350902084283422884034256233641387793310306","20954328038043383968653227341547489558748024941604970666391828647333046834222"],["1","0"]]}
{"protocol":"original","nPublic":2,"IC":[["8849055511082207197386816220933697359788971969723311105914207102749058732262","16959719579845113100884343214094921890994800988324717557455180752030411170003","1"],["2484443707386195254509798174506364533244136674881306666282125111587938066433","19427682058362356154903959035863203819006762969409806927732486823153723237810","1"],["7130785916432156053719609234300145030816421804617499884567479079011422988955","6189201216535197259817162464187743592146560869870792185911497850033335074298","1"]],"vk_a":[["9872559917844294477536347504934832320655533991650169902417802683379346119997","843772301140357563436689776827970926589741902080524202569657734909662469474"],["7531191948728096398667205760901764150746858276390769174947239681261174603496","19390457837724221793002986178650274717908327325393349077639674652630431124169"],["1","0"]],"vk_b":["501420461369693852599229883919204710886593732833724468965832578166009925713","10317710816126649044200749005377880660464978872377653125422472992191645650124","1"],"vk_c":[["10051649206767283312353552575431286293027310220624929913814364593030185419531","14791785180254274883514790322022956673529295334754985355268675419718338162079"],["7936893319622378351783826963378019679916863061363731322724584049671768481832","14935855848155905276829387453433187422477676703547349346896558674964769292707"],["1","0"]],"vk_gb_1":["16883378629371629842761088081108977348103248355774740955500038886325479370413","1920838522949484267352900228504548578245603817578514439559866317386119521003","1"],"vk_gb_2":[["11108842905356484096288635838337079212811899426526012687887143514045721790804","21031591569096646322506103672254640960873689370092824091794804577786886967908"],["817213165866263014069075203124784173584879880614844748456154653668382729159","864218259110131214014552546475739264317537208733507094863210326585244180593"],["1","0"]],"vk_g":[["14650542782728886534422292348945287981512706836643134176709351124791472680543","18960277976951274828172086500759014849906184580071220971216975068158250858162"],["14398341775515438116369872062699814349975225443081016536963355934433867545051","7430323674093579778297501612631986638268006525016526795405658142628213235594"],["1","0"]],"vk_z":[["9232595145194514136236063963352093468662621616460757765591947153028653483170","21870021077089796283233323883357370909479704574901541789297553490533610487318"],["1344881225032961007807685156782552847773328464965034725847677510387048101542","17695086597077289574292295853159694727053610025432390166769224554889027950018"],["1","0"]]}

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc