Comparing version 0.1.20 to 0.1.21
285
cli.js
@@ -30,5 +30,10 @@ #!/usr/bin/env node | ||
const loadR1cs = require("r1csfile").load; | ||
const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder; | ||
const version = require("./package").version; | ||
const loadSyms = require("./src/loadsyms"); | ||
const printR1cs = require("./src/printr1cs"); | ||
const argv = require("yargs") | ||
@@ -45,7 +50,7 @@ .version(version) | ||
-c or --circuit <circuitFile> | ||
-r or --r1cs <r1csFile> | ||
Filename of the compiled circuit file generated by circom. | ||
Default: circuit.json | ||
Default: circuit.r1cs | ||
@@ -77,7 +82,7 @@ --pk or --provingkey <provingKeyFile> | ||
-c or --circuit <circuitFile> | ||
--ws --wasm <wasmFile> | ||
Filename of the compiled circuit file generated by circom. | ||
Default: circuit.json | ||
Default: circuit.r1cs | ||
@@ -94,3 +99,3 @@ -i or --input <inputFile> | ||
-w or --witness | ||
--wt --witness | ||
@@ -101,6 +106,2 @@ Output filename with the generated witness. | ||
--lo or --logoutput | ||
Output all the Output signals | ||
--lg or --logget | ||
@@ -118,3 +119,5 @@ | ||
--s or --sanitycheck | ||
generate a proof command | ||
@@ -125,3 +128,3 @@ ======================== | ||
-w or --witness | ||
--wt or --witness | ||
@@ -225,7 +228,7 @@ Input filename used to calculate the proof. | ||
-c or --circuit <circuitFile> | ||
-r or --r1cs <r1csFile> | ||
Filename of the compiled circuit file generated by circom. | ||
Default: circuit.json | ||
Default: circuit.r1cs | ||
@@ -239,12 +242,20 @@ print constraints | ||
-c or --circuit <circuitFile> | ||
-r or --r1cs <r1csFile> | ||
Filename of the compiled circuit file generated by circom. | ||
Default: circuit.json | ||
Default: circuit.r1cs | ||
-s or --sym <symFile> | ||
Filename of the debuging symbols file generated by circom. | ||
Default: circuit.sym | ||
`) | ||
.alias("c", "circuit") | ||
.alias("r", "r1cs") | ||
.alias("s", "sym") | ||
.alias("pk", "provingkey") | ||
.alias("vk", "verificationkey") | ||
.alias("w", "witness") | ||
.alias("wt", "witness") | ||
.alias("ws", "wasm") | ||
.alias("p", "proof") | ||
@@ -268,6 +279,8 @@ .alias("i", "input") | ||
const circuitName = (argv.circuit) ? argv.circuit : "circuit.json"; | ||
const r1csName = (argv.r1cs) ? argv.r1cs : "circuit.r1cs"; | ||
const symName = (argv.sym) ? argv.sym : "circuit.sym"; | ||
const provingKeyName = (argv.provingkey) ? argv.provingkey : "proving_key.json"; | ||
const verificationKeyName = (argv.verificationkey) ? argv.verificationkey : "verification_key.json"; | ||
const inputName = (argv.input) ? argv.input : "input.json"; | ||
const wasmName = (argv.wasm) ? argv.wasm : "circuit.wasm"; | ||
const witnessName = (argv.witness) ? argv.witness : "witness.json"; | ||
@@ -277,4 +290,8 @@ const proofName = (argv.proof) ? argv.proof : "proof.json"; | ||
const verifierName = (argv.verifier) ? argv.verifier : "verifier.sol"; | ||
const protocol = (argv.protocol) ? argv.protocol : "original"; | ||
const protocol = (argv.protocol) ? argv.protocol : "groth"; | ||
run().then(() => { | ||
process.exit(); | ||
}); | ||
function p256(n) { | ||
@@ -287,129 +304,155 @@ let nstr = n.toString(16); | ||
try { | ||
if (argv._[0].toUpperCase() == "INFO") { | ||
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8")); | ||
const cir = new zkSnark.Circuit(cirDef); | ||
async function run() { | ||
try { | ||
if (argv._[0].toUpperCase() == "INFO") { | ||
const cir = await loadR1cs(r1csName); | ||
console.log(`# Wires: ${cir.nVars}`); | ||
console.log(`# Constraints: ${cir.nConstraints}`); | ||
console.log(`# Private Inputs: ${cir.nPrvInputs}`); | ||
console.log(`# Public Inputs: ${cir.nPubInputs}`); | ||
console.log(`# Outputs: ${cir.nOutputs}`); | ||
console.log(`# Wires: ${cir.nVars}`); | ||
console.log(`# Constraints: ${cir.nConstraints}`); | ||
console.log(`# Private Inputs: ${cir.nPrvInputs}`); | ||
console.log(`# Public Inputs: ${cir.nPubInputs}`); | ||
console.log(`# Outputs: ${cir.nOutputs}`); | ||
} else if (argv._[0].toUpperCase() == "PRINTCONSTRAINTS") { | ||
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8")); | ||
const cir = new zkSnark.Circuit(cirDef); | ||
} else if (argv._[0].toUpperCase() == "PRINTCONSTRAINTS") { | ||
const cir = await loadR1cs(r1csName, true, true); | ||
cir.printConstraints(); | ||
const sym = await loadSyms(symName); | ||
} else if (argv._[0].toUpperCase() == "SETUP") { | ||
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8")); | ||
const cir = new zkSnark.Circuit(cirDef); | ||
printR1cs(cir, sym); | ||
} else if (argv._[0].toUpperCase() == "SETUP") { | ||
const cir = await loadR1cs(r1csName, true); | ||
if (!zkSnark[protocol]) throw new Error("Invalid protocol"); | ||
const setup = zkSnark[protocol].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"); | ||
fs.writeFileSync(verificationKeyName, JSON.stringify(stringifyBigInts(setup.vk_verifier), null, 1), "utf-8"); | ||
process.exit(0); | ||
} else if (argv._[0].toUpperCase() == "CALCULATEWITNESS") { | ||
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8")); | ||
const cir = new zkSnark.Circuit(cirDef); | ||
const input = unstringifyBigInts(JSON.parse(fs.readFileSync(inputName, "utf8"))); | ||
fs.writeFileSync(provingKeyName, JSON.stringify(stringifyBigInts(setup.vk_proof), null, 1), "utf-8"); | ||
fs.writeFileSync(verificationKeyName, JSON.stringify(stringifyBigInts(setup.vk_verifier), null, 1), "utf-8"); | ||
process.exit(0); | ||
} else if (argv._[0].toUpperCase() == "CALCULATEWITNESS") { | ||
const wasm = await fs.promises.readFile(wasmName); | ||
const input = unstringifyBigInts(JSON.parse(await fs.promises.readFile(inputName, "utf8"))); | ||
const witness = cir.calculateWitness(input, { | ||
logOutput: argv.logoutput, | ||
logSet: argv.logset, | ||
logGet: argv.logget, | ||
logTrigger: argv.logtrigger | ||
}); | ||
fs.writeFileSync(witnessName, JSON.stringify(stringifyBigInts(witness), null, 1), "utf-8"); | ||
process.exit(0); | ||
} else if (argv._[0].toUpperCase() == "PROOF") { | ||
const witness = unstringifyBigInts(JSON.parse(fs.readFileSync(witnessName, "utf8"))); | ||
const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync(provingKeyName, "utf8"))); | ||
let options; | ||
let sym; | ||
if (argv.logset || argv.logget || argv.logtrigger || argv.sanitycheck) { | ||
options = { | ||
sanityCheck: true | ||
}; | ||
if (argv.logset) { | ||
if (!sym) sym = await loadSyms(symName); | ||
options.logSetSignal= function(labelIdx, value) { | ||
console.log("SET " + sym.labelIdx2Name[labelIdx] + " <-- " + value.toString()); | ||
}; | ||
} | ||
if (argv.logget) { | ||
if (!sym) sym = await loadSyms(symName); | ||
options.logGetSignal= function(varIdx, value) { | ||
console.log("GET " + sym.labelIdx2Name[varIdx] + " --> " + value.toString()); | ||
}; | ||
} | ||
if (argv.logtrigger) { | ||
if (!sym) sym = await loadSyms(symName); | ||
options.logStartComponent= function(cIdx) { | ||
console.log("START: " + sym.componentIdx2Name[cIdx]); | ||
}; | ||
options.logFinishComponent= function(cIdx) { | ||
console.log("FINISH: " + sym.componentIdx2Name[cIdx]); | ||
}; | ||
} | ||
} | ||
const protocol = provingKey.protocol; | ||
if (!zkSnark[protocol]) throw new Error("Invalid protocol"); | ||
const {proof, publicSignals} = zkSnark[protocol].genProof(provingKey, witness); | ||
const wc = await WitnessCalculatorBuilder(wasm, options); | ||
fs.writeFileSync(proofName, JSON.stringify(stringifyBigInts(proof), null, 1), "utf-8"); | ||
fs.writeFileSync(publicName, JSON.stringify(stringifyBigInts(publicSignals), null, 1), "utf-8"); | ||
process.exit(0); | ||
} else if (argv._[0].toUpperCase() == "VERIFY") { | ||
const public = unstringifyBigInts(JSON.parse(fs.readFileSync(publicName, "utf8"))); | ||
const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8"))); | ||
const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8"))); | ||
const w = await wc.calculateWitness(input); | ||
const protocol = verificationKey.protocol; | ||
if (!zkSnark[protocol]) throw new Error("Invalid protocol"); | ||
await fs.promises.writeFile(witnessName, JSON.stringify(stringifyBigInts(w), null, 1)); | ||
const isValid = zkSnark[protocol].isValid(verificationKey, proof, public); | ||
} else if (argv._[0].toUpperCase() == "PROOF") { | ||
const witness = unstringifyBigInts(JSON.parse(fs.readFileSync(witnessName, "utf8"))); | ||
const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync(provingKeyName, "utf8"))); | ||
if (isValid) { | ||
console.log("OK"); | ||
const protocol = provingKey.protocol; | ||
if (!zkSnark[protocol]) throw new Error("Invalid protocol"); | ||
const {proof, publicSignals} = zkSnark[protocol].genProof(provingKey, witness); | ||
fs.writeFileSync(proofName, JSON.stringify(stringifyBigInts(proof), null, 1), "utf-8"); | ||
fs.writeFileSync(publicName, JSON.stringify(stringifyBigInts(publicSignals), null, 1), "utf-8"); | ||
process.exit(0); | ||
} else { | ||
console.log("INVALID"); | ||
process.exit(1); | ||
} | ||
} else if (argv._[0].toUpperCase() == "GENERATEVERIFIER") { | ||
} else if (argv._[0].toUpperCase() == "VERIFY") { | ||
const public = unstringifyBigInts(JSON.parse(fs.readFileSync(publicName, "utf8"))); | ||
const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8"))); | ||
const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8"))); | ||
const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8"))); | ||
const protocol = verificationKey.protocol; | ||
if (!zkSnark[protocol]) throw new Error("Invalid protocol"); | ||
let verifierCode; | ||
if (verificationKey.protocol == "original") { | ||
verifierCode = generateVerifier_original(verificationKey); | ||
} else if (verificationKey.protocol == "groth") { | ||
verifierCode = generateVerifier_groth(verificationKey); | ||
} else if (verificationKey.protocol == "kimleeoh") { | ||
verifierCode = generateVerifier_kimleeoh(verificationKey); | ||
} else { | ||
throw new Error("InvalidProof"); | ||
} | ||
const isValid = zkSnark[protocol].isValid(verificationKey, proof, public); | ||
fs.writeFileSync(verifierName, verifierCode, "utf-8"); | ||
process.exit(0); | ||
if (isValid) { | ||
console.log("OK"); | ||
process.exit(0); | ||
} else { | ||
console.log("INVALID"); | ||
process.exit(1); | ||
} | ||
} else if (argv._[0].toUpperCase() == "GENERATEVERIFIER") { | ||
} else if (argv._[0].toUpperCase() == "GENERATECALL") { | ||
const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8"))); | ||
const public = unstringifyBigInts(JSON.parse(fs.readFileSync(publicName, "utf8"))); | ||
const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8"))); | ||
let verifierCode; | ||
if (verificationKey.protocol == "original") { | ||
verifierCode = generateVerifier_original(verificationKey); | ||
} else if (verificationKey.protocol == "groth") { | ||
verifierCode = generateVerifier_groth(verificationKey); | ||
} else if (verificationKey.protocol == "kimleeoh") { | ||
verifierCode = generateVerifier_kimleeoh(verificationKey); | ||
} else { | ||
throw new Error("InvalidProof"); | ||
} | ||
let inputs = ""; | ||
for (let i=0; i<public.length; i++) { | ||
if (inputs != "") inputs = inputs + ","; | ||
inputs = inputs + p256(public[i]); | ||
} | ||
fs.writeFileSync(verifierName, verifierCode, "utf-8"); | ||
process.exit(0); | ||
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")||(proof.protocol == "kimleeoh")) { | ||
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 if (argv._[0].toUpperCase() == "GENERATECALL") { | ||
const public = unstringifyBigInts(JSON.parse(fs.readFileSync(publicName, "utf8"))); | ||
const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8"))); | ||
let inputs = ""; | ||
for (let i=0; i<public.length; i++) { | ||
if (inputs != "") inputs = inputs + ","; | ||
inputs = inputs + p256(public[i]); | ||
} | ||
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")||(proof.protocol == "kimleeoh")) { | ||
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"); | ||
} | ||
console.log(S); | ||
process.exit(0); | ||
} else { | ||
throw new Error("InvalidProof"); | ||
throw new Error("Invalid Command"); | ||
} | ||
console.log(S); | ||
process.exit(0); | ||
} else { | ||
throw new Error("Invalid Command"); | ||
} catch(err) { | ||
console.log(err.stack); | ||
console.log("ERROR: " + err); | ||
process.exit(1); | ||
} | ||
} catch(err) { | ||
console.log(err.stack); | ||
console.log("ERROR: " + err); | ||
process.exit(1); | ||
} | ||
@@ -416,0 +459,0 @@ |
{ | ||
"name": "snarkjs", | ||
"version": "0.1.20", | ||
"version": "0.1.21", | ||
"description": "zkSNARKs implementation in JavaScript", | ||
@@ -33,5 +33,7 @@ "main": "index.js", | ||
"chai": "^4.2.0", | ||
"circom_runtime": "0.0.3", | ||
"escape-string-regexp": "^1.0.5", | ||
"eslint": "^5.16.0", | ||
"keccak": "^2.0.0", | ||
"r1csfile": "0.0.1", | ||
"yargs": "^12.0.5" | ||
@@ -41,4 +43,5 @@ }, | ||
"eslint-plugin-mocha": "^5.3.0", | ||
"lodash": "^4.17.15", | ||
"mocha": "^5.2.0" | ||
} | ||
} |
@@ -110,3 +110,3 @@ /* | ||
if (vs!="1") { | ||
vs = vs + v.toString();; | ||
vs = vs + v.toString(); | ||
} | ||
@@ -113,0 +113,0 @@ } |
@@ -37,3 +37,3 @@ /* | ||
vk_proof : { | ||
protocol: "groth", | ||
protocol: "kimleeoh", | ||
nVars: circuit.nVars, | ||
@@ -43,3 +43,3 @@ nPublic: circuit.nPubInputs + circuit.nOutputs | ||
vk_verifier: { | ||
protocol: "groth", | ||
protocol: "kimleeoh", | ||
nPublic: circuit.nPubInputs + circuit.nOutputs | ||
@@ -46,0 +46,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
311744
44
7319
8
3
7
3
+ Addedcircom_runtime@0.0.3
+ Addedr1csfile@0.0.1
+ Addedcircom_runtime@0.0.3(transitive)
+ Addedfnv-plus@1.3.1(transitive)
+ Addedr1csfile@0.0.1(transitive)