<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>blockdev</title>
        <link>https://paragraph.com/@blockdev</link>
        <description>ZK security engineer at Ethereum Foundation, smart contract auditor at Spearbit.</description>
        <lastBuildDate>Wed, 29 Apr 2026 15:56:43 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <image>
            <title>blockdev</title>
            <url>https://storage.googleapis.com/papyrus_images/32ca4317c285ba5c589cb4ea4aef9ebc9c32ecaa7a8baedec24a40ff927d8fe5.png</url>
            <link>https://paragraph.com/@blockdev</link>
        </image>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[🌪Tornado Cash 101🌪]]></title>
            <link>https://paragraph.com/@blockdev/tornado-cash-101</link>
            <guid>OciAI5NvRLceImDLNkfy</guid>
            <pubDate>Mon, 25 Jul 2022 17:43:38 GMT</pubDate>
            <description><![CDATA[I hope this article helps you understand how Tornado Cash works on a technical level. I’ll assume you are already familiar with Smart contracts, merkle trees, and have a basic understanding of Zero Knowledge Proofs (ZKP) or Circom circuits. A Tornado Cash system lets an address A deposit some constant amount of ETH (say 1 ETH), and lets another address B withdraw it, breaking the link between these two addresses. On a high level, this is how it works:Before a deposit, A generates some secret ...]]></description>
            <content:encoded><![CDATA[<p>I hope this article helps you understand how Tornado Cash works on a technical level. I’ll assume you are already familiar with Smart contracts, merkle trees, and have a basic understanding of Zero Knowledge Proofs (ZKP) or Circom circuits.</p><p>A Tornado Cash system lets an address <code>A</code> deposit some constant amount of ETH (say 1 ETH), and lets another address <code>B</code> withdraw it, breaking the link between these two addresses. On a high level, this is how it works:</p><ul><li><p>Before a deposit, <code>A</code> generates some secret data, hashes it and sends it to Tornado Cash’s contract <code>C</code> along with 1 ETH.</p></li><li><p><code>C</code> adds this hash of secret data into its data structure.</p></li><li><p><code>A</code> shares the secret data with <code>B</code>.</p></li><li><p><code>B</code> uses ZKP to prove to <code>C</code> that it knows the secret data, and sends the proof to <code>C</code>. The secret data is kept private from <code>C</code> and any observer using ZKP.</p></li><li><p><code>C</code> verifies the proof, and if correct, transfers 1 ETH to <code>B</code>, Otherwise it rejects the proof as invalid and no ETH is transferred.</p></li></ul><p>The rest of the focus of this article is to understand the setup that makes this entire thing possible, i.e.:</p><ul><li><p><code>B</code> can withdraw from <code>C</code> only if it proves that it knows a secret corresponding to some deposit.</p></li><li><p>Once a secret is used to withdraw, it cannot be used again. Otherwise, it would be possible to drain all the ETH deposited in <code>C</code>. You may recognize this as the <em>double spending</em> problem.</p></li></ul><p>So, let’s dive right into it…</p><hr><h2 id="h-secret-hashes-merkle-tree" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Secret, Hashes, Merkle tree</h2><p>We’ll first forgo the double spending issue and focus on how someone knowing a valid secret data can withdraw from <code>C</code> without revealing the secret. Tornado Cash system has 2 components. The first is a smart contract deployed on-chain. It’s responsible for handling deposits and withdraws. The second is a ZK-SNARK circuit written in Circom. When this circuit is compiled, it also outputs a smart contract verifier that can verify the validity of the proof.</p><h3 id="h-depositors-actions" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Depositor’s actions</h3><p>Before depositing 1 ETH to <code>C</code>, <code>A</code> generates some random bytes called <code>secret</code>. It remains private to <code>A</code>. Now, to make a deposit, <code>A</code> performs these steps:</p><ul><li><p>Computes <code>h = Hash(secret)</code>. <code>Hash()</code> is a hashing function, and <code>secret</code> is called a <em>pre-image</em> of <code>h</code> in the context of <code>Hash()</code>. Tornado cash uses <code>MiMCSponge()</code> hash function provided by Circom library.</p></li><li><p>Executes a transaction calling <code>C.deposit(h)</code> and also sends 1 ETH along with it.</p></li></ul><h3 id="h-on-chain-actions-on-deposit" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">On-chain actions on deposit</h3><p>Now that <code>C.deposit(h)</code> has been called, we’ll see what happens in that function. Internally, <code>C</code> maintains a merkle tree where each leaf corresponds to a successful deposit, or is otherwise empty which will be replaced in future with a successful deposit. The tree has a fixed size already determined at the time of contract deployment. So the number of deposits the contract can have is already pre-determined. We can index each leaf if you imagine them to be in an array <code>leaf</code>. If the number of total leaves is <code>n</code>, <code>leaf[0]</code> is the first leaf, and <code>leaf[n-1]</code> is the last leaf.</p><ul><li><p><code>C</code> ensures that 1 ETH has been sent to it, otherwise it reverts.</p></li><li><p>Let’s say <em>i</em> deposits have already happened, then <code>leaf[i]</code> is assigned the value of <code>h</code> and the merkle root is re-computed. It’s the same as inserting <code>h</code> into the merkle tree at <em>i</em>-th leaf.</p></li><li><p>A <code>Deposit</code> event is emitted logging the index <code>i</code>, <code>h</code> and the new <code>root</code>.</p></li></ul><p>Note that, we have only revealed <code>h = Hash(secret)</code>. Since we have used a hash function, it’s not possible to compute the <em>pre-image</em> <code>secret</code> from <code>h</code>, if sufficient randomness was used to generate <code>secret</code>.</p><h3 id="h-zk-snark-circuits-a-short-detour" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">ZK-SNARK circuits - a short detour</h3><p>The rest of the article depends on some knowledge of ZK-SNARK or Circom circuits. If you have written a Circom circuit before, you can skip this section.</p><p>A circuit represents a computation on given public and private inputs, and the constraints among these inputs, the intermediate values and the final output. One party supplies the public and private inputs to the circuit and generates a <code>proof</code>(a sequence of bytes). Hence, this party is called the <code>prover</code>.</p><p>Another party when supplied with a <code>proof</code> and all the public inputs, verifies the validity of this <code>proof</code>. Thus, the private inputs are not revealed and the verifier can be certain the computation was performed correctly. The proof will be only determined as valid if the performed computation satisfies the constraints defined in the circuit.</p><p>Let’s continue with Tornado Cash now…</p><h3 id="h-withdrawers-actions" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">Withdrawer’s actions</h3><p>Now <code>B</code> has to withdraw 1 ETH. For this, <code>A</code> reveals <code>secret</code> only to <code>B</code>. Now <code>B</code> performs the following actions:</p><ul><li><p>Since <code>B</code> now knows <code>secret</code>, it computes <code>h = Hash(secret)</code>. <code>B</code> observes all the <code>Deposit</code> events emitted so far, and reconstructs the merkle path <code>M</code> from <code>h</code> to its current <code>root</code>.</p></li></ul><p>Tornado Cash provides a Circom circuit takes in the following as input:</p><ul><li><p><code>secret</code> : private input</p></li><li><p><code>root</code> : the public input</p></li><li><p>merkle path from <code>h</code> to <code>root</code> : private input</p></li><li><p><code>recipient</code> address : public input. <code>B</code> passes its addresses for this input. It’ll be clarified later on why it’s required.</p></li></ul><p>The circuit takes the hash of <code>secret</code>, verifies that it’s equal to <code>h</code>, then verifies that the merkle path is indeed a path from <code>h</code> to <code>root</code>. As a result, the circuit generates a <code>proof</code>, which is a sequence of bytes.</p><p>Basically, <code>B</code> is proving that: <code>B</code> knows a secret such that its hash is part of this merkle tree. This <code>proof</code> is a blob of bytes, and reveals nothing beyond the validity of the statement above, thanks to ZK-SNARKs.</p><ul><li><p><code>B</code> passes the proof and all the public inputs to the circuit by executing a transaction <code>C.withdraw(proof, root, recipient)</code>.</p></li></ul><h3 id="h-on-chain-actions-on-withdraw" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">On-chain actions on withdraw</h3><p><code>C.withdraw(proof, root, recipient)</code> has been called by <code>B</code>. These actions follow:</p><ul><li><p><code>C</code> checks if <code>root</code> is one of the recent roots of the merkle tree. If not, the transaction reverts. The reason it’s not restricted to the current root is because the merkle root might have changed between the time when <code>B</code> picked the root and when <code>C</code> executes this transaction, due to new deposits.</p></li><li><p>Now, <code>C</code> invokes the verifier to verify the validity of the proof using <code>proof, root, recipient</code>. If <code>proof</code> is indeed valid, <code>C</code> sends 1 ETH to <code>recipient</code>, which is <code>B</code> in this case.</p></li></ul><p>Now, we can understand why the circuit needs <code>recipient</code> as an input. If <code>recipient</code> was not required by a circuit for proof generation, anyone could frontrun this transaction by passing the same <code>proof</code> and <code>root</code>, and withdraw 1 ETH to their address instead.</p><hr><p>At this stage, let’s introspect on what we have achieved so far and what’s remaining.</p><p>With our current system, we have broken the link between the depositor <code>A</code>, and withdrawer <code>B</code>. We haven’t posted any information on-chain which can link them. There are only two on-chain actions. <code>A</code> calls <code>C.deposit(h)</code>, and <code>B</code> calls <code>C.withdraw(proof, root, recipient)</code>. <code>root</code> is publicly known, and <code>recipient</code> is just the address which receives the withdrawn funds. ZK-SNARK ensures that there’s nothing revealed by <code>proof</code>.</p><p>However, we still need to fix the <em>double spending</em> problem mentioned earlier. <code>B</code> can call <code>C.withdraw()</code> with the same arguments, and it will be able to withdraw 1 more ETH. This can be repeated until it drains all the funds in <code>C</code>. To prevent this, we need to ensure that <code>secret</code> cannot be used more than once…</p><hr><h2 id="h-nullifier-just-a-fancy-word-i-promise" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Nullifier (just a fancy word, I promise)</h2><p>Somehow we have to indicate that <code>secret</code> has been used after the first withdraw. We know that computing a <em>pre-image</em> of a hash is an impossible task. We just leverage this fact and introduce a new parameter called <code>nullifier</code>. For a long time, I was intimidated by this name, however there’s nothing fancy here. It’s just some minor modifications to the circuit. Here’s how it works:</p><ul><li><p>In addition to <code>secret</code>, we now have another private input <code>nullifier</code>. Where before, the depositor <code>A</code> generated <code>secret</code> and inserted <code>Hash(secret)</code> into the merkle tree, it now generates <code>(secret, nullifier)</code> pair and inserts <code>Hash(secret, nullifier)</code> (hash of the concatenation).</p></li><li><p>The ZK-circuit now takes two more inputs:</p><ul><li><p><code>nullifier</code> : private input</p></li><li><p><code>nullifierHash</code> : public input. It is meant to be equal to <code>Hash(nullifier)</code>.</p></li></ul></li><li><p>The circuit now verifies that <code>nullifierHash == Hash(nullifier)</code> and <code>h == Hash(secret, nullifier)</code>, and then verifies the merkle path from <code>h</code> to <code>root</code>. If everything checks out, it generates a <code>proof</code>.</p></li><li><p>Since a new public input <code>nullifierHash</code> is added to the circuit, the withdrawer <code>B</code>, now has to pass that in to <code>C.withdraw()</code>. So the call now becomes <code>C.withdraw(proof, root, nullifierHash, recipient)</code>.</p></li><li><p><code>C</code> now maintains an additional mapping <code>used</code>. <code>withdraw()</code> now first ensures that <code>used[nullifierHash]</code> is <em>false</em>, otherwise it reverts. It means the corresponding deposit has been withdrawn. We’ll explain it later on why that’s the case.</p></li><li><p>Now the usual proof verification happens, and if its valid, 1 ETH is transferred to <code>recipient</code>, and <code>used[nullifierHash]</code> is set to <em>true</em>.</p></li></ul><p>If you observe the on-chain activities using the same approach we used before, you can convince yourself that there is still no way to link <code>A</code> and <code>B</code>. However, this system now prevents the double spending problem. But how?</p><p>The withdrawal will be successful only for someone who knows <code>secret</code> <em>and</em> <code>nullifier</code>. Since, <code>nullifierHash</code> is now marked in the contract as used after the first withdrawal, it becomes impossible to withdraw using the same <code>nullifier</code> again. We managed to prevent the double spending problem without revealing <code>secret</code>!</p><hr><p>I have presented a simplified version of Tornado Cash. If you look at its current code at <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/tornadocash/tornado-core/blob/master/circuits/withdraw.circom#L33-L35">Github</a>, you’ll notice one more feature related to relayer. This enables an address to withdraw even if it doesn’t have any ETH to pay for gas. Hence, it’s a useful feature if you want to move to a new address anonymously.</p><p><a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://github.com/tornadocash/tornado-core/blob/master/circuits/withdraw.circom#L33-L35">https://github.com/tornadocash/tornado-core/blob/master/circuits/withdraw.circom#L33-L35</a></p><hr><p>That’s the end 🌪. Hope it was useful, and you enjoyed reading it! 🫂</p>]]></content:encoded>
            <author>blockdev@newsletter.paragraph.com (blockdev)</author>
        </item>
    </channel>
</rss>