miksi-core/test/circuits/deposit.test.ts
test/circuits/deposit.test.tsRaw
1const path = require("path");
2const tester = require("circom").tester;
3const chai = require("chai");
4const assert = chai.assert;
5const circomlib = require("circomlib");
6const smt = require("circomlib").smt;
7
8export {};
9
10describe("deposit test", function () {
11 this.timeout(200000);
12
13
14 it("Test Deposit", async () => {
15 const circuit = await tester(
16 path.join(__dirname, "../../circuits", "deposit.circom"),
17 {reduceConstraints: false}
18 );
19
20 const nLevels = 5;
21 const secret = "1234567890";
22
23 const coinCode = "0";
24 const amount = '1000000000000000000';
25 const nullifier = "567891234";
26
27 const poseidon = circomlib.poseidon.createHash(6, 8, 57);
28 const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
29
30 // add commitment into SMT
31 let tree = await smt.newMemEmptyTrie();
32 await tree.insert(1, 0);
33 // await tree.insert(2, 0);
34
35 let rootOld = tree.root;
36 let res = await tree.find(commitment);
37 // console.log(res);
38 assert(!res.found);
39 let siblingsOld = res.siblings;
40 while (siblingsOld.length < nLevels) {
41 siblingsOld.push("0");
42 };
43 console.log("siblingsOld", siblingsOld);
44
45 await tree.insert(commitment, 0);
46 let rootNew = tree.root;
47
48 res = await tree.find(commitment);
49 // console.log(res);
50 assert(res.found);
51 let siblingsNew = res.siblings;
52 while (siblingsNew.length < nLevels) {
53 siblingsNew.push("0");
54 };
55 console.log("siblingsNew", siblingsNew);
56 console.log("rootOld", rootOld);
57 console.log("rootNew", rootNew);
58
59 const witness = await circuit.calculateWitness({
60 "coinCode": coinCode,
61 "amount": amount,
62 "secret": secret,
63 "nullifier": nullifier,
64 "oldKey": "1",
65 "siblingsOld": siblingsOld,
66 "siblingsNew": siblingsNew,
67 "rootOld": rootOld,
68 "rootNew": rootNew,
69 "commitment": commitment
70 });
71 await circuit.checkConstraints(witness);
72 });
73});