Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
written_public_data_slots_tree_check.cpp
Go to the documentation of this file.
2
6
7namespace bb::avm2::simulation {
8
21{
22 FF leaf_slot = unconstrained_compute_leaf_slot(contract_address, slot);
23
24 const auto& tree = written_public_data_slots_tree_stack.top();
25 const auto snapshot = tree.get_snapshot();
26 auto [exists, low_leaf_index] = tree.get_low_indexed_leaf(leaf_slot);
27 auto sibling_path = tree.get_sibling_path(low_leaf_index);
28 auto low_leaf_preimage = tree.get_leaf_preimage(low_leaf_index);
29
30 indexed_tree_check.assert_read(slot,
32 .address = contract_address,
33 .siloing_separator = DOM_SEP__PUBLIC_LEAF_SLOT,
34 },
35 exists,
37 .value = low_leaf_preimage.leaf.slot,
38 .next_value = low_leaf_preimage.nextKey,
39 .next_index = low_leaf_preimage.nextIndex,
40 },
41 low_leaf_index,
42 sibling_path,
43 snapshot);
44 return exists;
45}
46
58void WrittenPublicDataSlotsTreeCheck::insert(const AztecAddress& contract_address, const FF& slot)
59{
60 FF leaf_slot = unconstrained_compute_leaf_slot(contract_address, slot);
61
62 auto& tree = written_public_data_slots_tree_stack.top();
63 AppendOnlyTreeSnapshot prev_snapshot = tree.get_snapshot();
64 auto insertion_result = tree.insert_indexed_leaves({ { WrittenPublicDataSlotLeafValue(leaf_slot) } });
65 auto& [low_leaf_preimage, low_leaf_index, low_leaf_sibling_path] = insertion_result.low_leaf_witness_data.at(0);
66 std::span<FF> insertion_sibling_path = insertion_result.insertion_witness_data.at(0).path;
67
68 // If we pass a insertion sibling path, indexed tree check will assert that the leaf doesn't exist,
69 // otherwise, it will assert that the leaf exists.
70 bool exists = leaf_slot == low_leaf_preimage.leaf.slot;
71
72 AppendOnlyTreeSnapshot next_snapshot =
75 .address = contract_address,
76 .siloing_separator = DOM_SEP__PUBLIC_LEAF_SLOT,
77 },
78 std::nullopt, // No public inputs write
80 .value = low_leaf_preimage.leaf.slot,
81 .next_value = low_leaf_preimage.nextKey,
82 .next_index = low_leaf_preimage.nextIndex,
83 },
84 low_leaf_index,
85 low_leaf_sibling_path,
86 prev_snapshot,
87 exists ? std::nullopt : std::optional(insertion_sibling_path));
88
89 // This will throw an unexpected exception if it fails.
90 BB_ASSERT_EQ(next_snapshot, tree.get_snapshot(), "Next snapshot mismatch");
91}
92
101
108{
109 // -1 Since the tree has a prefill leaf at index 0.
110 return static_cast<uint32_t>(written_public_data_slots_tree_stack.top().get_snapshot().next_available_leaf_index) -
111 1;
112}
113
125
133{
134 // Commit the current top of the stack down one level.
138}
139
150
151} // namespace bb::avm2::simulation
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define DOM_SEP__PUBLIC_LEAF_SLOT
bool contains(const AztecAddress &contract_address, const FF &slot) override
Checks whether a public data slot exists in the written public data slots tree.
uint32_t size() const override
Returns the number of written public data slots in the tree.
AppendOnlyTreeSnapshot get_snapshot() const override
Returns the current tree snapshot from the top of the checkpoint stack.
void revert_checkpoint() override
Reverts the current checkpoint by discarding the top of the tree state stack.
void insert(const AztecAddress &contract_address, const FF &slot) override
Inserts a public data slot into the written public data slots tree.
void create_checkpoint() override
Creates a checkpoint by pushing a copy of the current tree state onto the stack.
void commit_checkpoint() override
Commits the current checkpoint by replacing the previous tree state with the current one.
AVM range check gadget for witness generation.
FF unconstrained_compute_leaf_slot(const AztecAddress &contract_address, const FF &slot)
Definition merkle.cpp:26
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13