Skip to content

Commit

Permalink
Merge pull request #23 from vlayer-xyz/main
Browse files Browse the repository at this point in the history
Refactor: extract memcpy function
  • Loading branch information
ax0 authored Mar 14, 2024
2 parents 7375596 + ec7fb16 commit 050d26a
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<32, PROOF_LEN, MAX_VALUE_LEN>
let k = if (in_range) {i} else {0}; // Restrict index to range {0, ..., depth - 2}

// Populate node array
for j in 0..MAX_TRIE_NODE_LENGTH
{
node[j] = path[j + k*MAX_TRIE_NODE_LENGTH];
}
memcpy(&mut node, path, k*MAX_TRIE_NODE_LENGTH);

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.

Expand All @@ -105,10 +102,7 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<32, PROOF_LEN, MAX_VALUE_LEN>
}

// Treat final node as the preceding nodes...
for j in 0..MAX_TRIE_NODE_LENGTH
{
node[j] = path[j + (depth - 1)*MAX_TRIE_NODE_LENGTH];
}
memcpy(&mut node, path, (depth - 1)*MAX_TRIE_NODE_LENGTH);

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

Expand Down Expand Up @@ -176,10 +170,7 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<20, PROOF_LEN, MAX_VALUE_LEN>
let k = if (in_range) {i} else {0}; // Restrict index to range {0, ..., depth - 2}

// Populate node array
for j in 0..MAX_TRIE_NODE_LENGTH
{
node[j] = path[j + k*MAX_TRIE_NODE_LENGTH];
}
memcpy(&mut node, path, 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.

Expand All @@ -191,10 +182,7 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<20, PROOF_LEN, MAX_VALUE_LEN>
}

// Treat final node as the preceding nodes...
for j in 0..MAX_TRIE_NODE_LENGTH
{
node[j] = path[j + (depth - 1)*MAX_TRIE_NODE_LENGTH];
}
memcpy(&mut node, path, (depth - 1)*MAX_TRIE_NODE_LENGTH);

assert(verify_node_hash(node, extracted_hash));

Expand Down Expand Up @@ -225,6 +213,20 @@ impl<PROOF_LEN, MAX_VALUE_LEN> TrieProof<20, PROOF_LEN, MAX_VALUE_LEN>
}
}

/// Fills destination array with content of source array starting from the starting position.
///
/// # Arguments
/// * `dest` - Destination array
/// * `src` - Source array
/// * `start_pos` - Starting position in source array
fn memcpy<N, M>(dest: &mut [u8; N], src: [u8; M], start_pos: u64)
{
for i in 0..N
{
(*dest)[i] = src[start_pos + i];
}
}

/// Trie node hash verifier. Returns true if the node has the specified keccak256 hash.
///
/// # Arguments
Expand Down Expand Up @@ -541,6 +543,15 @@ fn left_byte_shift<N>(input: [u8; N], n: u64) -> [u8; N]
out
}

#[test]
fn memcpy_test()
{
let mut dest = [0; 10];
let src = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
memcpy(&mut dest, src, 5);
assert(dest == [6,7,8,9,10,11,12,13,14,15]);
}

#[test]
fn byte_value_test()
{
Expand Down

0 comments on commit 050d26a

Please sign in to comment.