Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
merkle_check_trace.cpp
Go to the documentation of this file.
1#include <cstdint>
2#include <memory>
3
10
11namespace bb::avm2::tracegen {
12
14
17{
18 using C = Column;
19
20 // Skip 0th row since this gadget has shifts
21 uint32_t row = 1;
22
23 for (const auto& event : events) {
24 const size_t full_path_len = event.sibling_path.size();
25 // Iterate over the path starting at the leaf.
26 // Root is not included in the path.
27 // For the current level, gather info about the current pair of nodes
28 // being hashed along with the path-length remaining after this level
29 // to complete the merkle check.
30
31 bool write = event.new_leaf_value.has_value();
32 assert(write == event.new_root.has_value());
33
34 FF read_node = event.leaf_value;
35 FF write_node = event.new_leaf_value.value_or(FF(0));
36
37 FF root = event.root;
38 FF new_root = event.new_root.value_or(FF(0));
39
40 uint64_t current_index_in_layer = event.leaf_index;
41 for (size_t i = 0; i < full_path_len; ++i) {
42 const FF sibling = event.sibling_path[i];
43
44 // path-length decrements by 1 for each level until it reaches 1
45 const FF path_len = FF(full_path_len - i);
46 const FF remaining_path_len = path_len - 1;
47
48 // end == 1 when the remaining_path_len == 0
49 const bool end = remaining_path_len == 0;
50 const bool start = i == 0; // First row in the chain is a start row
51 const bool index_is_even = current_index_in_layer % 2 == 0;
52 const FF read_left_node = index_is_even ? read_node : sibling;
53 const FF read_right_node = index_is_even ? sibling : read_node;
54 const FF read_output_hash = Poseidon2::hash({ read_left_node, read_right_node });
55
56 const FF write_left_node = write ? index_is_even ? write_node : sibling : FF(0);
57 const FF write_right_node = write ? index_is_even ? sibling : write_node : FF(0);
58 const FF write_output_hash = write ? Poseidon2::hash({ write_left_node, write_right_node }) : FF(0);
59
60 trace.set(
61 row,
62 { { { C::merkle_check_sel, 1 },
63 { C::merkle_check_read_node, read_node },
64 { C::merkle_check_write, write },
65 { C::merkle_check_write_node, write_node },
66 { C::merkle_check_index, current_index_in_layer },
67 { C::merkle_check_path_len, path_len },
68 { C::merkle_check_remaining_path_len_inv, remaining_path_len }, // Will be inverted in batch later
69 { C::merkle_check_read_root, root },
70 { C::merkle_check_write_root, new_root },
71 { C::merkle_check_sibling, sibling },
72 { C::merkle_check_start, start },
73 { C::merkle_check_end, end },
74 { C::merkle_check_index_is_even, index_is_even },
75 { C::merkle_check_read_left_node, read_left_node },
76 { C::merkle_check_read_right_node, read_right_node },
77 { C::merkle_check_write_left_node, write_left_node },
78 { C::merkle_check_write_right_node, write_right_node },
79 { C::merkle_check_read_output_hash, read_output_hash },
80 { C::merkle_check_write_output_hash, write_output_hash } } });
81
82 // Update the current/target node value for the next iteration
83 read_node = read_output_hash;
84 write_node = write_output_hash;
85 current_index_in_layer >>= 1;
86
87 row++;
88 }
89 assert(read_node == root);
90 assert(write_node == new_root);
91 }
92
93 // Batch invert the columns.
94 trace.invert_columns({ { C::merkle_check_remaining_path_len_inv } });
95}
96
100 .add<lookup_merkle_check_merkle_poseidon2_write_settings, InteractionType::LookupSequential>();
101
102} // namespace bb::avm2::tracegen
InteractionDefinition & add(auto &&... args)
void process(const simulation::EventEmitterInterface< simulation::MerkleCheckEvent >::Container &events, TraceContainer &trace)
static const InteractionDefinition interactions
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
TestTraceContainer trace
lookup_settings< lookup_merkle_check_merkle_poseidon2_read_settings_ > lookup_merkle_check_merkle_poseidon2_read_settings
AvmFlavorSettings::FF FF
Definition field.hpp:10
void write(std::vector< uint8_t > &buf, ClientIVC::VerificationKey const &vk)
simulation::PublicDataTreeReadWriteEvent event