-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add traces reader to proof producer
- Loading branch information
Showing
13 changed files
with
439 additions
and
27 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -637,3 +637,7 @@ callgrind.* | |
|
||
.ycm_extra_config.py | ||
.color_coded | ||
|
||
# Ignore generated protobufs | ||
*.pb.h | ||
*.pb.cc |
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
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
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
124 changes: 124 additions & 0 deletions
124
proof-producer/bin/proof-producer/include/nil/proof-generator/traces_reader.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,124 @@ | ||
#include <fstream> | ||
#include <vector> | ||
#include <string> | ||
#include <cstdint> | ||
#include <unordered_map> | ||
|
||
#include <nil/blueprint/zkevm/zkevm_word.hpp> | ||
|
||
#include <nil/proof-generator/meta_utils.hpp> | ||
#include <nil/proof-generator/traces.pb.h> | ||
|
||
|
||
namespace nil { | ||
namespace proof_generator { | ||
|
||
struct StackOp { | ||
bool is_read; | ||
int idx; | ||
blueprint::zkevm_word_type value; | ||
std::byte op_code; | ||
}; | ||
|
||
struct MemoryOp { | ||
bool is_read; | ||
int idx; | ||
std::byte value; | ||
std::byte op_code; | ||
}; | ||
|
||
struct ExecutionTraces { | ||
std::vector<StackOp> stack_ops; | ||
std::vector<MemoryOp> memory_ops; | ||
std::unordered_map<std::string, std::vector<std::vector<std::byte>>> storage_proofs; | ||
std::vector<std::byte> executed_op_codes; | ||
}; | ||
|
||
// Convert protobuf Uint256 to zkevm_word_type | ||
[[nodiscard]] blueprint::zkevm_word_type proto_uint256_to_zkevm_word(const pb::Uint256& pb_uint) { | ||
blueprint::zkevm_word_type result = 0; | ||
for (size_t i = 0; i < pb_uint.word_parts_size() && i < 4; i++) { | ||
result |= (static_cast<blueprint::zkevm_word_type>(pb_uint.word_parts(i)) << (i * 64)); | ||
} | ||
return result; | ||
} | ||
|
||
// Convert protobuf Proof to vector of bytes | ||
[[nodiscard]] std::vector<std::byte> proto_proof_to_cpp(const pb::Proof& pb_proof) { | ||
const auto& data = pb_proof.proof_data(); | ||
return std::vector<std::byte>( | ||
reinterpret_cast<const std::byte*>(data.data()), | ||
reinterpret_cast<const std::byte*>(data.data() + data.size()) | ||
); | ||
} | ||
|
||
[[nodiscard]] std::optional<pb::ExecutionTraces> read_pb_traces_from_file(const boost::filesystem::path& filename) { | ||
if (!is_valid_path(filename.c_str()) || !can_read_from_file(filename.c_str())) { | ||
return std::nullopt; | ||
} | ||
|
||
auto file = open_file<std::ifstream>(filename.c_str(), std::ios::in | std::ios::binary); | ||
if (!file) { | ||
return std::nullopt; | ||
} | ||
|
||
pb::ExecutionTraces pb_traces; | ||
if (!pb_traces.ParseFromIstream(&*file)) { | ||
return std::nullopt; | ||
} | ||
|
||
return pb_traces; | ||
} | ||
|
||
[[nodiscard]] std::optional<ExecutionTraces> deserialize_traces_from_file(const boost::filesystem::path& filename) { | ||
const auto pb_traces = read_pb_traces_from_file(filename); | ||
if (!pb_traces) { | ||
return std::nullopt; | ||
} | ||
|
||
ExecutionTraces traces; | ||
|
||
// Convert stack operations | ||
traces.stack_ops.reserve(pb_traces->stack_ops_size()); | ||
for (const auto& pb_sop : pb_traces->stack_ops()) { | ||
traces.stack_ops.push_back(StackOp{ | ||
/*is_read=*/ pb_sop.is_read(), | ||
/*idx=*/ static_cast<int>(pb_sop.index()), | ||
/*value=*/ proto_uint256_to_zkevm_word(pb_sop.value()), | ||
/*op_code=*/ static_cast<std::byte>(pb_sop.op_code()) | ||
}); | ||
} | ||
|
||
// Convert memory operations | ||
traces.memory_ops.reserve(pb_traces->memory_ops_size()); | ||
for (const auto& pb_mop : pb_traces->memory_ops()) { | ||
traces.memory_ops.push_back(MemoryOp{ | ||
/*is_read=*/ pb_mop.is_read(), | ||
/*idx=*/ static_cast<int>(pb_mop.index()), | ||
/*value=*/ static_cast<std::byte>(pb_mop.value()[0]), | ||
/*op_code=*/ static_cast<std::byte>(pb_mop.op_code()) | ||
}); | ||
} | ||
|
||
// Convert storage proofs | ||
for (const auto& [addr_hex, pb_traces_addr] : pb_traces->storage_proofs_by_address()) { | ||
auto& proofs = traces.storage_proofs[addr_hex]; | ||
proofs.reserve(pb_traces_addr.proof_list_size()); | ||
|
||
for (const auto& pb_proof : pb_traces_addr.proof_list()) { | ||
proofs.push_back(proto_proof_to_cpp(pb_proof)); | ||
} | ||
} | ||
|
||
// Read executed op codes | ||
traces.executed_op_codes.reserve(pb_traces->executed_op_codes_size()); | ||
const auto& executed_op_codes = pb_traces->executed_op_codes(); | ||
std::transform(executed_op_codes.begin(), executed_op_codes.end(), traces.executed_op_codes.begin(), [](auto elem) { | ||
return static_cast<std::byte>(elem); | ||
}); | ||
|
||
return traces; | ||
} | ||
|
||
} // namespace proof_generator | ||
} // namespace nil |
Oops, something went wrong.