Submitted by xuwinnie
The range check in the binops 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.
There are three main steps in function get_binop_subresults:
At first, we perform a lookup to get the composite result for and, or and xor.
Then, we decomposite it into a larger array all_results.
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:
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).
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.
Math
miladpiri (zkSync) confirmed
Alex the Entreprenerd (judge) commented:
