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 const FF remaining_path_len_inv = remaining_path_len == 0 ? FF(0) : remaining_path_len.invert();
48
49 // end == 1 when the remaining_path_len == 0
50 const bool end = remaining_path_len == 0;
51 const bool start = i == 0; // First row in the chain is a start row
52 const bool index_is_even = current_index_in_layer % 2 == 0;
53 const FF read_left_node = index_is_even ? read_node : sibling;
54 const FF read_right_node = index_is_even ? sibling : read_node;
55 const FF read_output_hash = Poseidon2::hash({ read_left_node, read_right_node });
56
57 const FF write_left_node = write ? index_is_even ? write_node : sibling : FF(0);
58 const FF write_right_node = write ? index_is_even ? sibling : write_node : FF(0);
59 const FF write_output_hash = write ? Poseidon2::hash({ write_left_node, write_right_node }) : FF(0);
60
61 trace.set(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_inv },
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_constant_2, 2 },
80 { C::merkle_check_read_output_hash, read_output_hash },
81 { C::merkle_check_write_output_hash, write_output_hash } } });
82
83 // Update the current/target node value for the next iteration
84 read_node = read_output_hash;
85 write_node = write_output_hash;
86 current_index_in_layer >>= 1;
87
88 row++;
89 }
90 assert(read_node == root);
91 assert(write_node == new_root);
92 }
93}
94
98 .add<lookup_merkle_check_merkle_poseidon2_write_settings, InteractionType::LookupSequential>();
99
100} // 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(B &buf, field2< base_field, Params > const &value)
simulation::PublicDataTreeReadWriteEvent event