@@ -206,7 +206,10 @@ impl LogEncryption for AES128 {
206206 // = |full_pt| + 16 - (|full_pt| - 16 * (|full_pt| // 16))
207207 // = 16 + 16 * (|full_pt| // 16)
208208 // = 16 * (1 + |full_pt| // 16)
209- assert (ciphertext_bytes .len () == 16 * (1 + (PlaintextLen * 32 ) / 16 ));
209+ std:: static_assert (
210+ ciphertext_bytes .len () == 16 * (1 + (PlaintextLen * 32 ) / 16 ),
211+ "unexpected ciphertext length" ,
212+ );
210213
211214 // *****************************************************************************
212215 // Compute the header ciphertext
@@ -223,7 +226,10 @@ impl LogEncryption for AES128 {
223226 // bytes larger than the input in this case.
224227 let header_ciphertext_bytes = aes128_encrypt (header_plaintext , header_iv , header_sym_key );
225228 // I recall that converting a slice to an array incurs constraints, so I'll check the length this way instead:
226- assert (header_ciphertext_bytes .len () == HEADER_CIPHERTEXT_SIZE_IN_BYTES );
229+ std:: static_assert (
230+ header_ciphertext_bytes .len () == HEADER_CIPHERTEXT_SIZE_IN_BYTES ,
231+ "unexpected ciphertext header length" ,
232+ );
227233
228234 // *****************************************************************************
229235 // Prepend / append more bytes of data to the ciphertext, before converting back
@@ -238,7 +244,7 @@ impl LogEncryption for AES128 {
238244
239245 let mut log_bytes = get_arr_of_size__log_bytes__from_PT ::<PlaintextLen * 32 >();
240246
241- assert (
247+ std:: static_assert (
242248 log_bytes .len () % 31 == 0 ,
243249 "Unexpected error: log_bytes.len() should be divisible by 31, by construction." ,
244250 );
@@ -258,11 +264,22 @@ impl LogEncryption for AES128 {
258264 for i in 0 ..log_bytes_padding_to_mult_31 .len () {
259265 log_bytes [offset + i ] = log_bytes_padding_to_mult_31 [i ];
260266 }
261-
262- assert (
263- offset + log_bytes_padding_to_mult_31 .len () == log_bytes .len (),
264- "Something has gone wrong" ,
267+ offset += log_bytes_padding_to_mult_31 .len ();
268+
269+ // Ideally we would be able to have a static assert where we check that the offset would be such that we've
270+ // written to the entire log_bytes array, but we cannot since Noir does not treat the offset as a comptime
271+ // value (despite the values that it goes through being known at each stage). We instead check that the
272+ // computation used to obtain the offset computes the expected value (which we _can_ do in a static check), and
273+ // then add a cheap runtime check to also validate that the offset matches this.
274+ std:: static_assert (
275+ 1
276+ + header_ciphertext_bytes .len ()
277+ + ciphertext_bytes .len ()
278+ + log_bytes_padding_to_mult_31 .len ()
279+ == log_bytes .len (),
280+ "unexpected log length" ,
265281 );
282+ assert (offset == log_bytes .len (), "unexpected encrypted log length" );
266283
267284 // *****************************************************************************
268285 // Convert bytes back to fields
0 commit comments