Skip to content

Commit

Permalink
fixes for storage verification result value conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed May 19, 2024
1 parent ae041b8 commit c8fcade
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 52 deletions.
2 changes: 1 addition & 1 deletion starknet/src/fact_registry/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion starknet/src/library/keccak_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn keccak_words64(input: Words64Sequence) -> Words64Sequence {
}


fn u64_to_u8_array(input: Span<u64>, len_bytes: usize) -> Array<u8> {
pub fn u64_to_u8_array(input: Span<u64>, len_bytes: usize) -> Array<u8> {
let mut bytes: Array<u8> = array![];
let (full_words, remainder) = u32_safe_divmod(len_bytes, 8);

Expand Down
42 changes: 20 additions & 22 deletions starknet/src/library/rlp_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion starknet/src/library/trie_proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 11 additions & 24 deletions starknet/src/library/words64_utils.cairo
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -54,35 +56,20 @@ pub fn words64_to_u256(input: Span<u64>) -> 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
}

Expand Down
6 changes: 3 additions & 3 deletions starknet/tests/test_fact_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit c8fcade

Please sign in to comment.