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 #1246 ensure cond is boolean in api.Select #1247

Merged
merged 2 commits into from
Aug 14, 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 frontend/cs/scs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ func (builder *builder) Select(b frontend.Variable, i1, i2 frontend.Variable) fr
return i1
}

// ensure the condition is a boolean
builder.AssertIsBoolean(b)

u := builder.Sub(i1, i2)
l := builder.Mul(u, b)

Expand Down
37 changes: 37 additions & 0 deletions internal/regression_tests/issue1246/issue1246_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package issue1246_test

import (
"testing"

"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
)

// Circuit definition
// here we aim to catch the case where the API doesn't enforce the condition to be a boolean
type notBoolCond struct {
Condition, Y1, Y2 frontend.Variable
}

func (circuit *notBoolCond) Define(api frontend.API) error {
d := api.Select(circuit.Condition, circuit.Y1, circuit.Y2)

// per api definition, d should hold either Y1 or Y2.
// we have y1 = 2, y2 = 4, condition = 2 (non boolean)
// r = condition(y1-y2) + y2
api.AssertIsEqual(d, 0)

return nil
}

func TestSelectConditionNonBool(t *testing.T) {
assert := test.NewAssert(t)

assert.CheckCircuit(&notBoolCond{},
test.WithInvalidAssignment(&notBoolCond{
Condition: 2,
Y1: 2,
Y2: 4,
}),
)
}
Loading