Range check fails to bind limbs to value, enabling invalid witnesses
The KoalaBearRangeCheck helper splits a value into two hinted limbs and checks each limb's bit length, but never constrains the limbs to recombine into the original value. A malicious prover could supply arbitrary limbs that pass the bit checks, so the circuit would accept out-of-range or inconsistent witnesses.
Submitted by Matte, also found by Almanax, Cryptor, and zubyoz
crates/recursion/gnark-ffi/go/sp1/koalabear/koalabear.go #L79-L97
In @crates/recursion/gnark-ffi/go/sp1/koalabear/koalabear.go, the helper KoalaBearRangeCheck() splits an input value into two hinted parts, a 24-bit lowLimb and a 7-bit highLimb, and then only enforces per-limb bitlength constraints (via RangeChecker.Check or ToBinary, depending on GROTH16). It also adds a guard that when highLimb equals the maximal 7-bit value, lowLimb must be zero. Critically, the function never introduces a constraint that ties the hinted limbs back to the original variable; i.e., it does not assert value == highLimb * 2^24 + lowLimb. Without this recomposition equation, the limb checks do not constrain value at all.
Key nuance: gnark hints do not constrain the circuit by themselves. They are advice values. Any relation between a hint’s inputs and outputs must be enforced with explicit constraints. Here, SplitLimbsHint computes limbs from value and even returns an error if value >= modulus, but this check exists only inside the hint function. Since KoalaBearRangeCheck() never asserts that the limbs recombine to value, a malicious prover could supply arbitrary limb values that merely satisfy bitlength checks, and the circuit would accept them. Consequently, KoalaBearRangeCheck() is insufficient as a standalone field-membership or range proof.
Contrast with the correct pattern already present in the code: in reduceWithMaxBits() (same file), after calling SplitLimbsHint on remainder, the code asserts remainder == highLimb * 2^24 + lowLimb before applying the bit checks and boundary guard. That recomposition constraint is exactly what’s missing from KoalaBearRangeCheck() and should be replicated there.
This helper is invoked broadly in @crates/recursion/gnark-ffi/go/sp1/sp1.go over all scalar witness elements and each component of extension-field witnesses. Any path that relies on KoalaBearRangeCheck() to enforce validity—without subsequently reducing and asserting equalities involving the original value—is vulnerable to accepting out-of-range or inconsistent witnesses.
Exploitation sketch
- Pick any witness
valuethat benefits later constraints (e.g., zeroing a product or aligning a digest preimage). - Choose
highLimb/lowLimbwithin their bit bounds, avoiding the boundary casehighLimb == 127to bypass the conditional zero check. - Since
KoalaBearRangeCheck()lacksvaluerecomposition, the check passes regardless of any relation betweenvalueand the limbs. - Downstream gadgets then treat
valueas valid; if no subsequent reduction/equality tiesvalueto canonical field form, this permits out-of-range or inconsistent inputs to drive logic and commitments, breaking soundness.
Recommendation
Augment KoalaBearRangeCheck() to add the missing recomposition constraint that binds the hinted limbs to the original variable for both proving systems. A minimal, local fix is to assert value == highLimb * 2^24 + lowLimb immediately after obtaining the hint, then keep the existing limb range checks and the boundary guard. Prefer integer shifts for powers of two (e.g., 1 << 24, 1 << 7) instead of math.Pow to avoid float conversions.
Add regression tests:
- A minimal circuit that only calls
KoalaBearRangeCheck(x)should currently accept arbitraryx; after the fix, it must accept only whenxequals the recomposed limbs. - A negative test where
valueand(highLimb, lowLimb)disagree must be rejected.
--- a/crates/recursion/gnark-ffi/go/sp1/koalabear/koalabear.go
+++ b/crates/recursion/gnark-ffi/go/sp1/koalabear/koalabear.go
@@ func (p *Chip) KoalaBearRangeCheck(value frontend.Variable) {
- lowLimb := new_result[0]
- highLimb := new_result[1]
+ lowLimb := new_result[0]
+ highLimb := new_result[1]
+
+ // Bind limbs to the source variable.
+ p.api.AssertIsEqual(
+ p.api.Add(
+ p.api.Mul(highLimb, frontend.Variable(uint64(1<<24))),
+ lowLimb,
+ ),
+ value,
+ )
@@
- shouldCheck := p.api.IsZero(p.api.Sub(highLimb, uint64(math.Pow(2, 7))-1))
+ // Optional: prefer shifts for 2^k constants.
+ shouldCheck := p.api.IsZero(p.api.Sub(highLimb, uint64((1<<7)-1)))
p.api.AssertIsEqual(
p.api.Mul(
shouldCheck,
lowLimb,
),
frontend.Variable(0),
)
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The helper enforces bit-length limits on hinted limbs but omits the recomposition constraint that ties those limbs back to the original value.
Impact
If no later reduction or equality ties the value to canonical field form, a malicious prover could break soundness by feeding out-of-range or inconsistent witnesses into downstream logic and commitments.
What the attacker needed
Ability to craft a malicious witness; circuit path relies on this helper alone
The fix
The helper should assert that the original value equals highLimb * 2^24 + lowLimb immediately after obtaining the hint, keeping the existing limb checks and boundary guard.
How it was found
- Pick a witness value that benefits later constraints.
- Choose highLimb and lowLimb within their bit bounds, avoiding the boundary case highLimb == 127.
- Because there is no recomposition constraint, the range check passes regardless of the relation between value and the limbs.
- Downstream gadgets treat value as valid, permitting out-of-range or inconsistent inputs to drive logic and commitments.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A zero-knowledge circuit proves statements without revealing the data. Here a helper was meant to prove a number fits in a certain range by splitting it into two smaller pieces and checking each piece. But it forgot to prove the pieces actually add back up to the original number, so a cheater could pick any pieces that pass the checks and the circuit would believe the original number was valid.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.