-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add overflow and underflow checks for unsigned integers in bril…
…lig (#4445) # Description ## Problem\* When overflow checks were added they were only added for ACIR. This adds add, sub and multiply overflow checks for unsigned operations in brillig. ## Summary\* ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
1 parent
ebaf05a
commit 21fc4b8
Showing
10 changed files
with
158 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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); | ||
} | ||
|