Skip to content
Crypto Write-up Advanced

zkSync Lite

A vulnerability in zkSync Lite's packed floating point format allowed a malicious user to generate a zero-knowledge proof with an arbitrary mantissa value, bypassing proof verification. This could enable unauthorized token minting, freezing, transferring, transaction tampering, and preventing state tree recovery.

medium.com

Published on medium.com

Immunefi

Immunefi Editor, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

zkSync Lite is a scaling engine for Ethereum built on ZK Rollup architecture. Its functionality covers low gas transfers of ETH and ERC20 tokens, atomic swaps and limit orders, with native L2 NFT support. In a ZK Rollup all funds are held by a smart contract on the mainchain, while computation and storage happen off-chain. For every Rollup block a state transition zero-knowledge proof (SNARK) is generated and verified by the mainchain contract. That SNARK proves the validity of every transaction in the block, and the public data update for each block is published over the mainchain network as cheap calldata.

To reduce the gas cost of that public calldata, zkSync uses a specialised serialisation called the packed floating point format. Amounts and fees for transactions such as transfers and swaps are serialised in this format. It requires an additional witness in the zkSync circuit, the unpacked amount corresponding to the packed amount, so that balances and other operations can be calculated accurately without complex floating-point arithmetic.

The bug described here is in zkSync Lite and is explicitly not related to zkSync Era.

Setup

There are no accounts, roles or tool versions to set up. The relevant code lives in the franklin-crypto library, and the central function is parse_with_exponent_le, which decodes packed floating points into unpacked allocated numbers. It takes a bit decomposition and repacks it into an AllocatedNum, the type used in the circuit to represent numbers.

Because the mantissa is unconstrained, anyone is able to run a prover with any arbitrary value as mantissa and generate a proof verifiable with the same verification keys.

Finding it

The researcher, LonelySloth, looked at how parse_with_exponent_le allocates the mantissa. The function asserts that the length of the bits equals the sum of the exponent length and the mantissa length, then allocates numbers for the various components, including the exponent and mantissa results, using AllocatedNum::alloc.

That call creates a new, private witness without imposing any constraints on the value of that witness. Nothing ties the allocated mantissa to the bits that were decomposed, so an arbitrary value can be placed there. Because the proof is generated over that unconstrained witness, a prover can produce a proof that the circuit's verification keys will still accept.

From the original on medium.com

Exploitation

Step 1: choose arbitrary packed and unpacked amounts

The attacker picks an arbitrary pair of (packed_amount, unpacked_amount). The packed amount is the value that appears in the public transaction data, and the unpacked amount is the witness the circuit uses for balance calculations.

Step 2: run the prover with an arbitrary mantissa

Because the mantissa is allocated with AllocatedNum::alloc and never constrained, the prover can be run with any arbitrary value as the mantissa. The resulting proof is valid under the same verification keys that the mainchain contract uses.

Step 3: submit the proof

The report states that tampering occurs with transaction data during packing and unpacking, enabling a bypass of the proof generation system and acceptance of altered transaction data.

Root cause

The mistake is in the parsing of floating point numbers, specifically in the allocation of bits to the mantissa variable. AllocatedNum::alloc creates a private witness with no constraint on its value, so the mantissa is effectively free. The circuit's verification keys accept any proof built on that unconstrained witness, which means the proof generation system can be bypassed and altered transaction data accepted.

Removing unconstrained allocations and adding appropriate constraints are the crucial steps in securing the circuit against these attacks.

Impact

The report describes the vulnerability as Critical with multiple impacts, including unauthorised minting, freezing and transferring of tokens, tampering with transactions, and preventing recovery of the state tree by other users. The report describes the mechanism as enabling token minting, transaction tampering and token theft.

From the original on medium.com

Fix

The fix was applied on 7 November 2023 in commit [redacted] in the matter-labs/franklin-crypto repository. The mantissa is now created with a new method, into_allocated_num, which enforces constraints and ensures the value of the mantissa is derived from the expected calculations.

Text
https://github.com/matter-labs/franklin-crypto/commit/[redacted]

