Skip to content
Merged
Show file tree
Hide file tree
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 Jan 7, 2025
b84e84f
Rename example
aakoshh Jan 7, 2025
1d2f3b7
Merge branch 'master' into 6929-bytecode-blowup
TomAFrench Jan 8, 2025
9a32903
Use a conditional variable to reproduce the blowup
aakoshh Jan 8, 2025
a48e557
Merge branch '6929-bytecode-blowup' of github.com:noir-lang/noir into…
aakoshh Jan 8, 2025
9da27ad
Merge remote-tracking branch 'origin/master' into 6929-bytecode-blowup
aakoshh Jan 8, 2025
8a80fb8
Return array from compute_incoming_body_ciphertext
aakoshh Jan 8, 2025
f8983e8
Remove the encryption parts, they don't make a difference
aakoshh Jan 8, 2025
239f694
Smaller numbers for smaller SSA
aakoshh Jan 8, 2025
b174397
Add another mem2reg before flattening to remove load and store
aakoshh Jan 8, 2025
38455f9
Using the original numbers to be sure
aakoshh Jan 8, 2025
8a9652e
Check in with the small numbers; if we see a regression it will be in…
aakoshh Jan 8, 2025
b086d50
Only use the original bit size for upcasts
aakoshh Jan 8, 2025
3e3b885
Restrict truncation to 254 bits
aakoshh Jan 8, 2025
9ba164c
Restrict truncation to 254 bits in the formula
aakoshh Jan 8, 2025
ec48354
Merge remote-tracking branch 'origin/master' into 6929-bytecode-blowup
aakoshh Jan 8, 2025
8dac3ba
Fix typo in directory name
aakoshh Jan 9, 2025
13b5871
Move to be under execution_success
aakoshh Jan 9, 2025
8a98e68
Merge branch 'master' into 6929-bytecode-blowup
aakoshh Jan 9, 2025
4343158
.
TomAFrench Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Comment thread
aakoshh marked this conversation as resolved.
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];
Comment thread
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] {
Comment thread
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)
}