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.

107 lines
3.0 KiB

5 years ago
  1. # javascript implementation of zkSnark
  2. This is a javascript implementation of zkSnarks.
  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.m; // number of constrains
  24. circuit.n; // number of signals
  25. circuit.p; // number of public signals (nPublicInputs + nOutputs)
  26. // The array of signals is always sorted in this order:
  27. // [ outputs, publicInputs, privedInputs, internalSignals, constants]
  28. // returns a,b and c coeficients of the `signalId` on a given `constrain`
  29. circuit.a(constrain, signalId)
  30. circuit.b(constrain, signalId)
  31. circuit.c(constrain, signalId)
  32. circuit.nOutputs // number of public outputs
  33. circuit.nPublicInputs // number of public inputs
  34. circuit.nPrivateInputs // number of private inputs
  35. circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs)
  36. circuit.outputIdx(i) // returns the index of the i'th output
  37. circuit.inputIdx(i) // returns the index of the i'th input
  38. circuit.inputPublicIdx(i) // returns the index of the i'th public input
  39. circuit.inputPrivateIdx(i) // returns the index of the i'th private input
  40. // returns signal Idx given a signalId
  41. // if the idx >= n , it is a constant
  42. // if the idx == -1, the signal does not exist
  43. circuit.signalId2idx(signalId);
  44. // returns an array aliases names for a given signalId
  45. circuit.signalNames(signalId)
  46. // input is a key value object where keys are the signal names
  47. // of all the inputs (public and private)
  48. // returns an array of values that represent the witness
  49. circuit.generateWitness(input)
  50. ```
  51. ### Trusted setup
  52. ```
  53. const setup = zkSnark.setup(circuit);
  54. fs.writeFileSink("myCircuit.vk_proof", JSON.stringify(setup.vk_proof), "utf8");
  55. fs.writeFileSink("myCircuit.vk_verifier", JSON.stringify(setup.vk_verifier), "utf8");
  56. setup.toxic // Must be discarded.
  57. ```
  58. ### Generate proof
  59. ```
  60. const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
  61. const circuit = new zkSnark.Circuit(circuitDef);
  62. const input = {
  63. "main.pubIn1": "123",
  64. "main.out1": "456"
  65. }
  66. const witness = circuit.generateWitness(input);
  67. const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8"));
  68. const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness);
  69. ```
  70. ### Verifier
  71. ```
  72. const vk_verifier = JSON.parse(fs.readFileSync("myCircuit.vk_verifier", "utf8"));
  73. if (zkSnark.isValid(vk_verifier, proof, publicSignals)) {
  74. console.log("The proof is valid");
  75. } else {
  76. console.log("The proof is not valid");
  77. }
  78. ```