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 and codygunton committed Apr 6, 2023
1 parent 570771f commit 4356e86
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 proof_system::plonk

0 comments on commit 4356e86

Please sign in to comment.