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 binary decomposition of 0 #853

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 22 additions & 4 deletions internal/regression_tests/issue_836_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ func TestIssue836Cmp(t *testing.T) {
Right: 5,
ExpCmpRes: -1,
}
assignmentHintBad2 := CmpCircuit{
Left: 10,
Right: 0,
ExpCmpRes: -1,
}
toReplaceHint, err := getNBitsHint()
if err != nil {
t.Fatalf("couldn't find hint to replace: %v", err)
}
assert.CheckCircuit(&CmpCircuit{}, test.WithValidAssignment(&assignmentNoHintGood), test.WithInvalidAssignment(&assignmentNoHintBad))
assert.CheckCircuit(&CmpCircuit{}, test.WithInvalidAssignment(&assignmentHintBad), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
assert.CheckCircuit(&CmpCircuit{}, test.WithInvalidAssignment(&assignmentHintBad), test.WithInvalidAssignment(&assignmentHintBad2), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
}

func TestIssue836AssertIsLess(t *testing.T) {
Expand All @@ -101,12 +106,16 @@ func TestIssue836AssertIsLess(t *testing.T) {
Smaller: 10,
Bigger: 5,
}
assignmentHintBad2 := AssertIsLessOrEqCircuit{
Smaller: 10,
Bigger: 0,
}
toReplaceHint, err := getNBitsHint()
if err != nil {
t.Fatalf("couldn't find hint to replace: %v", err)
}
assert.CheckCircuit(&AssertIsLessOrEqCircuit{}, test.WithValidAssignment(&assignmentNoHintGood), test.WithInvalidAssignment(&assignmentNoHintBad))
assert.CheckCircuit(&AssertIsLessOrEqCircuit{}, test.WithInvalidAssignment(&assignmentHintBad), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
assert.CheckCircuit(&AssertIsLessOrEqCircuit{}, test.WithInvalidAssignment(&assignmentHintBad), test.WithInvalidAssignment(&assignmentHintBad2), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
}

func TestIssue836MathCmpAssertIsLessEqBounded(t *testing.T) {
Expand All @@ -123,12 +132,17 @@ func TestIssue836MathCmpAssertIsLessEqBounded(t *testing.T) {
Left: 10,
Right: 5,
}
assignmentHintBad2 := MathCmpAssertIsLessOrEqCircuitBounded{
Left: 10,
Right: 0,
}

toReplaceHint, err := getNBitsHint()
if err != nil {
t.Fatalf("couldn't find hint to replace: %v", err)
}
assert.CheckCircuit(&MathCmpAssertIsLessOrEqCircuitBounded{}, test.WithValidAssignment(&assignmentNoHintGood), test.WithInvalidAssignment(&assignmentNoHintBad))
assert.CheckCircuit(&MathCmpAssertIsLessOrEqCircuitBounded{}, test.WithInvalidAssignment(&assignmentHintBad), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
assert.CheckCircuit(&MathCmpAssertIsLessOrEqCircuitBounded{}, test.WithInvalidAssignment(&assignmentHintBad), test.WithInvalidAssignment(&assignmentHintBad2), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
}

func TestIssueXXXMathCmpAssertIsLessEqFull(t *testing.T) {
Expand All @@ -145,12 +159,16 @@ func TestIssueXXXMathCmpAssertIsLessEqFull(t *testing.T) {
Left: 10,
Right: 5,
}
assignmentHintBad2 := MathCmpAssertIsLessOrEqCircuitBounded{
Left: 10,
Right: 0,
}
toReplaceHint, err := getNBitsHint()
if err != nil {
t.Fatalf("couldn't find hint to replace: %v", err)
}
assert.CheckCircuit(&MathCmpAssertIsLessOrEqCircuitFull{}, test.WithValidAssignment(&assignmentNoHintGood), test.WithInvalidAssignment(&assignmentNoHintBad))
assert.CheckCircuit(&MathCmpAssertIsLessOrEqCircuitFull{}, test.WithInvalidAssignment(&assignmentHintBad), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
assert.CheckCircuit(&MathCmpAssertIsLessOrEqCircuitFull{}, test.WithInvalidAssignment(&assignmentHintBad), test.WithInvalidAssignment(&assignmentHintBad2), test.NoTestEngine(), test.WithSolverOpts(solver.OverrideHint(toReplaceHint, maliciousNbitsHint)))
}

func maliciousNbitsHint(mod *big.Int, inputs []*big.Int, results []*big.Int) error {
Expand Down
2 changes: 1 addition & 1 deletion std/math/bits/conversion_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func toBinary(api frontend.API, v frontend.Variable, opts ...BaseConversionOptio
if !omitReducednessCheck {
if cmper, ok := api.Compiler().(bitsComparatorConstant); ok {
bound := new(big.Int).Sub(api.Compiler().Field(), big.NewInt(1))
cmper.MustBeLessOrEqCst(bits, api.Compiler().Field(), bound)
cmper.MustBeLessOrEqCst(bits, bound, v)
} else {
panic("builder does not expose comparison to constant")
}
Expand Down
Loading