-
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: zero out input to
to_radix
calls if inactive (#4116)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* In situations where a `to_le_bytes` call is in an inactive if-statement, the requirement for the produced byte array to represent the input field is not disabled. This can result in situations such as below where we want to decompose a value into a number of bytes which is dependent on the inputs to the circuit. https://github.com/porco-rosso-j/safe-recovery-noir/blob/b80a1fd49d370bd095827318b378c8fdef2e2d9b/circuits/social/src/root.nr#L11-L22 All branches of this snippet are limited by the fact that the first will fail on `index` with values greater than 1. ``` fn main(x: Field, cond: bool) { if cond { let bad_byte_array = x.to_le_bytes(1); assert_eq(bad_byte_array.len(), 1); } } ``` This compiles down to ``` acir fn main f0 { b0(v0: Field, v1: u1): enable_side_effects v1 v31, v32 = call to_le_radix(v0, u32 2⁸, u32 1) v34 = cast v1 as Field v35 = mul v31, v34 constrain v35 == v34 enable_side_effects u1 1 return } ``` The `to_le_radix` will revert for all `v0 >= 8` no matter the value of `v1`. I've modified the `flatten_cfg` pass such that we multiply in the side effects variable to zero out the input should the instruction be deactivated. ## 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
0590432
commit 3f5bad3
Showing
5 changed files
with
36 additions
and
7 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: 0 additions & 2 deletions
2
test_programs/compile_success_empty/intrinsic_die/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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
use dep::std; | ||
// This test checks that we perform dead-instruction-elimination on intrinsic functions. | ||
fn main(x: Field) { | ||
let bytes = x.to_be_bytes(32); | ||
|
||
let hash = std::hash::pedersen_commitment([x]); | ||
let _p1 = std::scalar_mul::fixed_base_embedded_curve(x, 0); | ||
} |
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,2 @@ | ||
x = "2040124" | ||
cond = false |
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