From 9118ef3378e20f7cf6b3259a802a43570e7c264d Mon Sep 17 00:00:00 2001 From: Fabricio Date: Thu, 26 Sep 2024 21:29:49 -0600 Subject: [PATCH 1/5] First batch of replacements --- crates/contracts/src/storage.cairo | 20 ++++++--- crates/evm/src/backend/starknet_backend.cairo | 18 +++++++- .../instructions/duplication_operations.cairo | 21 ++++++--- .../environmental_information.cairo | 45 ++++++++++++++----- .../src/instructions/push_operations.cairo | 7 ++- crates/evm/src/instructions/sha3.cairo | 14 +++++- crates/evm/src/memory.cairo | 24 ++++++---- crates/evm/src/precompiles/blake2f.cairo | 44 +++++++++++------- .../precompiles/ec_operations/ec_mul.cairo | 13 +++++- crates/evm/src/stack.cairo | 16 ++++--- crates/evm/src/state.cairo | 7 ++- crates/snforge_utils/src/lib.cairo | 14 ++++-- 12 files changed, 179 insertions(+), 64 deletions(-) diff --git a/crates/contracts/src/storage.cairo b/crates/contracts/src/storage.cairo index c2501e69d..b43a9f023 100644 --- a/crates/contracts/src/storage.cairo +++ b/crates/contracts/src/storage.cairo @@ -38,12 +38,17 @@ impl StoreBytecode of Store { // afterwards. let base: felt252 = 0; let mut packed_bytecode = array![]; - let mut i = 0; - while i != (chunks_count + 1) { + // let mut i = 0; + // while i != (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); + // i += 1; + // }; + 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); - i += 1; }; let bytecode = load_packed_bytes(packed_bytecode.span(), bytecode_len); SyscallResult::Ok(StorageBytecode { bytecode: bytecode.span() }) @@ -131,10 +136,13 @@ mod tests { fn test_store_bytecode_multiple_chunks() { let mut state = account_contract_state(); let mut bytecode_array = array![]; - let mut i = 0; - while i != 100 { + // let mut i = 0; + // while i != 100 { + // bytecode_array.append(i); + // i += 1; + // }; + for i in 0..100_u8 { bytecode_array.append(i); - i += 1; }; let bytecode = bytecode_array.span(); // Write the bytecode to the storage diff --git a/crates/evm/src/backend/starknet_backend.cairo b/crates/evm/src/backend/starknet_backend.cairo index c5cba92c7..946c4d66d 100644 --- a/crates/evm/src/backend/starknet_backend.cairo +++ b/crates/evm/src/backend/starknet_backend.cairo @@ -138,7 +138,11 @@ pub fn fetch_balance(self: @Address) -> u256 { /// `Ok(())` if the commit was successful, otherwise an `EVMError`. fn commit_accounts(ref state: State) -> Result<(), EVMError> { let mut account_keys = state.accounts.keyset.to_span(); - while let Option::Some(evm_address) = account_keys.pop_front() { + // while let Option::Some(evm_address) = account_keys.pop_front() { + // let account = state.accounts.changes.get(*evm_address).deref(); + // commit_account(@account, ref state); + // }; + for evm_address in account_keys { let account = state.accounts.changes.get(*evm_address).deref(); commit_account(@account, ref state); }; @@ -231,7 +235,17 @@ fn emit_events(ref self: State) -> Result<(), EVMError> { /// commit_storage MUST be called after commit_accounts. fn commit_storage(ref self: State) -> Result<(), EVMError> { let mut storage_keys = self.accounts_storage.keyset.to_span(); - while let Option::Some(state_key) = storage_keys.pop_front() { + // while let Option::Some(state_key) = storage_keys.pop_front() { + // let (evm_address, key, value) = self.accounts_storage.changes.get(*state_key).deref(); + // let mut account = self.get_account(evm_address); + // // @dev: EIP-6780 - If selfdestruct on an account created, dont commit data + // if account.is_selfdestruct() && account.is_created() { + // continue; + // } + // IAccountDispatcher { contract_address: account.starknet_address() } + // .write_storage(key, value); + // }; + for state_key in storage_keys { let (evm_address, key, value) = self.accounts_storage.changes.get(*state_key).deref(); let mut account = self.get_account(evm_address); // @dev: EIP-6780 - If selfdestruct on an account created, dont commit data diff --git a/crates/evm/src/instructions/duplication_operations.cairo b/crates/evm/src/instructions/duplication_operations.cairo index db322d326..5dee277ee 100644 --- a/crates/evm/src/instructions/duplication_operations.cairo +++ b/crates/evm/src/instructions/duplication_operations.cairo @@ -123,25 +123,32 @@ mod tests { // ensures all values start from index `from` upto index `to` of stack are `0x0` fn ensures_zeros(ref stack: Stack, from: u32, to: u32) { - let mut idx: u32 = from; + // let mut idx: u32 = from; if to > from { return; } - while idx != to { + // while idx != to { + // assert(stack.peek_at(idx).unwrap() == 0x00, 'should be zero'); + // idx += 1; + // }; + + for idx in from..to { assert(stack.peek_at(idx).unwrap() == 0x00, 'should be zero'); - idx += 1; }; } // push `n` number of `0x0` to the stack fn push_zeros(ref stack: Stack, n: u8) { - let mut i = 0; - while i != n { + // let mut i = 0; + // while i != n { + // stack.push(0x0).unwrap(); + // i += 1; + // } + for _ in 0..n { stack.push(0x0).unwrap(); - i += 1; - } + }; } #[test] diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index 2661198fa..118435966 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -90,10 +90,13 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait { // Fill the rest of the data to load with zeros // TODO: optimize once we have dw-based exponentiation - let mut i = 32 - bytes_len; - while i != 0 { + // let mut i = 32 - bytes_len; + // while i != 0 { + // data_to_load *= 256; + // i -= 1; + // }; + for _ in 0..32 - bytes_len { data_to_load *= 256; - i -= 1; }; self.stack.push(data_to_load) } @@ -652,8 +655,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. - let mut i = 0; - while i != (size / 32) + 1 { + // let mut i = 0; + // while i != (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); + + // i += 1; + // }; + for i in 0..(size / 32) + 1 { vm .memory .store( @@ -664,8 +681,6 @@ mod tests { let initial: u256 = vm.memory.load_internal(dest_offset + (i * 32)).into(); assert_eq!(initial, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - i += 1; }; // When @@ -775,16 +790,24 @@ mod tests { let result: u256 = vm.memory.load_internal(dest_offset).into(); let mut results: Array = u256_to_bytes_array(result); - let mut i = 0; - while i != size { + // let mut i = 0; + // while i != 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]); + // } + + // i += 1; + // }; + 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]); } - - i += 1; }; } diff --git a/crates/evm/src/instructions/push_operations.cairo b/crates/evm/src/instructions/push_operations.cairo index 31a53a698..05c62e2c9 100644 --- a/crates/evm/src/instructions/push_operations.cairo +++ b/crates/evm/src/instructions/push_operations.cairo @@ -333,9 +333,12 @@ mod tests { fn get_n_0xFF(mut n: u8) -> Span { let mut array: Array = ArrayTrait::new(); array.append(0x00); - while n != 0 { + // while n != 0 { + // array.append(0xFF); + // n -= 1; + // }; + for _ in 0..n { array.append(0xFF); - n -= 1; }; array.span() } diff --git a/crates/evm/src/instructions/sha3.cairo b/crates/evm/src/instructions/sha3.cairo index e04e36590..5a8cd618c 100644 --- a/crates/evm/src/instructions/sha3.cairo +++ b/crates/evm/src/instructions/sha3.cairo @@ -102,7 +102,18 @@ 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 { - while amount != 0 { + // while amount != 0 { + // 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; + // amount -= 1; + // }; + 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); @@ -111,7 +122,6 @@ fn fill_array_with_memory_words( to_hash.append(high_l); offset += 32; - amount -= 1; }; offset } diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index b24ec7ca7..b1828f22b 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -369,12 +369,17 @@ pub(crate) impl InternalMemoryMethods of InternalMemoryTrait { fn load_aligned_words( ref self: Memory, mut chunk_index: usize, final_chunk: usize, ref elements: Array ) { - while chunk_index != final_chunk { - let value = self.items.get(chunk_index.into()); + // while chunk_index != final_chunk { + // let value = self.items.get(chunk_index.into()); + // // Pushes 16 items to `elements` + // helpers::split_word_128(value.into(), ref elements); + // chunk_index += 1; + // } + 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); - chunk_index += 1; - } + }; } /// Loads a `u256` element from the memory chunk at a specified offset. @@ -810,10 +815,13 @@ mod tests { memory.load_n_internal(16, ref results, 0); assert(results.len() == 16, 'error'); - let mut i = 0; - while i != results.len() { - assert(*results[i] == 0xFF, 'byte value loaded not correct'); - i += 1; + // let mut i = 0; + // while i != results.len() { + // assert(*results[i] == 0xFF, 'byte value loaded not correct'); + // i += 1; + // } + for result in results { + assert(result == 0xFF, 'byte value loaded not correct'); } } diff --git a/crates/evm/src/precompiles/blake2f.cairo b/crates/evm/src/precompiles/blake2f.cairo index 94ecdd935..81149ce30 100644 --- a/crates/evm/src/precompiles/blake2f.cairo +++ b/crates/evm/src/precompiles/blake2f.cairo @@ -39,22 +39,32 @@ pub impl Blake2f of Precompile { let mut h: Array = Default::default(); let mut m: Array = Default::default(); - let mut i = 0; + // let mut i = 0; let mut pos = 4; - while i != 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()); - i += 1; - pos += 8; + // while i != 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()); + // i += 1; + // pos += 8; + // }; + 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; }; - let mut i = 0; + // let mut i = 0; let mut pos = 68; - while i != 16 { + // while i != 16 { + // // safe unwrap, because we have made sure of the input length to be 213 + // m.append(input.slice(pos, 8).from_le_bytes().unwrap()); + // i += 1; + // pos += 8; + // }; + for _ in 0..16_u8 { // safe unwrap, because we have made sure of the input length to be 213 m.append(input.slice(pos, 8).from_le_bytes().unwrap()); - i += 1; - pos += 8; + pos += 8 }; let mut t: Array = Default::default(); @@ -68,12 +78,16 @@ pub impl Blake2f of Precompile { let mut return_data: Array = Default::default(); - let mut i = 0; - while i != res.len() { - let bytes = (*res[i]).to_le_bytes_padded(); - return_data.append_span(bytes); + // let mut i = 0; + // while i != res.len() { + // let bytes = (*res[i]).to_le_bytes_padded(); + // return_data.append_span(bytes); - i += 1; + // i += 1; + // }; + for result in res { + let bytes = (*result).to_le_bytes_padded(); + return_data.append_span(bytes); }; Result::Ok((gas, return_data.span())) diff --git a/crates/evm/src/precompiles/ec_operations/ec_mul.cairo b/crates/evm/src/precompiles/ec_operations/ec_mul.cairo index 6c94d7d97..10c2c2f80 100644 --- a/crates/evm/src/precompiles/ec_operations/ec_mul.cairo +++ b/crates/evm/src/precompiles/ec_operations/ec_mul.cairo @@ -117,7 +117,18 @@ fn get_bits_little(s: u256) -> Array { fn ec_mul_inner(pt: (u384, u384), mut bits: Array) -> Option<(u384, u384)> { let (mut temp_x, mut temp_y) = pt; let mut result: Option<(u384, u384)> = Option::None; - while let Option::Some(bit) = bits.pop_front() { + // while let Option::Some(bit) = bits.pop_front() { + // if bit != 0 { + // match result { + // Option::Some((xr, yr)) => result = ec_safe_add(temp_x, temp_y, xr, yr), + // Option::None => result = Option::Some((temp_x, temp_y)), + // }; + // }; + // let (_temp_x, _temp_y) = double_ec_point_unchecked(temp_x, temp_y); + // temp_x = _temp_x; + // temp_y = _temp_y; + // }; + for bit in bits { if bit != 0 { match result { Option::Some((xr, yr)) => result = ec_safe_add(temp_x, temp_y, xr, yr), diff --git a/crates/evm/src/stack.cairo b/crates/evm/src/stack.cairo index 06e7896f7..109d01704 100644 --- a/crates/evm/src/stack.cairo +++ b/crates/evm/src/stack.cairo @@ -208,9 +208,12 @@ impl StackImpl of StackTrait { fn pop_n(ref self: Stack, mut n: usize) -> Result, EVMError> { ensure(!(n > self.len()), EVMError::StackUnderflow)?; let mut popped_items = ArrayTrait::::new(); - while n != 0 { + // while n != 0 { + // popped_items.append(self.pop().unwrap()); + // n -= 1; + // }; + for _ in 0..n { popped_items.append(self.pop().unwrap()); - n -= 1; }; Result::Ok(popped_items) } @@ -346,11 +349,14 @@ mod tests { fn test_should_fail_when_overflow() { // Given let mut stack = StackTrait::new(); - let mut i = 0; + // let mut i = 0; // When - while i != constants::STACK_MAX_DEPTH { - i += 1; + // while i != constants::STACK_MAX_DEPTH { + // i += 1; + // stack.push(1).unwrap(); + // }; + for _ in 0..constants::STACK_MAX_DEPTH { stack.push(1).unwrap(); }; diff --git a/crates/evm/src/state.cairo b/crates/evm/src/state.cairo index 1dd6fde36..354a4e33d 100644 --- a/crates/evm/src/state.cairo +++ b/crates/evm/src/state.cairo @@ -93,11 +93,16 @@ impl StateChangeLogImpl, +Copy> of StateChangeLogTrait { fn clone(ref self: StateChangeLog) -> StateChangeLog { let mut cloned_changes = Default::default(); let mut keyset_span = self.keyset.to_span(); - while let Option::Some(key) = keyset_span.pop_front() { + // while let Option::Some(key) = keyset_span.pop_front() { + // let value = self.changes.get(*key).deref(); + // cloned_changes.insert(*key, NullableTrait::new(value)); + // }; + for key in keyset_span { let value = self.changes.get(*key).deref(); 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 9809c2aec..40a370ce1 100644 --- a/crates/snforge_utils/src/lib.cairo +++ b/crates/snforge_utils/src/lib.cairo @@ -305,12 +305,18 @@ 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(); - let mut offset: usize = 0; - while offset != serialized_value.len() { + // let mut offset: usize = 0; + // while offset != serialized_value.len() { + // store_felt252( + // target.starknet, storage_address + offset.into(), *serialized_value.at(offset) + // ); + // offset += 1; + // } + for offset in 0..serialized_value.len() { store_felt252( target.starknet, storage_address + offset.into(), *serialized_value.at(offset) ); - offset += 1; - } + }; + } } From 9ab12a73fd7f52b8f74a60ae35904062ca931ce7 Mon Sep 17 00:00:00 2001 From: Fabricio Date: Fri, 27 Sep 2024 21:52:00 -0600 Subject: [PATCH 2/5] Second batch of replacements --- crates/contracts/src/storage.cairo | 2 +- crates/evm/src/precompiles.cairo | 9 +- crates/snforge_utils/src/lib.cairo | 62 +++++++-- crates/utils/src/crypto/blake2_compress.cairo | 9 +- .../utils/src/eth_transaction/eip2930.cairo | 5 +- crates/utils/src/felt_vec.cairo | 68 +++++++--- crates/utils/src/helpers.cairo | 67 +++++++--- crates/utils/src/rlp.cairo | 10 +- crates/utils/src/serialization.cairo | 34 +++-- crates/utils/src/traits.cairo | 10 +- crates/utils/src/traits/array.cairo | 9 +- crates/utils/src/traits/bytes.cairo | 121 +++++++++++++----- crates/utils/src/traits/eth_address.cairo | 22 +++- crates/utils/src/traits/integer.cairo | 2 +- 14 files changed, 309 insertions(+), 121 deletions(-) diff --git a/crates/contracts/src/storage.cairo b/crates/contracts/src/storage.cairo index b43a9f023..f92181530 100644 --- a/crates/contracts/src/storage.cairo +++ b/crates/contracts/src/storage.cairo @@ -18,7 +18,7 @@ pub struct StorageBytecode { const BYTES_PER_FELT: NonZero = 31; -/// An implamentation of the `Store` trait for our specific `StorageBytecode` type. +/// An implementation of the `Store` trait for our specific `StorageBytecode` type. /// The packing-unpacking is done inside the `read` and `write` methods, thus transparent to the /// user. /// The bytecode is stored sequentially, starting from storage address 0, for compatibility purposes diff --git a/crates/evm/src/precompiles.cairo b/crates/evm/src/precompiles.cairo index a467a7b60..aa31c87fc 100644 --- a/crates/evm/src/precompiles.cairo +++ b/crates/evm/src/precompiles.cairo @@ -40,10 +40,13 @@ pub const FIRST_ROLLUP_PRECOMPILE_ADDRESS: u256 = 0x100; pub fn eth_precompile_addresses() -> Set { let mut precompile_addresses: Array = array![]; //TODO(2.8) use range operator - let mut i = FIRST_ETHEREUM_PRECOMPILE_ADDRESS; - while i <= LAST_ETHEREUM_PRECOMPILE_ADDRESS { + // let mut i = FIRST_ETHEREUM_PRECOMPILE_ADDRESS; + // while i <= LAST_ETHEREUM_PRECOMPILE_ADDRESS { + // precompile_addresses.append(i.try_into().unwrap()); + // i = i + 1; + // }; + for i in FIRST_ETHEREUM_PRECOMPILE_ADDRESS..LAST_ETHEREUM_PRECOMPILE_ADDRESS + 0x01 { precompile_addresses.append(i.try_into().unwrap()); - i = i + 1; }; SetTrait::from_array(precompile_addresses) } diff --git a/crates/snforge_utils/src/lib.cairo b/crates/snforge_utils/src/lib.cairo index 40a370ce1..70cabd103 100644 --- a/crates/snforge_utils/src/lib.cairo +++ b/crates/snforge_utils/src/lib.cairo @@ -216,9 +216,37 @@ pub mod snforge_utils { fn build(self: @EventsFilter) -> ContractEvents { let events = (*self.events.events).span(); let mut filtered_events = array![]; - let mut i = 0; - - while i < events.len() { + // let mut i = 0; + + // while i < 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.data_filter.is_some() { + // if !event.data.includes((*self.data_filter).unwrap()) { + // include = false; + // } + // } + + // if include { + // filtered_events.append(event.clone()); + // } + + // i += 1; + // }; + for i in 0..events.len() { let (from, event) = events.at(i).clone(); let mut include = true; @@ -243,8 +271,6 @@ pub mod snforge_utils { if include { filtered_events.append(event.clone()); } - - i += 1; }; ContractEvents { events: filtered_events } @@ -268,15 +294,22 @@ pub mod snforge_utils { let mut expected_data = array![]; event.append_keys_and_data(ref expected_keys, ref expected_data); - let mut i = 0; + // let mut i = 0; let mut found = false; - while i < self.events.len() { + // while i < self.events.len() { + // let event = self.events.at(i); + // if event.keys == @expected_keys && event.data == @expected_data { + // found = true; + // break; + // } + // i += 1; + // }; + 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; } - i += 1; }; assert(found, 'Expected event was not emitted'); @@ -289,14 +322,21 @@ pub mod snforge_utils { let mut expected_data = array![]; event.append_keys_and_data(ref expected_keys, ref expected_data); - let mut i = 0; - while i < self.events.len() { + // let mut i = 0; + // while i < self.events.len() { + // let event = self.events.at(i); + // assert( + // event.keys != @expected_keys || event.data != @expected_data, + // 'Unexpected event was emitted' + // ); + // i += 1; + // }; + 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' ); - i += 1; } } } diff --git a/crates/utils/src/crypto/blake2_compress.cairo b/crates/utils/src/crypto/blake2_compress.cairo index 4ce2840e4..706d1d712 100644 --- a/crates/utils/src/crypto/blake2_compress.cairo +++ b/crates/utils/src/crypto/blake2_compress.cairo @@ -70,10 +70,13 @@ fn rotate_right(value: u64, n: u32) -> u64 { /// updated state vector pub fn compress(rounds: usize, h: Span, m: Span, t: Span, f: bool) -> Span { let mut v = VecTrait::::new(); - let mut i = 0; - while i != 16 { + // let mut i = 0; + // while i != 16 { + // v.push(0); + // i += 1; + // }; + for _ in 0..16_u8 { v.push(0); - i += 1; }; let IV = IV(); diff --git a/crates/utils/src/eth_transaction/eip2930.cairo b/crates/utils/src/eth_transaction/eip2930.cairo index 2766720a9..159779431 100644 --- a/crates/utils/src/eth_transaction/eip2930.cairo +++ b/crates/utils/src/eth_transaction/eip2930.cairo @@ -17,7 +17,10 @@ pub impl AccessListItemImpl of AccessListItemTrait { let AccessListItem { ethereum_address, mut storage_keys } = *self; let mut storage_keys_arr = array![]; - while let Option::Some(storage_key) = storage_keys.pop_front() { + // while let Option::Some(storage_key) = storage_keys.pop_front() { + // storage_keys_arr.append((ethereum_address, *storage_key)); + // }; + for storage_key in storage_keys { storage_keys_arr.append((ethereum_address, *storage_key)); }; diff --git a/crates/utils/src/felt_vec.cairo b/crates/utils/src/felt_vec.cairo index 82706e75d..25887487f 100644 --- a/crates/utils/src/felt_vec.cairo +++ b/crates/utils/src/felt_vec.cairo @@ -56,16 +56,24 @@ pub impl Felt252VecTraitImpl< /// * A Span representing bytes conversion of `self` in little endian format fn to_le_bytes(ref self: Felt252Vec) -> Span { let mut res: Array = array![]; - let mut i = 0; + // let mut i = 0; + + // while i != self.len() { + // if self[i] == Zero::zero() { + // res.append(Zero::zero()); + // } else { + // res.append_span(self[i].to_le_bytes()); + // } - while i != self.len() { + // i += 1; + // }; + + for i in 0..self.len() { if self[i] == Zero::zero() { res.append(Zero::zero()); } else { res.append_span(self[i].to_le_bytes()); } - - i += 1; }; res.span() @@ -214,10 +222,13 @@ pub impl Felt252VecTraitImpl< } let stop = idx + vec.len(); - let mut i = idx; - while i != stop { + // let mut i = idx; + // while i != stop { + // self.set(i, vec[i - idx]); + // i += 1; + // }; + for i in idx..stop { self.set(i, vec[i - idx]); - i += 1; }; Result::Ok(()) @@ -266,11 +277,14 @@ pub impl Felt252VecTraitImpl< fn duplicate(ref self: Felt252Vec) -> Felt252Vec { let mut new_vec = Default::default(); - let mut i: u32 = 0; + // let mut i: u32 = 0; - while i != self.len { + // while i != self.len { + // new_vec.push(self[i]); + // i += 1; + // }; + for i in 0..self.len { new_vec.push(self[i]); - i += 1; }; new_vec @@ -297,12 +311,15 @@ pub impl Felt252VecTraitImpl< fn clone_slice(ref self: Felt252Vec, idx: usize, len: usize) -> Felt252Vec { let mut new_vec = Default::default(); - let mut i: u32 = 0; + // let mut i: u32 = 0; - while i != len { - new_vec.push(self[idx + i]); + // while i != len { + // new_vec.push(self[idx + i]); - i += 1; + // i += 1; + // }; + for i in 0..len { + new_vec.push(self[idx + i]); }; new_vec @@ -328,14 +345,20 @@ pub impl Felt252VecTraitImpl< return false; }; - let mut i = 0; + // let mut i = 0; let mut result = true; - while i != lhs.len() { + // while i != lhs.len() { + // if lhs[i] != rhs[i] { + // result = false; + // break; + // } + // i += 1; + // }; + for i in 0..lhs.len() { if lhs[i] != rhs[i] { result = false; break; } - i += 1; }; result } @@ -368,11 +391,14 @@ pub impl Felt252VecTraitImpl< return Result::Err(Felt252VecTraitErrors::Overflow); } - let mut i = start_idx; - while i != start_idx + len { - self.set(i, value); + // let mut i = start_idx; + // while i != start_idx + len { + // self.set(i, value); - i += 1; + // i += 1; + // }; + for i in start_idx..start_idx + len { + self.set(i, value); }; Result::Ok(()) diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index d10b7ad6d..7bd09f8a0 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -87,10 +87,14 @@ pub fn split_word(mut value: u256, mut len: usize, ref dst: Array) { /// * `value` - The u128 value to split into bytes /// * `len` - The number of bytes to split the value into pub fn split_u128_le(ref dest: Array, mut value: u128, mut len: usize) { - while len != 0 { + // while len != 0 { + // dest.append((value % 256).try_into().unwrap()); + // value /= 256; + // len -= 1; + // }; + for _ in 0..len { dest.append((value % 256).try_into().unwrap()); value /= 256; - len -= 1; }; } @@ -137,11 +141,17 @@ pub fn load_word(mut len: usize, words: Span) -> u256 { let mut current: u256 = 0; let mut counter = 0; - while len != 0 { + // while len != 0 { + // let loaded: u8 = *words[counter]; + // let tmp = current * 256; + // current = tmp + loaded.into(); + // len -= 1; + // counter += 1; + // }; + for _ in 0..len { let loaded: u8 = *words[counter]; let tmp = current * 256; current = tmp + loaded.into(); - len -= 1; counter += 1; }; @@ -156,21 +166,29 @@ pub fn load_word(mut len: usize, words: Span) -> u256 { /// # Returns /// An `Array` representing the big-endian byte representation of the input value. pub fn u256_to_bytes_array(mut value: u256) -> Array { - let mut counter = 0; + // let mut counter = 0; let mut bytes_arr: Array = ArrayTrait::new(); // low part - while counter != 16 { + // while counter != 16 { + // bytes_arr.append((value.low & 0xFF).try_into().unwrap()); + // value.low /= 256; + // counter += 1; + // }; + for _ in 0..16_u8 { bytes_arr.append((value.low & 0xFF).try_into().unwrap()); value.low /= 256; - counter += 1; }; - let mut counter = 0; + // let mut counter = 0; // high part - while counter != 16 { + // while counter != 16 { + // bytes_arr.append((value.high & 0xFF).try_into().unwrap()); + // value.high /= 256; + // counter += 1; + // }; + for _ in 0..16_u8 { bytes_arr.append((value.high & 0xFF).try_into().unwrap()); value.high /= 256; - counter += 1; }; // Reverse the array as memory is arranged in big endian order. @@ -277,11 +295,14 @@ mod tests { // 16 bytes values let mut arr6 = ArrayTrait::new(); - arr6.append(0xff); - let mut counter: u128 = 0; - while counter < 15 { + // arr6.append(0xff); + // let mut counter: u128 = 0; + // while counter < 15 { + // arr6.append(0xff); + // counter += 1; + // }; + for _ in 0..16_u8 { arr6.append(0xff); - counter += 1; }; let res6 = helpers::load_word(16, arr6.span()); assert(340282366920938463463374607431768211455 == res6, 'res6: wrong load'); @@ -319,10 +340,13 @@ mod tests { assert(res4.len() == 16, 'res4: wrong length'); assert(*res4[0] == 0xfe, 'res4: wrong MSB value'); - let mut counter: usize = 1; - while counter < 16 { + // let mut counter: usize = 1; + // while counter < 16 { + // assert(*res4[counter] == 0xff, 'res4: wrong value at index'); + // counter += 1; + // }; + for counter in 1..16_u32 { assert(*res4[counter] == 0xff, 'res4: wrong value at index'); - counter += 1; }; } @@ -360,11 +384,14 @@ mod tests { let mut dst4: Array = ArrayTrait::new(); helpers::split_word(max_u128, 16, ref dst4); assert(dst4.len() == 16, 'dst4: wrong length'); - let mut counter: usize = 0; + // let mut counter: usize = 0; assert(*dst4[15] == 0xfe, 'dst4: wrong LSB value'); - while counter < 15 { + // while counter < 15 { + // assert_eq!(*dst4[counter], 0xff); + // counter += 1; + // }; + for counter in 0..dst4.len() - 1 { assert_eq!(*dst4[counter], 0xff); - counter += 1; }; } } diff --git a/crates/utils/src/rlp.cairo b/crates/utils/src/rlp.cairo index 513fd52a9..bec641d3d 100644 --- a/crates/utils/src/rlp.cairo +++ b/crates/utils/src/rlp.cairo @@ -100,7 +100,15 @@ pub impl RLPImpl of RLPTrait { /// * If encoding a long sequence (should not happen in current implementation) fn encode_sequence(mut input: Span) -> Span { let mut joined_encodings: Array = Default::default(); - while let Option::Some(item) = input.pop_front() { + // while let Option::Some(item) = input.pop_front() { + // match item { + // RLPItem::String(string) => { + // joined_encodings.append_span(Self::encode_string(*string)); + // }, + // RLPItem::List(_) => { panic_with_felt252('List encoding unimplemented') } + // } + // }; + for item in input { match item { RLPItem::String(string) => { joined_encodings.append_span(Self::encode_string(*string)); diff --git a/crates/utils/src/serialization.cairo b/crates/utils/src/serialization.cairo index 34939f43b..01596a163 100644 --- a/crates/utils/src/serialization.cairo +++ b/crates/utils/src/serialization.cairo @@ -95,22 +95,30 @@ pub fn serialize_transaction_signature( /// /// * `Option>` - The deserialized bytes if successful, or None if deserialization fails. pub fn deserialize_bytes(self: Span) -> Option> { - let mut i = 0; + // let mut i = 0; let mut bytes: Array = Default::default(); - while i != self.len() { - let v: Option = (*self[i]).try_into(); + // while i != self.len() { + // let v: Option = (*self[i]).try_into(); + + // match v { + // Option::Some(v) => { bytes.append(v); }, + // Option::None => { break; } + // } + + // i += 1; + // }; + for felt in self { + let v: Option = (*felt).try_into(); match v { Option::Some(v) => { bytes.append(v); }, Option::None => { break; } } - - i += 1; }; // it means there was an error in the above loop - if (i != self.len()) { + if (bytes.len() != self.len()) { Option::None } else { Option::Some(bytes) @@ -129,13 +137,17 @@ pub fn deserialize_bytes(self: Span) -> Option> { pub fn serialize_bytes(self: Span) -> Array { let mut array: Array = Default::default(); - let mut i = 0; + // let mut i = 0; - while i != self.len() { - let value: felt252 = (*self[i]).into(); - array.append(value); + // while i != self.len() { + // let value: felt252 = (*self[i]).into(); + // array.append(value); - i += 1; + // i += 1; + // }; + for item in self { + let value: felt252 = (*item).into(); + array.append(value); }; array diff --git a/crates/utils/src/traits.cairo b/crates/utils/src/traits.cairo index 3cf2fbf2f..a9f94dd75 100644 --- a/crates/utils/src/traits.cairo +++ b/crates/utils/src/traits.cairo @@ -106,11 +106,15 @@ 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; - let mut i: u32 = 0; - while i != len { + // let mut i: u32 = 0; + // while i != len { + // let byte: u256 = (*self.at(i)).into(); + // result += byte.shl(8 * (offset - i).into()); + // i += 1; + // }; + for i in 0..len { let byte: u256 = (*self.at(i)).into(); result += byte.shl(8 * (offset - i).into()); - i += 1; }; let address: felt252 = result.try_into_result()?; diff --git a/crates/utils/src/traits/array.cairo b/crates/utils/src/traits/array.cairo index 44a2bcb62..924714e61 100644 --- a/crates/utils/src/traits/array.cairo +++ b/crates/utils/src/traits/array.cairo @@ -39,10 +39,13 @@ pub impl ArrayExtension> of ArrayExtTrait { /// * `value` - The value to append. /// * `n` - The number of times to append the value. fn append_n<+Copy>(ref self: Array, value: T, mut n: usize) { - while n != 0 { - self.append(value); + // while n != 0 { + // self.append(value); - n -= 1; + // n -= 1; + // }; + for _ in 0..n { + self.append(value); }; } diff --git a/crates/utils/src/traits/bytes.cairo b/crates/utils/src/traits/bytes.cairo index c780098f2..18a125d5d 100644 --- a/crates/utils/src/traits/bytes.cairo +++ b/crates/utils/src/traits/bytes.cairo @@ -59,13 +59,23 @@ pub impl U8SpanExImpl of U8SpanExTrait { // Fill the last input word let mut last_input_word: u64 = 0; - let mut byte_counter: u8 = 0; + // let mut byte_counter: u8 = 0; // We enter a second loop for clarity. // O(2n) should be okay // We might want to regroup every computation into a single loop with appropriate `if` // branching For optimisation - while byte_counter.into() != last_input_num_bytes { + // while byte_counter.into() != 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; }, + // }; + // byte_counter += 1; + // }; + 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(); @@ -73,7 +83,6 @@ pub impl U8SpanExImpl of U8SpanExTrait { }, Option::None => { break; }, }; - byte_counter += 1; }; (u64_words, last_input_word, last_input_num_bytes) @@ -183,10 +192,13 @@ pub impl U8SpanExImpl of U8SpanExTrait { }; // append the data - let mut i = 0; - while i != self.len() { - arr.append(*self[i]); - i += 1; + // let mut i = 0; + // while i != self.len() { + // arr.append(*self[i]); + // i += 1; + // }; + for item in self { + arr.append(*item); }; arr.span() @@ -257,11 +269,15 @@ pub impl ToBytesImpl< let mask = Bounded::::MAX.into(); let mut bytes: Array = Default::default(); - let mut i: u8 = 0; - while i != bytes_used { + // let mut i: u8 = 0; + // while i != bytes_used { + // let val = Bitshift::::shr(self, eight * (bytes_used - i - 1).into()); + // bytes.append((val & mask).try_into().unwrap()); + // i += 1; + // }; + for i in 0..bytes_used { let val = Bitshift::::shr(self, eight * (bytes_used - i - 1).into()); bytes.append((val & mask).try_into().unwrap()); - i += 1; }; bytes.span() @@ -283,11 +299,15 @@ pub impl ToBytesImpl< let mut bytes: Array = Default::default(); - let mut i: u8 = 0; - while i != bytes_used { + // let mut i: u8 = 0; + // while i != bytes_used { + // let val = self.shr(eight * i.into()); + // bytes.append((val & mask).try_into().unwrap()); + // i += 1; + // }; + for i in 0..bytes_used { let val = self.shr(eight * i.into()); bytes.append((val & mask).try_into().unwrap()); - i += 1; }; bytes.span() @@ -450,16 +470,23 @@ pub impl ByteArrayExt of ByteArrayExTrait { let (nb_full_words, pending_word_len) = DivRem::div_rem( bytes.len(), 31_u32.try_into().unwrap() ); - let mut i = 0; - while i != nb_full_words { + // let mut i = 0; + // while i != nb_full_words { + // let mut word: felt252 = 0; + // let mut j = 0; + // while j != 31 { + // word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + // j += 1; + // }; + // arr.append_word(word.try_into().unwrap(), 31); + // i += 1; + // }; + for _ in 0..nb_full_words { let mut word: felt252 = 0; - let mut j = 0; - while j != 31 { + for _ in 0..31_u8 { word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); - j += 1; }; arr.append_word(word.try_into().unwrap(), 31); - i += 1; }; if pending_word_len == 0 { @@ -467,11 +494,15 @@ pub impl ByteArrayExt of ByteArrayExTrait { }; let mut pending_word: felt252 = 0; - let mut i = 0; + // let mut i = 0; + + // while i != pending_word_len { + // pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + // i += 1; + // }; - while i != pending_word_len { + for _ in 0..pending_word_len { pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); - i += 1; }; arr.append_word(pending_word.try_into().unwrap(), pending_word_len); arr @@ -493,11 +524,14 @@ pub impl ByteArrayExt of ByteArrayExTrait { /// * A Span containing the bytes from the ByteArray fn into_bytes(self: ByteArray) -> Span { let mut output: Array = Default::default(); - let len = self.len(); - let mut i = 0; - while i != len { + // let len = self.len(); + // let mut i = 0; + // while i != len { + // output.append(self[i]); + // i += 1; + // }; + for i in 0..self.len() { output.append(self[i]); - i += 1; }; output.span() } @@ -545,13 +579,24 @@ pub impl ByteArrayExt of ByteArrayExTrait { // Fill the last input word let mut last_input_word: u64 = 0; - let mut byte_counter: u8 = 0; + // let mut byte_counter: u8 = 0; // We enter a second loop for clarity. // O(2n) should be okay // We might want to regroup every computation into a single loop with appropriate `if` // branching For optimisation - while byte_counter.into() != last_input_num_bytes { + // while byte_counter.into() != 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; }, + // }; + // byte_counter += 1; + // }; + + 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(); @@ -559,7 +604,6 @@ pub impl ByteArrayExt of ByteArrayExTrait { }, Option::None => { break; }, }; - byte_counter += 1; }; (u64_words, last_input_word, last_input_num_bytes) @@ -612,11 +656,15 @@ mod tests { let res = ByteArrayExTrait::from_bytes(arr.span()); // Ensure that the result is complete and keeps the same order - let mut i = 0; - while i != arr.len() { + // let mut i = 0; + // while i != arr.len() { + // assert(*arr[i] == res[i], 'byte mismatch'); + // i += 1; + // }; + for i in 0..arr.len() { assert(*arr[i] == res[i], 'byte mismatch'); - i += 1; }; + } #[test] @@ -685,10 +733,13 @@ mod tests { let res = ByteArrayExTrait::from_bytes(arr.span()); // Ensure that the result is complete and keeps the same order - let mut i = 0; - while i != arr.len() { + // let mut i = 0; + // while i != arr.len() { + // assert(*arr[i] == res[i], 'byte mismatch'); + // i += 1; + // }; + for i in 0..arr.len() { assert(*arr[i] == res[i], 'byte mismatch'); - i += 1; }; } diff --git a/crates/utils/src/traits/eth_address.cairo b/crates/utils/src/traits/eth_address.cairo index c2f260716..1bac64224 100644 --- a/crates/utils/src/traits/eth_address.cairo +++ b/crates/utils/src/traits/eth_address.cairo @@ -13,11 +13,15 @@ pub impl EthAddressExImpl of EthAddressExTrait { let bytes_used: u256 = 20; let value: u256 = self.into(); let mut bytes: Array = Default::default(); - let mut i = 0; - while i != bytes_used { + // let mut i = 0; + // while i != bytes_used { + // let val = value.shr(8 * (bytes_used - i - 1)); + // bytes.append((val & 0xFF).try_into().unwrap()); + // i += 1; + // }; + for i in 0..bytes_used { let val = value.shr(8 * (bytes_used - i - 1)); bytes.append((val & 0xFF).try_into().unwrap()); - i += 1; }; bytes @@ -40,12 +44,16 @@ pub impl EthAddressExImpl of EthAddressExTrait { } let offset: u32 = len - 1; let mut result: u256 = 0; - let mut i: u32 = 0; - while i != len { + // let mut i: u32 = 0; + // while i != len { + // let byte: u256 = (*input.at(i)).into(); + // result += byte.shl((8 * (offset - i)).into()); + + // i += 1; + // }; + for i in 0..len { let byte: u256 = (*input.at(i)).into(); result += byte.shl((8 * (offset - i)).into()); - - i += 1; }; result.try_into() } diff --git a/crates/utils/src/traits/integer.cairo b/crates/utils/src/traits/integer.cairo index 9ee504f46..ebd00f99e 100644 --- a/crates/utils/src/traits/integer.cairo +++ b/crates/utils/src/traits/integer.cairo @@ -192,7 +192,7 @@ pub impl BitsUsedImpl< let bytes_used = self.bytes_used(); let last_byte = self.shr(eight * (bytes_used.into() - One::one())); - // safe unwrap since we know atmost 8 bits are used + // safe unwrap since we know at most 8 bits are used let bits_used: u8 = bits_used_internal::bits_used_in_byte(last_byte.try_into().unwrap()); bits_used.into() + 8 * (bytes_used - 1).into() From bd4cf1737d00970e3ec489990e5e693f2d22ba5b Mon Sep 17 00:00:00 2001 From: Fabricio Date: Fri, 27 Sep 2024 22:11:53 -0600 Subject: [PATCH 3/5] Clean commented code --- crates/contracts/src/storage.cairo | 12 ---- crates/evm/src/backend/starknet_backend.cairo | 14 ---- .../instructions/duplication_operations.cairo | 10 --- .../environmental_information.cairo | 31 -------- .../src/instructions/push_operations.cairo | 4 -- crates/evm/src/instructions/sha3.cairo | 11 --- crates/evm/src/memory.cairo | 11 --- crates/evm/src/precompiles.cairo | 6 -- crates/evm/src/precompiles/blake2f.cairo | 21 ------ .../precompiles/ec_operations/ec_mul.cairo | 11 --- crates/evm/src/stack.cairo | 9 --- crates/evm/src/state.cairo | 4 -- crates/snforge_utils/src/lib.cairo | 56 +-------------- crates/utils/src/crypto/blake2_compress.cairo | 5 -- .../utils/src/eth_transaction/eip2930.cairo | 3 - crates/utils/src/felt_vec.cairo | 43 ------------ crates/utils/src/helpers.cairo | 39 ----------- crates/utils/src/rlp.cairo | 8 --- crates/utils/src/serialization.cairo | 19 ----- crates/utils/src/traits.cairo | 6 -- crates/utils/src/traits/array.cairo | 5 -- crates/utils/src/traits/bytes.cairo | 70 ------------------- crates/utils/src/traits/eth_address.cairo | 13 ---- 23 files changed, 1 insertion(+), 410 deletions(-) diff --git a/crates/contracts/src/storage.cairo b/crates/contracts/src/storage.cairo index f92181530..e517cab66 100644 --- a/crates/contracts/src/storage.cairo +++ b/crates/contracts/src/storage.cairo @@ -38,13 +38,6 @@ impl StoreBytecode of Store { // afterwards. let base: felt252 = 0; let mut packed_bytecode = array![]; - // let mut i = 0; - // while i != (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); - // i += 1; - // }; 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(); @@ -136,11 +129,6 @@ mod tests { fn test_store_bytecode_multiple_chunks() { let mut state = account_contract_state(); let mut bytecode_array = array![]; - // let mut i = 0; - // while i != 100 { - // bytecode_array.append(i); - // i += 1; - // }; for i in 0..100_u8 { bytecode_array.append(i); }; diff --git a/crates/evm/src/backend/starknet_backend.cairo b/crates/evm/src/backend/starknet_backend.cairo index 946c4d66d..dba29c576 100644 --- a/crates/evm/src/backend/starknet_backend.cairo +++ b/crates/evm/src/backend/starknet_backend.cairo @@ -138,10 +138,6 @@ pub fn fetch_balance(self: @Address) -> u256 { /// `Ok(())` if the commit was successful, otherwise an `EVMError`. fn commit_accounts(ref state: State) -> Result<(), EVMError> { let mut account_keys = state.accounts.keyset.to_span(); - // while let Option::Some(evm_address) = account_keys.pop_front() { - // let account = state.accounts.changes.get(*evm_address).deref(); - // commit_account(@account, ref state); - // }; for evm_address in account_keys { let account = state.accounts.changes.get(*evm_address).deref(); commit_account(@account, ref state); @@ -235,16 +231,6 @@ fn emit_events(ref self: State) -> Result<(), EVMError> { /// commit_storage MUST be called after commit_accounts. fn commit_storage(ref self: State) -> Result<(), EVMError> { let mut storage_keys = self.accounts_storage.keyset.to_span(); - // while let Option::Some(state_key) = storage_keys.pop_front() { - // let (evm_address, key, value) = self.accounts_storage.changes.get(*state_key).deref(); - // let mut account = self.get_account(evm_address); - // // @dev: EIP-6780 - If selfdestruct on an account created, dont commit data - // if account.is_selfdestruct() && account.is_created() { - // continue; - // } - // IAccountDispatcher { contract_address: account.starknet_address() } - // .write_storage(key, value); - // }; for state_key in storage_keys { let (evm_address, key, value) = self.accounts_storage.changes.get(*state_key).deref(); let mut account = self.get_account(evm_address); diff --git a/crates/evm/src/instructions/duplication_operations.cairo b/crates/evm/src/instructions/duplication_operations.cairo index 5dee277ee..3085f75ac 100644 --- a/crates/evm/src/instructions/duplication_operations.cairo +++ b/crates/evm/src/instructions/duplication_operations.cairo @@ -129,11 +129,6 @@ mod tests { return; } - // while idx != to { - // assert(stack.peek_at(idx).unwrap() == 0x00, 'should be zero'); - // idx += 1; - // }; - for idx in from..to { assert(stack.peek_at(idx).unwrap() == 0x00, 'should be zero'); }; @@ -141,11 +136,6 @@ mod tests { // push `n` number of `0x0` to the stack fn push_zeros(ref stack: Stack, n: u8) { - // let mut i = 0; - // while i != n { - // stack.push(0x0).unwrap(); - // i += 1; - // } for _ in 0..n { stack.push(0x0).unwrap(); }; diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index 118435966..b9b78a161 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -90,11 +90,6 @@ pub impl EnvironmentInformationImpl of EnvironmentInformationTrait { // Fill the rest of the data to load with zeros // TODO: optimize once we have dw-based exponentiation - // let mut i = 32 - bytes_len; - // while i != 0 { - // data_to_load *= 256; - // i -= 1; - // }; for _ in 0..32 - bytes_len { data_to_load *= 256; }; @@ -655,21 +650,6 @@ 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. - // let mut i = 0; - // while i != (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); - - // i += 1; - // }; for i in 0..(size / 32) + 1 { vm .memory @@ -790,17 +770,6 @@ mod tests { let result: u256 = vm.memory.load_internal(dest_offset).into(); let mut results: Array = u256_to_bytes_array(result); - // let mut i = 0; - // while i != 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]); - // } - - // i += 1; - // }; for i in 0..size { // For out of bound bytes, 0s will be copied. if (i + offset >= bytecode.len()) { diff --git a/crates/evm/src/instructions/push_operations.cairo b/crates/evm/src/instructions/push_operations.cairo index 05c62e2c9..d6bd057cb 100644 --- a/crates/evm/src/instructions/push_operations.cairo +++ b/crates/evm/src/instructions/push_operations.cairo @@ -333,10 +333,6 @@ mod tests { fn get_n_0xFF(mut n: u8) -> Span { let mut array: Array = ArrayTrait::new(); array.append(0x00); - // while n != 0 { - // array.append(0xFF); - // n -= 1; - // }; for _ in 0..n { array.append(0xFF); }; diff --git a/crates/evm/src/instructions/sha3.cairo b/crates/evm/src/instructions/sha3.cairo index 5a8cd618c..0abed6655 100644 --- a/crates/evm/src/instructions/sha3.cairo +++ b/crates/evm/src/instructions/sha3.cairo @@ -102,17 +102,6 @@ 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 { - // while amount != 0 { - // 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; - // amount -= 1; - // }; for _ in 0..amount { let loaded = self.memory.load(offset); let ((high_h, low_h), (high_l, low_l)) = loaded.split_into_u64_le(); diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index b1828f22b..f6ed17400 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -369,12 +369,6 @@ pub(crate) impl InternalMemoryMethods of InternalMemoryTrait { fn load_aligned_words( ref self: Memory, mut chunk_index: usize, final_chunk: usize, ref elements: Array ) { - // while chunk_index != final_chunk { - // let value = self.items.get(chunk_index.into()); - // // Pushes 16 items to `elements` - // helpers::split_word_128(value.into(), ref elements); - // chunk_index += 1; - // } for i in chunk_index..final_chunk { let value = self.items.get(i.into()); // Pushes 16 items to `elements` @@ -815,11 +809,6 @@ mod tests { memory.load_n_internal(16, ref results, 0); assert(results.len() == 16, 'error'); - // let mut i = 0; - // while i != results.len() { - // assert(*results[i] == 0xFF, 'byte value loaded not correct'); - // i += 1; - // } for result in results { assert(result == 0xFF, 'byte value loaded not correct'); } diff --git a/crates/evm/src/precompiles.cairo b/crates/evm/src/precompiles.cairo index aa31c87fc..e5cb90500 100644 --- a/crates/evm/src/precompiles.cairo +++ b/crates/evm/src/precompiles.cairo @@ -39,12 +39,6 @@ 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![]; - //TODO(2.8) use range operator - // let mut i = FIRST_ETHEREUM_PRECOMPILE_ADDRESS; - // while i <= LAST_ETHEREUM_PRECOMPILE_ADDRESS { - // precompile_addresses.append(i.try_into().unwrap()); - // i = i + 1; - // }; for i in FIRST_ETHEREUM_PRECOMPILE_ADDRESS..LAST_ETHEREUM_PRECOMPILE_ADDRESS + 0x01 { precompile_addresses.append(i.try_into().unwrap()); }; diff --git a/crates/evm/src/precompiles/blake2f.cairo b/crates/evm/src/precompiles/blake2f.cairo index 81149ce30..76652ada1 100644 --- a/crates/evm/src/precompiles/blake2f.cairo +++ b/crates/evm/src/precompiles/blake2f.cairo @@ -39,28 +39,14 @@ pub impl Blake2f of Precompile { let mut h: Array = Default::default(); let mut m: Array = Default::default(); - // let mut i = 0; let mut pos = 4; - // while i != 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()); - // i += 1; - // pos += 8; - // }; 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; }; - // let mut i = 0; let mut pos = 68; - // while i != 16 { - // // safe unwrap, because we have made sure of the input length to be 213 - // m.append(input.slice(pos, 8).from_le_bytes().unwrap()); - // i += 1; - // pos += 8; - // }; for _ in 0..16_u8 { // safe unwrap, because we have made sure of the input length to be 213 m.append(input.slice(pos, 8).from_le_bytes().unwrap()); @@ -78,13 +64,6 @@ pub impl Blake2f of Precompile { let mut return_data: Array = Default::default(); - // let mut i = 0; - // while i != res.len() { - // let bytes = (*res[i]).to_le_bytes_padded(); - // return_data.append_span(bytes); - - // i += 1; - // }; for result in res { let bytes = (*result).to_le_bytes_padded(); return_data.append_span(bytes); diff --git a/crates/evm/src/precompiles/ec_operations/ec_mul.cairo b/crates/evm/src/precompiles/ec_operations/ec_mul.cairo index 10c2c2f80..814d7e0e4 100644 --- a/crates/evm/src/precompiles/ec_operations/ec_mul.cairo +++ b/crates/evm/src/precompiles/ec_operations/ec_mul.cairo @@ -117,17 +117,6 @@ fn get_bits_little(s: u256) -> Array { fn ec_mul_inner(pt: (u384, u384), mut bits: Array) -> Option<(u384, u384)> { let (mut temp_x, mut temp_y) = pt; let mut result: Option<(u384, u384)> = Option::None; - // while let Option::Some(bit) = bits.pop_front() { - // if bit != 0 { - // match result { - // Option::Some((xr, yr)) => result = ec_safe_add(temp_x, temp_y, xr, yr), - // Option::None => result = Option::Some((temp_x, temp_y)), - // }; - // }; - // let (_temp_x, _temp_y) = double_ec_point_unchecked(temp_x, temp_y); - // temp_x = _temp_x; - // temp_y = _temp_y; - // }; for bit in bits { if bit != 0 { match result { diff --git a/crates/evm/src/stack.cairo b/crates/evm/src/stack.cairo index 109d01704..506e87d9f 100644 --- a/crates/evm/src/stack.cairo +++ b/crates/evm/src/stack.cairo @@ -208,10 +208,6 @@ impl StackImpl of StackTrait { fn pop_n(ref self: Stack, mut n: usize) -> Result, EVMError> { ensure(!(n > self.len()), EVMError::StackUnderflow)?; let mut popped_items = ArrayTrait::::new(); - // while n != 0 { - // popped_items.append(self.pop().unwrap()); - // n -= 1; - // }; for _ in 0..n { popped_items.append(self.pop().unwrap()); }; @@ -349,13 +345,8 @@ mod tests { fn test_should_fail_when_overflow() { // Given let mut stack = StackTrait::new(); - // let mut i = 0; // When - // while i != constants::STACK_MAX_DEPTH { - // i += 1; - // stack.push(1).unwrap(); - // }; for _ in 0..constants::STACK_MAX_DEPTH { stack.push(1).unwrap(); }; diff --git a/crates/evm/src/state.cairo b/crates/evm/src/state.cairo index 354a4e33d..d094eb91c 100644 --- a/crates/evm/src/state.cairo +++ b/crates/evm/src/state.cairo @@ -93,10 +93,6 @@ impl StateChangeLogImpl, +Copy> of StateChangeLogTrait { fn clone(ref self: StateChangeLog) -> StateChangeLog { let mut cloned_changes = Default::default(); let mut keyset_span = self.keyset.to_span(); - // while let Option::Some(key) = keyset_span.pop_front() { - // let value = self.changes.get(*key).deref(); - // cloned_changes.insert(*key, NullableTrait::new(value)); - // }; for key in keyset_span { let value = self.changes.get(*key).deref(); cloned_changes.insert(*key, NullableTrait::new(value)); diff --git a/crates/snforge_utils/src/lib.cairo b/crates/snforge_utils/src/lib.cairo index 70cabd103..479db1182 100644 --- a/crates/snforge_utils/src/lib.cairo +++ b/crates/snforge_utils/src/lib.cairo @@ -216,36 +216,7 @@ pub mod snforge_utils { fn build(self: @EventsFilter) -> ContractEvents { let events = (*self.events.events).span(); let mut filtered_events = array![]; - // let mut i = 0; - - // while i < 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.data_filter.is_some() { - // if !event.data.includes((*self.data_filter).unwrap()) { - // include = false; - // } - // } - - // if include { - // filtered_events.append(event.clone()); - // } - - // i += 1; - // }; + for i in 0..events.len() { let (from, event) = events.at(i).clone(); let mut include = true; @@ -294,16 +265,7 @@ pub mod snforge_utils { let mut expected_data = array![]; event.append_keys_and_data(ref expected_keys, ref expected_data); - // let mut i = 0; let mut found = false; - // while i < self.events.len() { - // let event = self.events.at(i); - // if event.keys == @expected_keys && event.data == @expected_data { - // found = true; - // break; - // } - // i += 1; - // }; for i in 0..self.events.len() { let event = self.events.at(i); if event.keys == @expected_keys && event.data == @expected_data { @@ -322,15 +284,6 @@ pub mod snforge_utils { let mut expected_data = array![]; event.append_keys_and_data(ref expected_keys, ref expected_data); - // let mut i = 0; - // while i < self.events.len() { - // let event = self.events.at(i); - // assert( - // event.keys != @expected_keys || event.data != @expected_data, - // 'Unexpected event was emitted' - // ); - // i += 1; - // }; for i in 0..self.events.len() { let event = self.events.at(i); assert( @@ -345,13 +298,6 @@ 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(); - // let mut offset: usize = 0; - // while offset != serialized_value.len() { - // store_felt252( - // target.starknet, storage_address + offset.into(), *serialized_value.at(offset) - // ); - // offset += 1; - // } 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/crypto/blake2_compress.cairo b/crates/utils/src/crypto/blake2_compress.cairo index 706d1d712..e008e7271 100644 --- a/crates/utils/src/crypto/blake2_compress.cairo +++ b/crates/utils/src/crypto/blake2_compress.cairo @@ -70,11 +70,6 @@ fn rotate_right(value: u64, n: u32) -> u64 { /// updated state vector pub fn compress(rounds: usize, h: Span, m: Span, t: Span, f: bool) -> Span { let mut v = VecTrait::::new(); - // let mut i = 0; - // while i != 16 { - // v.push(0); - // i += 1; - // }; for _ in 0..16_u8 { v.push(0); }; diff --git a/crates/utils/src/eth_transaction/eip2930.cairo b/crates/utils/src/eth_transaction/eip2930.cairo index 159779431..d59ed6be3 100644 --- a/crates/utils/src/eth_transaction/eip2930.cairo +++ b/crates/utils/src/eth_transaction/eip2930.cairo @@ -17,9 +17,6 @@ pub impl AccessListItemImpl of AccessListItemTrait { let AccessListItem { ethereum_address, mut storage_keys } = *self; let mut storage_keys_arr = array![]; - // while let Option::Some(storage_key) = storage_keys.pop_front() { - // storage_keys_arr.append((ethereum_address, *storage_key)); - // }; for storage_key in storage_keys { storage_keys_arr.append((ethereum_address, *storage_key)); }; diff --git a/crates/utils/src/felt_vec.cairo b/crates/utils/src/felt_vec.cairo index 25887487f..d8046859c 100644 --- a/crates/utils/src/felt_vec.cairo +++ b/crates/utils/src/felt_vec.cairo @@ -56,17 +56,6 @@ pub impl Felt252VecTraitImpl< /// * A Span representing bytes conversion of `self` in little endian format fn to_le_bytes(ref self: Felt252Vec) -> Span { let mut res: Array = array![]; - // let mut i = 0; - - // while i != self.len() { - // if self[i] == Zero::zero() { - // res.append(Zero::zero()); - // } else { - // res.append_span(self[i].to_le_bytes()); - // } - - // i += 1; - // }; for i in 0..self.len() { if self[i] == Zero::zero() { @@ -222,11 +211,6 @@ pub impl Felt252VecTraitImpl< } let stop = idx + vec.len(); - // let mut i = idx; - // while i != stop { - // self.set(i, vec[i - idx]); - // i += 1; - // }; for i in idx..stop { self.set(i, vec[i - idx]); }; @@ -277,12 +261,6 @@ pub impl Felt252VecTraitImpl< fn duplicate(ref self: Felt252Vec) -> Felt252Vec { let mut new_vec = Default::default(); - // let mut i: u32 = 0; - - // while i != self.len { - // new_vec.push(self[i]); - // i += 1; - // }; for i in 0..self.len { new_vec.push(self[i]); }; @@ -311,13 +289,6 @@ pub impl Felt252VecTraitImpl< fn clone_slice(ref self: Felt252Vec, idx: usize, len: usize) -> Felt252Vec { let mut new_vec = Default::default(); - // let mut i: u32 = 0; - - // while i != len { - // new_vec.push(self[idx + i]); - - // i += 1; - // }; for i in 0..len { new_vec.push(self[idx + i]); }; @@ -345,15 +316,7 @@ pub impl Felt252VecTraitImpl< return false; }; - // let mut i = 0; let mut result = true; - // while i != lhs.len() { - // if lhs[i] != rhs[i] { - // result = false; - // break; - // } - // i += 1; - // }; for i in 0..lhs.len() { if lhs[i] != rhs[i] { result = false; @@ -391,12 +354,6 @@ pub impl Felt252VecTraitImpl< return Result::Err(Felt252VecTraitErrors::Overflow); } - // let mut i = start_idx; - // while i != start_idx + len { - // self.set(i, value); - - // i += 1; - // }; for i in start_idx..start_idx + len { self.set(i, value); }; diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 7bd09f8a0..fe4701ecc 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -87,11 +87,6 @@ pub fn split_word(mut value: u256, mut len: usize, ref dst: Array) { /// * `value` - The u128 value to split into bytes /// * `len` - The number of bytes to split the value into pub fn split_u128_le(ref dest: Array, mut value: u128, mut len: usize) { - // while len != 0 { - // dest.append((value % 256).try_into().unwrap()); - // value /= 256; - // len -= 1; - // }; for _ in 0..len { dest.append((value % 256).try_into().unwrap()); value /= 256; @@ -141,13 +136,6 @@ pub fn load_word(mut len: usize, words: Span) -> u256 { let mut current: u256 = 0; let mut counter = 0; - // while len != 0 { - // let loaded: u8 = *words[counter]; - // let tmp = current * 256; - // current = tmp + loaded.into(); - // len -= 1; - // counter += 1; - // }; for _ in 0..len { let loaded: u8 = *words[counter]; let tmp = current * 256; @@ -166,26 +154,14 @@ pub fn load_word(mut len: usize, words: Span) -> u256 { /// # Returns /// An `Array` representing the big-endian byte representation of the input value. pub fn u256_to_bytes_array(mut value: u256) -> Array { - // let mut counter = 0; let mut bytes_arr: Array = ArrayTrait::new(); // low part - // while counter != 16 { - // bytes_arr.append((value.low & 0xFF).try_into().unwrap()); - // value.low /= 256; - // counter += 1; - // }; for _ in 0..16_u8 { bytes_arr.append((value.low & 0xFF).try_into().unwrap()); value.low /= 256; }; - // let mut counter = 0; // high part - // while counter != 16 { - // bytes_arr.append((value.high & 0xFF).try_into().unwrap()); - // value.high /= 256; - // counter += 1; - // }; for _ in 0..16_u8 { bytes_arr.append((value.high & 0xFF).try_into().unwrap()); value.high /= 256; @@ -295,12 +271,6 @@ mod tests { // 16 bytes values let mut arr6 = ArrayTrait::new(); - // arr6.append(0xff); - // let mut counter: u128 = 0; - // while counter < 15 { - // arr6.append(0xff); - // counter += 1; - // }; for _ in 0..16_u8 { arr6.append(0xff); }; @@ -340,11 +310,6 @@ mod tests { assert(res4.len() == 16, 'res4: wrong length'); assert(*res4[0] == 0xfe, 'res4: wrong MSB value'); - // let mut counter: usize = 1; - // while counter < 16 { - // assert(*res4[counter] == 0xff, 'res4: wrong value at index'); - // counter += 1; - // }; for counter in 1..16_u32 { assert(*res4[counter] == 0xff, 'res4: wrong value at index'); }; @@ -386,10 +351,6 @@ mod tests { assert(dst4.len() == 16, 'dst4: wrong length'); // let mut counter: usize = 0; assert(*dst4[15] == 0xfe, 'dst4: wrong LSB value'); - // while counter < 15 { - // assert_eq!(*dst4[counter], 0xff); - // counter += 1; - // }; for counter in 0..dst4.len() - 1 { assert_eq!(*dst4[counter], 0xff); }; diff --git a/crates/utils/src/rlp.cairo b/crates/utils/src/rlp.cairo index bec641d3d..9c991671e 100644 --- a/crates/utils/src/rlp.cairo +++ b/crates/utils/src/rlp.cairo @@ -100,14 +100,6 @@ pub impl RLPImpl of RLPTrait { /// * If encoding a long sequence (should not happen in current implementation) fn encode_sequence(mut input: Span) -> Span { let mut joined_encodings: Array = Default::default(); - // while let Option::Some(item) = input.pop_front() { - // match item { - // RLPItem::String(string) => { - // joined_encodings.append_span(Self::encode_string(*string)); - // }, - // RLPItem::List(_) => { panic_with_felt252('List encoding unimplemented') } - // } - // }; for item in input { match item { RLPItem::String(string) => { diff --git a/crates/utils/src/serialization.cairo b/crates/utils/src/serialization.cairo index 01596a163..8d384c799 100644 --- a/crates/utils/src/serialization.cairo +++ b/crates/utils/src/serialization.cairo @@ -95,19 +95,8 @@ pub fn serialize_transaction_signature( /// /// * `Option>` - The deserialized bytes if successful, or None if deserialization fails. pub fn deserialize_bytes(self: Span) -> Option> { - // let mut i = 0; let mut bytes: Array = Default::default(); - // while i != self.len() { - // let v: Option = (*self[i]).try_into(); - - // match v { - // Option::Some(v) => { bytes.append(v); }, - // Option::None => { break; } - // } - - // i += 1; - // }; for felt in self { let v: Option = (*felt).try_into(); @@ -137,14 +126,6 @@ pub fn deserialize_bytes(self: Span) -> Option> { pub fn serialize_bytes(self: Span) -> Array { let mut array: Array = Default::default(); - // let mut i = 0; - - // while i != self.len() { - // let value: felt252 = (*self[i]).into(); - // array.append(value); - - // i += 1; - // }; for item in self { let value: felt252 = (*item).into(); array.append(value); diff --git a/crates/utils/src/traits.cairo b/crates/utils/src/traits.cairo index a9f94dd75..76fe4b17e 100644 --- a/crates/utils/src/traits.cairo +++ b/crates/utils/src/traits.cairo @@ -106,12 +106,6 @@ 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; - // let mut i: u32 = 0; - // while i != len { - // let byte: u256 = (*self.at(i)).into(); - // result += byte.shl(8 * (offset - i).into()); - // i += 1; - // }; for i in 0..len { let byte: u256 = (*self.at(i)).into(); result += byte.shl(8 * (offset - i).into()); diff --git a/crates/utils/src/traits/array.cairo b/crates/utils/src/traits/array.cairo index 924714e61..35b3669f7 100644 --- a/crates/utils/src/traits/array.cairo +++ b/crates/utils/src/traits/array.cairo @@ -39,11 +39,6 @@ pub impl ArrayExtension> of ArrayExtTrait { /// * `value` - The value to append. /// * `n` - The number of times to append the value. fn append_n<+Copy>(ref self: Array, value: T, mut n: usize) { - // while n != 0 { - // self.append(value); - - // n -= 1; - // }; for _ in 0..n { self.append(value); }; diff --git a/crates/utils/src/traits/bytes.cairo b/crates/utils/src/traits/bytes.cairo index 18a125d5d..ca63f19a0 100644 --- a/crates/utils/src/traits/bytes.cairo +++ b/crates/utils/src/traits/bytes.cairo @@ -65,16 +65,6 @@ 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 - // while byte_counter.into() != 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; }, - // }; - // byte_counter += 1; - // }; 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) => { @@ -192,11 +182,6 @@ pub impl U8SpanExImpl of U8SpanExTrait { }; // append the data - // let mut i = 0; - // while i != self.len() { - // arr.append(*self[i]); - // i += 1; - // }; for item in self { arr.append(*item); }; @@ -269,12 +254,6 @@ pub impl ToBytesImpl< let mask = Bounded::::MAX.into(); let mut bytes: Array = Default::default(); - // let mut i: u8 = 0; - // while i != bytes_used { - // let val = Bitshift::::shr(self, eight * (bytes_used - i - 1).into()); - // bytes.append((val & mask).try_into().unwrap()); - // i += 1; - // }; for i in 0..bytes_used { let val = Bitshift::::shr(self, eight * (bytes_used - i - 1).into()); bytes.append((val & mask).try_into().unwrap()); @@ -299,12 +278,6 @@ pub impl ToBytesImpl< let mut bytes: Array = Default::default(); - // let mut i: u8 = 0; - // while i != bytes_used { - // let val = self.shr(eight * i.into()); - // bytes.append((val & mask).try_into().unwrap()); - // i += 1; - // }; for i in 0..bytes_used { let val = self.shr(eight * i.into()); bytes.append((val & mask).try_into().unwrap()); @@ -470,17 +443,6 @@ pub impl ByteArrayExt of ByteArrayExTrait { let (nb_full_words, pending_word_len) = DivRem::div_rem( bytes.len(), 31_u32.try_into().unwrap() ); - // let mut i = 0; - // while i != nb_full_words { - // let mut word: felt252 = 0; - // let mut j = 0; - // while j != 31 { - // word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); - // j += 1; - // }; - // arr.append_word(word.try_into().unwrap(), 31); - // i += 1; - // }; for _ in 0..nb_full_words { let mut word: felt252 = 0; for _ in 0..31_u8 { @@ -494,12 +456,6 @@ pub impl ByteArrayExt of ByteArrayExTrait { }; let mut pending_word: felt252 = 0; - // let mut i = 0; - - // while i != pending_word_len { - // pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); - // i += 1; - // }; for _ in 0..pending_word_len { pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); @@ -524,12 +480,6 @@ pub impl ByteArrayExt of ByteArrayExTrait { /// * A Span containing the bytes from the ByteArray fn into_bytes(self: ByteArray) -> Span { let mut output: Array = Default::default(); - // let len = self.len(); - // let mut i = 0; - // while i != len { - // output.append(self[i]); - // i += 1; - // }; for i in 0..self.len() { output.append(self[i]); }; @@ -585,16 +535,6 @@ pub impl ByteArrayExt of ByteArrayExTrait { // O(2n) should be okay // We might want to regroup every computation into a single loop with appropriate `if` // branching For optimisation - // while byte_counter.into() != 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; }, - // }; - // byte_counter += 1; - // }; for byte_counter in 0..last_input_num_bytes { last_input_word += match self.at(full_u64_word_count * 8 + byte_counter.into()) { @@ -656,11 +596,6 @@ mod tests { let res = ByteArrayExTrait::from_bytes(arr.span()); // Ensure that the result is complete and keeps the same order - // let mut i = 0; - // while i != arr.len() { - // assert(*arr[i] == res[i], 'byte mismatch'); - // i += 1; - // }; for i in 0..arr.len() { assert(*arr[i] == res[i], 'byte mismatch'); }; @@ -733,11 +668,6 @@ mod tests { let res = ByteArrayExTrait::from_bytes(arr.span()); // Ensure that the result is complete and keeps the same order - // let mut i = 0; - // while i != arr.len() { - // assert(*arr[i] == res[i], 'byte mismatch'); - // i += 1; - // }; for i in 0..arr.len() { assert(*arr[i] == res[i], 'byte mismatch'); }; diff --git a/crates/utils/src/traits/eth_address.cairo b/crates/utils/src/traits/eth_address.cairo index 1bac64224..5a00dc1e2 100644 --- a/crates/utils/src/traits/eth_address.cairo +++ b/crates/utils/src/traits/eth_address.cairo @@ -13,12 +13,6 @@ pub impl EthAddressExImpl of EthAddressExTrait { let bytes_used: u256 = 20; let value: u256 = self.into(); let mut bytes: Array = Default::default(); - // let mut i = 0; - // while i != bytes_used { - // let val = value.shr(8 * (bytes_used - i - 1)); - // bytes.append((val & 0xFF).try_into().unwrap()); - // i += 1; - // }; for i in 0..bytes_used { let val = value.shr(8 * (bytes_used - i - 1)); bytes.append((val & 0xFF).try_into().unwrap()); @@ -44,13 +38,6 @@ pub impl EthAddressExImpl of EthAddressExTrait { } let offset: u32 = len - 1; let mut result: u256 = 0; - // let mut i: u32 = 0; - // while i != len { - // let byte: u256 = (*input.at(i)).into(); - // result += byte.shl((8 * (offset - i)).into()); - - // i += 1; - // }; for i in 0..len { let byte: u256 = (*input.at(i)).into(); result += byte.shl((8 * (offset - i)).into()); From 567586529b9b02932026613ec2e06d32ce2b1a65 Mon Sep 17 00:00:00 2001 From: Fabricio Date: Fri, 27 Sep 2024 22:13:25 -0600 Subject: [PATCH 4/5] scarb fmt --- crates/contracts/src/storage.cairo | 12 ++- .../environmental_information.cairo | 45 +++++---- crates/evm/src/instructions/sha3.cairo | 21 ++-- crates/evm/src/memory.cairo | 11 ++- crates/evm/src/precompiles.cairo | 8 +- crates/evm/src/precompiles/blake2f.cairo | 6 +- crates/evm/src/state.cairo | 1 - crates/snforge_utils/src/lib.cairo | 97 +++++++++++-------- crates/utils/src/felt_vec.cairo | 16 +-- crates/utils/src/helpers.cairo | 33 ++++--- crates/utils/src/traits.cairo | 9 +- crates/utils/src/traits/bytes.cairo | 75 +++++++------- crates/utils/src/traits/eth_address.cairo | 18 ++-- 13 files changed, 194 insertions(+), 158 deletions(-) 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() } } From ed41812c3e43033ec73f4f3da55c3937eab6f4fe Mon Sep 17 00:00:00 2001 From: Fabricio Date: Sat, 28 Sep 2024 08:55:45 -0600 Subject: [PATCH 5/5] Apply code review recommendations --- crates/evm/src/instructions/duplication_operations.cairo | 2 -- crates/utils/src/helpers.cairo | 1 - crates/utils/src/serialization.cairo | 4 ++-- crates/utils/src/traits/bytes.cairo | 2 -- 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/evm/src/instructions/duplication_operations.cairo b/crates/evm/src/instructions/duplication_operations.cairo index 3085f75ac..5752279ba 100644 --- a/crates/evm/src/instructions/duplication_operations.cairo +++ b/crates/evm/src/instructions/duplication_operations.cairo @@ -123,8 +123,6 @@ mod tests { // ensures all values start from index `from` upto index `to` of stack are `0x0` fn ensures_zeros(ref stack: Stack, from: u32, to: u32) { - // let mut idx: u32 = from; - if to > from { return; } diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 108d71138..d50eb3418 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -352,7 +352,6 @@ mod tests { let mut dst4: Array = ArrayTrait::new(); helpers::split_word(max_u128, 16, ref dst4); 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 { assert_eq!(*dst4[counter], 0xff); diff --git a/crates/utils/src/serialization.cairo b/crates/utils/src/serialization.cairo index 8d384c799..371f9dd1c 100644 --- a/crates/utils/src/serialization.cairo +++ b/crates/utils/src/serialization.cairo @@ -97,8 +97,8 @@ pub fn serialize_transaction_signature( pub fn deserialize_bytes(self: Span) -> Option> { let mut bytes: Array = Default::default(); - for felt in self { - let v: Option = (*felt).try_into(); + for item in self { + let v: Option = (*item).try_into(); match v { Option::Some(v) => { bytes.append(v); }, diff --git a/crates/utils/src/traits/bytes.cairo b/crates/utils/src/traits/bytes.cairo index 9c9b3bd4a..6cec31aee 100644 --- a/crates/utils/src/traits/bytes.cairo +++ b/crates/utils/src/traits/bytes.cairo @@ -59,7 +59,6 @@ pub impl U8SpanExImpl of U8SpanExTrait { // Fill the last input word let mut last_input_word: u64 = 0; - // let mut byte_counter: u8 = 0; // We enter a second loop for clarity. // O(2n) should be okay @@ -536,7 +535,6 @@ pub impl ByteArrayExt of ByteArrayExTrait { // Fill the last input word let mut last_input_word: u64 = 0; - // let mut byte_counter: u8 = 0; // We enter a second loop for clarity. // O(2n) should be okay