From 417459be0fd65bf7a386be5cf455e67660ecb219 Mon Sep 17 00:00:00 2001 From: Arkadiusz Konior Date: Wed, 14 Feb 2024 15:04:04 +0100 Subject: [PATCH] Split BlockHeaderPartial to encoded and decoded part --- .../circuits/lib/src/get_header.nr | 34 ++++++++++++------- .../circuits/lib/src/get_header_test.nr | 23 +++++++------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/ethereum_history_api/circuits/lib/src/get_header.nr b/ethereum_history_api/circuits/lib/src/get_header.nr index 69c50901f..bf9febdec 100644 --- a/ethereum_history_api/circuits/lib/src/get_header.nr +++ b/ethereum_history_api/circuits/lib/src/get_header.nr @@ -12,53 +12,61 @@ global TRANSACTIONS_ROOT_INDEX = 4; global RECEIPTS_ROOT_INDEX = 5; global BLOCK_NUMBER_INDEX = 8; +struct BlockHeaderFromOracle { + partial: BlockHeaderPartial, + encoded: BlockHeaderEncoded +} + struct BlockHeaderPartial { state_root: [u8; HASH_LENGTH], transactions_root: [u8; HASH_LENGTH], receipts_root: [u8; HASH_LENGTH], number: Field, - hash: [u8; HASH_LENGTH], + hash: [u8; HASH_LENGTH] +} + +struct BlockHeaderEncoded { encoded_len: Field, encoded: [u8; MAX_HEADER_RLP_SIZE] } #[oracle(get_header)] -unconstrained fn get_header_oracle(_block_no: Field) -> BlockHeaderPartial {} +unconstrained fn get_header_oracle(_block_no: Field) -> BlockHeaderFromOracle {} -unconstrained fn get_header_unconstrained(block_no: Field) -> BlockHeaderPartial { +unconstrained fn get_header_unconstrained(block_no: Field) -> BlockHeaderFromOracle { get_header_oracle(block_no) } pub(crate) fn verify_header(block_no: Field) -> BlockHeaderPartial { let header = get_header_unconstrained(block_no); - let rlp_list:RLP_List = decode1(header.encoded); + let rlp_list:RLP_List = decode1(header.encoded.encoded); assert( sub_array_equals( - header.state_root, - header.encoded, + header.partial.state_root, + header.encoded.encoded, rlp_list.offset[STATE_ROOT_INDEX] ), "state_root does not match" ); assert( sub_array_equals( - header.transactions_root, - header.encoded, + header.partial.transactions_root, + header.encoded.encoded, rlp_list.offset[TRANSACTIONS_ROOT_INDEX] ), "transactions_root does not match" ); assert( sub_array_equals( - header.receipts_root, - header.encoded, + header.partial.receipts_root, + header.encoded.encoded, rlp_list.offset[RECEIPTS_ROOT_INDEX] ), "receipts_root does not match" ); assert( sub_array_equals( - keccak256(header.encoded, header.encoded_len as u32), - header.hash, + keccak256(header.encoded.encoded, header.encoded.encoded_len as u32), + header.partial.hash, 0 ), "hash does not match" ); - header + header.partial } diff --git a/ethereum_history_api/circuits/lib/src/get_header_test.nr b/ethereum_history_api/circuits/lib/src/get_header_test.nr index e1cb1f746..06b5518a8 100644 --- a/ethereum_history_api/circuits/lib/src/get_header_test.nr +++ b/ethereum_history_api/circuits/lib/src/get_header_test.nr @@ -1,5 +1,5 @@ use dep::std::test::OracleMock; -use crate::get_header::{verify_header, BlockHeaderPartial}; +use crate::get_header::{verify_header, BlockHeaderPartial, BlockHeaderEncoded, BlockHeaderFromOracle}; use crate::arrays::alter_array; global state_root = [ @@ -29,42 +29,43 @@ global hash = [ global encoded_len: Field = 515; -global blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash, encoded_len, encoded }; +global blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash }; +global blockEncoded = BlockHeaderEncoded { encoded, encoded_len }; #[test] fn test_get_header_success() { - let _ = OracleMock::mock("get_header").returns(blockPartial); + let _ = OracleMock::mock("get_header").returns(BlockHeaderFromOracle { partial: blockPartial, encoded: blockEncoded }); assert(verify_header(0).state_root == state_root); } #[test(should_fail_with = "state_root does not match")] fn test_get_header_invalid_state_root() { let state_root = alter_array(state_root); - let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash, encoded_len, encoded }; - let _ = OracleMock::mock("get_header").returns(blockPartial); + let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash }; + let _ = OracleMock::mock("get_header").returns(BlockHeaderFromOracle { partial: blockPartial, encoded: blockEncoded }); assert(verify_header(0).state_root == state_root); } #[test(should_fail_with = "transactions_root does not match")] fn test_get_header_invalid_transactions_root() { let transactions_root = alter_array(transactions_root); - let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash, encoded_len, encoded }; - let _ = OracleMock::mock("get_header").returns(blockPartial); + let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash }; + let _ = OracleMock::mock("get_header").returns(BlockHeaderFromOracle { partial: blockPartial, encoded: blockEncoded }); assert(verify_header(0).state_root == state_root); } #[test(should_fail_with = "receipts_root does not match")] fn test_get_header_invalid_receipt_root() { let receipts_root = alter_array(receipts_root); - let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash, encoded_len, encoded }; - let _ = OracleMock::mock("get_header").returns(blockPartial); + let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash }; + let _ = OracleMock::mock("get_header").returns(BlockHeaderFromOracle { partial: blockPartial, encoded: blockEncoded }); assert(verify_header(0).state_root == state_root); } #[test(should_fail_with = "hash does not match")] fn test_get_header_invalid_hash() { let hash = alter_array(hash); - let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash, encoded_len, encoded }; - let _ = OracleMock::mock("get_header").returns(blockPartial); + let blockPartial = BlockHeaderPartial { state_root, transactions_root, receipts_root, number, hash }; + let _ = OracleMock::mock("get_header").returns(BlockHeaderFromOracle { partial: blockPartial, encoded: blockEncoded }); assert(verify_header(0).state_root == state_root); }