-
Notifications
You must be signed in to change notification settings - Fork 386
feat: Add overflow and underflow checks for unsigned integers in brillig #4445
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5646128
fix: Remove old coercions done in brillig gen
sirasistant d77d1e7
feat: Add underflow and overflow checks in brillig
sirasistant 323c529
test: Add test for overflows
sirasistant 39b5cdb
clippy
sirasistant 5932f06
test: Add brillig wrapping test
sirasistant 6065719
clippy
sirasistant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test_programs/execution_success/brillig_fns_as_values/Prover.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| x = "0" | ||
| x = "1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [package] | ||
| name = "brillig_wrapping" | ||
| type = "bin" | ||
| authors = [""] | ||
|
|
||
| [dependencies] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| x = 0 | ||
| y = 255 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| use dep::std; | ||
|
|
||
| unconstrained fn main(x: u8, y: u8) { | ||
| assert(std::wrapping_sub(x, 1) == y); | ||
| assert(std::wrapping_add(y, 1) == x); | ||
| assert(std::wrapping_mul(y, y) == 1); | ||
| } | ||
|
|
5 changes: 5 additions & 0 deletions
5
test_programs/noir_test_success/brillig_overflow_checks/Nargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [package] | ||
| name = "brillig_overflow_checks" | ||
| type = "bin" | ||
| authors = [""] | ||
| [dependencies] |
Empty file.
23 changes: 23 additions & 0 deletions
23
test_programs/noir_test_success/brillig_overflow_checks/src/main.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use dep::std::field::bn254::{TWO_POW_128, assert_gt}; | ||
|
|
||
| #[test(should_fail_with = "attempt to add with overflow")] | ||
| unconstrained fn test_overflow_add() { | ||
| let a: u8 = 255; | ||
| let b: u8 = 1; | ||
| assert_eq(a + b, 0); | ||
| } | ||
|
|
||
| #[test(should_fail_with = "attempt to subtract with overflow")] | ||
| unconstrained fn test_overflow_sub() { | ||
| let a: u8 = 0; | ||
| let b: u8 = 1; | ||
| assert_eq(a - b, 255); | ||
| } | ||
|
|
||
| #[test(should_fail_with = "attempt to multiply with overflow")] | ||
| unconstrained fn test_overflow_mul() { | ||
| let a: u8 = 128; | ||
| let b: u8 = 2; | ||
| assert_eq(a * b, 0); | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.