Reduction gate in binop operation is unsafe
The range check in the binop reduction gate only verifies that the and and or results are less than 128, but does not check the xor result. An attacker could forge arbitrary results for the and and or opcodes, and cause an overflowed UInt8 xor result to remain in the circuit, potentially leading to unexpected behaviour.
Submitted by xuwinnie
The range check in the binop’s reduction gate is insufficient. After decomposing the composite_result, and_result and or_result could be arbitrary integer less than 128 and xor_result could overflow. Attacker can forge arbitrary result for opcode and and or; as for xor, an overflowed UInt8 will stay in the circuit, which can lead to unexpected behavior.
Proof of Concept
There are three main steps in function get_binop_subresults:
let mut composite_result = [Variable::placeholder(); 32];
for ((a, b), dst) in a.iter().zip(b.iter()).zip(composite_result.iter_mut()) {
let [result] = cs.perform_lookup::<2, 1>(table_id, &[a.get_variable(), b.get_variable()]);
*dst = result;
}
At first, we perform a lookup to get the composite result for and, or and xor.
for (src, decomposition) in composite_result.iter().zip(all_results.array_chunks::<3>()) {
if cs.gate_is_allowed::<ReductionGate<F, 4>>() {
let mut gate = ReductionGate::<F, 4>::empty();
gate.params = ReductionGateParams {
reduction_constants: [F::SHIFTS[0], F::SHIFTS[16], F::SHIFTS[32], F::ZERO],
};
gate.reduction_result = *src;
gate.terms = [
decomposition[0],
decomposition[1],
decomposition[2],
zero_var,
];
gate.add_to_cs(cs);
}
Then, we decomposite it into a larger array all_results.
for (((and, or), xor), src) in and_results
.iter_mut()
.zip(or_results.iter_mut())
.zip(xor_results.iter_mut())
.zip(all_results.array_chunks::<3>())
{
*and = src[0];
*or = src[1];
*xor = src[2];
}
let and_results = and_results.map(|el| unsafe { UInt8::from_variable_unchecked(el) });
let or_results = or_results.map(|el| unsafe { UInt8::from_variable_unchecked(el) });
let xor_results = xor_results.map(|el| unsafe { UInt8::from_variable_unchecked(el) });
Finally, we get three separate results from all_results.
In reduction gate, the type we are handling is Variable, which means they can be any element in the prime field. we enforce that composit_value = (xor_result as u64) << 32 | (or_result as u64) << 16 | (and_result as u64). To ensure the decomposited result is indeed what we expected, we also need to make sure all of them are less than 128. So we do a range check here:
for source_set in all_results.array_chunks::<3>() {
// value is irrelevant, it's just a range check
let _: [Variable; 1] = cs.perform_lookup::<2, 1>(table_id, &[source_set[0], source_set[1]]);
}
However, the check only ensures and_result and or_result are less than 128. In a prime field, for any given and_result and or_result, there will always be a xor_result such that composit_value = (xor_result as u64) << 32 | (or_result as u64) << 16 | (and_result as u64), though the xor_result may overflow (uint8).
let xor_results = xor_results.map(|el| unsafe { UInt8::from_variable_unchecked(el) });
In the last step, when we wrap the variable into a UInt8, we are using from_variable_unchecked, which means there is no overflow check. As a result, if an attacker provides incorrect result for and_result and or_result, an overflowed UInt8 xor_result will stay in our circuit and unexpected behavior may happen in the future.
Recommended Mitigation Steps
// check all three
for source_set in all_results.array_chunks::<3>() {
// value is irrelevant, it's just a range check
let _: [Variable; 1] = cs.perform_lookup::<2, 1>(table_id, &[source_set[0], source_set[1]]);
let _: [Variable; 1] = cs.perform_lookup::<2, 1>(table_id, &[source_set[1], source_set[2]]);
}
Assessed type
Math
Alex the Entreprenerd (judge) commented:
The Warden has found a missing constraint for
binop, due to a lack in an overflow check, malicious parameters could be passed in the circuit.
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
Likely the range check in the reduction gate only validates two of the three decomposed results, omitting a check on the xor result, and the unsafe conversion to UInt8 does not enforce an overflow check.
Impact
An attacker could forge arbitrary results for the and and or opcodes, and an overflowed UInt8 xor result could remain in the circuit, potentially leading to unexpected behaviour.
What the attacker needed
Ability to supply malicious circuit inputs
The fix
The range check should be extended to also validate the xor result by performing a lookup on the second and third elements of each triplet.
How it was found
- Perform a lookup to get the composite result for and, or and xor.
- Decompose the composite result into a larger array all_results.
- Apply a range check that only ensures and_result and or_result are less than 128.
- Wrap the variables into UInt8 using from_variable_unchecked without an overflow check.
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 circuits, every value must be constrained to behave as expected. Here, a check that was supposed to ensure the results of bitwise operations fit in 8 bits only checked two of the three results, so a malicious user could supply a value that overflows and breaks the circuit's assumptions.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.