Variants to test

  1. Audit every AllocatedNum::alloc call in a circuit and check whether the allocated value is later constrained to the bits or inputs it is supposed to represent. A positive result is an allocation whose value can be set freely by the prover while the proof still verifies.
  2. Look at other packed or compressed serialisation formats that carry a separate unpacked witness, such as amount and fee encodings. A positive result is a mismatch between the packed public value and the unpacked witness that the circuit accepts.
  3. Check parsing helpers that assert only on lengths (for example, that the bit length equals exponent length plus mantissa length) but do not bind the parsed components to those bits. A positive result is a length check that passes while the component values are unconstrained.
  4. Test whether arbitrary pairs of packed and unpacked values can be used to generate a proof accepted by the deployed verification keys. A positive result is an accepted proof for a transaction whose public data does not match the witness.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

A security researcher who goes by the name LonelySloth found a serious flaw in a piece of software called zkSync Lite. On 5 October 2023 he reported it privately to the team that maintains zkSync, rather than publishing it or using it to steal money. That kind of private report is called responsible disclosure: you tell the people who can fix the problem, give them time to fix it, and only then talk about it publicly.

The flaw was rated Critical, the most serious level. The report lists the vulnerability's impacts as unauthorised minting, freezing and transferring of tokens, tampering with transactions, and preventing other users from recovering the state tree. The zkSync team responded fast and the vulnerability has been fixed, and LonelySloth was awarded $200,000 for the finding.

The report also says clearly that this bug was in zkSync Lite and is not related to zkSync Era, which is a different product from the same company.

This article walks through the same story the technical write-up tells, but from zero. You do not need to know what a proof, a circuit or a witness is before you start; each of those ideas is explained the first time it appears.

Background: what zkSync Lite is and why proofs matter

From the original on medium.com

Ethereum and the gas problem

Ethereum is a public blockchain: a shared ledger that many independent computers keep copies of, and that anyone can read. To change the ledger you send a transaction, and the computers that maintain it (the mainchain) do some work to record it. That work costs a fee called gas, and the fee is paid in a currency called ETH.

Because every one of those computers has to repeat the same work, Ethereum can only process a limited number of transactions, and fees rise when the network is busy. This is the scaling problem: how do you let many more people transact without making every computer do every calculation?

Rollups in one sentence

zkSync Lite is a scaling engine for Ethereum. Its job is to take lots of transactions off the main Ethereum chain, do the heavy calculation somewhere else, and then hand Ethereum a small summary that Ethereum can check cheaply. The report lists what zkSync Lite currently supports: low gas transfers of ETH and ERC20 tokens, atomic swaps and limit orders, plus native support for NFTs on the second layer.

Two terms in that list are worth unpacking:

  • ERC20 tokens are tokens built on Ethereum by a common standard, so wallets and exchanges know how to handle them. Think of them as a standard shape of container that everyone agrees on.
  • NFTs (non-fungible tokens) are tokens that are each unique rather than interchangeable, commonly used to represent a specific digital item.

An atomic swap is an exchange of one asset for another that either happens completely or not at all, never halfway. A limit order is an instruction to trade only if the price reaches a level you set.

What a ZK Rollup actually does

zkSync Lite is built on ZK Rollup architecture. In a ZK Rollup, all the money sits in a smart contract on the mainchain. A smart contract is a program that lives on the blockchain and runs automatically when someone sends it a transaction; nobody can quietly change its behaviour after it is deployed. Because the funds are held there, the mainchain remains the place where the money really is.

The computation and storage, by contrast, happen off-chain, meaning outside the mainchain, on zkSync's own systems. That is where the saving comes from: Ethereum does not repeat every calculation.

But then Ethereum has to be convinced that the off-chain work was done correctly. It does that with a state transition zero-knowledge proof, also called a SNARK. A zero-knowledge proof is a way for one computer to convince another that a statement is true without the second computer having to redo all the work itself. A useful everyday comparison is a receipt from a very thorough accountant: you do not re-add every column yourself, you check that the receipt is genuine and that it covers the right period.

