-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,131 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
libs/nil_core/include/zkevm_framework/core/mpt/node.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#ifndef ZKEMV_FRAMEWORK_LIBS_NIL_CORE_INCLUDE_ZKEVM_FRAMEWORK_CORE_MPT_NODE_HPP_ | ||
#define ZKEMV_FRAMEWORK_LIBS_NIL_CORE_INCLUDE_ZKEVM_FRAMEWORK_CORE_MPT_NODE_HPP_ | ||
|
||
#include <cstdint> | ||
#include <ssz++.hpp> | ||
#include <vector> | ||
|
||
#include "zkevm_framework/core/mpt/path.hpp" | ||
|
||
namespace core { | ||
namespace mpt { | ||
|
||
constexpr size_t kBranchesNum = 16; | ||
|
||
using Bytes = std::vector<std::byte>; | ||
using Reference = Bytes; | ||
|
||
enum class NodeTypeFlag : std::uint8_t { | ||
kLeafNode = 0, | ||
kExtensionNode = 1, | ||
kBranchNode = 2 | ||
}; | ||
|
||
class Serializable { | ||
public: | ||
virtual ~Serializable() = default; | ||
virtual Bytes Encode() const = 0; | ||
}; | ||
|
||
class PathHolder { | ||
public: | ||
PathHolder() = default; | ||
explicit PathHolder(Path path); | ||
|
||
Path path; | ||
}; | ||
|
||
class ValueHolder { | ||
public: | ||
ValueHolder() = default; | ||
explicit ValueHolder(const std::vector<std::byte>& value); | ||
|
||
const std::vector<std::byte>& value() const; | ||
void set_value(const std::vector<std::byte>& new_value); | ||
|
||
protected: | ||
ssz::list<std::byte, 100000000> value_; | ||
}; | ||
|
||
class LeafNode : public ValueHolder, | ||
public PathHolder, | ||
public ssz::ssz_variable_size_container, | ||
public Serializable { | ||
public: | ||
LeafNode() = default; | ||
LeafNode(const Path& path, const std::vector<std::byte>& new_value); | ||
|
||
Bytes Encode() const override; | ||
|
||
SSZ_CONT(path, value_) | ||
}; | ||
|
||
class ExtensionNode : public PathHolder, | ||
public ssz::ssz_variable_size_container, | ||
public Serializable { | ||
public: | ||
ExtensionNode() = default; | ||
ExtensionNode(const Path& path, const Reference& next); | ||
|
||
Bytes Encode() const override; | ||
const Reference& get_next_ref() const; | ||
|
||
SSZ_CONT(path, next_ref_) | ||
|
||
private: | ||
ssz::list<std::byte, 1000> next_ref_; | ||
}; | ||
|
||
class BranchNode : public ValueHolder, | ||
public ssz::ssz_variable_size_container, | ||
public Serializable { | ||
public: | ||
BranchNode() = default; | ||
BranchNode(const std::array<Reference, kBranchesNum>& refs, | ||
const std::vector<std::byte>& value); | ||
|
||
Bytes Encode() const override; | ||
std::array<Reference, kBranchesNum> get_branches() const; | ||
void ClearBranch(std::byte nibble); | ||
void SetBranch(std::byte nibble, const std::vector<std::byte>& value); | ||
|
||
SSZ_CONT(branches_, value_) | ||
|
||
private: | ||
ssz::list<ssz::list<std::byte, 1000>, kBranchesNum> branches_; | ||
}; | ||
|
||
using Node = std::variant<LeafNode, ExtensionNode, BranchNode>; | ||
|
||
Node DecodeNode(const Bytes& bytes); | ||
|
||
} // namespace mpt | ||
} // namespace core | ||
|
||
#endif // ZKEMV_FRAMEWORK_LIBS_NIL_CORE_INCLUDE_ZKEVM_FRAMEWORK_CORE_MPT_NODE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#ifndef ZKEMV_FRAMEWORK_LIBS_NIL_CORE_INCLUDE_ZKEVM_FRAMEWORK_CORE_MPT_PATH_HPP_ | ||
#define ZKEMV_FRAMEWORK_LIBS_NIL_CORE_INCLUDE_ZKEVM_FRAMEWORK_CORE_MPT_PATH_HPP_ | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <ssz++.hpp> | ||
#include <vector> | ||
|
||
namespace core { | ||
namespace mpt { | ||
|
||
class Path : public ssz::ssz_variable_size_container { | ||
public: | ||
Path(); | ||
Path(const Path &other) = default; | ||
Path(const std::vector<std::byte> &data, std::size_t offset = 0); | ||
|
||
int size() const; | ||
bool empty() const; | ||
std::byte operator[](size_t idx) const; | ||
Path operator+(const Path &other) const; | ||
bool operator==(const Path &other) const; | ||
std::byte at(std::size_t idx) const; | ||
|
||
bool StartsWith(const Path &other) const; | ||
Path *Consume(std::size_t amount); | ||
Path CommonPrefix(const Path &other) const; | ||
|
||
// Methods used by sszpp. NO hash_tree_root METHOD PROVIDED | ||
constexpr std::size_t ssz_size() const noexcept { return 1 + size() / 2; } | ||
|
||
constexpr void serialize(ssz::ssz_iterator auto result) const { | ||
std::size_t nibblesLen = size(); | ||
bool isOdd = (nibblesLen % 2 == 1); | ||
|
||
// If even size, we just insert empty byte at the beginning | ||
auto prefix = std::byte{0x00}; | ||
|
||
// If odd, we put kOddFlag at the first nibble and the first element of Path after | ||
// it, so we could insert by full bytes afterwards | ||
if (isOdd) { | ||
prefix = kOddFlag | at(0); | ||
} | ||
|
||
std::copy(static_cast<const std::byte *>(static_cast<const void *>(&prefix)), | ||
static_cast<const std::byte *>(static_cast<const void *>(&prefix)) + 1, | ||
result); | ||
++result; | ||
|
||
for (std::size_t i = (isOdd ? 1 : 0); i < nibblesLen; i += 2) { | ||
std::byte nextByte = (operator[](i) << 4) | operator[](i + 1); | ||
std::copy( | ||
static_cast<const std::byte *>(static_cast<const void *>(&nextByte)), | ||
static_cast<const std::byte *>(static_cast<const void *>(&nextByte)) + 1, | ||
result); | ||
++result; | ||
} | ||
} | ||
|
||
constexpr void deserialize(const std::ranges::sized_range auto &bytes) { | ||
bool isOddLen = (bytes.front() & kOddFlag) == kOddFlag; | ||
if (isOddLen) { | ||
offset_ = 1; | ||
} else { | ||
offset_ = 2; | ||
} | ||
|
||
data_.reserve(std::ranges::size(bytes)); | ||
std::ranges::copy(bytes, std::back_inserter(data_)); | ||
} | ||
|
||
private: | ||
Path ConstructFromPrefix(std::size_t length) const; | ||
|
||
static constexpr std::byte kOddFlag = std::byte{0x10}; | ||
std::vector<std::byte> data_; | ||
std::size_t offset_ = 0; | ||
}; | ||
|
||
} // namespace mpt | ||
} // namespace core | ||
|
||
#endif // ZKEMV_FRAMEWORK_LIBS_NIL_CORE_INCLUDE_ZKEVM_FRAMEWORK_CORE_MPT_MPT_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# SSZ++ can be compiled only with GCC 13+ | ||
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
message(FATAL_ERROR "Data types library can be built only with GCC 13+") | ||
endif() | ||
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13.0) | ||
message(FATAL_ERROR "Data types library can be built only with GCC 13+") | ||
endif() | ||
|
||
find_package(sszpp REQUIRED) | ||
|
||
set(SOURCES | ||
mpt/mpt.cpp | ||
mpt/node.cpp | ||
mpt/path.cpp | ||
) | ||
|
||
target_sources(${LIBRARY_NAME} PRIVATE ${SOURCES}) | ||
|
||
target_link_libraries(${LIBRARY_NAME} PRIVATE sszpp::sszpp) |
Oops, something went wrong.