-
Notifications
You must be signed in to change notification settings - Fork 15
/
withdraw.circom
57 lines (44 loc) · 1.69 KB
/
withdraw.circom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
include "./merkleTree.circom";
template CommitmentHasher() {
signal input secret;
signal input nullifier;
signal output nullifierHash;
signal output commitment;
component hasher = HashLeftRight();
hasher.left <== secret;
hasher.right <== nullifier;
component hasherNullifier = HashLeftRight();
hasherNullifier.left <== nullifier;
hasherNullifier.right <== nullifier;
commitment <== hasher.hash;
nullifierHash <== hasherNullifier.hash;
}
template Withdraw(levels) {
signal input root;
signal input nullifierHash;
signal input recipient; // not taking part in any computations
signal input fee; // not taking part in any computations
signal private input nullifier;
signal private input secret;
signal private input pathElements[levels];
signal private input pathIndices[levels];
component hasher = CommitmentHasher();
hasher.nullifier <== nullifier;
hasher.secret <== secret;
hasher.nullifierHash === nullifierHash;
component tree = MerkleTreeCheck(levels);
tree.leaf <== hasher.commitment;
tree.root <== root;
for (var i = 0; i < levels; i++) {
tree.pathElements[i] <== pathElements[i];
tree.pathIndices[i] <== pathIndices[i];
}
// Add hidden signals to make sure that tampering with recipient or fee will invalidate the snark proof
// Most likely it is not required, but it's better to stay on the safe side and it only takes 2 constraints
// Squares are used to prevent optimizer from removing those constraints
signal recipientSquare;
signal feeSquare;
recipientSquare <== recipient * recipient;
feeSquare <== fee * fee;
}
component main = Withdraw(20);