Skip to content

Commit

Permalink
Fix path serialised_size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou committed Dec 15, 2023
1 parent 1a8a3a0 commit f4997c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion merklecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,14 @@ namespace merkle
/// @brief The size of the serialised path in number of bytes
size_t serialised_size() const
{
return sizeof(_leaf) + elements.size() * sizeof(Element);
return sizeof(_leaf) +
sizeof(uint64_t) + // leaf index
sizeof(uint64_t) + // max index
sizeof(uint64_t) + // number of elements
elements.size() * (
sizeof(Element::hash) + // hash
sizeof(uint8_t) // direction
);
}

/// @brief Index of the leaf of the path
Expand Down
4 changes: 4 additions & 0 deletions test/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ int main()
auto path = mt.path(i);
if (!path->verify(root))
throw std::runtime_error("path verification failed");
std::vector<uint8_t> serialised_path;
path->serialise(serialised_path);
if (path->serialised_size() != serialised_path.size())
throw std::runtime_error("serialised_size() != serialised_path.size()");
}
}

Expand Down

0 comments on commit f4997c0

Please sign in to comment.