Skip to content

fix(scs): don't panic on a recorded addition with a zero coefficient - #1836

Open
0xShadowX wants to merge 2 commits into
Consensys-Incorporated:masterfrom
0xShadowX:fix/scs-add-zero-coeff
Open

0xShadowX wants to merge 2 commits into
Consensys-Incorporated:masterfrom
0xShadowX:fix/scs-add-zero-coeff

Conversation

@0xShadowX

Copy link
Copy Markdown

Compiling this with the SCS builder fails with parse circuit: div by 0, while R1CS and the test engine accept it:

t1 := api.Add(c.A, c.B, api.Neg(c.B))
t2 := api.Add(api.Mul(2, c.A), c.B, api.Neg(c.B))

reduce merges B and -B into a single term with coefficient 0, so the first Add records A*1 + B*0. The second one finds that entry in addConstraintExist, the q3*q2 == q1*q4 check passes as 0 == 0, and then Inverse(q2) fails and hits the panic("div by 0") // shouldn't happen branch. #1068 fixed the same kind of zero coefficient for Mul, but the Add path can still get one through cancellation.

The fix returns "not found" when q2 has no inverse, so the builder just adds a new constraint for this (rare) case. Nothing changes for circuits that compiled before.

Tests:

go test -count=1 -run TestExistDiv03 ./frontend/cs/scs/
go test -short ./frontend/... ./internal/stats/...

TestExistDiv03 fails on master with div by 0 and passes with this change; the stats test shows no constraint count changes.

  • regression test added
  • no generated files touched

reduce keeps a term whose merged coefficient is zero, so x + y - y records
the addition x*1 + y*0. A later 2x + y - y finds that entry and tries to
compute the output coefficient as q4/q2 = 0/0, which panics. Return "not
found" there and add a new constraint instead.
@0xShadowX
0xShadowX requested a review from a team as a code owner September 27, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant