-
Notifications
You must be signed in to change notification settings - Fork 406
fix: Reproduce and fix bytecode blowup #6972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
439c58c
Try to reproduce the bytecode size blowup.
aakoshh b84e84f
Rename example
aakoshh 1d2f3b7
Merge branch 'master' into 6929-bytecode-blowup
TomAFrench 9a32903
Use a conditional variable to reproduce the blowup
aakoshh a48e557
Merge branch '6929-bytecode-blowup' of github.com:noir-lang/noir into…
aakoshh 9da27ad
Merge remote-tracking branch 'origin/master' into 6929-bytecode-blowup
aakoshh 8a80fb8
Return array from compute_incoming_body_ciphertext
aakoshh f8983e8
Remove the encryption parts, they don't make a difference
aakoshh 239f694
Smaller numbers for smaller SSA
aakoshh b174397
Add another mem2reg before flattening to remove load and store
aakoshh 38455f9
Using the original numbers to be sure
aakoshh 8a9652e
Check in with the small numbers; if we see a regression it will be in…
aakoshh b086d50
Only use the original bit size for upcasts
aakoshh 3e3b885
Restrict truncation to 254 bits
aakoshh 9ba164c
Restrict truncation to 254 bits in the formula
aakoshh ec48354
Merge remote-tracking branch 'origin/master' into 6929-bytecode-blowup
aakoshh 8dac3ba
Fix typo in directory name
aakoshh 13b5871
Move to be under execution_success
aakoshh 8a98e68
Merge branch 'master' into 6929-bytecode-blowup
aakoshh 4343158
.
TomAFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_no_bug/encrypted_log_regression/Nargo.toml
This file contains hidden or 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 = "encrypted_log_regression" | ||
| type = "bin" | ||
| authors = [""] | ||
| compiler_version = ">=0.31.0" | ||
|
|
||
| [dependencies] |
116 changes: 116 additions & 0 deletions
116
test_programs/compile_success_no_bug/encrypted_log_regression/src/main.nr
This file contains hidden or 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,116 @@ | ||
| // The code below is inspired by [compute_encrypted_log](https://github.com/AztecProtocol/aztec-packages/blob/b42756bc10175fea9eb60544759e9dbe41ae5e76/noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr#L111) | ||
| // which resulted in a bytecode size blowup when compiled to ACIR, see https://github.com/noir-lang/noir/issues/6929 | ||
| // The issue was around `encrypted_bytes[offset + i]` generating large amounts of gates, as per the `flamegraph.sh` tool in aztec-packages. | ||
| // The details around encryption and addresses have been stripped away, focusing on just copying bytes of equivalent size arrays. | ||
|
|
||
| use std::aes128::aes128_encrypt; | ||
|
|
||
| global PRIVATE_LOG_SIZE_IN_FIELDS: u32 = 18; | ||
| global ENCRYPTED_PAYLOAD_SIZE_IN_BYTES: u32 = (PRIVATE_LOG_SIZE_IN_FIELDS - 1) * 31; | ||
| global EPH_PK_SIZE: u32 = 32; | ||
| global HEADER_SIZE: u32 = 48; | ||
| global OVERHEAD_PADDING: u32 = 15; | ||
| global OVERHEAD_SIZE: u32 = EPH_PK_SIZE + HEADER_SIZE + OVERHEAD_PADDING; | ||
| global PLAINTEXT_LENGTH_SIZE: u32 = 2; | ||
| global MAX_PRIVATE_LOG_PLAINTEXT_SIZE_IN_BYTES: u32 = | ||
| ENCRYPTED_PAYLOAD_SIZE_IN_BYTES - OVERHEAD_SIZE - PLAINTEXT_LENGTH_SIZE - 1 /* aes padding */; | ||
|
|
||
| fn main( | ||
| eph_pk_bytes: [u8; EPH_PK_SIZE], | ||
| aes_secret: [u8; 32], | ||
| incoming_header_ciphertext: [u8; HEADER_SIZE], | ||
| extended_plaintext: [u8; MAX_PRIVATE_LOG_PLAINTEXT_SIZE_IN_BYTES + PLAINTEXT_LENGTH_SIZE], | ||
| shift: u32, | ||
| ) -> pub [u8; ENCRYPTED_PAYLOAD_SIZE_IN_BYTES] { | ||
| // unsafe { | ||
| // compute_encrypted_log_unconstrained( | ||
| // eph_pk_bytes, | ||
| // aes_secret, | ||
| // incoming_header_ciphertext, | ||
| // extended_plaintext, | ||
| // shift, | ||
| // ) | ||
| // } | ||
| compute_encrypted_log( | ||
| eph_pk_bytes, | ||
| aes_secret, | ||
| incoming_header_ciphertext, | ||
| extended_plaintext, | ||
| shift, | ||
| ) | ||
| } | ||
|
|
||
| unconstrained fn compute_encrypted_log_unconstrained<let P: u32, let M: u32>( | ||
| eph_pk_bytes: [u8; EPH_PK_SIZE], | ||
| aes_secret: [u8; 32], | ||
| incoming_header_ciphertext: [u8; HEADER_SIZE], | ||
| plaintext: [u8; MAX_PRIVATE_LOG_PLAINTEXT_SIZE_IN_BYTES + PLAINTEXT_LENGTH_SIZE], | ||
| shift: u32, | ||
| ) -> [u8; M] { | ||
| compute_encrypted_log( | ||
| eph_pk_bytes, | ||
| aes_secret, | ||
| incoming_header_ciphertext, | ||
| plaintext, | ||
| shift, | ||
| ) | ||
| } | ||
|
|
||
| fn compute_encrypted_log<let P: u32, let M: u32>( | ||
| eph_pk_bytes: [u8; EPH_PK_SIZE], | ||
| aes_secret: [u8; 32], | ||
| incoming_header_ciphertext: [u8; HEADER_SIZE], | ||
| plaintext: [u8; P], | ||
| shift: u32, | ||
| ) -> [u8; M] { | ||
| let mut encrypted_bytes = [0; M]; | ||
| let mut offset = 0; | ||
|
|
||
| // eph_pk | ||
| for i in 0..EPH_PK_SIZE { | ||
| encrypted_bytes[offset + i] = eph_pk_bytes[i]; | ||
| } | ||
| offset += EPH_PK_SIZE; | ||
|
|
||
| // incoming_header | ||
| for i in 0..HEADER_SIZE { | ||
| encrypted_bytes[offset + i] = incoming_header_ciphertext[i]; | ||
| } | ||
| offset += HEADER_SIZE; | ||
|
|
||
| // Padding. | ||
| offset += OVERHEAD_PADDING; | ||
|
|
||
| // incoming_body | ||
| let incoming_body_ciphertext = compute_incoming_body_ciphertext(plaintext, aes_secret); | ||
| // Then we fill in the rest as the incoming body ciphertext | ||
| let size = M - offset; | ||
|
|
||
| // NOTE: This made the bytecode size blowup disappear in aztec packages, | ||
| // but in this reproduction the size seems to be statically known regardless. | ||
| //let size = M - 32 - HEADER_SIZE - OVERHEAD_PADDING; | ||
|
|
||
| assert_eq(size, incoming_body_ciphertext.len(), "ciphertext length mismatch"); | ||
| for i in 0..size { | ||
| // NOTE: Adding `shift` makes the index dynamic enough to force ACIR to use memory operations. | ||
| // Without `shift` it assigns directly to witnesses, which is good, but not what we wanted to reproduce. | ||
| encrypted_bytes[offset + i + shift] = incoming_body_ciphertext[i]; | ||
|
aakoshh marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| encrypted_bytes | ||
| } | ||
|
|
||
| pub fn compute_incoming_body_ciphertext<let P: u32>( | ||
| plaintext: [u8; P], | ||
| aes_secret: [u8; 32], | ||
| ) -> [u8] { | ||
|
aakoshh marked this conversation as resolved.
Outdated
|
||
| let full_key = aes_secret; | ||
| let mut sym_key = [0; 16]; | ||
| let mut iv = [0; 16]; | ||
|
|
||
| for i in 0..16 { | ||
| sym_key[i] = full_key[i]; | ||
| iv[i] = full_key[i + 16]; | ||
| } | ||
| aes128_encrypt(plaintext, iv, sym_key) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.