Skip to content

Commit

Permalink
compute tree (#298)
Browse files Browse the repository at this point in the history
* compute merkle tree
  • Loading branch information
dbanks12 authored Mar 30, 2023
1 parent 28f3e92 commit 395e829
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cpp/src/barretenberg/stdlib/merkle_tree/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ inline barretenberg::fr compute_tree_root_native(std::vector<barretenberg::fr> c
return layer[0];
}

// TODO write test
inline std::vector<barretenberg::fr> compute_tree_native(std::vector<barretenberg::fr> const& input)
{
// Check if the input vector size is a power of 2.
ASSERT(input.size() > 0);
ASSERT(numeric::is_power_of_two(input.size()));
auto layer = input;
std::vector<barretenberg::fr> tree(input);
while (layer.size() > 1) {
std::vector<barretenberg::fr> next_layer(layer.size() / 2);
for (size_t i = 0; i < next_layer.size(); ++i) {
if (plonk::SYSTEM_COMPOSER == plonk::PLOOKUP) {
next_layer[i] = crypto::pedersen_hash::lookup::hash_multiple({ layer[i * 2], layer[i * 2 + 1] });
} else {
next_layer[i] = crypto::pedersen_hash::hash_multiple({ layer[i * 2], layer[i * 2 + 1] });
}
tree.push_back(next_layer[i]);
}
layer = std::move(next_layer);
}

return tree;
}

} // namespace merkle_tree
} // namespace stdlib
} // namespace plonk

0 comments on commit 395e829

Please sign in to comment.