diff --git a/target_chains/starknet/contracts/src/byte_array.cairo b/target_chains/starknet/contracts/src/byte_array.cairo index 38e28322a5..241145fd75 100644 --- a/target_chains/starknet/contracts/src/byte_array.cairo +++ b/target_chains/starknet/contracts/src/byte_array.cairo @@ -10,7 +10,7 @@ pub struct ByteArray { num_last_bytes: u8, // Bytes in big endian. Each item except the last one stores 31 bytes. // If `num_last_bytes < 31`, unused most significant bytes of the last item will be unused. - data: Array, + data: Array, } impl DebugByteArray of Debug { @@ -19,10 +19,7 @@ impl DebugByteArray of Debug { let mut data = self.data.clone(); loop { match data.pop_front() { - Option::Some(v) => { - let v: u256 = v.into(); - write!(f, "{:?}, ", v).unwrap(); - }, + Option::Some(v) => { write!(f, "{:?}, ", v).unwrap(); }, Option::None => { break; }, } }; @@ -33,7 +30,7 @@ impl DebugByteArray of Debug { #[generate_trait] pub impl ByteArrayImpl of ByteArrayTrait { /// Creates a byte array with the data. - fn new(data: Array, num_last_bytes: u8) -> ByteArray { + fn new(data: Array, num_last_bytes: u8) -> ByteArray { if data.len() == 0 { assert!(num_last_bytes == 0); } else { @@ -50,7 +47,7 @@ pub impl ByteArrayImpl of ByteArrayTrait { /// Removes 31 or less bytes from the start of the array. /// Returns the value and the number of bytes. - fn pop_front(ref self: ByteArray) -> Option<(bytes31, u8)> { + fn pop_front(ref self: ByteArray) -> Option<(felt252, u8)> { let item = self.data.pop_front()?; if self.data.is_empty() { let num_bytes = self.num_last_bytes; @@ -84,18 +81,18 @@ mod tests { #[test] fn byte_array_3_zeros() { - let mut array = ByteArrayImpl::new(array_try_into(array![0]), 3); + let mut array = ByteArrayImpl::new(array![0], 3); assert!(array.len() == 3); - assert!(array.pop_front() == Option::Some((0.try_into().unwrap(), 3))); + assert!(array.pop_front() == Option::Some((0, 3))); assert!(array.len() == 0); assert!(array.pop_front() == Option::None); } #[test] fn byte_array_3_bytes() { - let mut array = ByteArrayImpl::new(array_try_into(array![0x010203]), 3); + let mut array = ByteArrayImpl::new(array![0x010203], 3); assert!(array.len() == 3); - assert!(array.pop_front() == Option::Some((0x010203.try_into().unwrap(), 3))); + assert!(array.pop_front() == Option::Some((0x010203, 3))); assert!(array.len() == 0); assert!(array.pop_front() == Option::None); } @@ -103,9 +100,9 @@ mod tests { #[test] fn byte_array_single_full() { let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f; - let mut array = ByteArrayImpl::new(array_try_into(array![value_31_bytes]), 31); + let mut array = ByteArrayImpl::new(array![value_31_bytes], 31); assert!(array.len() == 31); - assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31))); + assert!(array.pop_front() == Option::Some((value_31_bytes, 31))); assert!(array.len() == 0); assert!(array.pop_front() == Option::None); } @@ -114,13 +111,11 @@ mod tests { fn byte_array_two_full() { let value_31_bytes = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f; let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f; - let mut array = ByteArrayImpl::new( - array_try_into(array![value_31_bytes, value2_31_bytes]), 31 - ); + let mut array = ByteArrayImpl::new(array![value_31_bytes, value2_31_bytes], 31); assert!(array.len() == 62); - assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31))); + assert!(array.pop_front() == Option::Some((value_31_bytes, 31))); assert!(array.len() == 31); - assert!(array.pop_front() == Option::Some((value2_31_bytes.try_into().unwrap(), 31))); + assert!(array.pop_front() == Option::Some((value2_31_bytes, 31))); assert!(array.len() == 0); assert!(array.pop_front() == Option::None); } @@ -131,14 +126,14 @@ mod tests { let value2_31_bytes = 0x2122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f; let value3_5_bytes = 0x4142434445; let mut array = ByteArrayImpl::new( - array_try_into(array![value_31_bytes, value2_31_bytes, value3_5_bytes]), 5 + array![value_31_bytes, value2_31_bytes, value3_5_bytes], 5 ); assert!(array.len() == 67); - assert!(array.pop_front() == Option::Some((value_31_bytes.try_into().unwrap(), 31))); + assert!(array.pop_front() == Option::Some((value_31_bytes, 31))); assert!(array.len() == 36); - assert!(array.pop_front() == Option::Some((value2_31_bytes.try_into().unwrap(), 31))); + assert!(array.pop_front() == Option::Some((value2_31_bytes, 31))); assert!(array.len() == 5); - assert!(array.pop_front() == Option::Some((value3_5_bytes.try_into().unwrap(), 5))); + assert!(array.pop_front() == Option::Some((value3_5_bytes, 5))); assert!(array.pop_front() == Option::None); } @@ -151,18 +146,18 @@ mod tests { #[test] #[should_panic] fn byte_array_last_too_large() { - ByteArrayImpl::new(array_try_into(array![1, 2, 3]), 35); + ByteArrayImpl::new(array![1, 2, 3], 35); } #[test] #[should_panic] fn byte_array_last_zero_invalid() { - ByteArrayImpl::new(array_try_into(array![1, 2, 0]), 0); + ByteArrayImpl::new(array![1, 2, 0], 0); } #[test] #[should_panic] fn byte_array_last_too_many_bytes() { - ByteArrayImpl::new(array_try_into(array![1, 2, 0x010203]), 2); + ByteArrayImpl::new(array![1, 2, 0x010203], 2); } } diff --git a/target_chains/starknet/contracts/src/reader.cairo b/target_chains/starknet/contracts/src/reader.cairo index f0501a0f5f..bb41fe9f4a 100644 --- a/target_chains/starknet/contracts/src/reader.cairo +++ b/target_chains/starknet/contracts/src/reader.cairo @@ -101,7 +101,7 @@ pub impl ReaderImpl of ReaderTrait { /// Reads the specified number of bytes as a new byte array. fn read_byte_array(ref self: Reader, num_bytes: usize) -> ByteArray { - let mut array: Array = array![]; + let mut array: Array = array![]; let mut num_last_bytes = 0; let mut num_remaining_bytes = num_bytes; loop { @@ -173,26 +173,26 @@ impl ReaderPrivateImpl of ReaderPrivateTrait { // Moved out from `read_bytes` because we cannot use `return` or `?` within a loop. fn read_bytes_iteration( - ref self: Reader, num_bytes: usize, ref array: Array + ref self: Reader, num_bytes: usize, ref array: Array ) -> (usize, bool) { if num_bytes >= 31 { let high = self.read_num_bytes(15); let low = self.read_num_bytes(16); let value: felt252 = u256 { high, low }.try_into().expect(UNEXPECTED_OVERFLOW); - array.append(value.try_into().expect(UNEXPECTED_OVERFLOW)); + array.append(value); (31, false) } else if num_bytes > 16 { // num_bytes < 31 let high = self.read_num_bytes((num_bytes - 16).try_into().expect(UNEXPECTED_OVERFLOW)); let low = self.read_num_bytes(16); let value: felt252 = u256 { high, low }.try_into().expect(UNEXPECTED_OVERFLOW); - array.append(value.try_into().expect(UNEXPECTED_OVERFLOW)); + array.append(value); (num_bytes, true) } else { // bytes < 16 let low = self.read_num_bytes(num_bytes.try_into().expect(UNEXPECTED_OVERFLOW)); let value: felt252 = low.try_into().expect(UNEXPECTED_OVERFLOW); - array.append(value.try_into().expect(UNEXPECTED_OVERFLOW)); + array.append(value); (num_bytes, true) } } diff --git a/target_chains/starknet/contracts/tests/data.cairo b/target_chains/starknet/contracts/tests/data.cairo index 306bc5e2b8..065e67517a 100644 --- a/target_chains/starknet/contracts/tests/data.cairo +++ b/target_chains/starknet/contracts/tests/data.cairo @@ -48,7 +48,7 @@ pub fn good_update1() -> ByteArray { 226866843267230707879834616967256711063296411939069440476882347301771901839, 95752383404870925303422787, ]; - ByteArrayImpl::new(array_try_into(bytes), 11) + ByteArrayImpl::new(bytes, 11) } // A wormhole VAA from a random update pulled from Hermes. @@ -86,7 +86,7 @@ pub fn good_vm1() -> ByteArray { 52685537088250779930155363779405986390839624071318818148325576008719597568, 14615204155786886573933667335033405822686404253588533, ]; - ByteArrayImpl::new(array_try_into(bytes), 22) + ByteArrayImpl::new(bytes, 22) } // A first update for a certain timestamp pulled from Hermes. @@ -134,7 +134,7 @@ pub fn unique_update1() -> ByteArray { 28583007876111384456149499846085318299326698960792831530075402396150538907, 126290914008245563820443505, ]; - ByteArrayImpl::new(array_try_into(bytes), 11) + ByteArrayImpl::new(bytes, 11) } // An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37 @@ -159,7 +159,7 @@ pub fn mainnet_guardian_set_upgrade1() -> ByteArray { 55852237138651071644815135002358067220635692701051811455610533875912981641, 190413173566657072516608762222993749133, ]; - ByteArrayImpl::new(array_try_into(bytes), 16) + ByteArrayImpl::new(bytes, 16) } // An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37 @@ -210,7 +210,7 @@ pub fn mainnet_guardian_set_upgrade2() -> ByteArray { 75218391584551901010047495874303520775865073092730040058902770251005073864, 13453, ]; - ByteArrayImpl::new(array_try_into(bytes), 2) + ByteArrayImpl::new(bytes, 2) } // An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37 @@ -261,7 +261,7 @@ pub fn mainnet_guardian_set_upgrade3() -> ByteArray { 75218391584551901010047495874303520775865073092730040058902770251005073864, 13453, ]; - ByteArrayImpl::new(array_try_into(bytes), 2) + ByteArrayImpl::new(bytes, 2) } // An actual mainnet wormhole governance VAA from https://github.com/pyth-network/pyth-crosschain/blob/main/contract_manager/src/contracts/wormhole.ts#L32-L37 @@ -312,7 +312,7 @@ pub fn mainnet_guardian_set_upgrade4() -> ByteArray { 75218391584551901010047495874303520775865073092730040058902770251005073864, 13453, ]; - ByteArrayImpl::new(array_try_into(bytes), 2) + ByteArrayImpl::new(bytes, 2) } pub const TEST_GUARDIAN_ADDRESS1: felt252 = 0x686b9ea8e3237110eaaba1f1b7467559a3273819; @@ -328,7 +328,7 @@ pub fn empty_set_upgrade() -> ByteArray { 1131377253, 210141960835432704, ]; - ByteArrayImpl::new(array_try_into(bytes), 8) + ByteArrayImpl::new(bytes, 8) } // A wormhole guardian set upgrade instruction with emitter not expected by the test. @@ -341,7 +341,7 @@ pub fn wrong_emitter_upgrade() -> ByteArray { 1131377253, 307122819832911374634462256129025725147663742791077927773782095897, ]; - ByteArrayImpl::new(array_try_into(bytes), 28) + ByteArrayImpl::new(bytes, 28) } // A wormhole guardian set upgrade instruction with set index = 3 not expected by the test. @@ -354,7 +354,7 @@ pub fn wrong_index_upgrade() -> ByteArray { 1131377253, 210624583337115497886730203944140689990237281548333499058561169433, ]; - ByteArrayImpl::new(array_try_into(bytes), 28) + ByteArrayImpl::new(bytes, 28) } // A wormhole governance guardian set upgrade instruction signed by test guardian #1 containing test guardian #2 as the new guardian set. @@ -367,7 +367,7 @@ pub fn upgrade_to_test2() -> ByteArray { 1131377253, 210624583337114749311237613435643962969294824395451022190048752713, ]; - ByteArrayImpl::new(array_try_into(bytes), 28) + ByteArrayImpl::new(bytes, 28) } // A Pyth governance instruction to set fee signed by the test guardian #1. @@ -379,7 +379,7 @@ pub fn pyth_set_fee() -> ByteArray { 49565958604199796163020368, 8072278384728444780182694421117884443886221966887092226, ]; - ByteArrayImpl::new(array_try_into(bytes), 23) + ByteArrayImpl::new(bytes, 23) } // A Pyth governance instruction to set data sources signed by the test guardian #1. @@ -393,7 +393,7 @@ pub fn pyth_set_data_sources() -> ByteArray { 223938022913800988696085410923418445187967252047785407181969631814277398528, 301, ]; - ByteArrayImpl::new(array_try_into(bytes), 14) + ByteArrayImpl::new(bytes, 14) } // A Pyth governance instruction to set wormhole address signed by the test guardian #1. @@ -406,7 +406,7 @@ pub fn pyth_set_wormhole() -> ByteArray { 148907253456057279176930315687485033494639386197985334929728922792833758561, 3789456330195130818, ]; - ByteArrayImpl::new(array_try_into(bytes), 8) + ByteArrayImpl::new(bytes, 8) } // A Pyth governance instruction to request governance data source transfer signed by the test guardian #1. @@ -418,7 +418,7 @@ pub fn pyth_request_transfer() -> ByteArray { 51983810243429054512432720, 101886477340929157123538945, ]; - ByteArrayImpl::new(array_try_into(bytes), 11) + ByteArrayImpl::new(bytes, 11) } // A Pyth governance instruction to authorize governance data source transfer signed by the test guardian #1. @@ -434,7 +434,7 @@ pub fn pyth_auth_transfer() -> ByteArray { 721420288, 20782639266000304984163621011457, ]; - ByteArrayImpl::new(array_try_into(bytes), 18) + ByteArrayImpl::new(bytes, 18) } // A Pyth governance instruction to set fee with alternative emitter signed by the test guardian #1. @@ -446,7 +446,7 @@ pub fn pyth_set_fee_alt_emitter() -> ByteArray { 51983810243429054512498256, 8072278384728444780182694421117884443886221966887092226, ]; - ByteArrayImpl::new(array_try_into(bytes), 23) + ByteArrayImpl::new(bytes, 23) } // A Pyth governance instruction to upgrade the contract signed by the test guardian #1. @@ -459,7 +459,7 @@ pub fn pyth_upgrade_fake1() -> ByteArray { 148907253453589022320407306335457538262203456299261498528172020674942501293, 9624434269354675143, ]; - ByteArrayImpl::new(array_try_into(bytes), 8) + ByteArrayImpl::new(bytes, 8) } // A Pyth governance instruction to upgrade the contract signed by the test guardian #1. @@ -472,7 +472,7 @@ pub fn pyth_upgrade_not_pyth() -> ByteArray { 148907253453589022305803196061110108233921773465491227564264876752079119569, 6736708290019375278, ]; - ByteArrayImpl::new(array_try_into(bytes), 8) + ByteArrayImpl::new(bytes, 8) } // A Pyth governance instruction to upgrade the contract signed by the test guardian #1. @@ -485,7 +485,7 @@ pub fn pyth_upgrade_wrong_magic() -> ByteArray { 148907253453589022340563264373887392414227070562033595690783947835630084766, 5698494087895763928, ]; - ByteArrayImpl::new(array_try_into(bytes), 8) + ByteArrayImpl::new(bytes, 8) } // A Pyth governance instruction to upgrade the contract signed by the test guardian #1. @@ -498,7 +498,7 @@ pub fn pyth_upgrade_invalid_hash() -> ByteArray { 148907253453589022218037939353255655322518022029545083499057126097303896064, 505, ]; - ByteArrayImpl::new(array_try_into(bytes), 8) + ByteArrayImpl::new(bytes, 8) } // An update pulled from Hermes and re-signed by the test guardian #1. @@ -520,7 +520,7 @@ pub fn test_price_update1() -> ByteArray { 87135893730137265929093180553063146337041045646221968026289709394440932141, 245333243912241114598596888050489286502591033459250287888834, ]; - ByteArrayImpl::new(array_try_into(bytes), 25) + ByteArrayImpl::new(bytes, 25) } // An update pulled from Hermes and re-signed by the test guardian #1. @@ -542,7 +542,7 @@ pub fn test_price_update2() -> ByteArray { 370855179649505412564259994413632062925303311800103998016489412083011059699, 1182295126766215829784496273374889928477877265080355104888778, ]; - ByteArrayImpl::new(array_try_into(bytes), 25) + ByteArrayImpl::new(bytes, 25) } // An update pulled from Hermes and re-signed by the test guardian #1 with another emitter address. @@ -564,7 +564,7 @@ pub fn test_update2_alt_emitter() -> ByteArray { 370855179649505412564259994413632062925303311800103998016489412083011059699, 1182295126766215829784496273374889928477877265080355104888778, ]; - ByteArrayImpl::new(array_try_into(bytes), 25) + ByteArrayImpl::new(bytes, 25) } // An update pulled from Hermes and re-signed by the test guardian #2. @@ -586,5 +586,5 @@ pub fn test_update2_set2() -> ByteArray { 370855179649505412564259994413632062925303311800103998016489412083011059699, 1182295126766215829784496273374889928477877265080355104888778, ]; - ByteArrayImpl::new(array_try_into(bytes), 25) + ByteArrayImpl::new(bytes, 25) } diff --git a/target_chains/starknet/contracts/tests/wormhole.cairo b/target_chains/starknet/contracts/tests/wormhole.cairo index 34968ebbb7..301f5e6015 100644 --- a/target_chains/starknet/contracts/tests/wormhole.cairo +++ b/target_chains/starknet/contracts/tests/wormhole.cairo @@ -8,7 +8,7 @@ use pyth::wormhole::{ }; use pyth::reader::ReaderImpl; use pyth::byte_array::{ByteArray, ByteArrayImpl}; -use pyth::util::{UnwrapWithFelt252, array_try_into}; +use pyth::util::{UnwrapWithFelt252, array_try_into, one_shift_left_bytes_u256}; use core::starknet::{ContractAddress, EthAddress}; use core::panic_with_felt252; use super::data; @@ -352,16 +352,18 @@ pub fn corrupted_vm( ByteArrayImpl::new(new_data, num_last_bytes) } -// Returns a `bytes31` value with 2 bytes changed. We need to change at least 2 bytes +// Returns an item of ByteArray data with 2 bytes changed. We need to change at least 2 bytes // because a single byte can be a recovery id, where only 1 bit matters so // a modification of recovery id can result in a valid VM. -fn corrupted_bytes(input: bytes31, index: usize, random1: usize, random2: usize) -> bytes31 { +fn corrupted_bytes(input: felt252, index: usize, random1: usize, random2: usize) -> felt252 { let index2 = (index + 1) % 31; + let input: u256 = input.into(); let mut value: u256 = 0; - let mut i = 0; + let mut i: usize = 0; while i < 31 { - let real_byte = input.at(30 - i); + let real_byte = (input / one_shift_left_bytes_u256(30 - i.try_into().unwrap())) % 0x100; + let real_byte: u8 = real_byte.try_into().unwrap(); let new_byte = if i == index { corrupted_byte(real_byte, random1) } else if i == index2 { diff --git a/target_chains/starknet/tools/test_vaas/src/lib.rs b/target_chains/starknet/tools/test_vaas/src/lib.rs index 00d9dbdaba..6e928b1e6c 100644 --- a/target_chains/starknet/tools/test_vaas/src/lib.rs +++ b/target_chains/starknet/tools/test_vaas/src/lib.rs @@ -66,10 +66,7 @@ pub fn print_as_cairo_fn(data: &[u8], name: impl Display, comment: impl Display) println!(" {item},"); } println!(" ];"); - println!( - " ByteArrayImpl::new(array_try_into(bytes), {})", - data.num_last_bytes - ); + println!(" ByteArrayImpl::new(bytes, {})", data.num_last_bytes); println!("}}"); }