Version hash is not correctly enforced in code unpacker
The code unpacker's constraint logic was inverted: it enforced that if the version hash matched, the state must be fetched from the queue, rather than requiring the version hash to match when fetching from the queue. This meant that an incorrect version hash could pass without any constraint being enforced.
Submitted by xuwinnie, also found by 0xsanson
In code unpacker, version hash is not correctly enforced.
Proof of Concept
In code unpacker, we enforce that version hash is correct.
let version_hash_matches = UInt16::equals(cs, &chunks[1], &versioned_hash_top_16_bits);
state.state_get_from_queue.conditionally_enforce_true(cs, version_hash_matches);
The second line means, if version hash matches, state_get_from_queue must be true. But this is not what we want. The logic should be if we are getting from queue, version hash must match. Otherwise, even if version hash is incorrect, this line passes as no constraint is actually enforced.
Let’s take a closer look at fn conditionally_enforce_true, bool should_enforce is the third parameter.
pub fn conditionally_enforce_true<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
should_enforce: Self,
) {
// this is equal to having !self && should_enforce == false;
// so (1 - self) * should_enforce == 0
if cs.gate_is_allowed::<FmaGateInBaseFieldWithoutConstant<F>>() {
let zero_var = cs.allocate_constant(F::ZERO);
let gate = FmaGateInBaseFieldWithoutConstant {
params: FmaGateInBaseWithoutConstantParams {
coeff_for_quadtaric_part: F::MINUS_ONE,
linear_term_coeff: F::ONE,
},
quadratic_part: (self.variable, should_enforce.variable),
linear_part: should_enforce.variable,
rhs_part: zero_var,
};
gate.add_to_cs(cs);
} else {
unimplemented!()
}
}
Recommended Mitigation Steps
version_hash_matches.conditionally_enforce_true(cs, state.state_get_from_queue);
Assessed type
Context
Alex the Entreprenerd (judge) commented:
The Warden has shown how, due to a constant enforcement on version hashes, future hash versions would fail the check.
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 arguments to conditionally_enforce_true were swapped, so the condition and the enforced statement were reversed.
Impact
An incorrect version hash could be accepted without enforcement, potentially allowing invalid state transitions.
What the attacker needed
None
The fix
The call should be changed to enforce that the version hash matches when the state is fetched from the queue.
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.
In zero-knowledge proof systems, constraints are rules that must hold for a computation to be valid. Here, a rule was written backwards: it checked the wrong direction, so a wrong version hash could slip through without being caught.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.