forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(meta): Comptime keccak (noir-lang#5854)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> Just under 1/3 of a public transfer in Aztec is due to a keccak hash of the selector. We should ideally be simplifying this through our normal compiler passes as the inputs are all known at compile time. This is difficult at the moment due to having the same compilation pipeline for SSA and ACIR and would require changing how we inline. For an easy win we can enable comptime hashing of keccak. This also just more generally provides another one of our foreign functions in the comptime environment which will be needed in the future. ## Summary\* I added functions in the interpreter for `keccakf1600` and `to_le_radix`. I also included a test under `compile_succes_empty` that calls keccak in a comptime env. The `assert_constant` builtin is also used by our byte decomposition functions. For this we can just return true as we already check that no non-comptime variables are referenced in comptime code. ## Additional Context ## Documentation\* Check one: - [] No documentation needed. - [ ] Documentation included in this PR. - [X] **[For Experimental Features]** 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: jfecher <jake@aztecprotocol.com>
Showing
5 changed files
with
114 additions
and
6 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
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/comptime_keccak/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_keccak" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.33.0" | ||
|
||
[dependencies] |
31 changes: 31 additions & 0 deletions
31
test_programs/compile_success_empty/comptime_keccak/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,31 @@ | ||
// Tests a very simple program. | ||
// | ||
// The features being tested is keccak256 in brillig | ||
fn main() { | ||
comptime | ||
{ | ||
let x = 0xbd; | ||
let result = [ | ||
0x5a, 0x50, 0x2f, 0x9f, 0xca, 0x46, 0x7b, 0x26, 0x6d, 0x5b, 0x78, 0x33, 0x65, 0x19, 0x37, 0xe8, 0x05, 0x27, 0x0c, 0xa3, 0xf3, 0xaf, 0x1c, 0x0d, 0xd2, 0x46, 0x2d, 0xca, 0x4b, 0x3b, 0x1a, 0xbf | ||
]; | ||
// We use the `as` keyword here to denote the fact that we want to take just the first byte from the x Field | ||
// The padding is taken care of by the program | ||
let digest = keccak256([x as u8], 1); | ||
assert(digest == result); | ||
//#1399: variable message size | ||
let message_size = 4; | ||
let hash_a = keccak256([1, 2, 3, 4], message_size); | ||
let hash_b = keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size); | ||
|
||
assert(hash_a == hash_b); | ||
|
||
let message_size_big = 8; | ||
let hash_c = keccak256([1, 2, 3, 4, 0, 0, 0, 0], message_size_big); | ||
|
||
assert(hash_a != hash_c); | ||
} | ||
} | ||
|
||
comptime fn keccak256<let N: u32>(data: [u8; N], msg_len: u32) -> [u8; 32] { | ||
std::hash::keccak256(data, msg_len) | ||
} |