Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: concatenate_input now use bitshift #65

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions contracts/src/utils/keccak256.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alexandria_math::BitShift;
use core::byte_array::{ByteArray, ByteArrayTrait};
use core::integer::u128_byte_reverse;
use core::keccak::cairo_keccak;
Expand Down Expand Up @@ -252,18 +253,19 @@ pub fn one_shift_left_bytes_u256(n_bytes: u8) -> u256 {
fn concatenate_input(bytes: Span<ByteData>) -> ByteArray {
let mut output_string: ByteArray = Default::default();
let mut cur_idx = 0;

loop {
if (cur_idx == bytes.len()) {
break ();
if cur_idx == bytes.len() {
break;
}
let byte = *bytes.at(cur_idx);
if (byte.size == 32) {
// in order to store a 32-bytes entry in a ByteArray, we need to first append the upper 1-byte part , then the lower 31-bytes part
let up_byte = (byte.value / FELT252_MASK).try_into().unwrap();
output_string.append_word(up_byte, 1);
let down_byte = (byte.value & FELT252_MASK).try_into().unwrap();
output_string.append_word(down_byte, 31);
if byte.size == 32 {
// Extract the upper 1-byte part
let up_byte = up_bytes(byte.value);
output_string.append_word(up_byte.try_into().unwrap(), 1);

// Extract the lower 31-byte part
let down_byte = down_bytes(byte.value);
output_string.append_word(down_byte.try_into().unwrap(), 31);
} else {
output_string.append_word(byte.value.try_into().unwrap(), byte.size);
}
Expand Down Expand Up @@ -362,6 +364,15 @@ fn finalize_padding(ref input: Array<u64>, num_padding_words: u32) {

// --------------------------------------------------------------------------------------------
// END SECTION
/// Retrieve the 1 up byte of a given u256 input
fn up_bytes(input: u256) -> u256 {
BitShift::shr(input, 248) & 0xFF
}

/// Retrieve the 31 low byte of a given u256 input
fn down_bytes(input: u256) -> u256 {
input & FELT252_MASK
}

/// The general function that computes the keccak hash for an input span of ByteData
///
Expand Down Expand Up @@ -390,10 +401,34 @@ mod tests {
use starknet::contract_address_const;
use super::{
reverse_endianness, ByteData, HYPERLANE_ANNOUNCEMENT, compute_keccak, u64_word_size,
zero_keccak_hash, ADDRESS_SIZE
zero_keccak_hash, ADDRESS_SIZE, up_bytes, down_bytes
};
const TEST_STARKNET_DOMAIN: u32 = 23448594;

#[test]
fn test_up_bytes() {
let input = 0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
let expected = 0x01;
assert_eq!(up_bytes(input), expected);

let input = 0x11FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
let expected = 0x11;
assert_eq!(up_bytes(input), expected);

let input = 0x11;
let expected = 0;
assert_eq!(up_bytes(input), expected);
}
#[test]
fn test_down_bytes() {
let input = 0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
let expected = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
assert_eq!(down_bytes(input), expected);

let input = 0x0100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
let expected = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
assert_eq!(down_bytes(input), expected);
}

#[test]
fn test_reverse_endianness() {
Expand Down
Loading