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.

76 lines
1.7 KiB

5 years ago
  1. const {unstringifyBigInts} = require("./stringifybigint.js");
  2. const fs = require("fs");
  3. const assert = require("assert");
  4. const version = require("../package").version;
  5. const argv = require("yargs")
  6. .version(version)
  7. .usage(`node buildpkey.js -i "witness.json" -o "witness.bin"
  8. Default: circuit.json
  9. `)
  10. .alias("i", "input")
  11. .alias("o", "output")
  12. .help("h")
  13. .alias("h", "help")
  14. .epilogue(`Copyright (C) 2018 0kims association
  15. This program comes with ABSOLUTELY NO WARRANTY;
  16. This is free software, and you are welcome to redistribute it
  17. under certain conditions; see the COPYING file in the official
  18. repo directory at https://github.com/iden3/circom `)
  19. .argv;
  20. const inputName = (argv.input) ? argv.input : "witness.json";
  21. const outputName = (argv.output) ? argv.output : "witness.bin";
  22. const witness = unstringifyBigInts(JSON.parse(fs.readFileSync(inputName, "utf8")));
  23. function writeUint32(h, val) {
  24. h.dataView.setUint32(h.offset, val, true);
  25. h.offset += 4;
  26. }
  27. function writeBigInt(h, bi) {
  28. for (let i=0; i<8; i++) {
  29. const v = bi.shiftRight(i*32).and(0xFFFFFFFF).toJSNumber();
  30. writeUint32(h, v);
  31. }
  32. }
  33. function calculateBuffLen(witness) {
  34. let size = 0;
  35. // beta2, delta2
  36. size += witness.length * 32;
  37. return size;
  38. }
  39. const buffLen = calculateBuffLen(witness);
  40. const buff = new ArrayBuffer(buffLen);
  41. const h = {
  42. dataView: new DataView(buff),
  43. offset: 0
  44. };
  45. // writeUint32(h, witness.length);
  46. for (let i=0; i<witness.length; i++) {
  47. writeBigInt(h, witness[i]);
  48. }
  49. assert.equal(h.offset, buffLen);
  50. var wstream = fs.createWriteStream(outputName);
  51. wstream.write(Buffer.from(buff));
  52. wstream.end();