Skip to content

Commit

Permalink
Check is_satisfied for bytecode circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
makxenov authored and akokoshn committed Aug 5, 2024
1 parent f55b0e6 commit 898da57
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
44 changes: 40 additions & 4 deletions bin/assigner/include/checks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/blueprint/utils/connectedness_check.hpp>
#include <nil/blueprint/utils/satisfiability_check.hpp>
#include <nil/blueprint/zkevm/bytecode.hpp>

template<typename BlueprintFieldType>
using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>;

template<typename BlueprintFieldType>
bool check_connectedness(
const nil::blueprint::circuit<
nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>& bp,
ArithmetizationType<BlueprintFieldType>>& bp,
const nil::blueprint::assignment<
nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>& assignment,
ArithmetizationType<BlueprintFieldType>>& assignment,
const std::vector<
nil::crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>>&
instance_input,
Expand Down Expand Up @@ -58,14 +62,46 @@ bool check_connectedness(
template<typename BlueprintFieldType>
bool is_satisfied(
const nil::blueprint::circuit<
nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>& bp,
ArithmetizationType<BlueprintFieldType>>& bp,
const nil::blueprint::assignment<
nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>& assignment) {
ArithmetizationType<BlueprintFieldType>>& assignment) {
if (!nil::blueprint::is_satisfied(bp, assignment)) {
std::cerr << "ERROR: is not satisfied\n";
return false;
}
return true;
}

template<typename BlueprintFieldType>
bool check_bytecode_satisfied(
nil::blueprint::assignment<ArithmetizationType<BlueprintFieldType>>& bytecode_table) {
using component_type =
nil::blueprint::components::zkevm_bytecode<ArithmetizationType<BlueprintFieldType>,
BlueprintFieldType>;

// Prepare witness container to make an instance of the component
typename component_type::manifest_type m = component_type::get_manifest();
size_t witness_amount = *(m.witness_amount->begin());
std::vector<std::uint32_t> witnesses(witness_amount);
for (std::uint32_t i = 0; i < witness_amount; i++) {
witnesses[i] = i;
}

constexpr size_t max_code_size = 24576;
component_type component_instance = component_type(
witnesses, std::array<std::uint32_t, 1>{0}, std::array<std::uint32_t, 1>{0}, max_code_size);

nil::blueprint::circuit<nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
bp;
auto lookup_tables = component_instance.component_lookup_tables();
for (auto& [k, v] : lookup_tables) {
bp.reserve_table(k);
}

// TODO: pass a proper public input here
typename component_type::input_type input({}, {}, typename component_type::var());
nil::blueprint::components::generate_circuit(component_instance, bp, bytecode_table, input, 0);
return ::is_satisfied(bp, bytecode_table);
}

#endif // ZKEMV_FRAMEWORK_LIBS_CHECKS_INCLUDE_CHECKS_HPP_
9 changes: 9 additions & 0 deletions bin/assigner/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <boost/log/trivial.hpp>
#include <boost/program_options.hpp>
#include <nil/blueprint/zkevm/bytecode.hpp>
#include <nil/crypto3/algebra/curves/bls12.hpp>
#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
Expand All @@ -23,6 +24,7 @@
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/params.hpp>

#include "assigner.hpp"
#include "checks.hpp"
#include "zkevm_framework/assigner_runner/runner.hpp"
#include "zkevm_framework/assigner_runner/state_parser.hpp"
Expand Down Expand Up @@ -83,6 +85,13 @@ int curve_dependent_main(const std::string& input_block_file_name,
std::cerr << "Check assignment tables failed" << std::endl;
return 1;
}*/

// Check if bytecode table is satisfied to the bytecode constraints
auto &bytecode_table = runner.get_assignments()[nil::evm_assigner::assigner<BlueprintFieldType>::BYTECODE_TABLE_INDEX];
if (!check_bytecode_satisfied<BlueprintFieldType>(bytecode_table)) {
std::cerr << "Bytecode table is not satisfied!" << std::endl;
return 1;
}
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class single_thread_runner {
std::optional<std::string> fill_assignments(const data_types::Block& input_block);

/// @brief Get reference to assignents
const std::vector<nil::blueprint::assignment<ArithmetizationType>>& get_assignments() const;
std::vector<nil::blueprint::assignment<ArithmetizationType>>& get_assignments();

private:
void initialize_assignments(const std::vector<std::array<std::size_t, 4>>& column_sizes);
Expand Down
8 changes: 4 additions & 4 deletions libs/assigner_runner/src/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ std::optional<std::string> single_thread_runner<BlueprintFieldType>::fill_assign

// create assigner instance
auto assigner_ptr =
std::make_shared<nil::blueprint::assigner<BlueprintFieldType>>(m_assignments);
std::make_shared<nil::evm_assigner::assigner<BlueprintFieldType>>(m_assignments);

// get header of the current block
const auto block_header = input_block.m_currentBlock;
Expand Down Expand Up @@ -217,7 +217,7 @@ std::optional<std::string> single_thread_runner<BlueprintFieldType>::fill_assign
<< " gas = " << transaction.m_gas << "\n"
<< " code size = " << transaction.m_data.size() << "\n";

auto res = nil::blueprint::evaluate(host_interface, ctx, rev, &msg, code.data(),
auto res = nil::evm_assigner::evaluate(host_interface, ctx, rev, &msg, code.data(),
code.size(), assigner_ptr, m_target_circuit);

BOOST_LOG_TRIVIAL(debug) << "evaluate result = " << to_str(res.status_code) << "\n";
Expand All @@ -232,9 +232,9 @@ std::optional<std::string> single_thread_runner<BlueprintFieldType>::fill_assign
}

template<typename BlueprintFieldType>
const std::vector<nil::blueprint::assignment<
std::vector<nil::blueprint::assignment<
typename single_thread_runner<BlueprintFieldType>::ArithmetizationType>>&
single_thread_runner<BlueprintFieldType>::get_assignments() const {
single_thread_runner<BlueprintFieldType>::get_assignments() {
return m_assignments;
}

Expand Down

0 comments on commit 898da57

Please sign in to comment.