-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add comptime support for
modulus_*
compiler builtins (#5530)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR changes the function `std::compat::is_bn254` to run in a `comptime` context. To do this I've had to implement a few compiler builtins within the interpreter. I've had to mangle `std::compat::is_bn254` a bit to get around restrictions on when a function can be called in a `comptime` context however. This works around around the loop unrolling issue experienced in #4535. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
1 parent
e318f45
commit e8a2f8b
Showing
6 changed files
with
113 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
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,7 +1,20 @@ | ||
global BN254_MODULUS_BE_BYTES: [u8] = &[ | ||
comptime global BN254_MODULUS_BE_BYTES: [u8] = &[ | ||
48, 100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40, 51, 232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 1 | ||
]; | ||
|
||
pub fn is_bn254() -> bool { | ||
crate::field::modulus_be_bytes() == BN254_MODULUS_BE_BYTES | ||
comptime { | ||
// We can't use the `Eq` trait here due to limitations on calling non-comptime functions | ||
// defined within the same crate. | ||
let mut eq = true; | ||
|
||
let modulus_be_bytes = crate::field::modulus_be_bytes(); | ||
// We can't do `BN254_MODULUS_BE_BYTES.len()` due to limitations on calling non-comptime functions. | ||
assert_eq(crate::field::modulus_num_bits(), 254); | ||
for i in 0..32 { | ||
eq &= modulus_be_bytes[i] == BN254_MODULUS_BE_BYTES[i]; | ||
} | ||
|
||
eq | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
test_programs/execution_success/comptime_slice_equality/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,7 @@ | ||
[package] | ||
name = "comptime_slice_equality" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.31.0" | ||
|
||
[dependencies] |
5 changes: 5 additions & 0 deletions
5
test_programs/execution_success/comptime_slice_equality/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,5 @@ | ||
fn main() { | ||
comptime { | ||
assert_eq(&[1], &[1]); | ||
} | ||
} |