Skip to content

Commit

Permalink
feat: swap endianness in-place in keccak implementation (#6128)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR removes the `temp` array created when swapping the endianness of
the `block_bytes` array and just swaps the values in place. This removes
some unnecessary reads/writes from brillig.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Sep 23, 2024
1 parent 98bc460 commit e3cdebe
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions noir_stdlib/src/hash/keccak.nr
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,27 @@ pub(crate) fn keccak256<let N: u32>(input: [u8; N], message_size: u32) -> [u8; 3
block_bytes[message_size] = 1;
block_bytes[real_blocks_bytes - 1] = 0x80;

// keccak lanes interpret memory as little-endian integers,
// means we need to swap our byte ordering
let num_limbs = max_blocks * LIMBS_PER_BLOCK; //max_blocks_length / WORD_SIZE;
for i in 0..num_limbs {
let mut temp = [0; WORD_SIZE];
let word_size_times_i = WORD_SIZE * i;
for j in 0..WORD_SIZE {
temp[j] = block_bytes[word_size_times_i+j];
}
for j in 0..WORD_SIZE {
block_bytes[word_size_times_i + j] = temp[7 - j];
}
}

let mut sliced_buffer = Vec::new();
// populate a vector of 64-bit limbs from our byte array
for i in 0..num_limbs {
let word_size_times_i = i * WORD_SIZE;
let ws_times_i_plus_7 = word_size_times_i + 7;
let limb_start = WORD_SIZE * i;

let mut sliced = 0;
if (word_size_times_i + WORD_SIZE > max_blocks_length) {
let slice_size = max_blocks_length - word_size_times_i;
if (limb_start + WORD_SIZE > max_blocks_length) {
let slice_size = max_blocks_length - limb_start;
let byte_shift = (WORD_SIZE - slice_size) * 8;
let mut v = 1;
for k in 0..slice_size {
sliced += v * (block_bytes[ws_times_i_plus_7-k] as Field);
sliced += v * (block_bytes[limb_start+k] as Field);
v *= 256;
}
let w = 1 << (byte_shift as u8);
sliced *= w as Field;
} else {
let mut v = 1;
for k in 0..WORD_SIZE {
sliced += v * (block_bytes[ws_times_i_plus_7-k] as Field);
sliced += v * (block_bytes[limb_start+k] as Field);
v *= 256;
}
}
Expand Down Expand Up @@ -156,4 +143,3 @@ mod tests {
assert_eq(keccak256(input, 13), result);
}
}

0 comments on commit e3cdebe

Please sign in to comment.