Skip to content

Commit f86661e

Browse files
committed
chore: make the aes asserts static
These are all comptime values, and so the asserts would compile to either no-ops or unconditional panics. We want for them to be static, so that we find these things at compile time instead.
1 parent 87fc924 commit f86661e

File tree

1 file changed

+24
-7
lines changed
  • noir-projects/aztec-nr/aztec/src/messages/encryption

1 file changed

+24
-7
lines changed

noir-projects/aztec-nr/aztec/src/messages/encryption/aes128.nr

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ impl LogEncryption for AES128 {
208208
// = |full_pt| + 16 - (|full_pt| - 16 * (|full_pt| // 16))
209209
// = 16 + 16 * (|full_pt| // 16)
210210
// = 16 * (1 + |full_pt| // 16)
211-
assert(ciphertext_bytes.len() == 16 * (1 + (PlaintextLen * 32) / 16));
211+
std::static_assert(
212+
ciphertext_bytes.len() == 16 * (1 + (PlaintextLen * 32) / 16),
213+
"unexpected ciphertext length",
214+
);
212215

213216
// *****************************************************************************
214217
// Compute the header ciphertext
@@ -225,7 +228,10 @@ impl LogEncryption for AES128 {
225228
// bytes larger than the input in this case.
226229
let header_ciphertext_bytes = aes128_encrypt(header_plaintext, header_iv, header_sym_key);
227230
// I recall that converting a slice to an array incurs constraints, so I'll check the length this way instead:
228-
assert(header_ciphertext_bytes.len() == HEADER_CIPHERTEXT_SIZE_IN_BYTES);
231+
std::static_assert(
232+
header_ciphertext_bytes.len() == HEADER_CIPHERTEXT_SIZE_IN_BYTES,
233+
"unexpected ciphertext header length",
234+
);
229235

230236
// *****************************************************************************
231237
// Prepend / append more bytes of data to the ciphertext, before converting back
@@ -240,7 +246,7 @@ impl LogEncryption for AES128 {
240246

241247
let mut log_bytes = get_arr_of_size__log_bytes__from_PT::<PlaintextLen * 32>();
242248

243-
assert(
249+
std::static_assert(
244250
log_bytes.len() % 31 == 0,
245251
"Unexpected error: log_bytes.len() should be divisible by 31, by construction.",
246252
);
@@ -260,11 +266,22 @@ impl LogEncryption for AES128 {
260266
for i in 0..log_bytes_padding_to_mult_31.len() {
261267
log_bytes[offset + i] = log_bytes_padding_to_mult_31[i];
262268
}
263-
264-
assert(
265-
offset + log_bytes_padding_to_mult_31.len() == log_bytes.len(),
266-
"Something has gone wrong",
269+
offset += log_bytes_padding_to_mult_31.len();
270+
271+
// Ideally we would be able to have a static assert where we check that the offset would be such that we've
272+
// written to the entire log_bytes array, but we cannot since Noir does not treat the offset as a comptime
273+
// value (despite the values that it goes through being known at each stage). We instead check that the
274+
// computation used to obtain the offset computes the expected value (which we _can_ do in a static check), and
275+
// then add a cheap runtime check to also validate that the offset matches this.
276+
std::static_assert(
277+
1
278+
+ header_ciphertext_bytes.len()
279+
+ ciphertext_bytes.len()
280+
+ log_bytes_padding_to_mult_31.len()
281+
== log_bytes.len(),
282+
"unexpected log length",
267283
);
284+
assert(offset == log_bytes.len(), "unexpected encrypted log length");
268285

269286
// *****************************************************************************
270287
// Convert bytes back to fields

0 commit comments

Comments
 (0)