forked from tornadocash/snarkjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3f2f303
Showing
15 changed files
with
1,471 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module.exports = { | ||
"plugins": [ | ||
"mocha" | ||
], | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2017 | ||
}, | ||
"extends": "eslint:recommended", | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
4 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"double" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"mocha/no-exclusive-tests": "error" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next | ||
|
||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# javascript implementation of zkSnark | ||
|
||
This is a javascript implementation of zkSnarks. | ||
|
||
This library allows to do the trusted setup, generate proofs and verify the proofs. | ||
|
||
This library uses the compiled circuits generated by the jaz compiler. | ||
|
||
## Install | ||
|
||
``` | ||
npm install zkSnark | ||
``` | ||
|
||
## Usage | ||
|
||
### import | ||
|
||
``` | ||
const zkSnark = require("zksnark"); | ||
``` | ||
|
||
### Load a circuit. | ||
|
||
``` | ||
// "myCircuit.cir" is the output of the jaz compiler | ||
|
||
const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8")); | ||
const circuit = new zkSnark.Circuit(circuitDef); | ||
``` | ||
|
||
### Inspect the circuit. | ||
|
||
``` | ||
// `signalId` can always be a number or an alias string | ||
|
||
circuit.m; // number of constrains | ||
circuit.n; // number of signals | ||
circuit.p; // number of public signals (nPublicInputs + nOutputs) | ||
|
||
// The array of signals is always sorted in this order: | ||
// [ outputs, publicInputs, privedInputs, internalSignals, constants] | ||
|
||
// returns a,b and c coeficients of the `signalId` on a given `constrain` | ||
circuit.a(constrain, signalId) | ||
circuit.b(constrain, signalId) | ||
circuit.c(constrain, signalId) | ||
|
||
circuit.nOutputs // number of public outputs | ||
circuit.nPublicInputs // number of public inputs | ||
circuit.nPrivateInputs // number of private inputs | ||
circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs) | ||
|
||
circuit.outputIdx(i) // returns the index of the i'th output | ||
circuit.inputIdx(i) // returns the index of the i'th input | ||
circuit.inputPublicIdx(i) // returns the index of the i'th public input | ||
circuit.inputPrivateIdx(i) // returns the index of the i'th private input | ||
|
||
// returns signal Idx given a signalId | ||
// if the idx >= n , it is a constant | ||
// if the idx == -1, the signal does not exist | ||
circuit.signalId2idx(signalId); | ||
|
||
// returns an array aliases names for a given signalId | ||
circuit.signalNames(signalId) | ||
|
||
// input is a key value object where keys are the signal names | ||
// of all the inputs (public and private) | ||
// returns an array of values that represent the witness | ||
circuit.generateWitness(input) | ||
``` | ||
|
||
### Trusted setup | ||
|
||
``` | ||
const setup = zkSnark.setup(circuit); | ||
fs.writeFileSink("myCircuit.vk_proof", JSON.stringify(setup.vk_proof), "utf8"); | ||
fs.writeFileSink("myCircuit.vk_verifier", JSON.stringify(setup.vk_verifier), "utf8"); | ||
setup.toxic // Must be discarded. | ||
``` | ||
|
||
### Generate proof | ||
|
||
``` | ||
const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8")); | ||
const circuit = new zkSnark.Circuit(circuitDef); | ||
const input = { | ||
"main.pubIn1": "123", | ||
"main.out1": "456" | ||
} | ||
const witness = circuit.generateWitness(input); | ||
const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8")); | ||
|
||
const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness); | ||
``` | ||
|
||
### Verifier | ||
|
||
``` | ||
const vk_verifier = JSON.parse(fs.readFileSync("myCircuit.vk_verifier", "utf8")); | ||
|
||
if (zkSnark.isValid(vk_verifier, proof, publicSignals)) { | ||
console.log("The proof is valid"); | ||
} else { | ||
console.log("The proof is not valid"); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
exports.Circuit = require "./src/circuit.js"; | ||
exports.setup = require "./src/setup.js"; | ||
exports.genProof = require "./src/prover.js"; | ||
exports.isValid = require "./src/verifier.js"; |
Oops, something went wrong.