From f4997c04877aba00691cee586687988d3e348a2d Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Fri, 15 Dec 2023 10:58:09 +0000 Subject: [PATCH] Fix path serialised_size calculation --- merklecpp.h | 9 ++++++++- test/paths.cpp | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/merklecpp.h b/merklecpp.h index 2cd0aea..ed5a975 100644 --- a/merklecpp.h +++ b/merklecpp.h @@ -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 diff --git a/test/paths.cpp b/test/paths.cpp index 3b80401..03ddb79 100644 --- a/test/paths.cpp +++ b/test/paths.cpp @@ -56,6 +56,10 @@ int main() auto path = mt.path(i); if (!path->verify(root)) throw std::runtime_error("path verification failed"); + std::vector serialised_path; + path->serialise(serialised_path); + if (path->serialised_size() != serialised_path.size()) + throw std::runtime_error("serialised_size() != serialised_path.size()"); } }