From a0e745fdbc4e1d638a77583ba3e75ff91aff584a Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:29:36 +0000 Subject: [PATCH 1/2] feat: optimize `verify_msg_block` --- src/sha256.nr | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/sha256.nr b/src/sha256.nr index 2a4cecd..0247722 100644 --- a/src/sha256.nr +++ b/src/sha256.nr @@ -232,25 +232,24 @@ fn verify_msg_block( msg_end = msg_end + INT_SIZE - msg_end % INT_SIZE; } + let max_read_index = std::cmp::min(message_size, msg_end); + // Reconstructed packed item. let mut msg_item: u32 = 0; // Inclusive at the end so that we can compare the last item. - let mut i: u32 = 0; for k in msg_start..=msg_end { - if k % INT_SIZE == 0 { + if (k != msg_start) & (k % INT_SIZE == 0) { // If we consumed some input we can compare against the block. - if (msg_start < message_size) & (k > msg_start) { - println(f"i is {i}"); - assert_eq(msg_block[i], msg_item as u32); - i = i + 1; - msg_item = 0; - } + let msg_block_index = (k - msg_start) / INT_SIZE - 1; + assert_eq(msg_block[msg_block_index], msg_item); + + msg_item = 0; } // Shift the accumulator msg_item = msg_item << 8; // If we have input to consume, add it at the rightmost position. - if k < message_size & k < msg_end { + if k < max_read_index { msg_item = msg_item + msg[k] as u32; } } From 667ddbd590050cb9ce821ef777c544b699fb6f97 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:37:02 +0000 Subject: [PATCH 2/2] feat: remove conditional from assignment --- src/sha256.nr | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/sha256.nr b/src/sha256.nr index 0247722..543b218 100644 --- a/src/sha256.nr +++ b/src/sha256.nr @@ -246,12 +246,10 @@ fn verify_msg_block( msg_item = 0; } - // Shift the accumulator - msg_item = msg_item << 8; + // If we have input to consume, add it at the rightmost position. - if k < max_read_index { - msg_item = msg_item + msg[k] as u32; - } + let msg_byte = if k < max_read_index { msg[k] } else { 0 }; + msg_item = (msg_item << 8) + msg_byte as u32; } }