diff --git a/crates/contracts/src/storage.cairo b/crates/contracts/src/storage.cairo index e517cab66..3f047b1a8 100644 --- a/crates/contracts/src/storage.cairo +++ b/crates/contracts/src/storage.cairo @@ -38,11 +38,13 @@ impl StoreBytecode of Store { // 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() }) } diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index b9b78a161..bcb0729f0 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -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'); @@ -770,14 +774,15 @@ mod tests { let result: u256 = vm.memory.load_internal(dest_offset).into(); let mut results: Array = 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]); + } + }; } // ************************************************************************* diff --git a/crates/evm/src/instructions/sha3.cairo b/crates/evm/src/instructions/sha3.cairo index 0abed6655..563a5fa12 100644 --- a/crates/evm/src/instructions/sha3.cairo +++ b/crates/evm/src/instructions/sha3.cairo @@ -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, 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 } diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index f6ed17400..601fc1bbc 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -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 ) { - 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. diff --git a/crates/evm/src/precompiles.cairo b/crates/evm/src/precompiles.cairo index e5cb90500..ff8dd9386 100644 --- a/crates/evm/src/precompiles.cairo +++ b/crates/evm/src/precompiles.cairo @@ -39,9 +39,11 @@ pub const FIRST_ROLLUP_PRECOMPILE_ADDRESS: u256 = 0x100; /// * `Set` - A set containing all Ethereum precompile addresses. pub fn eth_precompile_addresses() -> Set { let mut precompile_addresses: Array = 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) } diff --git a/crates/evm/src/precompiles/blake2f.cairo b/crates/evm/src/precompiles/blake2f.cairo index 76652ada1..ac16b81d2 100644 --- a/crates/evm/src/precompiles/blake2f.cairo +++ b/crates/evm/src/precompiles/blake2f.cairo @@ -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; diff --git a/crates/evm/src/state.cairo b/crates/evm/src/state.cairo index d094eb91c..0ea8ce3ca 100644 --- a/crates/evm/src/state.cairo +++ b/crates/evm/src/state.cairo @@ -98,7 +98,6 @@ impl StateChangeLogImpl, +Copy> of StateChangeLogTrait { cloned_changes.insert(*key, NullableTrait::new(value)); }; - StateChangeLog { changes: cloned_changes, keyset: self.keyset.clone(), } } } diff --git a/crates/snforge_utils/src/lib.cairo b/crates/snforge_utils/src/lib.cairo index 479db1182..0588fe66c 100644 --- a/crates/snforge_utils/src/lib.cairo +++ b/crates/snforge_utils/src/lib.cairo @@ -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 } } @@ -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'); } @@ -284,13 +289,16 @@ 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' + ); + } } } @@ -298,11 +306,14 @@ pub mod snforge_utils { 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) + ); + }; } } diff --git a/crates/utils/src/felt_vec.cairo b/crates/utils/src/felt_vec.cairo index d8046859c..cff02ceb0 100644 --- a/crates/utils/src/felt_vec.cairo +++ b/crates/utils/src/felt_vec.cairo @@ -57,13 +57,15 @@ pub impl Felt252VecTraitImpl< fn to_le_bytes(ref self: Felt252Vec) -> Span { let mut res: Array = 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() } diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index fe4701ecc..108d71138 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -136,12 +136,13 @@ pub fn load_word(mut len: usize, words: Span) -> 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 } @@ -156,16 +157,18 @@ pub fn load_word(mut len: usize, words: Span) -> u256 { pub fn u256_to_bytes_array(mut value: u256) -> Array { let mut bytes_arr: Array = 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(); @@ -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); }; } diff --git a/crates/utils/src/traits.cairo b/crates/utils/src/traits.cairo index 76fe4b17e..e5bfc3eb8 100644 --- a/crates/utils/src/traits.cairo +++ b/crates/utils/src/traits.cairo @@ -106,10 +106,11 @@ pub impl SpanU8TryIntoResultEthAddress of TryIntoResult, 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()) diff --git a/crates/utils/src/traits/bytes.cairo b/crates/utils/src/traits/bytes.cairo index ca63f19a0..9c9b3bd4a 100644 --- a/crates/utils/src/traits/bytes.cairo +++ b/crates/utils/src/traits/bytes.cairo @@ -65,15 +65,16 @@ pub impl U8SpanExImpl of U8SpanExTrait { // O(2n) should be okay // We might want to regroup every computation into a single loop with appropriate `if` // branching For optimisation - for byte_counter in 0..last_input_num_bytes { - last_input_word += match self.get(full_u64_word_count * 8 + byte_counter.into()) { - Option::Some(byte) => { - let byte: u64 = (*byte.unbox()).into(); - byte.shl(8_u64 * byte_counter.into()) - }, - Option::None => { break; }, + for byte_counter in 0 + ..last_input_num_bytes { + last_input_word += match self.get(full_u64_word_count * 8 + byte_counter.into()) { + Option::Some(byte) => { + let byte: u64 = (*byte.unbox()).into(); + byte.shl(8_u64 * byte_counter.into()) + }, + Option::None => { break; }, + }; }; - }; (u64_words, last_input_word, last_input_num_bytes) } @@ -254,10 +255,11 @@ pub impl ToBytesImpl< let mask = Bounded::::MAX.into(); let mut bytes: Array = Default::default(); - for i in 0..bytes_used { - let val = Bitshift::::shr(self, eight * (bytes_used - i - 1).into()); - bytes.append((val & mask).try_into().unwrap()); - }; + for i in 0 + ..bytes_used { + let val = Bitshift::::shr(self, eight * (bytes_used - i - 1).into()); + bytes.append((val & mask).try_into().unwrap()); + }; bytes.span() } @@ -278,10 +280,11 @@ pub impl ToBytesImpl< let mut bytes: Array = Default::default(); - for i in 0..bytes_used { - let val = self.shr(eight * i.into()); - bytes.append((val & mask).try_into().unwrap()); - }; + for i in 0 + ..bytes_used { + let val = self.shr(eight * i.into()); + bytes.append((val & mask).try_into().unwrap()); + }; bytes.span() } @@ -443,13 +446,15 @@ pub impl ByteArrayExt of ByteArrayExTrait { let (nb_full_words, pending_word_len) = DivRem::div_rem( bytes.len(), 31_u32.try_into().unwrap() ); - for _ in 0..nb_full_words { - let mut word: felt252 = 0; - for _ in 0..31_u8 { - word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + for _ in 0 + ..nb_full_words { + let mut word: felt252 = 0; + for _ in 0 + ..31_u8 { + word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + }; + arr.append_word(word.try_into().unwrap(), 31); }; - arr.append_word(word.try_into().unwrap(), 31); - }; if pending_word_len == 0 { return arr; @@ -457,9 +462,11 @@ pub impl ByteArrayExt of ByteArrayExTrait { let mut pending_word: felt252 = 0; - for _ in 0..pending_word_len { - pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); - }; + for _ in 0 + ..pending_word_len { + pending_word = pending_word * POW_256_1.into() + + (*bytes.pop_front().unwrap()).into(); + }; arr.append_word(pending_word.try_into().unwrap(), pending_word_len); arr } @@ -536,15 +543,16 @@ pub impl ByteArrayExt of ByteArrayExTrait { // We might want to regroup every computation into a single loop with appropriate `if` // branching For optimisation - for byte_counter in 0..last_input_num_bytes { - last_input_word += match self.at(full_u64_word_count * 8 + byte_counter.into()) { - Option::Some(byte) => { - let byte: u64 = byte.into(); - byte.shl(8_u64 * byte_counter.into()) - }, - Option::None => { break; }, + for byte_counter in 0 + ..last_input_num_bytes { + last_input_word += match self.at(full_u64_word_count * 8 + byte_counter.into()) { + Option::Some(byte) => { + let byte: u64 = byte.into(); + byte.shl(8_u64 * byte_counter.into()) + }, + Option::None => { break; }, + }; }; - }; (u64_words, last_input_word, last_input_num_bytes) } @@ -599,7 +607,6 @@ mod tests { for i in 0..arr.len() { assert(*arr[i] == res[i], 'byte mismatch'); }; - } #[test] diff --git a/crates/utils/src/traits/eth_address.cairo b/crates/utils/src/traits/eth_address.cairo index 5a00dc1e2..9f0122b29 100644 --- a/crates/utils/src/traits/eth_address.cairo +++ b/crates/utils/src/traits/eth_address.cairo @@ -13,10 +13,11 @@ pub impl EthAddressExImpl of EthAddressExTrait { let bytes_used: u256 = 20; let value: u256 = self.into(); let mut bytes: Array = Default::default(); - for i in 0..bytes_used { - let val = value.shr(8 * (bytes_used - i - 1)); - bytes.append((val & 0xFF).try_into().unwrap()); - }; + for i in 0 + ..bytes_used { + let val = value.shr(8 * (bytes_used - i - 1)); + bytes.append((val & 0xFF).try_into().unwrap()); + }; bytes } @@ -38,10 +39,11 @@ pub impl EthAddressExImpl of EthAddressExTrait { } let offset: u32 = len - 1; let mut result: u256 = 0; - for i in 0..len { - let byte: u256 = (*input.at(i)).into(); - result += byte.shl((8 * (offset - i)).into()); - }; + for i in 0 + ..len { + let byte: u256 = (*input.at(i)).into(); + result += byte.shl((8 * (offset - i)).into()); + }; result.try_into() } }