From c8fcade73471d6b321c9881ba1157b523d3b7f08 Mon Sep 17 00:00:00 2001 From: Alex Metelli Date: Mon, 20 May 2024 06:56:26 +0800 Subject: [PATCH] fixes for storage verification result value conversion --- starknet/src/fact_registry/contract.cairo | 2 +- starknet/src/library/keccak_utils.cairo | 2 +- starknet/src/library/rlp_utils.cairo | 42 +++++++++++------------ starknet/src/library/trie_proof.cairo | 1 - starknet/src/library/words64_utils.cairo | 35 ++++++------------- starknet/tests/test_fact_registry.cairo | 6 ++-- 6 files changed, 36 insertions(+), 52 deletions(-) diff --git a/starknet/src/fact_registry/contract.cairo b/starknet/src/fact_registry/contract.cairo index aa574e2..fb55f7a 100644 --- a/starknet/src/fact_registry/contract.cairo +++ b/starknet/src/fact_registry/contract.cairo @@ -113,7 +113,7 @@ pub mod FactRegistry { let slot_value = match result { Option::None => Words64Sequence { values: array![].span(), len_bytes: 0 }, - Option::Some(result) => { extract_element(result, 0) } + Option::Some(result) => result }; slot_value } diff --git a/starknet/src/library/keccak_utils.cairo b/starknet/src/library/keccak_utils.cairo index 7d0fdc0..d2ce1ef 100644 --- a/starknet/src/library/keccak_utils.cairo +++ b/starknet/src/library/keccak_utils.cairo @@ -13,7 +13,7 @@ pub fn keccak_words64(input: Words64Sequence) -> Words64Sequence { } -fn u64_to_u8_array(input: Span, len_bytes: usize) -> Array { +pub fn u64_to_u8_array(input: Span, len_bytes: usize) -> Array { let mut bytes: Array = array![]; let (full_words, remainder) = u32_safe_divmod(len_bytes, 8); diff --git a/starknet/src/library/rlp_utils.cairo b/starknet/src/library/rlp_utils.cairo index 3c485d1..deed85a 100644 --- a/starknet/src/library/rlp_utils.cairo +++ b/starknet/src/library/rlp_utils.cairo @@ -255,28 +255,26 @@ mod tests { assert_eq!(result.length, 54); } - // #[test] - // fn test_extract_data_specific() { - // let mut rlp = Words64Sequence { - // values: array![ - // 17889425271775927342, - // 7747611707377904165, - // 13770790249671850669, - // 10758299819545195701, - // 4563277353913962038, - // 17973550993138662906, - // 12418610901666554729, - // 11791013025377241442, - // 16720179567303 - // ] - // .span(), - // len_bytes: 70 - // }; - // let start_pos = 38; - // let size = 32; - // let result = super::extract_data(rlp, start_pos, size); - // println!("{:?}", result.values); - // } + #[test] + fn test_extract_data_specific() { + let mut rlp = Words64Sequence { + values: array![ + 16978043373031179566, + 7407922919091180751, + 14853551893213251245, + 4906994927831835881, + 10054857540239986558, + 2856817665 + ] + .span(), + len_bytes: 44 + }; + let start_pos = 33; + let size = 11; + let result = super::extract_data(rlp, start_pos, size); + assert_eq!(result.values, array![9946104055808884394, 4690945].span()); + assert_eq!(result.len_bytes, 11); + } #[test] fn test_extract_data() { diff --git a/starknet/src/library/trie_proof.cairo b/starknet/src/library/trie_proof.cairo index f1fd9ca..27a54a9 100644 --- a/starknet/src/library/trie_proof.cairo +++ b/starknet/src/library/trie_proof.cairo @@ -89,7 +89,6 @@ pub fn verify_proof( let node_children = extract_nibble(path, path_offset).try_into().unwrap(); let children = *node.at(node_children); assert!(children.length == 0, "Invalid children length"); - // out = Option::Some(Words64Sequence { values: array![].span(), len_bytes: 0 }); out = Option::None; break; } diff --git a/starknet/src/library/words64_utils.cairo b/starknet/src/library/words64_utils.cairo index 8ac7ad7..16e4ebc 100644 --- a/starknet/src/library/words64_utils.cairo +++ b/starknet/src/library/words64_utils.cairo @@ -1,7 +1,9 @@ use core::integer::u32_safe_divmod; use core::option::OptionTrait; use core::traits::Into; -use fossil::library::{array_utils::ArrayTraitExt, bitshift::BitShift, keccak_utils::keccak_words64}; +use fossil::library::{ + array_utils::ArrayTraitExt, bitshift::BitShift, keccak_utils::{keccak_words64, u64_to_u8_array} +}; use fossil::types::Words64Sequence; use starknet::EthAddress; @@ -54,35 +56,20 @@ pub fn words64_to_u256(input: Span) -> u256 { return (BitOr::bitor(BitOr::bitor(BitOr::bitor(l0, l1), l2), l3)).into(); } -pub fn words64_to_int(input: Words64Sequence) -> u256 { - assert!(input.len_bytes <= 32, "number of bytes must be less than or equal to 32"); - if input.len_bytes == 0 { - return 0; - } +pub fn words64_to_int(input: Words64Sequence) -> u256 { let mut result = 0_u256; - let num_full_words = input.len_bytes / 8; - let remaining_bytes = input.len_bytes % 8; - let mut i = 0; + let bytes = u64_to_u8_array(input.values, input.len_bytes); - while i < num_full_words { - result = - BitOr::bitor( - result, - BitShift::shl((*input.values.at(i)).into(), ((num_full_words - i - 1) * 64).into()) - ); + let len = bytes.len(); + let mut i = 0; + while i < len { + let byte = *bytes.at(i); + result = BitShift::shl(result, 8); + result = BitOr::bitor(result, byte.into()); i += 1; }; - if remaining_bytes > 0 { - let last_word = *input.values.at(num_full_words); - let mask = (BitShift::shl(1_u64, (remaining_bytes * 8).into()) - 1).into(); - result = - BitOr::bitor( - result, BitShift::shl((last_word & mask).into(), (num_full_words * 64).into()) - ); - } - result } diff --git a/starknet/tests/test_fact_registry.cairo b/starknet/tests/test_fact_registry.cairo index 71ccbef..5544fda 100644 --- a/starknet/tests/test_fact_registry.cairo +++ b/starknet/tests/test_fact_registry.cairo @@ -145,7 +145,7 @@ fn get_storage_test_success_with_some_data() { storage_proof.bytes, storage_proof.data ); - assert_eq!(result.values, array![551956115156281927, 37889].span()); + assert_eq!(result.values, array![9946104055808884394, 4690945].span()); } #[test] @@ -204,7 +204,7 @@ fn get_storage_test_fail_account_not_found() { #[test] -fn get_storage_uint_test_success() { +fn get_storage_uint_test_success_test() { let dsp = setup(); let block = proofs::blocks::BLOCK_3(); @@ -233,7 +233,7 @@ fn get_storage_uint_test_success() { storage_proof.bytes, storage_proof.data ); - assert_eq!(result, 698929238164896357460551); + assert_eq!(result, 0x8a07a8f1298f7eaa479401); } #[test]