tornado-core/circuits/withdraw.circom

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