tornado-core/circuits/withdraw.circom

67 lines
2.3 KiB
Plaintext
Raw Normal View History

2019-07-09 15:05:30 +02:00
include "../node_modules/circomlib/circuits/bitify.circom";
include "../node_modules/circomlib/circuits/pedersen.circom";
include "merkleTree.circom";
2019-07-10 14:35:46 +02:00
// computes Pedersen(nullifier + secret)
2019-07-09 15:05:30 +02:00
template CommitmentHasher() {
2019-07-19 18:37:38 +02:00
signal private input nullifier;
2019-07-09 15:05:30 +02:00
signal private input secret;
2019-07-19 18:37:38 +02:00
signal output commitment;
signal output nullifierHash;
2019-07-09 15:05:30 +02:00
2019-08-01 16:49:34 +02:00
component commitmentHasher = Pedersen(496);
component nullifierHasher = Pedersen(248);
component nullifierBits = Num2Bits(248);
component secretBits = Num2Bits(248);
2019-07-09 15:05:30 +02:00
nullifierBits.in <== nullifier;
secretBits.in <== secret;
2019-08-01 16:49:34 +02:00
for (var i = 0; i < 248; i++) {
2019-07-19 18:37:38 +02:00
nullifierHasher.in[i] <== nullifierBits.out[i];
commitmentHasher.in[i] <== nullifierBits.out[i];
2019-08-01 16:49:34 +02:00
commitmentHasher.in[i + 248] <== secretBits.out[i];
2019-07-09 15:05:30 +02:00
}
2019-07-19 18:37:38 +02:00
commitment <== commitmentHasher.out[0];
nullifierHash <== nullifierHasher.out[0];
2019-07-09 15:05:30 +02:00
}
2019-07-10 14:35:46 +02:00
// Verifies that commitment that corresponds to given secret and nullifier is included in the merkle tree of deposits
2019-07-09 15:05:30 +02:00
template Withdraw(levels, rounds) {
signal input root;
2019-07-19 18:37:38 +02:00
signal input nullifierHash;
2019-07-09 15:05:30 +02:00
signal input receiver; // not taking part in any computations
2019-09-06 22:54:37 +02:00
signal input relayer; // not taking part in any computations
2019-07-09 15:05:30 +02:00
signal input fee; // not taking part in any computations
2019-07-19 18:37:38 +02:00
signal private input nullifier;
2019-07-09 15:05:30 +02:00
signal private input secret;
signal private input pathElements[levels];
signal private input pathIndex[levels];
component hasher = CommitmentHasher();
hasher.nullifier <== nullifier;
hasher.secret <== secret;
2019-07-19 18:37:38 +02:00
nullifierHash === hasher.nullifierHash;
2019-07-09 15:05:30 +02:00
component tree = MerkleTree(levels, rounds);
2019-07-19 18:37:38 +02:00
tree.leaf <== hasher.commitment;
2019-07-10 14:35:46 +02:00
tree.root <== root;
for (var i = 0; i < levels; i++) {
tree.pathElements[i] <== pathElements[i];
tree.pathIndex[i] <== pathIndex[i];
}
2019-08-02 11:24:37 +02:00
// Add hidden signals to make sure that tampering with receiver 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 receiverSquare;
signal feeSquare;
2019-09-06 22:54:37 +02:00
signal relayerSquare;
2019-08-02 11:24:37 +02:00
receiverSquare <== receiver * receiver;
feeSquare <== fee * fee;
2019-09-06 22:54:37 +02:00
relayerSquare <== relayer * relayer;
2019-07-09 15:05:30 +02:00
}
2019-08-01 21:31:18 +02:00
component main = Withdraw(16, 220);