Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First dummy example of MUL opcode handling #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions cmake/cable/CableCompilerSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions lib/blueprint
Submodule blueprint added at 324d61
1 change: 1 addition & 0 deletions lib/crypto3
Submodule crypto3 added at a79aa5
4 changes: 2 additions & 2 deletions lib/evmone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${include_dir}>$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
${FPRINTER_DIR}
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions lib/evmone/execution_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include "instructions_opcodes.hpp"
#include <evmc/evmc.hpp>
#include <intx/intx.hpp>
#include <string>
#include <vector>

#include <nil/crypto3/zk/snark/arithmetization/plonk/params.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/blueprint/blueprint/plonk/assignment.hpp>

namespace evmone
{
namespace baseline
Expand All @@ -19,6 +25,32 @@ using uint256 = intx::uint256;
using bytes = std::basic_string<uint8_t>;
using bytes_view = std::basic_string_view<uint8_t>;

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<BlueprintFieldType>;
using var =
nil::crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>;
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<ArithmetizationType>;

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<AssignmentType> assignment_tables;
};


/// Provides memory for EVM stack.
class StackSpace
Expand Down Expand Up @@ -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 = {};

Expand Down Expand Up @@ -195,5 +229,18 @@ class ExecutionState
m_tx = host.get_tx_context();
return m_tx;
}

template <Opcode Op>
std::vector<var> get_assigner_input_variables(size_t table_idx)
{
// Default template specialization does nothing
return {};
}

template <Opcode Op>
void write_assignment_result(size_t table_idx, BlueprintFieldType::value_type value)
{
// Default template specialization does nothing
}
};
} // namespace evmone
23 changes: 21 additions & 2 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -137,6 +138,20 @@ inline bool check_memory(
return check_memory(gas_left, memory, offset, static_cast<uint64_t>(size));
}

// Template specializations for MUL opcode
template <>
inline std::vector<var> ExecutionState::get_assigner_input_variables<Opcode::OP_MUL>(size_t table_idx)
{
// TODO: Replace with meaningful input selection
return {var(), var()};
}

template <>
inline void ExecutionState::write_assignment_result<Opcode::OP_MUL>(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
{

Expand Down Expand Up @@ -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<var> inputs = state.get_assigner_input_variables<Opcode::OP_MUL>(table_idx);
auto mul = var_value(table, inputs[0]) * var_value(table, inputs[1]);
state.write_assignment_result<Opcode::OP_MUL>(table_idx, mul);
}

inline void sub(StackTop stack) noexcept
Expand Down
Loading