Skip to content

Commit

Permalink
Merge pull request #17 from Ahmed-Aghadi/prove_bug
Browse files Browse the repository at this point in the history
Fixed the issues in proofs verification
  • Loading branch information
ametel01 authored Jul 24, 2024
2 parents 2d67715 + 3c482f5 commit bca5d72
Show file tree
Hide file tree
Showing 9 changed files with 982 additions and 22 deletions.
2 changes: 1 addition & 1 deletion starknet/src/L1_headers_store/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub mod L1HeaderStore {
};

let provided_rlp_hash = keccak_words64(header_ints_sequence);
let provided_rlp_hash_u256 = words64_to_u256(provided_rlp_hash.values);
let provided_rlp_hash_u256 = words64_to_u256(provided_rlp_hash.values, provided_rlp_hash.len_bytes);

assert!(
child_block_parent_hash == provided_rlp_hash_u256,
Expand Down
9 changes: 4 additions & 5 deletions starknet/src/fact_registry/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub mod FactRegistry {
slot: u256,
proof_sizes_bytes: Array<usize>,
proofs_concat: Array<u64>,
) -> felt252 {
) -> Result<u256, felt252> {
let account_state_root = self.verified_account_storage_hash.read((account, block));
assert!(account_state_root != 0, "FactRegistry: block state root not found");

Expand All @@ -205,15 +205,14 @@ pub mod FactRegistry {
);

match result {
Result::Err(e) => { e },
Result::Err(e) => { Result::Err(e) },
Result::Ok(result) => {
if !result.is_some() {
'Result Found No Value'
Result::Err('Result is None')
} else {
let result_value = words64_to_int(result.unwrap());
self.verified_storage.write((account, block, slot), (true, result_value));
let res: felt252 = result_value.try_into().unwrap();
res
Result::Ok(result_value)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion starknet/src/fact_registry/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait IFactRegistry<TState> {
slot: u256,
proof_sizes_bytes: Array<usize>,
proofs_concat: Array<u64>,
) -> felt252;
) -> Result<u256, felt252>;
fn get_storage(
ref self: TState, block: u64, account: starknet::EthAddress, slot: u256,
) -> Option<u256>;
Expand Down
6 changes: 3 additions & 3 deletions starknet/src/library/rlp_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ pub fn extract_data(rlp: Words64Sequence, start: usize, size: u64) -> Words64Seq
if lastword_right_shift < left_shift {
right_part =
BitShift::shl(
value_at_end_word, (left_shift - lastword_right_shift).into() * 8
value_at_end_word, (left_shift - lastword_right_shift + 1).into() * 8
);
} else {
right_part =
BitShift::shr(
value_at_end_word, (lastword_right_shift - left_shift).into() * 8
BitShift::shl(
value_at_end_word, (lastword_right_shift - left_shift + 1).into() * 8
);
}
} else {
Expand Down
45 changes: 36 additions & 9 deletions starknet/src/library/words64_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl U256Words64 of Words64Trait<u256> {

/// `from_words64` converts a `Words64Sequence` back into an `u256`.
fn from_words64(self: Words64Sequence) -> u256 {
words64_to_u256(self.values)
words64_to_u256(self.values, self.len_bytes)
}
}

Expand Down Expand Up @@ -67,6 +67,7 @@ impl EthAddressWords64 of Words64Trait<EthAddress> {
///
/// # Arguments
/// * `input` - A `Span<u64>` containing up to four 64-bit words to be converted into a `u256`.
/// * `len_bytes` - The length of the `input` in bytes.
///
/// # Returns
/// * `u256` - A value representing the combination of the input words.
Expand All @@ -76,18 +77,34 @@ impl EthAddressWords64 of Words64Trait<EthAddress> {
///
/// This function takes a `Span<u64>` representing a `Words64Sequence` i.e sequence of up to four 64-bit words,
/// and combines them into a single `u256` value by using the shift and bitwise operator.
pub fn words64_to_u256(input: Span<u64>) -> u256 {
pub fn words64_to_u256(input: Span<u64>, len_bytes: usize) -> u256 {
assert!(input.len() <= 4, "input length must be less than or equal to 4");
if input.len() == 0 {
return 0;
}

let l0: u256 = BitShift::shl((*input.at(0)).into(), 192_u256);
let l1 = BitShift::shl((*input.at(1)).into(), 128);
let l2 = BitShift::shl((*input.at(2)).into(), 64);
let l3 = (*input.at(3)).into();
if input.len() == 1 {
return (*input.at(0)).into();
} else if input.len() == 2 {
let extraBitOffset: u256 = (16 - len_bytes.into()) * 8;
let l0: u256 = BitShift::shl((*input.at(0)).into(), 64_u256 - extraBitOffset);
let l1 = (*input.at(1)).into();
return BitOr::bitor(l0, l1);
} else if input.len() == 3 {
let extraBitOffset: u256 = (24 - len_bytes.into()) * 8;
let l0: u256 = BitShift::shl((*input.at(0)).into(), 128_u256 - extraBitOffset);
let l1 = BitShift::shl((*input.at(1)).into(), 64 - extraBitOffset);
let l2 = (*input.at(2)).into();
return BitOr::bitor(BitOr::bitor(l0, l1), l2);
} else {
let extraBitOffset: u256 = (32 - len_bytes.into()) * 8;
let l0: u256 = BitShift::shl((*input.at(0)).into(), 192_u256 - extraBitOffset);
let l1 = BitShift::shl((*input.at(1)).into(), 128 - extraBitOffset);
let l2 = BitShift::shl((*input.at(2)).into(), 64 - extraBitOffset);
let l3 = (*input.at(3)).into();

return (BitOr::bitor(BitOr::bitor(BitOr::bitor(l0, l1), l2), l3)).into();
return (BitOr::bitor(BitOr::bitor(BitOr::bitor(l0, l1), l2), l3)).into();
}
}

/// Converts a `Words64Sequence` into a `u256` value.
Expand Down Expand Up @@ -224,15 +241,25 @@ fn words64_to_nibbles_rec(word: u64, nibbles_len: usize, mut acc: Array<u64>) ->
#[cfg(test)]
mod tests {
#[test]
fn test_words64_to_u256() {
fn test_words64_to_u256_default_size() {
let input = array![
0xFFFFFFFFFFFFFFFF, 0xAAAAAAAAAAAAAAAA, 0xBBBBBBBBBBBBBBBB, 0xCCCCCCCCCCCCCCCC
];

let result = super::words64_to_u256(input.span());
let result = super::words64_to_u256(input.span(), 32);

assert_eq!(result, 0xffffffffffffffffaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccc);
}
#[test]
fn test_words64_to_u256_different_size() {
let input = array![
0xFFFFFFFFFFFFFFFF, 0xAAAAAAAAAAAAAAAA, 0xBBBBBBBBBBBBBBBB, 0xCCCCCCCCCCCC
];

let result = super::words64_to_u256(input.span(), 30);

assert_eq!(result, 0xffffffffffffffffaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbcccccccccccc);
}

#[test]
fn test_words64_to_nibbles_rec() {
Expand Down
Loading

0 comments on commit bca5d72

Please sign in to comment.