Mul/div relation should not be enforced when divisor is zero
The zero-knowledge circuit for the EraVM division opcode enforced a multiplication relation even when the divisor was zero, which does not hold for a non-zero dividend. An attacker could submit a transaction that made the circuit unprovable, causing a denial of service on the priority queue.
Submitted by xuwinnie
Lines of code
https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/era-zkevm_circuits/src/main_vm/opcodes/mul_div.rs#L286-L292 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/era-zkevm_circuits/src/main_vm/opcodes/mul_div.rs#L358
Impact
When applying opcode div, if dividend is nonzero and divisor is zero, the resulted quotient and remainder are both zero. The enforced mul/div relation does not hold. An unprovable transaction will dos the priority queue.
Proof of Concept
quotient_is_zero.conditionally_enforce_true(cs, divisor_is_zero);
remainder_is_zero.conditionally_enforce_true(cs, divisor_is_zero);
According to the EraVM spec, if divisor is zero, quotient and remainder should also be zero.
let uint256_zero = UInt256::zero(cs);
let rem_to_enforce = UInt32::parallel_select(
cs,
should_apply_mul,
&uint256_zero.inner,
&remainder_unchecked,
);
let a_to_enforce =
UInt32::parallel_select(cs, should_apply_mul, src0_view, "ient_unchecked);
let b_to_enforce = src1_view.clone();
let mul_low_to_enforce =
UInt32::parallel_select(cs, should_apply_mul, &mul_low_unchecked, &src0_view);
let mul_high_to_enforce = UInt32::parallel_select(
cs,
should_apply_mul,
&mul_high_unchecked,
&uint256_zero.inner,
);
let mul_relation = MulDivRelation {
a: a_to_enforce,
b: b_to_enforce,
rem: rem_to_enforce,
mul_low: mul_low_to_enforce,
mul_high: mul_high_to_enforce,
};
When dividing, the relation we need to enforce is src0 = q * src1 + rem. However, if src0 (dividend) is nonzero and src1 (divisor) is zero, both q and rem will be zero. The relation does not hold.
let apply_any = Boolean::multi_or(cs, &[should_apply_mul, should_apply_div]);
......
diffs_accumulator
.mul_div_relations
.push((apply_any, mul_div_relations));
In fact, this relation will be enforced as long as we apply div, which will make the operation unprovable.
Recommended Mitigation Steps
Don’t enforce mul/div relation when divisor is zero.
Assessed type
Context
miladpiri (zkSync) confirmed and commented:
Overconstrained.
Alex the Entreprenerd (judge) commented:
The Warden has shown a case in which the
mul/divrelation doesn’t require an additional constraint in the case of a zero divisor.
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 circuit unconditionally enforced the mul/div relation for division, even when the divisor was zero and the quotient and remainder were forced to zero, so the relation src0 = q * src1 + rem did not hold for a non-zero dividend.
What the attacker needed
Ability to submit a transaction to EraVM
The fix
The mul/div relation should not be enforced when the divisor is zero.
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 is a set of mathematical rules that a computer follows to prove it did a calculation correctly. Here, the rule for division was applied even when dividing by zero, which is not valid maths, so the proof could never be completed. That blocked the queue of pending transactions.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.