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
5
6namespace bb::avm2::simulation {
7
9 const RetrievedBytecodesTreeLeafPreimage& low_leaf_preimage, const FF& class_id)
10{
11 if (!field_gt.ff_gt(class_id, low_leaf_preimage.leaf.class_id)) {
12 throw std::runtime_error("Low leaf class_id is GTE class id");
13 }
14 if (low_leaf_preimage.nextKey != 0 && !field_gt.ff_gt(low_leaf_preimage.nextKey, class_id)) {
15 throw std::runtime_error("Class id is GTE low leaf next class id");
16 }
17}
18
20{
21 const auto snapshot = tree.get_snapshot();
22 auto [exists, low_leaf_index] = tree.get_low_indexed_leaf(class_id);
23 auto sibling_path = tree.get_sibling_path(low_leaf_index);
24 auto low_leaf_preimage = tree.get_leaf_preimage(low_leaf_index);
25
26 // Low leaf membership
27 FF low_leaf_hash = poseidon2.hash(low_leaf_preimage.get_hash_inputs());
28 merkle_check.assert_membership(low_leaf_hash, low_leaf_index, sibling_path, snapshot.root);
29
30 if (exists) {
31 if (low_leaf_preimage.leaf.class_id != class_id) {
32 throw std::runtime_error("Class id membership check failed");
33 }
34 } else {
36 }
37
40 .prev_snapshot = snapshot,
41 .next_snapshot = snapshot,
42 .low_leaf_preimage = low_leaf_preimage,
43 .low_leaf_hash = low_leaf_hash,
44 .low_leaf_index = low_leaf_index,
45 .write = false,
46 .append_data = std::nullopt,
47 });
48
49 return exists;
50}
51
53{
55 auto insertion_result = tree.insert_indexed_leaves({ { ClassIdLeafValue(class_id) } });
56 auto& [low_leaf_preimage, low_leaf_index, low_leaf_sibling_path] = insertion_result.low_leaf_witness_data.at(0);
57 std::span<FF> insertion_sibling_path = insertion_result.insertion_witness_data.at(0).path;
58
59 bool exists = class_id == low_leaf_preimage.leaf.class_id;
60
61 AppendOnlyTreeSnapshot next_snapshot = prev_snapshot;
63
64 FF low_leaf_hash = poseidon2.hash(low_leaf_preimage.get_hash_inputs());
65 if (exists) {
66 merkle_check.assert_membership(low_leaf_hash, low_leaf_index, low_leaf_sibling_path, prev_snapshot.root);
67 } else {
69 // Low leaf update
70 RetrievedBytecodesTreeLeafPreimage updated_low_leaf_preimage = low_leaf_preimage;
71 updated_low_leaf_preimage.nextIndex = prev_snapshot.nextAvailableLeafIndex;
72 updated_low_leaf_preimage.nextKey = class_id;
73
74 FF updated_low_leaf_hash = poseidon2.hash(updated_low_leaf_preimage.get_hash_inputs());
75
76 FF intermediate_root = merkle_check.write(
77 low_leaf_hash, updated_low_leaf_hash, low_leaf_index, low_leaf_sibling_path, prev_snapshot.root);
78
80 ClassIdLeafValue(class_id), low_leaf_preimage.nextIndex, low_leaf_preimage.nextKey);
81
82 FF new_leaf_hash = poseidon2.hash(new_leaf_preimage.get_hash_inputs());
83
84 FF write_root = merkle_check.write(
85 FF(0), new_leaf_hash, prev_snapshot.nextAvailableLeafIndex, insertion_sibling_path, intermediate_root);
86
87 next_snapshot = AppendOnlyTreeSnapshot{
88 .root = write_root,
89 .nextAvailableLeafIndex = prev_snapshot.nextAvailableLeafIndex + 1,
90 };
91 assert(next_snapshot == tree.get_snapshot());
92 append_data = RetrievedBytecodeAppendData{
93 .updated_low_leaf_hash = updated_low_leaf_hash,
94 .new_leaf_hash = new_leaf_hash,
95 .intermediate_root = intermediate_root,
96 };
97 }
98
101 .prev_snapshot = prev_snapshot,
102 .next_snapshot = next_snapshot,
103 .low_leaf_preimage = low_leaf_preimage,
104 .low_leaf_hash = low_leaf_hash,
105 .low_leaf_index = low_leaf_index,
106 .write = true,
107 .append_data = append_data,
108 });
109}
110
115
117{
118 // -1 Since the tree has a prefill leaf at index 0.
119 return static_cast<uint32_t>(tree.get_snapshot().nextAvailableLeafIndex) - 1;
120}
121
122} // namespace bb::avm2::simulation
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