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.

112 lines
3.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # 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. ```
  7. npm install zksnark
  8. ```
  9. ## Usage.
  10. ### Import.
  11. ```
  12. const zkSnark = require("zksnark");
  13. ```
  14. ### Load a circuit.
  15. ```
  16. // "myCircuit.cir" is the output of the jaz compiler
  17. const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
  18. const circuit = new zkSnark.Circuit(circuitDef);
  19. ```
  20. ### Inspect the circuit.
  21. ```
  22. // `signalId` can always be a number or an alias string
  23. circuit.nConstraints; // number of constraints
  24. circuit.nSignals; // number of signals
  25. circuit.nPublic; // number of public signals (nOutputs + nPublicInputs)
  26. // The array of signals is always sorted in this order:
  27. // [ 1, outputs, publicInputs, privateInputs, internalSignals, constants]
  28. // returns a,b and c coeficients of the `signalId` on a given `constraint`
  29. circuit.a(constraint, signalId)
  30. circuit.b(constraint, signalId)
  31. circuit.c(constraint, signalId)
  32. circuit.nOutputs // number of public outputs
  33. circuit.pubInputs // number of public inputs
  34. circuit.nPrvInputs // number of private inputs
  35. circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs)
  36. circuit.nVars // number of variables ( not including constants (one is a variable) )
  37. circuit.nSignals // number of signals ( including constants )
  38. circuit.outputIdx(i) // returns the index of the i'th output
  39. circuit.inputIdx(i) // returns the index of the i'th input
  40. circuit.pubInputIdx(i) // returns the index of the i'th public input
  41. circuit.prvInputIdx(i) // returns the index of the i'th private input
  42. circuit.varIdx(i) // returns the index of the i'th variable
  43. circuit.constantIdx(i) // returns the index of the i'th constant
  44. circuit.signalIdx(i) // returns the index of the i'th signal
  45. // returns signal Idx given a signalId
  46. // if the idx >= n , it is a constant
  47. // if the idx == -1, the signal does not exist
  48. circuit.signalId2idx(signalId);
  49. // returns an array aliases names for a given signalId
  50. circuit.signalNames(signalId)
  51. // input is a key value object where keys are the signal names
  52. // of all the inputs (public and private)
  53. // returns an array of values representing the witness
  54. circuit.calculateWitness(input)
  55. ```
  56. ### Trusted setup.
  57. ```
  58. const setup = zkSnark.setup(circuit);
  59. fs.writeFileSink("myCircuit.vk_proof", JSON.stringify(setup.vk_proof), "utf8");
  60. fs.writeFileSink("myCircuit.vk_verifier", JSON.stringify(setup.vk_verifier), "utf8");
  61. setup.toxic // Must be discarded.
  62. ```
  63. ### Generate proof.
  64. ```
  65. const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
  66. const circuit = new zkSnark.Circuit(circuitDef);
  67. const input = {
  68. "main.pubIn1": "123",
  69. "main.out1": "456"
  70. }
  71. const witness = circuit.calculateWitness(input);
  72. const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8"));
  73. const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness);
  74. ```
  75. ### Verifier.
  76. ```
  77. const vk_verifier = JSON.parse(fs.readFileSync("myCircuit.vk_verifier", "utf8"));
  78. if (zkSnark.isValid(vk_verifier, proof, publicSignals)) {
  79. console.log("The proof is valid");
  80. } else {
  81. console.log("The proof is not valid");
  82. }
  83. ```