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

fix binary decomposition of 0 #853

merged 3 commits into from
Oct 12, 2023

Conversation

lightning-li
Copy link
Contributor

@lightning-li lightning-li commented Oct 7, 2023

Description

Fixes # (852)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

  • Added test cases for api.AssertIsLessEq

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@CLAassistant
Copy link

CLAassistant commented Oct 7, 2023

CLA assistant check
All committers have signed the CLA.

@gbotrel gbotrel requested a review from ivokub October 11, 2023 13:52
@ivokub
Copy link
Collaborator

ivokub commented Oct 12, 2023

Suggested edit:

diff --git a/internal/regression_tests/issue_836_test.go b/internal/regression_tests/issue_836_test.go
index 99c27f3a..0830c291 100644
--- a/internal/regression_tests/issue_836_test.go
+++ b/internal/regression_tests/issue_836_test.go
@@ -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) {
@@ -110,8 +115,7 @@ func TestIssue836AssertIsLess(t *testing.T) {
 		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(&assignmentHintBad2), 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) {
@@ -128,15 +132,19 @@ 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) {
+func TestIssue836MathCmpAssertIsLessEqFull(t *testing.T) {
 	assert := test.NewAssert(t)
 	assignmentNoHintGood := MathCmpAssertIsLessOrEqCircuitFull{
 		Left:  5,
@@ -150,12 +158,16 @@ func TestIssueXXXMathCmpAssertIsLessEqFull(t *testing.T) {
 		Left:  10,
 		Right: 5,
 	}
+	assignmentHintBad2 := MathCmpAssertIsLessOrEqCircuitFull{
+		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 {

Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we had overlooked the case for 0 vs modulus.

Everything is good but I suggested a few more tests for avoiding regressions in the future. I also suggested a test name rename to track the initial issue.

For some reason I wasn't able to push to your branch even though maintainer edits were allowed. This feature usually works but I'm not sure what happened now. I'll approve after incorporating the suggested edits.

@ivokub ivokub self-requested a review October 12, 2023 11:45
@ivokub
Copy link
Collaborator

ivokub commented Oct 12, 2023

Thanks! It was a good catch. Welcome as a gnark contributor!

@ivokub
Copy link
Collaborator

ivokub commented Oct 12, 2023

It seems we need to update the circuit statistics.

Can you do:

cd internal/stats/generate/
go run main.go -s

and then commit the updated internal/stats/latest.stats file? Thanks.

@lightning-li
Copy link
Contributor Author

It seems we need to update the circuit statistics.

Can you do:

cd internal/stats/generate/
go run main.go -s

and then commit the updated internal/stats/latest.stats file? Thanks.

done~

@ivokub
Copy link
Collaborator

ivokub commented Oct 12, 2023

Merging with failed tests - it seems that CI doesn't have access to some secrets when PRs are based on external forks.

@ivokub ivokub merged commit f0a1b75 into Consensys:master Oct 12, 2023
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: binary decomposition of a variable is not strictly asserted to be less than the modulus
3 participants