Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions frontend/cs/scs/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ func TestExistDiv02(t *testing.T) {
assert.NoError(err)
}

type IssueDiv0Circuit3 struct {
A, B frontend.Variable
Res1, Res2 frontend.Variable
}

func (c *IssueDiv0Circuit3) Define(api frontend.API) error {
// B cancels out, which leaves a recorded addition with a zero coefficient
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))

api.AssertIsEqual(t1, c.Res1)
api.AssertIsEqual(t2, c.Res2)
return nil
}

func TestExistDiv03(t *testing.T) {
assert := test.NewAssert(t)
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &IssueDiv0Circuit3{})
assert.NoError(err)
w, err := frontend.NewWitness(&IssueDiv0Circuit3{
A: 11, B: 21,
Res1: 11, Res2: 22,
}, ecc.BN254.ScalarField())
assert.NoError(err)
_, err = ccs.Solve(w)
assert.NoError(err)
}

type TestZeroMulNoConstraintCircuit struct {
A, B frontend.Variable
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/cs/scs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ func (builder *builder[E]) addConstraintExist(a, b expr.Term[E], k E) (expr.Term
// compute n, the coefficient for the output wire
q2, ok = builder.cs.Inverse(q2)
if !ok {
panic("div by 0") // shouldn't happen
// q2 is zero when a term cancelled out in reduce (e.g. x + y - y),
// we can't compute n from it so we just add a new constraint.
return expr.Term[E]{}, false
}
q2 = builder.cs.Mul(q2, q4)
return expr.NewTerm(int(c.XC), q2), true
Expand Down