arnaucube
arnaucube/miksi-corePublic Self-hosted repository
miksi-core/circuits/deposit.circom
circuits/deposit.circomRaw
 1/*
 2
 3# deposit.circom
 4
 5                     +----------+              +----------+
 6PUB_nullifier+------>+          |              |          |
 7                     |          |              | SMT      |
 8PUB_coinCode+------->+          |              | Poseidon +<------+PUB_rootOld
 9                     | Poseidon +-+----------->+ Verifier |
10PUB_amount+--------->+          | |            | Non      |
11                     |          | |            | Existance+<------+PRI_siblings
12PRI_secret+--------->+          | |            |          |          +
13                     +----------+ |            +----------+          |
14                                  |                                  |
15                                  |                                  |
16                                  |            +----------+          |
17                                  |            |          |          |
18                                  |            |          |          |
19                   +----+         |            | SMT      +<---------+
20PUB_commitment+----> == +<--------+----------->+ Poseidon |
21                   +----+                      | Verifier |
22                                               |          +<------+PUB_rootNew
23                                               |          |
24                                               +----------+
25
26
27*/
28
29include "../node_modules/circomlib/circuits/comparators.circom";
30include "../node_modules/circomlib/circuits/poseidon.circom";
31include "../node_modules/circomlib/circuits/smt/smtverifier.circom";
32
33template Deposit(nLevels) {
34	signal input coinCode;
35	signal input amount;
36	signal private input secret;
37	signal private input nullifier;
38	signal private input oldKey;
39	signal private input siblingsOld[nLevels];
40	signal private input siblingsNew[nLevels];
41	signal input rootOld;
42	signal input rootNew;
43	signal input commitment;
44
45	component hash = Poseidon(4, 6, 8, 57);
46	hash.inputs[0] <== coinCode;
47	hash.inputs[1] <== amount;
48	hash.inputs[2] <== secret;
49	hash.inputs[3] <== nullifier;
50
51	component comCheck = IsEqual();
52	comCheck.in[0] <== hash.out;
53	comCheck.in[1] <== commitment;
54	comCheck.out === 1;
55
56	// TODO instead of 2 siblings input, get siblingsOld from siblingsNew[len-1]
57
58	component smtOld = SMTVerifier(nLevels);
59	smtOld.enabled <== 1;
60	smtOld.fnc <== 1;
61	smtOld.root <== rootOld;
62	for (var i=0; i<nLevels; i++) {
63		smtOld.siblings[i] <== siblingsOld[i];
64	}
65	/* smtOld.oldKey <== 1; */
66	smtOld.oldKey <== oldKey;
67	smtOld.oldValue <== 0;
68	smtOld.isOld0 <== 0;
69	smtOld.key <== hash.out;
70	smtOld.value <== 0;
71
72	component smtNew = SMTVerifier(nLevels);
73	smtNew.enabled <== 1;
74	smtNew.fnc <== 0;
75	smtNew.root <== rootNew;
76	for (var i=0; i<nLevels; i++) {
77		smtNew.siblings[i] <== siblingsNew[i];
78	}
79	smtNew.oldKey <== 0;
80	smtNew.oldValue <== 0;
81	smtNew.isOld0 <== 0;
82	smtNew.key <== hash.out;
83	smtNew.value <== 0;
84}
85
86component main = Deposit(5);