Word range check uses unconstrained `upper_all_one` local variable when field prime is `Mersenne31`
The word range check gadget for the Mersenne31 field prime had its constraint on the `upper_all_one` column commented out, leaving the check unconstrained. An attacker could therefore produce a word equal to the prime modulus that the gadget incorrectly accepts as valid.
0xpetern Follow with 1AutumnLeaf777, MissDida and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2025-08-pico-judging/issues/69
Found by
0xpetern, 1AutumnLeaf777, MissDida, cergyk
Description
The comments under the gadget used to enforce field prime range check for a word when field type is Mersenne31 are correct; They describe the correct methodology for checking that the word is indeed lower than Mersenne31 prime modulus. Unfortunately, the line which enforces the constraint on the column cols.upper_all_one is commented, leaving the whole gadget unconstrained:
field_range_check/word_range.rs#L121-L138:
FieldType::TypeMersenne31 => {
// Mersenne31 Modulus in big endian format
// 01111111 11111111 11111111 11111111
// 2^31 - 1
// All the four bytes are all within a certain range:
// 00000000 <= bytes[3] <= 01111111
// 00000000 <= bytes[2] <= 11111111
// 00000000 <= bytes[1] <= 11111111
// 00000000 <= bytes[0] <= 11111111
// which means 00000000 <= sum(bytes) <= 127 + 255 * 3,
// which means word == 2^31 - 1 <=> sum(bytes) == 127 + 255 * 3.
// Therefore, we use the IsZeroGadget to guarantee that sum(bytes) != 127 + 255 * 3.
let mut byte_sum = value[0] + value[1] + value[2] + value[3];
byte_sum -= AB::F::from_canonical_u32(127u32 + 255 * 3).into();
//@audit-ok commented line constrains `upper_all_one`
// IsZeroGadget::<F>::eval(builder, byte_sum, cols.upper_all_one, is_real.clone());
builder.when(is_real).assert_zero(cols.upper_all_one.result)
}
As a result the gadget can return true for the word 01111111 11111111 11111111 11111111 which is not valid (equal to the prime modulus).
Recommendation
Consider simply uncommenting the constraint line:
FieldType::TypeMersenne31 => {
// Mersenne31 Modulus in big endian format
// 01111111 11111111 11111111 11111111
// 2^31 - 1
// All the four bytes are all within a certain range:
// 00000000 <= bytes[3] <= 01111111
// 00000000 <= bytes[2] <= 11111111
// 00000000 <= bytes[1] <= 11111111
// 00000000 <= bytes[0] <= 11111111
// which means 00000000 <= sum(bytes) <= 127 + 255 * 3,
// which means word == 2^31 - 1 <=> sum(bytes) == 127 + 255 * 3.
// Therefore, we use the IsZeroGadget to guarantee that sum(bytes) != 127 + 255 * 3.
let mut byte_sum = value[0] + value[1] + value[2] + value[3];
byte_sum -= AB::F::from_canonical_u32(127u32 + 255 * 3).into();
- // IsZeroGadget::<F>::eval(builder, byte_sum, cols.upper_all_one, is_real.clone());
+ IsZeroGadget::<F>::eval(builder, byte_sum, cols.upper_all_one, is_real.clone());
builder.when(is_real).assert_zero(cols.upper_all_one.result)
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/brevis-network/pico/pull/61
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 constraint that enforces the `upper_all_one` column was commented out, so the gadget no longer verifies that the byte sum is not equal to the modulus.
Impact
The gadget could accept the invalid word equal to the prime modulus, potentially allowing invalid proofs to pass.
What the attacker needed
Ability to submit proofs using the gadget
The fix
The constraint line should be uncommented so that the IsZeroGadget is evaluated and the column is properly constrained; the protocol team fixed this issue.
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 proof system uses small programs called gadgets to check that numbers are in the right range. Here, a line of code that was supposed to enforce one part of that check was turned off, so the system could accept a number that should have been rejected. This could let someone create a false proof.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.