Skip to content

Commit

Permalink
fix binary decomposition of 0 (#853)
Browse files Browse the repository at this point in the history
* fix binary decomposition of 0

* add more regression testcases

* update circuit statistics
  • Loading branch information
lightning-li authored Oct 12, 2023
1 parent 4cb9063 commit f0a1b75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
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
Binary file modified internal/stats/latest.stats
Binary file not shown.
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

0 comments on commit f0a1b75

Please sign in to comment.