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.

67 lines
2.1 KiB

  1. const chai = require("chai");
  2. const fs = require("fs");
  3. const bigInt = require("../src/bigint.js");
  4. const Circuit = require("../src/circuit.js");
  5. const zkSnark = require("../index.js");
  6. const assert = chai.assert;
  7. function stringifyBigInts(o) {
  8. if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
  9. return o.toString(10);
  10. } else if (Array.isArray(o)) {
  11. return o.map(stringifyBigInts);
  12. } else if (typeof o == "object") {
  13. const res = {};
  14. for (let k in o) {
  15. res[k] = stringifyBigInts(o[k]);
  16. }
  17. return res;
  18. } else {
  19. return o;
  20. }
  21. }
  22. function unstringifyBigInts(o) {
  23. if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
  24. return bigInt(o);
  25. } else if (Array.isArray(o)) {
  26. return o.map(unstringifyBigInts);
  27. } else if (typeof o == "object") {
  28. const res = {};
  29. for (let k in o) {
  30. res[k] = unstringifyBigInts(o[k]);
  31. }
  32. return res;
  33. } else {
  34. return o;
  35. }
  36. }
  37. describe("zkSnark", () => {
  38. it("Load a circuit, create trusted setup, create a proof and validate", () => {
  39. const cirDef = JSON.parse(fs.readFileSync("../jaz/sum.json", "utf8"));
  40. const cir = new Circuit(cirDef);
  41. const setup = zkSnark.setup(cir);
  42. const strSetup = stringifyBigInts(setup);
  43. fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
  44. fs.writeFileSync("vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
  45. /*
  46. const setup = {};
  47. setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
  48. setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
  49. */
  50. const witness = cir.calculateWitness({"a": "33", "b": "34"});
  51. assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
  52. const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
  53. assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
  54. }).timeout(10000000);
  55. });