You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # snarkjs: JavaScript implementation of zkSNARKs.
  2. This is a JavaScript implementation of zkSNARK schemes.
  3. This library allows to do the trusted setup, generate proofs and verify the proofs.
  4. This library uses the compiled circuits generated by the jaz compiler.
  5. ## Install.
  6. ```sh
  7. npm install snarkjs
  8. ```
  9. ## Usage from command line.
  10. ```sh
  11. snarkjs --help
  12. ```
  13. Will show all the info in how to use the cli.
  14. ## Ussage from jacascript
  15. ### Import.
  16. ```js
  17. const zkSnark = require("snarkjs");
  18. ```
  19. ### Load a circuit.
  20. ```js
  21. // "myCircuit.cir" is the output of the jaz compiler
  22. const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
  23. const circuit = new zkSnark.Circuit(circuitDef);
  24. ```
  25. ### Inspect the circuit.
  26. ```js
  27. // `signalId` can always be a number or an alias string
  28. circuit.nConstraints; // number of constraints
  29. circuit.nSignals; // number of signals
  30. circuit.nPublic; // number of public signals (nOutputs + nPublicInputs)
  31. // The array of signals is always sorted in this order:
  32. // [ 1, outputs, publicInputs, privateInputs, internalSignals, constants]
  33. // returns a,b and c coeficients of the `signalId` on a given `constraint`
  34. circuit.a(constraint, signalId)
  35. circuit.b(constraint, signalId)
  36. circuit.c(constraint, signalId)
  37. circuit.nOutputs // number of public outputs
  38. circuit.pubInputs // number of public inputs
  39. circuit.nPrvInputs // number of private inputs
  40. circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs)
  41. circuit.nVars // number of variables ( not including constants (one is a variable) )
  42. circuit.nSignals // number of signals ( including constants )
  43. circuit.outputIdx(i) // returns the index of the i'th output
  44. circuit.inputIdx(i) // returns the index of the i'th input
  45. circuit.pubInputIdx(i) // returns the index of the i'th public input
  46. circuit.prvInputIdx(i) // returns the index of the i'th private input
  47. circuit.varIdx(i) // returns the index of the i'th variable
  48. circuit.constantIdx(i) // returns the index of the i'th constant
  49. circuit.signalIdx(i) // returns the index of the i'th signal
  50. // returns signal Idx given a signalId
  51. // if the idx >= n , it is a constant
  52. // if the idx == -1, the signal does not exist
  53. circuit.getSignalIdx(name);
  54. // returns an array aliases names of the i'th signal
  55. circuit.signalNames(i)
  56. // input is a key value object where keys are the signal names
  57. // of all the inputs (public and private)
  58. // returns an array of values representing the witness
  59. circuit.calculateWitness(input)
  60. ```
  61. ### Trusted setup.
  62. ```js
  63. const setup = zkSnark.setup(circuit);
  64. fs.writeFileSync("myCircuit.vk_proof", JSON.stringify(setup.vk_proof), "utf8");
  65. fs.writeFileSync("myCircuit.vk_verifier", JSON.stringify(setup.vk_verifier), "utf8");
  66. setup.toxic // Must be discarded.
  67. ```
  68. ### Generate proof.
  69. ```js
  70. const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
  71. const circuit = new zkSnark.Circuit(circuitDef);
  72. const input = {
  73. "main.pubIn1": "123",
  74. "main.out1": "456"
  75. }
  76. const witness = circuit.calculateWitness(input);
  77. const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8"));
  78. const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness);
  79. ```
  80. ### Verifier.
  81. ```js
  82. const vk_verifier = JSON.parse(fs.readFileSync("myCircuit.vk_verifier", "utf8"));
  83. if (zkSnark.isValid(vk_verifier, proof, publicSignals)) {
  84. console.log("The proof is valid");
  85. } else {
  86. console.log("The proof is not valid");
  87. }
  88. ```
  89. ## License
  90. snarkjs is part of the iden3 project copyright 2018 0KIMS association and published with GPL-3 license. Please check the COPYING file for more details.