For every Rollup block, one of these SNARKs is generated and then verified by the mainchain contract. The SNARK includes proof of the validity of every single transaction in that block. Separately, the public data update for every block is published over the mainchain network as cheap calldata. Calldata is the data you attach to a blockchain transaction; it is stored by the network and costs gas, but far less than doing computation.

So there are two things travelling to Ethereum for each block: a proof that says "all of this is correct", and a compact bundle of public data describing what changed. The security of the whole system rests on the proof being genuinely tied to that data.

Data packing: the feature at the centre of the bug

As the project's description says, zkSync uses a special mechanism to keep down the gas cost of public calldata. The mechanism is a serialisation format called the packed floating point format. Serialisation just means turning numbers and structured data into a flat sequence of bytes so they can be stored or sent; deserialisation (or unpacking) is turning them back.

Why floating point? A floating point number stores a value as two parts: a mantissa, which holds the significant digits, and an exponent, which says how far to shift the decimal point. The number 1.23 × 10³ is a mantissa of 1.23 with an exponent of 3. This lets you represent a very wide range of values in a small number of bits, which is exactly what you want when every byte costs gas.

The report describes a critical aspect of this implementation that turned out to be a significant vulnerability, one that could be exploited for malicious actions such as token minting and transaction tampering. It names two consequences:

  • Tampering occurs with the transaction data during the packing and unpacking process, which enables a bypass of the proof generation system and results in the acceptance of altered transaction data.
  • The primary issue concerns the parsing of floating point numbers during the allocation of bits computation to the mantissa variable. The researcher identified that it is possible to assign arbitrarily packed data to this variable, which can enable the generation of a proof that the circuit's verification keys will accept. Consequently, a malicious user can generate a proof using arbitrary pairs of (packed_amount, unpacked_amount), potentially leading to transaction tampering.

Two more ideas need explaining before that paragraph makes sense.

A circuit, in this context, is not a piece of electronics. It is a mathematical description of a computation, written in a form that a proof system can reason about. You can picture it as a huge set of equations with slots for inputs; the proof shows that, with the values placed in those slots, all the equations hold.

A witness is the private set of values that fills those slots. The proof convinces a verifier that a witness exists satisfying the circuit, without the verifier learning the witness. A verification key is the public file that a verifier uses to check proofs for a particular circuit. If a proof checks out against the verification key, the verifier accepts it. The whole point is that a valid proof should only be producible if the witness really satisfies the circuit's rules.

That last sentence is where the bug lives: the circuit had a slot that was never actually tied to any rule.

Packed floating point format in more detail

In zkSync's architecture, amounts and fees for transactions such as transfers and swaps are serialised using the packed amount format. This format is integral for reducing gas costs. It requires an additional witness in the zkSync circuit: the unpacked amount corresponding to the packed amount. This setup is crucial for ensuring accurate calculations of balances and other operations without resorting to complex floating-point arithmetic.

Read that carefully, because it is the heart of the design. The public data carries the compact packed amount. The circuit is also handed the full unpacked amount as a witness, so that it can do ordinary whole-number arithmetic on balances instead of fiddly floating point. The circuit's job is to check that the unpacked amount really is the unpacking of the packed amount. If that check is missing, the two numbers are free to disagree.

The code involved

The code for this system, particularly found in the franklin-crypto library, reveals the process of decoding packed floating points into unpacked allocated numbers. A library is a bundle of reusable code that other programs call into; franklin-crypto is the one that holds the circuit building blocks here.

A function, parse_with_exponent_le, is central to this process. A function is a named block of code that takes inputs and returns a result. This one takes a bit decomposition and repacks it into an AllocatedNum, a type used in the circuit for representing numbers. A bit decomposition is simply the number written out as its individual binary digits, zeros and ones, so that the circuit can reason about each bit separately.

The code asserts that the length of bits equals the sum of exponent length and mantissa length. An assert is a check in code that stops or fails if a condition is not met; here it confirms the incoming bits are the expected size. It then allocates numbers for various components, including the exponent and mantissa results, using the AllocatedNum::alloc function. This function creates a new, private witness without imposing any constraints on the value of that witness. Because of that, anyone is able to run a prover with any arbitrary value as mantissa to generate a proof that can be verified using the same verification keys.

