Skip to content

Commit

Permalink
Merge pull request #15 from vlayer-xyz/hash_mismatch_assertion_message
Browse files Browse the repository at this point in the history
Hash mismatch assertion messages and CI
  • Loading branch information
ax0 authored Mar 11, 2024
2 parents 702bc18 + 4c9f749 commit 7375596
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: tests

on: [push]

jobs:
test:
name: nargo test
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Nargo
uses: noir-lang/[email protected]
with:
toolchain: 0.24.0

- name: Run nargo test
run: nargo test
7 changes: 4 additions & 3 deletions lib/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Ethereum trie proof implementation
use dep::std::hash::keccak256;
mod test;

mod rlp; // Module for required RLP encoding/decoding

Expand Down Expand Up @@ -92,8 +93,8 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<32, PROOF_LEN, MAX_VALUE_LEN>
{
node[j] = path[j + k*MAX_TRIE_NODE_LENGTH];
}
assert(!in_range | verify_node_hash(node, extracted_hash)); // If within range, node hash should match the hash extracted from the preceding node.

assert(!in_range | verify_node_hash(node, extracted_hash), "Middle node hash does not match the hash extracted from the preceding node"); // If within range, node hash should match the hash extracted from the preceding node.

// Extract hash and advance key pointer if within range
let lookup: (u64, [u8; KEY_LENGTH], u64) = resolve_nibble32(key_nibbles, if in_range { key_ptr } else { 0 }, node); // Resolve next nibble(s)
Expand All @@ -109,7 +110,7 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<32, PROOF_LEN, MAX_VALUE_LEN>
node[j] = path[j + (depth - 1)*MAX_TRIE_NODE_LENGTH];
}

assert(verify_node_hash(node, extracted_hash));
assert(verify_node_hash(node, extracted_hash), "Terminal node hash does not match the hash extracted from the preceding node");

let rlp_list: rlp::RLP_List<2> = rlp::decode1_small_lis(node); // Terminal node must be a 2-node, and both slots contain less than 56 bytes.

Expand Down
37 changes: 37 additions & 0 deletions lib/src/test.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{TrieProof, MAX_TRIE_NODE_LENGTH};

// Test data for { blockNumber: 4605172n, address: '0xc7af99fe5513eb6710e6d5f44f9989da40f27f26', storageKeys: ['0x0000000000000000000000000000000000000000000000000000000000000000'] }
global key = [0; 32];
global proof = [
248, 145, 128, 128, 160, 242, 226, 219, 153, 145, 216, 146, 196, 177, 110, 8, 236, 110, 245, 27, 30, 110, 130, 166, 5, 250, 128, 246, 172, 40, 49, 223, 164, 207, 142, 243, 255, 128, 160, 226, 125, 126, 238, 165, 20, 178, 4, 144, 145, 3, 17, 100, 3, 5, 45, 148, 46, 118, 242, 188, 70, 192, 127, 61, 48, 105, 148, 15, 81, 129, 121, 128, 128, 128, 160, 7, 27, 1, 31, 219, 212, 173, 125, 30, 111, 151, 98, 190, 77, 26, 136, 223, 253, 230, 20, 166, 189, 57, 155, 243, 181, 186, 216, 244, 18, 73, 181, 128, 128, 160, 208, 164, 119, 86, 185, 235, 172, 90, 175, 221, 26, 105, 98, 126, 17, 180, 180, 254, 100, 143, 228, 195, 178, 176, 201, 61, 242, 180, 27, 125, 241, 222, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
global depth = 0x02;
global value = [
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xba, 0x52, 0xc7, 0x57, 0x64, 0xd6, 0xf5, 0x94, 0x73, 0x5d, 0xc7, 0x35, 0xbe, 0x7f, 0x18, 0x30, 0xcd, 0xf5, 0x8d, 0xdf
];
global storage_root = [
125, 195, 164, 218, 176, 85, 143, 71, 255, 31, 212, 187, 163, 127, 253, 114, 116, 63, 178, 142, 180, 220, 161, 96, 6, 227, 190, 95, 53, 18, 223, 84
];

#[test]
fn test_verify_storage_root_depth_2() {
let trie_proof = TrieProof { key, proof, depth, value };
let _ = trie_proof.verify_storage_root(storage_root);
}

#[test(should_fail_with="Middle node hash does not match the hash extracted from the preceding node")]
fn test_assertion_message_for_invalid_storage_root() {
let invalid_storage_root = [0; 32];

let trie_proof = TrieProof { key, proof, depth, value };
let _ = trie_proof.verify_storage_root(invalid_storage_root);
}

#[test(should_fail_with="Terminal node hash does not match the hash extracted from the preceding node")]
fn test_assertion_message_for_invalid_leaf() {
let mut proof_with_modified_teminal_node = proof;
proof_with_modified_teminal_node[MAX_TRIE_NODE_LENGTH] += 0x01;

let trie_proof = TrieProof { key, proof: proof_with_modified_teminal_node, depth, value };
let _ = trie_proof.verify_storage_root(storage_root);
}

0 comments on commit 7375596

Please sign in to comment.