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.

66 lines
2.7 KiB

  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. const fs = require("fs");
  11. const { groth } = require('snarkjs');
  12. const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
  13. const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
  14. const circomlib = require("circomlib");
  15. const smt = require("circomlib").smt;
  16. const Web3 = require("web3");
  17. const nLevels = 5;
  18. const coinCode = "0"; // refearing to ETH
  19. const ethAmount = '1';
  20. const amount = Web3.utils.toWei(ethAmount, 'ether');
  21. exports.calcWitness = (wasm, secret, nullifier, commitments) => __awaiter(this, void 0, void 0, function* () {
  22. const poseidon = circomlib.poseidon.createHash(6, 8, 57);
  23. const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
  24. let tree = yield smt.newMemEmptyTrie();
  25. yield tree.insert(1, 0);
  26. // old root
  27. const rootOld = tree.root;
  28. const resOld = yield tree.find(commitment);
  29. if (resOld.found) {
  30. console.error("leaf expect to not exist but exists");
  31. }
  32. let siblingsOld = resOld.siblings;
  33. while (siblingsOld.length < nLevels) {
  34. siblingsOld.push("0");
  35. }
  36. ;
  37. yield tree.insert(commitment, 0);
  38. // new root
  39. const rootNew = tree.root;
  40. const resNew = yield tree.find(commitment);
  41. if (!resNew.found) {
  42. console.error("leaf expect to exist but not exists");
  43. }
  44. let siblingsNew = resNew.siblings;
  45. while (siblingsNew.length < nLevels) {
  46. siblingsNew.push("0");
  47. }
  48. ;
  49. // calculate witness
  50. const input = unstringifyBigInts({
  51. "coinCode": coinCode,
  52. "amount": amount,
  53. "secret": secret,
  54. "nullifier": nullifier,
  55. "siblingsOld": siblingsOld,
  56. "siblingsNew": siblingsNew,
  57. "rootOld": rootOld,
  58. "rootNew": rootNew,
  59. "commitment": commitment
  60. });
  61. const options = {};
  62. const wc = yield WitnessCalculatorBuilder(wasm, options);
  63. const w = yield wc.calculateWitness(input);
  64. const witness = unstringifyBigInts(stringifyBigInts(w));
  65. return witness;
  66. });
  67. //# sourceMappingURL=miksi.js.map