A constraint, in proof systems, is a rule the witness must satisfy. "This number equals the sum of those two" is a constraint. "This number is one of the bits you were given" is a constraint. A witness value with no constraints is like a blank field on a form that nobody ever cross-checks against the supporting documents: whatever you write there is accepted.

A prover is the software that produces a proof. Running a prover is not a privileged act; the report's precondition for the attack is simply the ability to run a prover, that is, to produce proofs against the circuit's verification keys.

A malicious user can now generate a proof using an arbitrary value, which leads to transaction manipulation.

How the attack works, step by step

Step 1: choose arbitrary packed and unpacked amounts

The attacker picks an arbitrary pair of (packed_amount, unpacked_amount). The packed amount is the value that appears in the public transaction data, and the unpacked amount is the witness the circuit uses for balance calculations.

Why this step matters: normally these two numbers are forced to agree, because one is the compressed form of the other. Here the attacker gets to choose both independently. The public data can say one thing while the circuit computes balances from another.

Step 2: run the prover with an arbitrary mantissa

Because the mantissa is allocated with AllocatedNum::alloc and never constrained, the prover can be run with any arbitrary value as the mantissa. The resulting proof is valid under the same verification keys that the mainchain contract uses.

Why this step matters: the attacker is not breaking any cryptography. The proof system is working exactly as designed. The design simply never asked the mantissa to match the bits it was supposedly parsed from, so a proof built on a made-up mantissa is still a perfectly well-formed proof.

Step 3: submit the proof

The report states that tampering occurs with transaction data during packing and unpacking, enabling a bypass of the proof generation system and acceptance of altered transaction data.

Why this step matters: the mainchain contract is the last line of defence, and it did its job correctly. It checked the proof and the proof was valid. The failure happened earlier, in what the circuit required of the witness.

From the original on medium.com

Root cause

The mistake is in the parsing of floating point numbers, specifically in the allocation of bits to the mantissa variable. AllocatedNum::alloc creates a private witness with no constraint on its value, so the mantissa is effectively free. The circuit's verification keys accept any proof built on that unconstrained witness, which means the proof generation system can be bypassed and altered transaction data accepted.

Removing unconstrained allocations and adding appropriate constraints are the crucial steps in securing the circuit against these attacks.

In everyday terms: the circuit was handed a number and asked to trust it. A proof system can only be as strong as the rules you give it, and one missing rule is enough.

Impact

The report describes the vulnerability as Critical with multiple impacts, including unauthorised minting, freezing and transferring of tokens, tampering with transactions, and preventing recovery of the state tree by other users. The report describes the mechanism as enabling token minting, transaction tampering and token theft.

Some of those words deserve a plain explanation:

  • Minting means creating new tokens. Unauthorised minting means creating them without being entitled to, which in a currency system is the most direct way to destroy trust in it.
  • Freezing means making tokens unusable, so the owner cannot move them.
  • Transferring here means moving tokens that belong to someone else.
  • The state tree is the data structure that records the current balances and state of the system. If other users cannot recover it, they cannot reconstruct their position and continue safely.
From the original on medium.com

The fix

The vulnerability fix was applied on 7 November 2023 in a commit in the matter-labs/franklin-crypto repository. A commit is a recorded change to a code repository, with an identifier that lets anyone look up exactly what changed. A repository is the store of a project's source code and its history.

Text
https://github.com/matter-labs/franklin-crypto/commit/[redacted]

That block is the address of the fix. Opening it shows the change itself; the report points to it so that anyone can check the patch rather than take it on trust.

What changed:

  • The mantissa is now being created with a new method, into_allocated_num, to enforce constraints and ensure that the value of the mantissa is derived from the expected calculations, thereby addressing the vulnerability.
  • Removing unconstrained allocations and adding appropriate constraints are crucial steps in securing the circuit against the attacks described here, such as token minting, transaction tampering, and token theft.

In plain words: instead of creating a free-floating number and trusting it, the fixed code derives the mantissa from the bits it was given, so the number can no longer disagree with the data. The blank field on the form is now cross-checked against the supporting documents.

