Skip to content

Commit

Permalink
Use nil::evm_assigner namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
makxenov committed Jul 24, 2024
1 parent 16aaf3b commit f596f52
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 118 deletions.
8 changes: 4 additions & 4 deletions lib/assigner/evmone/baseline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ template <typename BlueprintFieldType>
struct Position
{
code_iterator code_it; ///< The position in the code.
nil::blueprint::zkevm_word<BlueprintFieldType>* stack_top; ///< The pointer to the stack top.
nil::evm_assigner::zkevm_word<BlueprintFieldType>* stack_top; ///< The pointer to the stack top.
};

/// Checks instruction requirements before execution.
Expand All @@ -96,8 +96,8 @@ struct Position
/// or EVMC_SUCCESS if everything is fine.
template <typename BlueprintFieldType>
inline evmc_status_code check_requirements(const CostTable& cost_table, int64_t& gas_left,
const nil::blueprint::zkevm_word<BlueprintFieldType>* stack_top,
const nil::blueprint::zkevm_word<BlueprintFieldType>* stack_bottom,
const nil::evm_assigner::zkevm_word<BlueprintFieldType>* stack_top,
const nil::evm_assigner::zkevm_word<BlueprintFieldType>* stack_bottom,
Opcode op) noexcept
{
assert(
Expand Down Expand Up @@ -196,7 +196,7 @@ template <typename BlueprintFieldType>
}
/// A helper to invoke the instruction implementation of the given opcode Op.
template <typename BlueprintFieldType>
[[release_inline]] inline Position<BlueprintFieldType> invoke(const CostTable& cost_table, const nil::blueprint::zkevm_word<BlueprintFieldType>* stack_bottom,
[[release_inline]] inline Position<BlueprintFieldType> invoke(const CostTable& cost_table, const nil::evm_assigner::zkevm_word<BlueprintFieldType>* stack_bottom,
Position<BlueprintFieldType> pos, int64_t& gas, ExecutionState<BlueprintFieldType>& state, const uint8_t& op) noexcept
{
if (const auto status = check_requirements<BlueprintFieldType>(cost_table, gas, pos.stack_top, stack_bottom, Opcode(op));
Expand Down
12 changes: 6 additions & 6 deletions lib/assigner/evmone/execution_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <rw.hpp>

namespace nil {
namespace blueprint {
namespace evm_assigner {
template<typename BlueprintFieldType>
struct assigner;
}
Expand All @@ -36,14 +36,14 @@ class StackSpace
static constexpr auto limit = 1024;

/// Returns the pointer to the "bottom", i.e. below the stack space.
[[nodiscard, clang::no_sanitize("bounds")]] nil::blueprint::zkevm_word<BlueprintFieldType>* bottom() noexcept
[[nodiscard, clang::no_sanitize("bounds")]] nil::evm_assigner::zkevm_word<BlueprintFieldType>* bottom() noexcept
{
return m_stack_space - 1;
}

private:
/// The storage allocated for maximum possible number of items.
nil::blueprint::zkevm_word<BlueprintFieldType> m_stack_space[limit];
nil::evm_assigner::zkevm_word<BlueprintFieldType> m_stack_space[limit];
};


Expand Down Expand Up @@ -146,8 +146,8 @@ class ExecutionState
size_t output_size = 0;

std::size_t call_id;
std::vector<nil::blueprint::rw_operation<BlueprintFieldType>> rw_trace;
std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> assigner;
std::vector<nil::evm_assigner::rw_operation<BlueprintFieldType>> rw_trace;
std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> assigner;

private:
evmc_tx_context m_tx = {};
Expand All @@ -171,7 +171,7 @@ class ExecutionState

ExecutionState(const evmc_message& message, evmc_revision revision,
const evmc_host_interface& host_interface, evmc_host_context* host_ctx, bytes_view _code,
bytes_view _data, size_t _call_id, std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> _assigner) noexcept
bytes_view _data, size_t _call_id, std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> _assigner) noexcept
: msg{&message},
host{host_interface, host_ctx},
rev{revision},
Expand Down
112 changes: 56 additions & 56 deletions lib/assigner/evmone/instructions.hpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/assigner/evmone/instructions_xmacro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@
ON_OPCODE_UNDEFINED(0x4f) \
\
ON_OPCODE_IDENTIFIER(OP_POP, instructions<BlueprintFieldType>::pop) \
ON_OPCODE_IDENTIFIER(OP_MLOAD, instructions<BlueprintFieldType>::template mload<typename nil::blueprint::zkevm_word<BlueprintFieldType>::value_type>) \
ON_OPCODE_IDENTIFIER(OP_MLOAD, instructions<BlueprintFieldType>::template mload<typename nil::evm_assigner::zkevm_word<BlueprintFieldType>::value_type>) \
ON_OPCODE_IDENTIFIER(OP_MLOAD8, instructions<BlueprintFieldType>::template mload<uint8_t>) \
ON_OPCODE_IDENTIFIER(OP_MLOAD16, instructions<BlueprintFieldType>::template mload<uint16_t>) \
ON_OPCODE_IDENTIFIER(OP_MLOAD32, instructions<BlueprintFieldType>::template mload<uint32_t>) \
ON_OPCODE_IDENTIFIER(OP_MLOAD64, instructions<BlueprintFieldType>::template mload<uint64_t>) \
ON_OPCODE_IDENTIFIER(OP_MSTORE, instructions<BlueprintFieldType>::template mstore<typename nil::blueprint::zkevm_word<BlueprintFieldType>::value_type>) \
ON_OPCODE_IDENTIFIER(OP_MSTORE, instructions<BlueprintFieldType>::template mstore<typename nil::evm_assigner::zkevm_word<BlueprintFieldType>::value_type>) \
ON_OPCODE_IDENTIFIER(OP_MSTORE8, instructions<BlueprintFieldType>::template mstore<uint8_t>) \
ON_OPCODE_IDENTIFIER(OP_MSTORE16, instructions<BlueprintFieldType>::template mstore<uint16_t>) \
ON_OPCODE_IDENTIFIER(OP_MSTORE32, instructions<BlueprintFieldType>::template mstore<uint32_t>) \
Expand Down
10 changes: 5 additions & 5 deletions lib/assigner/include/assigner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <rw.hpp>

namespace nil {
namespace blueprint {
namespace evm_assigner {

enum zkevm_circuit : uint8_t {
ALL = 0xFF,
Expand All @@ -45,7 +45,7 @@ namespace nil {
constexpr static size_t BYTECODE_TABLE_INDEX = 0;
constexpr static size_t RW_TABLE_INDEX = 1;

assigner(std::vector<assignment<ArithmetizationType>> &assignments): m_assignments(assignments) {}
assigner(std::vector<nil::blueprint::assignment<ArithmetizationType>> &assignments): m_assignments(assignments) {}

// TODO error handling
void handle_bytecode(size_t original_code_size, const uint8_t* code) {
Expand All @@ -59,13 +59,13 @@ namespace nil {
rw_trace, m_assignments[RW_TABLE_INDEX]);
}

std::vector<assignment<ArithmetizationType>> &m_assignments;
std::vector<nil::blueprint::assignment<ArithmetizationType>> &m_assignments;
};

template<typename BlueprintFieldType>
static evmc::Result evaluate(const evmc_host_interface* host, evmc_host_context* ctx,
evmc_revision rev, const evmc_message* msg, const uint8_t* code_ptr, size_t code_size,
std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> assigner, const std::string& target_circuit = "") {
std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> assigner, const std::string& target_circuit = "") {
if(zkevm_circuits_map.find(target_circuit) == zkevm_circuits_map.end()) {
std::cerr << "Unknown target circuit " << target_circuit << "\n";
return evmc::Result{EVMC_FAILURE, msg->gas};
Expand Down Expand Up @@ -109,7 +109,7 @@ namespace nil {
return evmc::Result{evmc_result};
}

} // namespace blueprint
} // namespace evm_assigner
} // namespace nil

#endif // EVM1_ASSIGNER_INCLUDE_NIL_BLUEPRINT_ASSIGNER_HPP_
6 changes: 3 additions & 3 deletions lib/assigner/include/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#include <nil/crypto3/random/algebraic_engine.hpp>

namespace nil {
namespace blueprint {
namespace evm_assigner {

template<typename BlueprintFieldType>
void process_bytecode_input(size_t original_code_size, const uint8_t* code,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bytecode_table) {
nil::blueprint::assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bytecode_table) {
using value_type = typename BlueprintFieldType::value_type;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;

Expand Down Expand Up @@ -106,7 +106,7 @@ namespace nil {
}
}

} // namespace blueprint
} // namespace evm_assigner
} // namespace nil

#endif // EVM1_ASSIGNER_INCLUDE_BYTECODE_HPP_
6 changes: 3 additions & 3 deletions lib/assigner/include/rw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <zkevm_word.hpp>

namespace nil {
namespace blueprint {
namespace evm_assigner {

template<typename BlueprintFieldType>
struct rw_operation{
Expand Down Expand Up @@ -120,7 +120,7 @@ namespace nil {

template<typename BlueprintFieldType>
void process_rw_operations(std::vector<rw_operation<BlueprintFieldType>>& rw_trace,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &rw_table) {
nil::blueprint::assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &rw_table) {
constexpr std::size_t OP = 0;
constexpr std::size_t ID = 1;
constexpr std::size_t ADDRESS = 2;
Expand Down Expand Up @@ -284,7 +284,7 @@ namespace nil {
}
}

} // namespace blueprint
} // namespace evm_assigner
} // namespace nil

#endif // EVM1_ASSIGNER_INCLUDE_RW_HPP_
28 changes: 14 additions & 14 deletions lib/assigner/include/vm_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class VMHost : public evmc::Host

public:
VMHost() = default;
explicit VMHost(evmc_tx_context& _tx_context, std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> _assigner, const std::string& _target_circuit = "") noexcept
explicit VMHost(evmc_tx_context& _tx_context, std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> _assigner, const std::string& _target_circuit = "") noexcept
: tx_context{_tx_context}, assigner{_assigner}, target_circuit{_target_circuit}
{}

VMHost(evmc_tx_context& _tx_context, evmc::accounts& _accounts, std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> _assigner, const std::string& _target_circuit = "") noexcept
VMHost(evmc_tx_context& _tx_context, evmc::accounts& _accounts, std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> _assigner, const std::string& _target_circuit = "") noexcept
: accounts{_accounts}, tx_context{_tx_context}, assigner{_assigner}, target_circuit{_target_circuit}
{}

Expand Down Expand Up @@ -215,7 +215,7 @@ class VMHost : public evmc::Host
}

private:
std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> assigner;
std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> assigner;
std::string target_circuit;

evmc::Result handle_call(const evmc_message& msg) {
Expand All @@ -234,18 +234,18 @@ class VMHost : public evmc::Host
}
auto& acc = accounts[msg.code_address];
if (msg.kind == EVMC_CALL) {
auto value_to_transfer = nil::blueprint::zkevm_word<BlueprintFieldType>(msg.value);
auto balance = nil::blueprint::zkevm_word<BlueprintFieldType>(sender_acc.balance);
auto value_to_transfer = nil::evm_assigner::zkevm_word<BlueprintFieldType>(msg.value);
auto balance = nil::evm_assigner::zkevm_word<BlueprintFieldType>(sender_acc.balance);
// Balance was already checked in evmone, so simply adjust it
sender_acc.balance = (balance - value_to_transfer).to_uint256be();
acc.balance = (value_to_transfer + nil::blueprint::zkevm_word<BlueprintFieldType>(acc.balance)).to_uint256be();
acc.balance = (value_to_transfer + nil::evm_assigner::zkevm_word<BlueprintFieldType>(acc.balance)).to_uint256be();
}
if (acc.code.empty())
{
return evmc::Result{EVMC_SUCCESS, msg.gas, 0, msg.input_data, msg.input_size};
}
// TODO: handle precompiled contracts
evmc::Result res = nil::blueprint::evaluate<BlueprintFieldType>(&get_interface(), to_context(),
evmc::Result res = nil::evm_assigner::evaluate<BlueprintFieldType>(&get_interface(), to_context(),
EVMC_LATEST_STABLE_REVISION, &msg, acc.code.data(), acc.code.size(), assigner, target_circuit);
return res;
}
Expand All @@ -267,7 +267,7 @@ class VMHost : public evmc::Host
init_msg.recipient = new_contract_address;
init_msg.sender = msg.sender;
init_msg.input_size = 0;
evmc::Result res = nil::blueprint::evaluate<BlueprintFieldType>(&get_interface(), to_context(),
evmc::Result res = nil::evm_assigner::evaluate<BlueprintFieldType>(&get_interface(), to_context(),
EVMC_LATEST_STABLE_REVISION, &init_msg, msg.input_data, msg.input_size, assigner, target_circuit);
if (res.status_code == EVMC_SUCCESS)
{
Expand All @@ -281,12 +281,12 @@ class VMHost : public evmc::Host
evmc::address calculate_address(const evmc_message& msg) {
// TODO: Implement for CREATE opcode, for now the result is only correct for CREATE2
// CREATE requires rlp encoding
auto seed = nil::blueprint::zkevm_word<BlueprintFieldType>(msg.create2_salt);
auto hash = nil::blueprint::zkevm_word<BlueprintFieldType>(ethash::keccak256(msg.input_data, msg.input_size));
auto sender = nil::blueprint::zkevm_word<BlueprintFieldType>(msg.sender);
auto sum = nil::blueprint::zkevm_word<BlueprintFieldType>(0xff) + seed + hash + sender;
auto seed = nil::evm_assigner::zkevm_word<BlueprintFieldType>(msg.create2_salt);
auto hash = nil::evm_assigner::zkevm_word<BlueprintFieldType>(ethash::keccak256(msg.input_data, msg.input_size));
auto sender = nil::evm_assigner::zkevm_word<BlueprintFieldType>(msg.sender);
auto sum = nil::evm_assigner::zkevm_word<BlueprintFieldType>(0xff) + seed + hash + sender;
auto rehash = ethash::keccak256(reinterpret_cast<const uint8_t*>(&sum.get_value()),
nil::blueprint::zkevm_word<BlueprintFieldType>::size);
nil::evm_assigner::zkevm_word<BlueprintFieldType>::size);
// Result address is the last 20 bytes of the hash
evmc::address res;
std::memcpy(res.bytes, rehash.bytes + 12, 20);
Expand All @@ -295,7 +295,7 @@ class VMHost : public evmc::Host
};

template<typename BlueprintFieldType>
evmc_host_context* vm_host_create_context(evmc_tx_context tx_context, std::shared_ptr<nil::blueprint::assigner<BlueprintFieldType>> assigner) {
evmc_host_context* vm_host_create_context(evmc_tx_context tx_context, std::shared_ptr<nil::evm_assigner::assigner<BlueprintFieldType>> assigner) {
auto host = new VMHost<BlueprintFieldType>{tx_context, assigner};
return host->to_context();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/assigner/include/zkevm_word.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>

namespace nil {
namespace blueprint {
namespace evm_assigner {

using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;
Expand Down Expand Up @@ -275,6 +275,6 @@ namespace nil {
}
return os;
}
} // namespace blueprint
} // namespace evm_assigner
} // namespace nil
#endif // EVM1_ASSIGNER_INCLUDE_NIL_BLUEPRINT_ZKEVM_WORD_HPP_
Loading

0 comments on commit f596f52

Please sign in to comment.