Skip to content

Commit

Permalink
proof-producer: rw & zkevm check fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaw committed Dec 10, 2024
1 parent eec3173 commit d0a06c5
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 120 deletions.
5 changes: 4 additions & 1 deletion crypto3/libs/blueprint/include/nil/blueprint/bbf/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#ifndef CRYPTO3_BLUEPRINT_PLONK_BBF_ENUMS_HPP
#define CRYPTO3_BLUEPRINT_PLONK_BBF_ENUMS_HPP

#include <ostream>
#include <map>

namespace nil {
namespace blueprint {
namespace bbf {
Expand All @@ -35,7 +38,7 @@ namespace nil {
enum column_type { witness = 0, public_input = 1, constant = 2, COLUMN_TYPES_COUNT = 3};

std::ostream &operator<<(std::ostream &os, const column_type &t) {
std::map<column_type, std::string> type_map = {
static std::map<column_type, std::string> type_map = {
{column_type::witness, "witness"},
{column_type::public_input, "public input"},
{column_type::constant, "constant"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace nil {

std::size_t call_id = 0;
std::size_t rw_counter = 0;
//_rw_operations.push_back(start_rw_operation());
for( auto &pt: pts){
boost::property_tree::ptree ptrace = pt.get_child("result.structLogs");
std::cout << "PT = " << ptrace.size() << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ namespace nil {
const zkevm_opcode_tester &tester
): rw_counter(1), transactions_amount(0){
// It may be done for multiple transactions;
// _rw_operations.push_back(start_rw_operation());
apply_tester(tester);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <numeric>
#include <algorithm>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_call_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <iostream>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>

namespace nil {
namespace blueprint {
namespace bbf{
template<typename FieldType>
class opcode_abstract;

// SHOULD NOT BE USED IN THE FINAL VERSION
template<typename FieldType>
class zkevm_dummy_operation : public opcode_abstract<FieldType> {
public:
virtual void fill_context(
typename generic_component<FieldType, GenerationStage::ASSIGNMENT>::context_type &context,
const opcode_input_type<FieldType, GenerationStage::ASSIGNMENT> &current_state
) override {
}
virtual void fill_context(
typename generic_component<FieldType, GenerationStage::CONSTRAINTS>::context_type &context,
const opcode_input_type<FieldType, GenerationStage::CONSTRAINTS> &current_state
) override {
std::cout << "DUMMY CONTSTRAINT, REPLACE ME WITH SOME ACTUAL IMPL" << std::endl;
}
virtual std::size_t rows_amount() override {
return 1;
}
};
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <numeric>
#include <algorithm>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_gas_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <numeric>
#include <algorithm>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_mcopy_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <numeric>
#include <algorithm>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_returndatacopy_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <numeric>
#include <algorithm>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_returndatasize_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <numeric>
#include <algorithm>

#include <nil/blueprint/zkevm/zkevm_word.hpp>
#include <nil/blueprint/zkevm_bbf/types/opcode.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/dummy.hpp>

namespace nil {
namespace blueprint {
namespace bbf{

template<typename FieldType>
using zkevm_staticcall_operation = zkevm_dummy_operation<FieldType>;
} // namespace bbf
} // namespace blueprint
} // namespace nil
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@
#include <nil/blueprint/zkevm_bbf/opcodes/eq.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/calldatacopy.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/stop.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/keccak.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/exp.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/keccak.hpp>

#include <nil/blueprint/zkevm_bbf/opcodes/mcopy.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/returndatasize.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/returndatacopy.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/call.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/staticcall.hpp>
#include <nil/blueprint/zkevm_bbf/opcodes/gas.hpp>

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -683,6 +690,14 @@ namespace nil {
opcodes[zkevm_opcode::POP] = std::make_shared<zkevm_pop_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::RETURN] = std::make_shared<zkevm_return_operation<BlueprintFieldType>>();

// not implemented yet opcodes
opcodes[zkevm_opcode::MCOPY] = std::make_shared<zkevm_mcopy_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::RETURNDATASIZE] = std::make_shared<zkevm_returndatasize_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::RETURNDATACOPY] = std::make_shared<zkevm_returndatacopy_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::CALL] = std::make_shared<zkevm_call_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::GAS] = std::make_shared<zkevm_gas_operation<BlueprintFieldType>>();
opcodes[zkevm_opcode::STATICCALL] = std::make_shared<zkevm_staticcall_operation<BlueprintFieldType>>();

// // DUP
opcodes[zkevm_opcode::DUP1] = std::make_shared<zkevm_dupx_operation<BlueprintFieldType>>(1);
opcodes[zkevm_opcode::DUP2] = std::make_shared<zkevm_dupx_operation<BlueprintFieldType>>(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,11 @@ namespace nil {
}

const auto rw_trace_path = get_rw_trace_path(trace_base_path);
const auto rw_operations = deserialize_rw_traces_from_file(rw_trace_path);
auto rw_operations = deserialize_rw_traces_from_file(rw_trace_path);
if (!rw_operations) {
return "can't read rw operations trace from file: " + rw_trace_path.string();
}
for (const auto& stack_op : rw_operations->stack_ops) {
input.rw_operations.push_back(stack_op);
}
for (const auto& memory_op : rw_operations->memory_ops) {
input.rw_operations.push_back(memory_op);
}
for (const auto& storage_op : rw_operations->storage_ops) {
input.rw_operations.push_back(storage_op);
}

input.rw_operations = std::move(rw_operations.value());

auto start = std::chrono::high_resolution_clock::now();
ComponentType instance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,14 @@ namespace nil {

typename nil::blueprint::bbf::context<BlueprintFieldType, nil::blueprint::bbf::GenerationStage::ASSIGNMENT> context_object(assignment_table, limits::max_rows);

nil::blueprint::bbf::rw_operations_vector input;
const auto rw_trace_path = get_rw_trace_path(trace_base_path);
const auto rw_operations = deserialize_rw_traces_from_file(rw_trace_path);
if (!rw_operations) {
auto input = deserialize_rw_traces_from_file(rw_trace_path);
if (!input) {
return "can't read rw from file: " + rw_trace_path.string();
}
for (const auto& stack_op : rw_operations->stack_ops) {
input.push_back(stack_op);
}
for (const auto& memory_op : rw_operations->memory_ops) {
input.push_back(memory_op);
}
for (const auto& storage_op : rw_operations->storage_ops) {
input.push_back(storage_op);
}
BOOST_LOG_TRIVIAL(debug) << "number RW operations " << input.size() << ":\n"
<< "stack " << rw_operations->stack_ops.size() << "\n"
<< "memory " << rw_operations->memory_ops.size() << "\n"
<< "storage " << rw_operations->storage_ops.size() << "\n";

auto start = std::chrono::high_resolution_clock::now();
ComponentType instance(context_object, std::move(input), limits::max_rw_size, limits::max_mpt_size);
ComponentType instance(context_object, input.value(), limits::max_rw_size, limits::max_mpt_size);
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
std::cout << "FILL ASSIGNMENT TABLE: " << duration.count() << "\n";
return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
namespace nil {
namespace proof_generator {

struct RWOperations {
std::vector<blueprint::bbf::rw_operation> stack_ops;
std::vector<blueprint::bbf::rw_operation> memory_ops;
std::vector<blueprint::bbf::rw_operation> storage_ops;
};


const char BYTECODE_EXTENSION[] = ".bc";
const char RW_EXTENSION[] = ".rw";
const char ZKEVM_EXTENSION[] = ".zkevm";
Expand Down Expand Up @@ -149,18 +144,18 @@ namespace nil {
return contract_bytecodes;
}

[[nodiscard]] std::optional<RWOperations> deserialize_rw_traces_from_file(const boost::filesystem::path& rw_traces_path) {
[[nodiscard]] std::optional<blueprint::bbf::rw_operations_vector> deserialize_rw_traces_from_file(const boost::filesystem::path& rw_traces_path) {
const auto pb_traces = read_pb_traces_from_file<executionproofs::RWTraces>(rw_traces_path);
if (!pb_traces) {
return std::nullopt;
}

RWOperations rw_traces;
blueprint::bbf::rw_operations_vector rw_traces;
rw_traces.reserve(pb_traces->stack_ops_size() + pb_traces->memory_ops_size() + pb_traces->storage_ops_size() + 1); // +1 slot for start op

// Convert stack operations
rw_traces.stack_ops.reserve(pb_traces->stack_ops_size());
for (const auto& pb_sop : pb_traces->stack_ops()) {
rw_traces.stack_ops.push_back(blueprint::bbf::stack_rw_operation(
rw_traces.push_back(blueprint::bbf::stack_rw_operation(
static_cast<uint64_t>(pb_sop.msg_id()),
static_cast<int32_t>(pb_sop.index()),
static_cast<uint64_t>(pb_sop.rw_idx()),
Expand All @@ -170,7 +165,6 @@ namespace nil {
}

// Convert memory operations
rw_traces.memory_ops.reserve(pb_traces->memory_ops_size());
for (const auto& pb_mop : pb_traces->memory_ops()) {
auto const value = string_to_bytes(pb_mop.value());
auto const op = blueprint::bbf::memory_rw_operation(
Expand All @@ -180,11 +174,10 @@ namespace nil {
!pb_mop.is_read(),
blueprint::zkevm_word_from_bytes(value)
);
rw_traces.memory_ops.push_back(op);
rw_traces.push_back(op);
}

// Convert storage operations
rw_traces.storage_ops.reserve(pb_traces->storage_ops_size());
for (const auto& pb_sop : pb_traces->storage_ops()) {
const auto& op = blueprint::bbf::storage_rw_operation(
static_cast<uint64_t>(pb_sop.msg_id()),
Expand All @@ -196,9 +189,20 @@ namespace nil {
blueprint::zkevm_word_from_string(pb_sop.address().address_bytes())
);
//TODO root and initial_root?
rw_traces.storage_ops.push_back(std::move(op));
rw_traces.push_back(std::move(op));
}

using nil::blueprint::bbf::rw_operation;
std::sort(rw_traces.begin(), rw_traces.end(), [](rw_operation a, rw_operation b){
return a < b;
});

BOOST_LOG_TRIVIAL(debug) << "number RW operations " << rw_traces.size() << ":\n"
<< "stack " << pb_traces->stack_ops_size() << "\n"
<< "memory " << pb_traces->memory_ops_size() << "\n"
<< "storage " << pb_traces->storage_ops_size() << "\n";


return rw_traces;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,11 @@ namespace nil {

// rw
const auto rw_trace_path = get_rw_trace_path(trace_base_path);
const auto rw_operations = deserialize_rw_traces_from_file(rw_trace_path);
auto rw_operations = deserialize_rw_traces_from_file(rw_trace_path);
if (!rw_operations) {
return "can't read rw from file: " + rw_trace_path.string();
}
for (const auto& stack_op : rw_operations->stack_ops) {
input.rw_operations.push_back(stack_op);
}
for (const auto& memory_op : rw_operations->memory_ops) {
input.rw_operations.push_back(memory_op);
}
for (const auto& storage_op : rw_operations->storage_ops) {
input.rw_operations.push_back(storage_op);
}

input.rw_operations = std::move(rw_operations.value());

// states
const auto zkevm_trace_path = get_zkevm_trace_path(trace_base_path);
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit d0a06c5

Please sign in to comment.