Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes #1157 ensures calls to AttachDebugInfo are surrounded with… #1160

Merged
merged 2 commits into from
Jun 11, 2024
Merged
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
3 changes: 3 additions & 0 deletions debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (circuit *divBy0Trace) Define(api frontend.API) error {
}

func TestTraceDivBy0(t *testing.T) {
if !debug.Debug {
t.Skip("skipping test in non debug mode")
}
assert := require.New(t)

var circuit, witness divBy0Trace
Expand Down
13 changes: 8 additions & 5 deletions frontend/cs/r1cs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,14 @@ func (builder *builder) Div(i1, i2 frontend.Variable) frontend.Variable {

if !v2Constant {
res := builder.newInternalVariable()
debug := builder.newDebugInfo("div", v1, "/", v2, " == ", res)
v2Inv := builder.newInternalVariable()
// note that here we ensure that v2 can't be 0, but it costs us one extra constraint
c1 := builder.cs.AddR1C(builder.newR1C(v2, v2Inv, builder.cstOne()), builder.genericGate)
c2 := builder.cs.AddR1C(builder.newR1C(v1, v2Inv, res), builder.genericGate)
builder.cs.AttachDebugInfo(debug, []int{c1, c2})
if debug.Debug {
debug := builder.newDebugInfo("div", v1, "/", v2, " == ", res)
builder.cs.AttachDebugInfo(debug, []int{c1, c2})
}
return res
}

Expand Down Expand Up @@ -542,8 +544,6 @@ func (builder *builder) IsZero(i1 frontend.Variable) frontend.Variable {
return builder.cstZero()
}

debug := builder.newDebugInfo("isZero", a)

// x = 1/a // in a hint (x == 0 if a == 0)
// m = -a*x + 1 // constrain m to be 1 if a == 0
// a * m = 0 // constrain m to be 0 if a != 0
Expand All @@ -563,7 +563,10 @@ func (builder *builder) IsZero(i1 frontend.Variable) frontend.Variable {
// a * m = 0 // constrain m to be 0 if a != 0
c2 := builder.cs.AddR1C(builder.newR1C(a, m, builder.cstZero()), builder.genericGate)

builder.cs.AttachDebugInfo(debug, []int{c1, c2})
if debug.Debug {
debug := builder.newDebugInfo("isZero", a)
builder.cs.AttachDebugInfo(debug, []int{c1, c2})
}

builder.MarkBoolean(m)

Expand Down
13 changes: 6 additions & 7 deletions frontend/cs/r1cs/api_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ func (builder *builder) mustBeLessOrEqVar(a, bound frontend.Variable) {

_, aConst := builder.constantValue(a)

debug := builder.newDebugInfo("mustBeLessOrEq", a, " <= ", bound)

nbBits := builder.cs.FieldBitLen()

aBits := bits.ToBinary(builder, a, bits.WithNbDigits(nbBits), bits.WithUnconstrainedOutputs(), bits.OmitModulusCheck())
Expand Down Expand Up @@ -179,7 +177,10 @@ func (builder *builder) mustBeLessOrEqVar(a, bound frontend.Variable) {
}
}

builder.cs.AttachDebugInfo(debug, added)
if debug.Debug {
debug := builder.newDebugInfo("mustBeLessOrEq", a, " <= ", bound)
builder.cs.AttachDebugInfo(debug, added)
}

}

Expand All @@ -204,9 +205,6 @@ func (builder *builder) MustBeLessOrEqCst(aBits []frontend.Variable, bound *big.
panic("AssertIsLessOrEqual: bound is too large, constraint will never be satisfied")
}

// debug info
debug := builder.newDebugInfo("mustBeLessOrEq", aForDebug, " <= ", builder.toVariable(bound))

// t trailing bits in the bound
t := 0
for i := 0; i < nbBits; i++ {
Expand Down Expand Up @@ -243,7 +241,8 @@ func (builder *builder) MustBeLessOrEqCst(aBits []frontend.Variable, bound *big.
}
}

if len(added) != 0 {
if debug.Debug && len(added) != 0 {
debug := builder.newDebugInfo("mustBeLessOrEq", aForDebug, " <= ", builder.toVariable(bound))
builder.cs.AttachDebugInfo(debug, added)
}
}
20 changes: 13 additions & 7 deletions frontend/cs/scs/api_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"math/big"

"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/debug"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/internal/expr"
Expand Down Expand Up @@ -182,8 +183,10 @@ func (builder *builder) AssertIsLessOrEqual(v frontend.Variable, bound frontend.
}

func (builder *builder) mustBeLessOrEqVar(a frontend.Variable, bound expr.Term) {

debug := builder.newDebugInfo("mustBeLessOrEq", a, " <= ", bound)
var debugInfo []constraint.DebugInfo
if debug.Debug {
debugInfo = []constraint.DebugInfo{builder.newDebugInfo("mustBeLessOrEq", a, " <= ", bound)}
}

nbBits := builder.cs.FieldBitLen()

Expand Down Expand Up @@ -219,14 +222,14 @@ func (builder *builder) mustBeLessOrEqVar(a frontend.Variable, bound expr.Term)
builder.addPlonkConstraint(sparseR1C{
xa: l.VID,
qL: l.Coeff,
}, debug)
}, debugInfo...)
} else {
// l * a[i] == 0
builder.addPlonkConstraint(sparseR1C{
xa: l.VID,
xb: aBits[i].(expr.Term).VID,
qM: l.Coeff,
}, debug)
}, debugInfo...)
}

}
Expand Down Expand Up @@ -254,8 +257,11 @@ func (builder *builder) MustBeLessOrEqCst(aBits []frontend.Variable, bound *big.
panic("AssertIsLessOrEqual: bound is too large, constraint will never be satisfied")
}

// debug info
debug := builder.newDebugInfo("mustBeLessOrEq", aForDebug, " <= ", bound)
// debugInfo info
var debugInfo []constraint.DebugInfo
if debug.Debug {
debugInfo = []constraint.DebugInfo{builder.newDebugInfo("mustBeLessOrEq", aForDebug, " <= ", bound)}
}

// t trailing bits in the bound
t := 0
Expand Down Expand Up @@ -289,7 +295,7 @@ func (builder *builder) MustBeLessOrEqCst(aBits []frontend.Variable, bound *big.
xa: l.VID,
xb: aBits[i].(expr.Term).VID,
qM: builder.tOne,
}, debug)
}, debugInfo...)
} else {
builder.AssertIsBoolean(aBits[i])
}
Expand Down
Loading