Skip to content

Commit

Permalink
add #[inline_always] on certain small methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm committed Feb 13, 2025
1 parent 8a8041c commit b995c10
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/sha256.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use constants::{
INT_SIZE_PTR, MSG_BLOCK, MSG_SIZE_PTR, STATE, TWO_POW_16, TWO_POW_24, TWO_POW_32, TWO_POW_8,
};

mod constants;
pub(crate) mod constants;
mod tests;

// Implementation of SHA-256 mapping a byte array of variable length to
Expand Down Expand Up @@ -335,6 +335,7 @@ fn verify_msg_block_equals_last(
}

// Set the rightmost `zeros` number of bytes to 0.
#[inline_always]
fn set_item_zeros(item: u32, zeros: u8) -> u32 {
lshift8(rshift8(item, zeros), zeros)
}
Expand All @@ -360,6 +361,7 @@ fn get_item_byte(mut msg_item: u32, msg_byte_ptr: BLOCK_BYTE_PTR) -> u8 {
// Project a byte into a position in a field based on the overall block pointer.
// For example putting 1 into pointer 5 would be 100, because overall we would
// have [____, 0100] with indexes [0123,4567].
#[inline_always]
fn byte_into_item(msg_byte: u8, msg_byte_ptr: BLOCK_BYTE_PTR) -> u32 {
let mut msg_item = msg_byte as u32;
// How many times do we have to shift to the left to get to the position we want?
Expand All @@ -369,6 +371,7 @@ fn byte_into_item(msg_byte: u8, msg_byte_ptr: BLOCK_BYTE_PTR) -> u32 {
}

// Construct a field out of 4 bytes.
#[inline_always]
fn make_item(b0: u8, b1: u8, b2: u8, b3: u8) -> u32 {
let mut item = b0 as u32;
item = lshift8(item, 1) + b1 as u32;
Expand All @@ -380,6 +383,7 @@ fn make_item(b0: u8, b1: u8, b2: u8, b3: u8) -> u32 {
// Shift by 8 bits to the left between 0 and 4 times.
// Checks `is_unconstrained()` to just use a bitshift if we're running in an unconstrained context,
// otherwise multiplies by 256.
#[inline_always]
fn lshift8(item: u32, shifts: u8) -> u32 {
if is_unconstrained() {
// Brillig wouldn't shift 0<<4 without overflow.
Expand Down

0 comments on commit b995c10

Please sign in to comment.