Skip to content

Commit

Permalink
More pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Dec 15, 2022
1 parent aa7a230 commit f34e614
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
44 changes: 42 additions & 2 deletions bytes-extended/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dep mem;
use std::{
bytes::Bytes,
constants::ZERO_B256,
vm::evm::evm_address::EvmAddress
};
use mem::alloc_stack_word;

Expand Down Expand Up @@ -42,7 +43,7 @@ fn write_value_to_stack(value: u64, byte_count: u64) -> raw_ptr {
/// starting at `ptr` are read.
/// * `byte_count` - The number of bytes of the original value. E.g. if the value
/// being read is a u32, this should be 4 bytes.
fn read_value_from_memory(ptr: raw_ptr, byte_count: u64) -> u64 {
fn read_value_from_memory<T>(ptr: raw_ptr, byte_count: u64) -> T {
// Allocate a whole word on the stack.
let stack_word_ptr = alloc_stack_word();
// Copy the `byte_count` bytes from `ptr` into `stack_word_ptr`.
Expand All @@ -51,7 +52,7 @@ fn read_value_from_memory(ptr: raw_ptr, byte_count: u64) -> u64 {
// right to be correctly read into a 4-byte u32.
ptr.copy_bytes_to(stack_word_ptr, byte_count);
// Get the word at stack_word_ptr.
let word = stack_word_ptr.read::<u64>();
let word = stack_word_ptr.read::<T>();
// Bit shift as neccesary.
word >> (BITS_PER_BYTE * (BYTES_PER_WORD - byte_count))
}
Expand All @@ -73,6 +74,21 @@ impl b256 {
}
}

pub const EVM_ADDRESS_BYTE_COUNT: u64 = 20u64;

impl EvmAddress {
/// Returns a pointer to the EvmAddress's packed bytes.
fn packed_bytes(self) -> raw_ptr {
__addr_of(self).add_uint_offset(B256_BYTE_COUNT - EVM_ADDRESS_BYTE_COUNT)
}

/// Gets an EvmAddress from a pointer to packed bytes.
fn from_packed_bytes(ptr: raw_ptr) -> Self {
let value = read_value_from_memory<b256>(ptr);
EvmAddress::from(value)
}
}

/// The number of bytes in a u64.
pub const U64_BYTE_COUNT: u64 = 8u64;

Expand Down Expand Up @@ -185,6 +201,28 @@ impl Bytes {
b256::from_packed_bytes(read_ptr)
}

// ===== EvmAddress ====

/// Writes an EvmAddress at the specified offset. Reverts if it violates the
/// bounds of self.
pub fn write_evm_address(ref mut self, offset: u64, value: EvmAddress) -> u64 {
self.write_packed_bytes(
offset,
value.packed_bytes(),
EVM_ADDRESS_BYTE_COUNT,
);
}

/// Reads an EvmAddress at the specified offset.
pub fn read_evm_address(ref mut self, offset: u64) -> EvmAddress {
let read_ptr = self.get_read_ptr(
offset,
EVM_ADDRESS_BYTE_COUNT,
);

EvmAddress::from_packed_bytes(read_ptr)
}

// ===== u64 ====

/// Writes a u64 at the specified offset. Reverts if it violates the
Expand Down Expand Up @@ -334,6 +372,8 @@ impl Bytes {
offset = _self.write_u16(offset, 32u16);
// Write the hash
offset = _self.write_b256(offset, hash);

assert(offset == _self.len);
_self
}
}
Expand Down
26 changes: 12 additions & 14 deletions multisig-ism/src/metadata.sw
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ pub struct MultisigMetadata {
}

const U8_BYTE_COUNT = 1u64;
const EVM_ADDRESS_BYTE_COUNT: u64 = 20u64;

fn domain_hash(origin: u32, mailbox: b256) -> b256 {
let suffix = "HYPERLANE";
let suffix_len = 9;

let mut bytes = Bytes::with_length(U32_BYTE_COUNT + B256_BYTE_COUNT + suffix_len);

bytes.write_u32(0, origin);
bytes.write_b256(U32_BYTE_COUNT, mailbox);
bytes.write_packed_bytes(U32_BYTE_COUNT + B256_BYTE_COUNT, __addr_of(suffix), suffix_len);
let mut offset = 0;
offset = bytes.write_u32(offset, origin);
offset = bytes.write_b256(offset, mailbox);
offset = bytes.write_packed_bytes(offset, __addr_of(suffix), suffix_len);

bytes.keccak256()
}
Expand All @@ -34,15 +33,13 @@ pub fn commitment(threshold: u8, validators: Vec<EvmAddress>) -> b256 {
let num_validators = validators.len();

let mut bytes = Bytes::with_length(U8_BYTE_COUNT + num_validators * EVM_ADDRESS_BYTE_COUNT);
bytes.write_u8(0, threshold);
let mut offset = 0;
offset = bytes.write_u8(offset, threshold);

let mut index = 0;
while index < num_validators {
bytes.write_packed_bytes(
U8_BYTE_COUNT + index * EVM_ADDRESS_BYTE_COUNT,
__addr_of(validators.get(index).unwrap()),
EVM_ADDRESS_BYTE_COUNT
);
let validator = validators.get(index).unwrap();
offset = bytes.write_evm_address(offset, validator);
index += 1;
}

Expand All @@ -54,9 +51,10 @@ pub fn checkpoint_hash(origin: u32, mailbox: b256, root: b256, index: u32) -> b2

let mut bytes = Bytes::with_length(B256_BYTE_COUNT + B256_BYTE_COUNT + U32_BYTE_COUNT);

bytes.write_b256(0, domain_hash);
bytes.write_b256(B256_BYTE_COUNT, root);
bytes.write_u32(B256_BYTE_COUNT + B256_BYTE_COUNT, index);
let mut offset = 0;
offset = bytes.write_b256(offset, domain_hash);
offset = bytes.write_b256(offset, root);
offset = bytes.write_u32(offset, index);

bytes.keccak256()
}
Expand Down

0 comments on commit f34e614

Please sign in to comment.