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.

98 lines
2.8 KiB

5 years ago
  1. const path = require("path");
  2. const snarkjs = require("snarkjs");
  3. const compiler = require("circom");
  4. const fs = require("fs")
  5. const bigInt = snarkjs.bigInt;
  6. const smt = require("../src/smt.js");
  7. const circuitSource = `
  8. include "../circuits/smt/smtverifier.circom";
  9. template SMT(nLevels) {
  10. signal input root;
  11. signal input mtp[nLevels];
  12. signal input hi;
  13. signal input hv;
  14. component smtClaimExists = SMTVerifier(nLevels);
  15. smtClaimExists.enabled <== 1;
  16. smtClaimExists.fnc <== 0;
  17. smtClaimExists.root <== root;
  18. for (var i=0; i<nLevels; i++) {
  19. smtClaimExists.siblings[i] <== mtp[i];
  20. }
  21. smtClaimExists.oldKey <== 0;
  22. smtClaimExists.oldValue <== 0;
  23. smtClaimExists.isOld0 <== 0;
  24. smtClaimExists.key <== hi;
  25. smtClaimExists.value <== hv;
  26. }
  27. component main = SMT(4);
  28. `;
  29. describe("smt3test", function () {
  30. this.timeout(200000);
  31. let circuitFileName;
  32. before( async () => {
  33. circuitFileName = path.join(__dirname, ".", "rawsmt3.circom");
  34. fs.writeFileSync(circuitFileName,circuitSource);
  35. });
  36. const levels = 4;
  37. async function testsmt3(e1, e2) {
  38. let tree = await smt.newMemEmptyTrie();
  39. // insert e1, e2
  40. await tree.insert(e1.hi, e1.hv);
  41. await tree.insert(e2.hi, e2.hv);
  42. // generate proof for e1
  43. const findInfo = await tree.find(e1.hi);
  44. const siblings = findInfo.siblings;
  45. while (siblings.length < levels) siblings.push(bigInt(0));
  46. const input = {
  47. root: tree.root,
  48. mtp: siblings,
  49. hi: e1.hi,
  50. hv: e1.hv,
  51. };
  52. const compiledCircuit = await compiler(
  53. circuitFileName,
  54. { reduceConstraints: false }
  55. );
  56. const circuit = new snarkjs.Circuit(compiledCircuit);
  57. const witness = circuit.calculateWitness(input);
  58. circuit.checkWitness(witness);
  59. }
  60. it("TestSmts", async () => {
  61. const e1 = {
  62. hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"),
  63. hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
  64. };
  65. const e2ok = {
  66. hi: bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"),
  67. hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
  68. };
  69. const e2fail = {
  70. hi: bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"),
  71. hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
  72. };
  73. console.log("test e1, e2ok");
  74. await testsmt3(e1, e2ok);
  75. console.log("test e1, e2fail");
  76. await testsmt3(e1, e2fail);
  77. });
  78. });