From 26c7bc6612102c0c238db953f393c0fd944c2d6c Mon Sep 17 00:00:00 2001 From: Mikhail Aksenov Date: Wed, 13 Mar 2024 16:16:53 +0200 Subject: [PATCH 1/2] Enable building with blueprint --- .gitmodules | 6 ++++++ cmake/cable/CableCompilerSettings.cmake | 6 ++++-- lib/CMakeLists.txt | 2 ++ lib/blueprint | 1 + lib/crypto3 | 1 + lib/evmone/CMakeLists.txt | 4 ++-- 6 files changed, 16 insertions(+), 4 deletions(-) create mode 160000 lib/blueprint create mode 160000 lib/crypto3 diff --git a/.gitmodules b/.gitmodules index 0f378cc0..b16c59af 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,9 @@ [submodule "evm-benchmarks"] path = test/evm-benchmarks url = https://github.com/ipsilon/evm-benchmarks +[submodule "lib/blueprint"] + path = lib/blueprint + url = ../../NilFoundation/zkllvm-blueprint.git +[submodule "lib/crypto3"] + path = lib/crypto3 + url = ../../NilFoundation/crypto3.git diff --git a/cmake/cable/CableCompilerSettings.cmake b/cmake/cable/CableCompilerSettings.cmake index d4a9c1af..e872fac6 100644 --- a/cmake/cable/CableCompilerSettings.cmake +++ b/cmake/cable/CableCompilerSettings.cmake @@ -84,8 +84,10 @@ macro(cable_configure_compiler) add_compile_options(-Wpedantic) endif() - # Enable basing warnings set and treat them as errors. - add_compile_options(-Werror -Wall -Wextra -Wshadow) + if (ENABLE_WARNINGS) + # Enable basing warnings set and treat them as errors. + add_compile_options(-Werror -Wall -Wextra -Wshadow) + endif() if(NOT cable_NO_CONVERSION_WARNINGS) # Enable conversion warnings if not explicitly disabled. diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 55231a2a..e16bf682 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -6,4 +6,6 @@ hunter_add_package(intx) find_package(intx CONFIG REQUIRED) add_subdirectory(fprinter) +add_subdirectory(crypto3) +add_subdirectory(blueprint) add_subdirectory(evmone) diff --git a/lib/blueprint b/lib/blueprint new file mode 160000 index 00000000..198325d8 --- /dev/null +++ b/lib/blueprint @@ -0,0 +1 @@ +Subproject commit 198325d8d0d19f1075b00a7475181d76cfb4c45a diff --git a/lib/crypto3 b/lib/crypto3 new file mode 160000 index 00000000..dc3900e3 --- /dev/null +++ b/lib/crypto3 @@ -0,0 +1 @@ +Subproject commit dc3900e3a57dfa497499d9fca25fa4e005f1a79b diff --git a/lib/evmone/CMakeLists.txt b/lib/evmone/CMakeLists.txt index d66bcd56..3600e094 100644 --- a/lib/evmone/CMakeLists.txt +++ b/lib/evmone/CMakeLists.txt @@ -30,7 +30,7 @@ add_library(evmone ${evmone_sources}) get_target_property(FPRINTER_DIR fprinter SOURCE_DIR) target_compile_features(evmone PUBLIC cxx_std_20) -target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE ethash::keccak) +target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE ethash::keccak crypto3::blueprint) target_include_directories(evmone PUBLIC $$ ${FPRINTER_DIR} @@ -42,7 +42,7 @@ if(EVMONE_X86_64_ARCH_LEVEL GREATER_EQUAL 2) set_source_files_properties(cpu_check.cpp PROPERTIES COMPILE_DEFINITIONS EVMONE_X86_64_ARCH_LEVEL=${EVMONE_X86_64_ARCH_LEVEL}) endif() -if(CABLE_COMPILER_GNULIKE) +if(CABLE_COMPILER_GNULIKE AND DISABLE_EXCEPTIONS) target_compile_options( evmone PRIVATE -fno-exceptions From e472514be5bcffaf484fa4521dfa6b46ecc27107 Mon Sep 17 00:00:00 2001 From: Mikhail Aksenov Date: Fri, 15 Mar 2024 14:32:52 +0200 Subject: [PATCH 2/2] First dummy example of MUL opcode handling --- lib/blueprint | 2 +- lib/crypto3 | 2 +- lib/evmone/execution_state.hpp | 47 ++++++++++++++++++++++++++++++++++ lib/evmone/instructions.hpp | 23 +++++++++++++++-- 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/lib/blueprint b/lib/blueprint index 198325d8..324d61a2 160000 --- a/lib/blueprint +++ b/lib/blueprint @@ -1 +1 @@ -Subproject commit 198325d8d0d19f1075b00a7475181d76cfb4c45a +Subproject commit 324d61a282324402f9c20f3ab06647e5f54412df diff --git a/lib/crypto3 b/lib/crypto3 index dc3900e3..a79aa50d 160000 --- a/lib/crypto3 +++ b/lib/crypto3 @@ -1 +1 @@ -Subproject commit dc3900e3a57dfa497499d9fca25fa4e005f1a79b +Subproject commit a79aa50d3d55a56847d380e98091924c8deb3c75 diff --git a/lib/evmone/execution_state.hpp b/lib/evmone/execution_state.hpp index 01e75539..4138d801 100644 --- a/lib/evmone/execution_state.hpp +++ b/lib/evmone/execution_state.hpp @@ -3,11 +3,17 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include "instructions_opcodes.hpp" #include #include #include #include +#include +#include +#include +#include + namespace evmone { namespace baseline @@ -19,6 +25,32 @@ using uint256 = intx::uint256; using bytes = std::basic_string; using bytes_view = std::basic_string_view; +using ArithmetizationParams = nil::crypto3::zk::snark::plonk_arithmetization_params; +using BlueprintFieldType = nil::crypto3::algebra::curves::pallas::base_field_type; +using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system; +using var = + nil::crypto3::zk::snark::plonk_variable; +constexpr size_t WitnessAmount = 5; +constexpr size_t PublicInputAmount = 2; +constexpr size_t ConstantAmount = 1; +constexpr size_t SelectorAmount = 30; +using AssignmentType = nil::blueprint::assignment; + +class EVMAssignerState +{ +public: + EVMAssignerState() + { + for (size_t i = 0; i < 30; ++i) + { + assignment_tables.emplace_back(WitnessAmount, PublicInputAmount, ConstantAmount, SelectorAmount); + } + + // Fill public input here + } + std::vector assignment_tables; +}; + /// Provides memory for EVM stack. class StackSpace @@ -138,6 +170,8 @@ class ExecutionState size_t output_offset = 0; size_t output_size = 0; + EVMAssignerState assigner_state; + private: evmc_tx_context m_tx = {}; @@ -195,5 +229,18 @@ class ExecutionState m_tx = host.get_tx_context(); return m_tx; } + + template + std::vector get_assigner_input_variables(size_t table_idx) + { + // Default template specialization does nothing + return {}; + } + + template + void write_assignment_result(size_t table_idx, BlueprintFieldType::value_type value) + { + // Default template specialization does nothing + } }; } // namespace evmone diff --git a/lib/evmone/instructions.hpp b/lib/evmone/instructions.hpp index 6229256e..d92d2639 100644 --- a/lib/evmone/instructions.hpp +++ b/lib/evmone/instructions.hpp @@ -15,6 +15,7 @@ namespace evmone { using code_iterator = const uint8_t*; +using nil::blueprint::var_value; /// Represents the pointer to the stack top item /// and allows retrieving stack items and manipulating the pointer. @@ -137,6 +138,20 @@ inline bool check_memory( return check_memory(gas_left, memory, offset, static_cast(size)); } +// Template specializations for MUL opcode +template <> +inline std::vector ExecutionState::get_assigner_input_variables(size_t table_idx) +{ + // TODO: Replace with meaningful input selection + return {var(), var()}; +} + +template <> +inline void ExecutionState::write_assignment_result(size_t table_idx, BlueprintFieldType::value_type value) { + // TODO: Replace with meaningful result var selection + assigner_state.assignment_tables[table_idx].witness(3, 4) = value; +} + namespace instr::core { @@ -166,9 +181,13 @@ inline void add(StackTop stack) noexcept stack.top() += stack.pop(); } -inline void mul(StackTop stack) noexcept +inline void mul(StackTop stack, ExecutionState& state) noexcept { - stack.top() *= stack.pop(); + size_t table_idx = 0; + AssignmentType &table = state.assigner_state.assignment_tables[table_idx]; + std::vector inputs = state.get_assigner_input_variables(table_idx); + auto mul = var_value(table, inputs[0]) * var_value(table, inputs[1]); + state.write_assignment_result(table_idx, mul); } inline void sub(StackTop stack) noexcept