tornado-core/circuits/withdraw.circom

67 lines
2.3 KiB
Plaintext
Raw Normal View History

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