Where else to look for the same mistake

They are worth repeating because the pattern, not just the specific bug, is the lesson.

  1. Audit every AllocatedNum::alloc call in a circuit and check whether the allocated value is later constrained to the bits or inputs it is supposed to represent. A positive result is an allocation whose value can be set freely by the prover while the proof still verifies.
  2. Look at other packed or compressed serialisation formats that carry a separate unpacked witness, such as amount and fee encodings. A positive result is a mismatch between the packed public value and the unpacked witness that the circuit accepts.
  3. Check parsing helpers that assert only on lengths (for example, that the bit length equals exponent length plus mantissa length) but do not bind the parsed components to those bits. A positive result is a length check that passes while the component values are unconstrained.
  4. Test whether arbitrary pairs of packed and unpacked values can be used to generate a proof accepted by the deployed verification keys. A positive result is an accepted proof for a transaction whose public data does not match the witness.

An audit is a deliberate, systematic review of code looking for exactly this kind of gap. Notice that item 3 describes the bug almost word for word: a check that the sizes add up, but no check that the pieces are the right pieces.

Acknowledgments

The report thanks the whitehat LonelySloth for responsibly disclosing such a crucial bug in a timely manner, and gives special recognition to the zkSync team, who promptly responded to the report and patched the issue. A whitehat is a security researcher who finds flaws and reports them to the people who can fix them, instead of exploiting them.

Glossary

  • Assert: a check in code that fails if a stated condition is not met.
  • Atomic swap: an exchange of one asset for another that completes fully or not at all.
  • Audit: a systematic review of code or a system looking for security weaknesses.
  • Bit decomposition: a number written out as its individual binary digits.
  • Calldata: the data attached to a blockchain transaction, stored by the network and paid for in gas.
  • Circuit: a mathematical description of a computation, in a form a proof system can reason about.
  • Commit: a recorded change in a code repository, identified by a hash.
  • Constraint: a rule that the values in a proof's witness must satisfy.
  • Critical: the most serious severity rating, indicating a flaw with severe consequences.
  • ERC20 token: a token on Ethereum that follows a common standard, so wallets and exchanges can handle it.
  • Exponent: the part of a floating point number that says how far to shift the decimal point.
  • Freezing: making tokens unusable so their owner cannot move them.
  • Function: a named block of code that takes inputs and returns a result.
  • Gas: the fee paid for computation and storage on a blockchain.
  • Library: a bundle of reusable code that other programs call into.
  • Limit order: an instruction to trade only if the price reaches a level you set.
  • Mainchain: the primary blockchain (here Ethereum) that the scaling system settles onto.
  • Mantissa: the part of a floating point number holding its significant digits.
  • Minting: creating new tokens.
  • NFT: a non-fungible token, a token that is unique rather than interchangeable.
  • Off-chain: happening outside the main blockchain.
  • Packed floating point format: zkSync's compact serialisation of amounts and fees, using a mantissa and an exponent to save calldata gas.
  • Prover: the software that produces a zero-knowledge proof.
  • Repository: the store of a project's source code and its change history.
  • Responsible disclosure: reporting a vulnerability privately to the party that can fix it, allowing time for a fix before public discussion.
  • Serialisation: turning structured data into a flat sequence of bytes for storage or transmission.
  • Smart contract: a program deployed on a blockchain that runs automatically when sent a transaction.
  • SNARK: a succinct zero-knowledge proof, a short proof that a computation was done correctly without redoing it.
  • State tree: the data structure recording the current balances and state of the system.
  • Transaction: a request to change the blockchain ledger.
  • Verification key: the public data used to check proofs for a particular circuit.
  • Whitehat: a security researcher who reports flaws to be fixed rather than exploiting them.
  • Witness: the private values that satisfy a circuit's constraints and that a proof is built over.
  • ZK Rollup: a scaling design where funds are held by a mainchain contract, computation happens off-chain, and a zero-knowledge proof of correctness is verified on the mainchain.
  • Zero-knowledge proof: a way to convince someone a statement is true without them redoing the work or learning the private inputs.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.