本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2026/1/21

ZK Circuit Pitfalls and How to Prevent Them

Table of Contents

Introduction

Zero knowledge proofs are becoming the backbone of blockchain as they tackle two major roadblocks that have kept crypto from going mainstream - privacy and scalability.

Because of this, it is very important to build secure ZK. The underlying foundations like  elliptic curves, pairings, and polynomial commitments are actually strong. Most vulnerabilities that arise are not in the cryptography itself, but in the applications built on top of them - such as ZK circuits

We can have strong cryptography, but if our circuit is missing a constraint then an attacker can exploit it to generate valid proofs and create the damage

So this article aims to walkthrough five pitfalls we keep seeing in ZK circuits. For each one, we will explain why it happens and how can we fix it

1. Under Constrained Circuits

Most developers come from regular programming background where x = a + b means "compute and assign the value to x." But in ZK circuits, especially Circom, you have two completely different operators:

  • <== creates actual constraints (the circuit computes and verify)

  • <-- just assigns values during witness generation (only computes but no verification is done)

When you write x <-- a + b, prover can set the value to anything during verification and it will be successful.

Example

Here is one example, there's no constraint linking newBalance to balance - amount.

A user can withdraw 50 tokens from a 100 token balance but claim their new balance as 1000000 tokens. They can pass any value and the system verifying the circuit proof will accept it because the circuit doesn’t check if newBalance is equal to balance - amount it only computes and assigns to newBalance

template BadWithdraw() {
    signal input balance;
    signal input amount;
    signal output newBalance;

    // This is only an assignment not a constraint!
    newBalance <-- balance - amount;
}

How to fix it

Always make sure the constraint is added properly ( <== not <-- )

template BadWithdraw() {
    signal input balance;
    signal input amount;
    signal output newBalance;

    // This is only an assignment not a constraint!
    newBalance <== balance - amount;
}

2. Arithmetic Overflow/Underflow

ZK circuits work with finite fields (Prime modulo p) and if calculations result in values outside the field range, then these numbers wrap around. This happens because numbers in ZK works with modulo prime P which keeps on rotating from 0 to P-1. So its important to have proper check on inputs, otherwise malicious prover or verifier can exploit this to their advantage

Example

If senderBalance = 100 and amount = 200 the subtraction underflows in below example but it doesn't error out and proof verification will be successful with a very large number in newBalance. If prime P value is 2^254 then the final result newBalance wraps to approximately 2^254 - 100, giving the sender a near-infinite balance.

template VulnerableTransfer() {
    signal input senderBalance;
    signal input amount;
    signal output newBalance;

    newBalance <== senderBalance - amount;
}

How to fix it

Make sure proper checks are added before the calculations so it doesn't underflow or overflow

template SafeTransfer_Bug2Fix() {
    signal input senderBalance;
    signal input amount;
    signal output newBalance;

    // Enforce senderBalance >= amount
    // LessThan outputs 1 if senderBalance < amount, so we check that output is 0 which ensures senderBalance >= amount

    component ge = LessThan(64);
    ge.in[0] <== senderBalance;
    ge.in[1] <== amount;
    ge.out === 0;

    // Safe subtraction
    newBalance <== senderBalance - amount;
}

3. Mismatching Bit Lengths

Circom libraries like LessThan(n) and Num2Bits(n) assume inputs are within n bits, but the ZK circuit never constrains them to that size by default. Due to this these libraries functions will not work as expected and an attacker can exploit its weakness.

Example

Prover supplies user_value which is more than 32 bits but LessThan expects only 32 bits. This could cause unexpected behaviour through underflow/overflow which can be exploited by malicious user to their advantage

component lt = LessThan(32);
lt.in[0] <== user_value;   // user_value NOT constrained to 32 bits
lt.in[1] <== 1000;
lt.out === 0;              // circuit expects user_value >= 1000

How to fix it

When using library components, explicitly add Num2Bits or equivalent checks for each input.

component a_bits = Num2Bits(32);
a_bits.in <== user_value;  // forces user_value < 2^32
component lt = LessThan(32);
lt.in[0] <== user_value;
lt.in[1] <== 1000;
lt.out === 0;

4. Non-Deterministic Circuits

The same inputs can produce different valid proofs.

Example

Let's take an example of square root computation:

template VulnerableSqrt() {
    signal input x;
    signal output sqrt;

    sqrt * sqrt === x;
}

In a finite field, any valid square root has two solutions: x and p − x. In the above example a positive x, both +√x and -√x satisfy this constraint. In field arithmetic, -√x is represented as p - √x, a valid field element. Due to this user can submit the same commitment twice with opposite signs, effectively doing same operation twice for example voting twice with the same tokens.

How to fix it
The constraint sqrt < p/2 accepts only one of input mentioned above, which makes the witness unique

template DeterministicSqrt() {
    signal input x;
    signal output sqrt;

    // witness must be a square root of x (mod field)
    sqrt * sqrt === x;

    // enforce: sqrt < p/2 (BN254 p/2 constant)
    component lt = LessThan(254);
    lt.in[0] <== sqrt;
    lt.in[1] <== 10944121435919637611123202872628637544274182200208017171849102093287904247808;
    lt.out === 1;
}

5. Public Inputs Not Constrained

Values are assigned during witness generation but not constrained in the circuit. Because of this malicious provers can change its value without breaking the proof.

Example

If a proof authorizes a transfer to a recipient, the circuit must constrain the recipient. Otherwise, a different recipient can be provided during proof verification without any error, or the proof can be front-run from the Mempool by an attacker who submits the same proof with different recipient, thereby receiving the transferred tokens even though the attacker is not eligible for them.

template Withdraw(levels) {
    signal input recipient; // not taking part in any computations
    // and no constraint on this recipient input
}

How to fix it

template Withdraw() {
    signal input recipient; // not taking part in any computations
    signal recipientSquare;
    
    // Add hidden signals to make sure that tampering with recipient
    // Most likely it is not required, but it's better to stay on the safe side

    recipientSquare <== recipient * recipient;
}

Final Thought

ZK circuits can have tiny mistakes, but they can have huge consequences. A single missing constraint can break security. Let’s build it right and stay safe out there.

For ZK audits, reach out to us

Author: Ashish Lachuria

Security Researcher at TECHFUND

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More