-
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.
fix: SSA typing for right shifts (#4302)
# Description ## Problem\* Partial work towards #4275 ## Summary\* Previously, SSA was issuing (lhs_type, Field) DIV for right shifts. However this is wrong if we support shifts by the bit size (`u1 >> 1`) This is interpreted in acir_gen as an euclidean division with bit_size = lhs_type.bit_size(). However, u1 >> 1 worked because of a + 1 that is in euclidean division https://github.com/noir-lang/noir/blob/0e073037b00212fd17fc8ca9c531b567614eb4c5/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs#L703 (and it failed in unconstrained functions since they don't codegen that extraneous + 1) ## 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. --------- Co-authored-by: TomAFrench <tom@tomfren.ch>
- Loading branch information
1 parent
7420dbb
commit 41ee1aa
Showing
5 changed files
with
37 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ fn main(x: u64, y: u64) { | |
assert(a << 7 == -128); | ||
assert(a << -a == -2); | ||
|
||
assert(x >> x == 0); | ||
} |
6 changes: 6 additions & 0 deletions
6
test_programs/execution_success/brillig_bit_shifts_runtime/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,6 @@ | ||
[package] | ||
name = "brillig_bit_shifts_runtime" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
test_programs/execution_success/brillig_bit_shifts_runtime/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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 64 | ||
y = 1 |
20 changes: 20 additions & 0 deletions
20
test_programs/execution_success/brillig_bit_shifts_runtime/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,20 @@ | ||
unconstrained fn main(x: u64, y: u64) { | ||
// runtime shifts on compile-time known values | ||
assert(64 << y == 128); | ||
assert(64 >> y == 32); | ||
// runtime shifts on runtime values | ||
assert(x << y == 128); | ||
assert(x >> y == 32); | ||
|
||
// Bit-shift with signed integers | ||
let mut a :i8 = y as i8; | ||
let mut b: i8 = x as i8; | ||
assert(b << 1 == -128); | ||
assert(b >> 2 == 16); | ||
assert(b >> a == 32); | ||
a = -a; | ||
assert(a << 7 == -128); | ||
assert(a << -a == -2); | ||
|
||
assert(x >> x == 0); | ||
} |