Skip to content

Commit

Permalink
scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrobles92 committed Sep 28, 2024
1 parent bd4cf17 commit 5675865
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 158 deletions.
12 changes: 7 additions & 5 deletions crates/contracts/src/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ impl StoreBytecode of Store<StorageBytecode> {
// afterwards.
let base: felt252 = 0;
let mut packed_bytecode = array![];
for i in 0..chunks_count + 1 {
let storage_address: StorageAddress = (base + i.into()).try_into().unwrap();
let chunk = storage_read_syscall(address_domain, storage_address).unwrap();
packed_bytecode.append(chunk);
};
for i in 0
..chunks_count
+ 1 {
let storage_address: StorageAddress = (base + i.into()).try_into().unwrap();
let chunk = storage_read_syscall(address_domain, storage_address).unwrap();
packed_bytecode.append(chunk);
};
let bytecode = load_packed_bytes(packed_bytecode.span(), bytecode_len);
SyscallResult::Ok(StorageBytecode { bytecode: bytecode.span() })
}
Expand Down
45 changes: 25 additions & 20 deletions crates/evm/src/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,22 @@ mod tests {
// Memory initialization with a value to verify that if the offset + size is out of the
// bound bytes, 0's have been copied.
// Otherwise, the memory value would be 0, and we wouldn't be able to check it.
for i in 0..(size / 32) + 1 {
vm
.memory
.store(
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
dest_offset + (i * 32)
);

let initial: u256 = vm.memory.load_internal(dest_offset + (i * 32)).into();

assert_eq!(initial, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
};
for i in 0
..(size / 32)
+ 1 {
vm
.memory
.store(
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
dest_offset + (i * 32)
);

let initial: u256 = vm.memory.load_internal(dest_offset + (i * 32)).into();

assert_eq!(
initial, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
);
};

// When
vm.exec_calldatacopy().expect('exec_calldatacopy failed');
Expand Down Expand Up @@ -770,14 +774,15 @@ mod tests {
let result: u256 = vm.memory.load_internal(dest_offset).into();
let mut results: Array<u8> = u256_to_bytes_array(result);

for i in 0..size {
// For out of bound bytes, 0s will be copied.
if (i + offset >= bytecode.len()) {
assert_eq!(*results[i], 0);
} else {
assert_eq!(*results[i], *bytecode[i + offset]);
}
};
for i in 0
..size {
// For out of bound bytes, 0s will be copied.
if (i + offset >= bytecode.len()) {
assert_eq!(*results[i], 0);
} else {
assert_eq!(*results[i], *bytecode[i + offset]);
}
};
}

// *************************************************************************
Expand Down
21 changes: 11 additions & 10 deletions crates/evm/src/instructions/sha3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ fn compute_memory_words_amount(size: u32, offset: u32, mem_len: u32) -> (u32, u3
fn fill_array_with_memory_words(
ref self: VM, ref to_hash: Array<u64>, mut offset: u32, mut amount: u32
) -> u32 {
for _ in 0..amount {
let loaded = self.memory.load(offset);
let ((high_h, low_h), (high_l, low_l)) = loaded.split_into_u64_le();
to_hash.append(low_h);
to_hash.append(high_h);
to_hash.append(low_l);
to_hash.append(high_l);

offset += 32;
};
for _ in 0
..amount {
let loaded = self.memory.load(offset);
let ((high_h, low_h), (high_l, low_l)) = loaded.split_into_u64_le();
to_hash.append(low_h);
to_hash.append(high_h);
to_hash.append(low_l);
to_hash.append(high_l);

offset += 32;
};
offset
}

Expand Down
11 changes: 6 additions & 5 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,12 @@ pub(crate) impl InternalMemoryMethods of InternalMemoryTrait {
fn load_aligned_words(
ref self: Memory, mut chunk_index: usize, final_chunk: usize, ref elements: Array<u8>
) {
for i in chunk_index..final_chunk {
let value = self.items.get(i.into());
// Pushes 16 items to `elements`
helpers::split_word_128(value.into(), ref elements);
};
for i in chunk_index
..final_chunk {
let value = self.items.get(i.into());
// Pushes 16 items to `elements`
helpers::split_word_128(value.into(), ref elements);
};
}

/// Loads a `u256` element from the memory chunk at a specified offset.
Expand Down
8 changes: 5 additions & 3 deletions crates/evm/src/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ pub const FIRST_ROLLUP_PRECOMPILE_ADDRESS: u256 = 0x100;
/// * `Set<EthAddress>` - A set containing all Ethereum precompile addresses.
pub fn eth_precompile_addresses() -> Set<EthAddress> {
let mut precompile_addresses: Array<EthAddress> = array![];
for i in FIRST_ETHEREUM_PRECOMPILE_ADDRESS..LAST_ETHEREUM_PRECOMPILE_ADDRESS + 0x01 {
precompile_addresses.append(i.try_into().unwrap());
};
for i in FIRST_ETHEREUM_PRECOMPILE_ADDRESS
..LAST_ETHEREUM_PRECOMPILE_ADDRESS
+ 0x01 {
precompile_addresses.append(i.try_into().unwrap());
};
SetTrait::from_array(precompile_addresses)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/evm/src/precompiles/blake2f.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ pub impl Blake2f of Precompile {

let mut pos = 4;
for _ in 0..8_u8 {
// safe unwrap, because we have made sure of the input length to be 213
h.append(input.slice(pos, 8).from_le_bytes().unwrap());
pos += 8;
// safe unwrap, because we have made sure of the input length to be 213
h.append(input.slice(pos, 8).from_le_bytes().unwrap());
pos += 8;
};

let mut pos = 68;
Expand Down
1 change: 0 additions & 1 deletion crates/evm/src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl StateChangeLogImpl<T, +Drop<T>, +Copy<T>> of StateChangeLogTrait<T> {
cloned_changes.insert(*key, NullableTrait::new(value));
};


StateChangeLog { changes: cloned_changes, keyset: self.keyset.clone(), }
}
}
Expand Down
97 changes: 54 additions & 43 deletions crates/snforge_utils/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -217,32 +217,34 @@ pub mod snforge_utils {
let events = (*self.events.events).span();
let mut filtered_events = array![];

for i in 0..events.len() {
let (from, event) = events.at(i).clone();
let mut include = true;

if let Option::Some(addr) = self.contract_address {
if from != *addr {
include = false;
}
}
for i in 0
..events
.len() {
let (from, event) = events.at(i).clone();
let mut include = true;

if let Option::Some(addr) = self.contract_address {
if from != *addr {
include = false;
}
}

if include && self.key_filter.is_some() {
if !(event.keys.span() == (*self.key_filter).unwrap()) {
include = false;
}
}
if include && self.key_filter.is_some() {
if !(event.keys.span() == (*self.key_filter).unwrap()) {
include = false;
}
}

if include && self.data_filter.is_some() {
if !event.data.includes((*self.data_filter).unwrap()) {
include = false;
}
}
if include && self.data_filter.is_some() {
if !event.data.includes((*self.data_filter).unwrap()) {
include = false;
}
}

if include {
filtered_events.append(event.clone());
}
};
if include {
filtered_events.append(event.clone());
}
};

ContractEvents { events: filtered_events }
}
Expand All @@ -266,13 +268,16 @@ pub mod snforge_utils {
event.append_keys_and_data(ref expected_keys, ref expected_data);

let mut found = false;
for i in 0..self.events.len() {
let event = self.events.at(i);
if event.keys == @expected_keys && event.data == @expected_data {
found = true;
break;
}
};
for i in 0
..self
.events
.len() {
let event = self.events.at(i);
if event.keys == @expected_keys && event.data == @expected_data {
found = true;
break;
}
};

assert(found, 'Expected event was not emitted');
}
Expand All @@ -284,25 +289,31 @@ pub mod snforge_utils {
let mut expected_data = array![];
event.append_keys_and_data(ref expected_keys, ref expected_data);

for i in 0..self.events.len() {
let event = self.events.at(i);
assert(
event.keys != @expected_keys || event.data != @expected_data,
'Unexpected event was emitted'
);
}
for i in 0
..self
.events
.len() {
let event = self.events.at(i);
assert(
event.keys != @expected_keys || event.data != @expected_data,
'Unexpected event was emitted'
);
}
}
}

/// Stores a value in the EVM storage of a given Starknet contract.
pub fn store_evm(target: Address, evm_key: u256, evm_value: u256) {
let storage_address = compute_storage_key(target.evm, evm_key);
let serialized_value = [evm_value.low.into(), evm_value.high.into()].span();
for offset in 0..serialized_value.len() {
store_felt252(
target.starknet, storage_address + offset.into(), *serialized_value.at(offset)
);
};

for offset in 0
..serialized_value
.len() {
store_felt252(
target.starknet,
storage_address + offset.into(),
*serialized_value.at(offset)
);
};
}
}
16 changes: 9 additions & 7 deletions crates/utils/src/felt_vec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ pub impl Felt252VecTraitImpl<
fn to_le_bytes(ref self: Felt252Vec<T>) -> Span<u8> {
let mut res: Array<u8> = array![];

for i in 0..self.len() {
if self[i] == Zero::zero() {
res.append(Zero::zero());
} else {
res.append_span(self[i].to_le_bytes());
}
};
for i in 0
..self
.len() {
if self[i] == Zero::zero() {
res.append(Zero::zero());
} else {
res.append_span(self[i].to_le_bytes());
}
};

res.span()
}
Expand Down
33 changes: 18 additions & 15 deletions crates/utils/src/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ pub fn load_word(mut len: usize, words: Span<u8>) -> u256 {
let mut current: u256 = 0;
let mut counter = 0;

for _ in 0..len {
let loaded: u8 = *words[counter];
let tmp = current * 256;
current = tmp + loaded.into();
counter += 1;
};
for _ in 0
..len {
let loaded: u8 = *words[counter];
let tmp = current * 256;
current = tmp + loaded.into();
counter += 1;
};

current
}
Expand All @@ -156,16 +157,18 @@ pub fn load_word(mut len: usize, words: Span<u8>) -> u256 {
pub fn u256_to_bytes_array(mut value: u256) -> Array<u8> {
let mut bytes_arr: Array<u8> = ArrayTrait::new();
// low part
for _ in 0..16_u8 {
bytes_arr.append((value.low & 0xFF).try_into().unwrap());
value.low /= 256;
};
for _ in 0
..16_u8 {
bytes_arr.append((value.low & 0xFF).try_into().unwrap());
value.low /= 256;
};

// high part
for _ in 0..16_u8 {
bytes_arr.append((value.high & 0xFF).try_into().unwrap());
value.high /= 256;
};
for _ in 0
..16_u8 {
bytes_arr.append((value.high & 0xFF).try_into().unwrap());
value.high /= 256;
};

// Reverse the array as memory is arranged in big endian order.
let mut counter = bytes_arr.len();
Expand Down Expand Up @@ -351,7 +354,7 @@ mod tests {
assert(dst4.len() == 16, 'dst4: wrong length');
// let mut counter: usize = 0;
assert(*dst4[15] == 0xfe, 'dst4: wrong LSB value');
for counter in 0..dst4.len() - 1 {
for counter in 0..dst4.len() - 1 {
assert_eq!(*dst4[counter], 0xff);
};
}
Expand Down
9 changes: 5 additions & 4 deletions crates/utils/src/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ pub impl SpanU8TryIntoResultEthAddress of TryIntoResult<Span<u8>, EthAddress> {
ensure(!(len > 20), EVMError::TypeConversionError(TYPE_CONVERSION_ERROR))?;
let offset: u32 = len.into() - 1;
let mut result: u256 = 0;
for i in 0..len {
let byte: u256 = (*self.at(i)).into();
result += byte.shl(8 * (offset - i).into());
};
for i in 0
..len {
let byte: u256 = (*self.at(i)).into();
result += byte.shl(8 * (offset - i).into());
};
let address: felt252 = result.try_into_result()?;

Result::Ok(address.try_into().unwrap())
Expand Down
Loading

0 comments on commit 5675865

Please sign in to comment.