tornado-core/circuits/withdraw.circom

68 lines
2.5 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() {
signal input nullifier;
signal 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-11-01 21:33:19 -04:00
template Withdraw(levels) {
2019-07-09 09:05:30 -04:00
signal input root;
2019-07-19 12:37:38 -04:00
signal input nullifierHash;
2019-11-07 02:04:29 -05:00
signal input recipient; // not taking part in any computations
2019-11-01 21:32:28 -04:00
signal input relayer; // not taking part in any computations
signal input fee; // not taking part in any computations
signal input refund; // 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];
2019-11-01 22:05:25 -04:00
signal private input pathIndices[levels];
2019-07-09 09:05:30 -04:00
component hasher = CommitmentHasher();
hasher.nullifier <== nullifier;
hasher.secret <== secret;
2019-11-03 03:41:05 -05:00
hasher.nullifierHash === nullifierHash;
2019-07-19 12:37:38 -04:00
component tree = MerkleTreeChecker(levels);
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];
2019-11-01 22:05:25 -04:00
tree.pathIndices[i] <== pathIndices[i];
2019-07-10 08:35:46 -04:00
}
// 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;
signal relayerSquare;
signal refundSquare;
recipientSquare <== recipient * recipient;
feeSquare <== fee * fee;
relayerSquare <== relayer * relayer;
refundSquare <== refund * refund;
2019-07-09 09:05:30 -04:00
}
2019-12-11 05:15:07 -05:00
component main = Withdraw(20);