Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
retrieved_bytecodes_tree_check.cpp
Go to the documentation of this file.
2
6
7namespace bb::avm2::simulation {
8
10 const RetrievedBytecodesTreeLeafPreimage& low_leaf_preimage, const FF& class_id)
11{
12 if (!field_gt.ff_gt(class_id, low_leaf_preimage.leaf.class_id)) {
13 throw std::runtime_error("Low leaf class_id is GTE class id");
14 }
15 if (low_leaf_preimage.nextKey != 0 && !field_gt.ff_gt(low_leaf_preimage.nextKey, class_id)) {
16 throw std::runtime_error("Class id is GTE low leaf next class id");
17 }
18}
19
21{
22 const auto snapshot = tree.get_snapshot();
23 auto [exists, low_leaf_index] = tree.get_low_indexed_leaf(class_id);
24 auto sibling_path = tree.get_sibling_path(low_leaf_index);
25 auto low_leaf_preimage = tree.get_leaf_preimage(low_leaf_index);
26
27 // Low leaf membership
28 FF low_leaf_hash = poseidon2.hash(low_leaf_preimage.get_hash_inputs());
29 merkle_check.assert_membership(low_leaf_hash, low_leaf_index, sibling_path, snapshot.root);
30
31 if (exists) {
32 if (low_leaf_preimage.leaf.class_id != class_id) {
33 throw std::runtime_error("Class id membership check failed");
34 }
35 } else {
37 }
38
41 .prev_snapshot = snapshot,
42 .next_snapshot = snapshot,
43 .low_leaf_preimage = low_leaf_preimage,
44 .low_leaf_hash = low_leaf_hash,
45 .low_leaf_index = low_leaf_index,
46 .write = false,
47 .append_data = std::nullopt,
48 });
49
50 return exists;
51}
52
54{
56 auto insertion_result = tree.insert_indexed_leaves({ { ClassIdLeafValue(class_id) } });
57 auto& [low_leaf_preimage, low_leaf_index, low_leaf_sibling_path] = insertion_result.low_leaf_witness_data.at(0);
58 std::span<FF> insertion_sibling_path = insertion_result.insertion_witness_data.at(0).path;
59
60 bool exists = class_id == low_leaf_preimage.leaf.class_id;
61
62 AppendOnlyTreeSnapshot next_snapshot = prev_snapshot;
64
65 FF low_leaf_hash = poseidon2.hash(low_leaf_preimage.get_hash_inputs());
66 if (exists) {
67 merkle_check.assert_membership(low_leaf_hash, low_leaf_index, low_leaf_sibling_path, prev_snapshot.root);
68 } else {
70 // Low leaf update
71 RetrievedBytecodesTreeLeafPreimage updated_low_leaf_preimage = low_leaf_preimage;
72 updated_low_leaf_preimage.nextIndex = prev_snapshot.next_available_leaf_index;
73 updated_low_leaf_preimage.nextKey = class_id;
74
75 FF updated_low_leaf_hash = poseidon2.hash(updated_low_leaf_preimage.get_hash_inputs());
76
77 FF intermediate_root = merkle_check.write(
78 low_leaf_hash, updated_low_leaf_hash, low_leaf_index, low_leaf_sibling_path, prev_snapshot.root);
79
81 ClassIdLeafValue(class_id), low_leaf_preimage.nextIndex, low_leaf_preimage.nextKey);
82
83 FF new_leaf_hash = poseidon2.hash(new_leaf_preimage.get_hash_inputs());
84
85 FF write_root = merkle_check.write(
86 FF(0), new_leaf_hash, prev_snapshot.next_available_leaf_index, insertion_sibling_path, intermediate_root);
87
88 next_snapshot = AppendOnlyTreeSnapshot{
89 .root = write_root,
90 .next_available_leaf_index = prev_snapshot.next_available_leaf_index + 1,
91 };
92 // This will throw an unexpected exception if it fails.
93 BB_ASSERT_EQ(next_snapshot, tree.get_snapshot(), "Next snapshot mismatch");
94 append_data = RetrievedBytecodeAppendData{
95 .updated_low_leaf_hash = updated_low_leaf_hash,
96 .new_leaf_hash = new_leaf_hash,
97 .intermediate_root = intermediate_root,
98 };
99 }
100
103 .prev_snapshot = prev_snapshot,
104 .next_snapshot = next_snapshot,
105 .low_leaf_preimage = low_leaf_preimage,
106 .low_leaf_hash = low_leaf_hash,
107 .low_leaf_index = low_leaf_index,
108 .write = true,
109 .append_data = append_data,
110 });
111}
112
117
119{
120 // -1 Since the tree has a prefill leaf at index 0.
121 return static_cast<uint32_t>(tree.get_snapshot().next_available_leaf_index) - 1;
122}
123
124} // namespace bb::avm2::simulation
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
virtual bool ff_gt(const FF &a, const FF &b)=0
AppendOnlyTreeSnapshot get_snapshot() const
SequentialInsertionResult< LeafType > insert_indexed_leaves(std::span< const LeafType > leaves)
GetLowIndexedLeafResponse get_low_indexed_leaf(const FF &key) const
IndexedLeaf< LeafType > get_leaf_preimage(size_t leaf_index) const
SiblingPath get_sibling_path(size_t leaf_index) const
void validate_low_leaf_jumps_over_class_id(const RetrievedBytecodesTreeLeafPreimage &low_leaf_preimage, const FF &class_id)
EventEmitterInterface< RetrievedBytecodesTreeCheckEvent > & events
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
IndexedLeaf< ClassIdLeafValue > RetrievedBytecodesTreeLeafPreimage
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< fr > get_hash_inputs() const