Skip to content

Commit

Permalink
precompiles: Add bls precompiles IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Sep 5, 2024
1 parent 6974fd7 commit 83bc986
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 19 deletions.
127 changes: 114 additions & 13 deletions test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,60 @@ PrecompileAnalysis point_evaluation_analyze(bytes_view, evmc_revision) noexcept
return {POINT_EVALUATION_PRECOMPILE_GAS, 64};
}

PrecompileAnalysis bls12_g1add_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_g1mul_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_g1msm_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_g2add_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_g2mul_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_g2msm_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_pairing_check_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_map_fp_to_g1_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

PrecompileAnalysis bls12_map_fp2_to_g2_analyze(bytes_view, evmc_revision) noexcept
{
// TODO: Implement
return {GasCostMax, 0};
}

ExecutionResult ecrecover_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
Expand Down Expand Up @@ -292,6 +346,51 @@ ExecutionResult blake2bf_execute(const uint8_t* input, [[maybe_unused]] size_t i
return {EVMC_SUCCESS, sizeof(h)};
}

ExecutionResult bls12_g1add_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_g1mul_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_g1msm_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_g2add_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_g2mul_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_g2msm_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_pairing_check_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_map_fp_to_g1_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

ExecutionResult bls12_map_fp2_to_g2_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
{
return {EVMC_PRECOMPILE_FAILURE, 0};
}

namespace
{
struct PrecompileTraits
Expand All @@ -301,19 +400,18 @@ struct PrecompileTraits
};

inline constexpr auto traits = []() noexcept {
std::array<PrecompileTraits, NumPrecompiles> tbl{{
{}, // undefined for 0
{ecrecover_analyze, ecrecover_execute},
{sha256_analyze, sha256_execute},
{ripemd160_analyze, ripemd160_execute},
{identity_analyze, identity_execute},
{expmod_analyze, expmod_stub},
{ecadd_analyze, ecadd_execute},
{ecmul_analyze, ecmul_execute},
{ecpairing_analyze, ecpairing_stub},
{blake2bf_analyze, blake2bf_execute},
{point_evaluation_analyze, point_evaluation_stub},
}};
std::array<PrecompileTraits, NumPrecompiles> tbl{{{}, // undefined for 0
{ecrecover_analyze, ecrecover_execute}, {sha256_analyze, sha256_execute},
{ripemd160_analyze, ripemd160_execute}, {identity_analyze, identity_execute},
{expmod_analyze, expmod_stub}, {ecadd_analyze, ecadd_execute},
{ecmul_analyze, ecmul_execute}, {ecpairing_analyze, ecpairing_stub},
{blake2bf_analyze, blake2bf_execute}, {point_evaluation_analyze, point_evaluation_stub},
{bls12_g1add_analyze, bls12_g1add_execute}, {bls12_g1mul_analyze, bls12_g1mul_execute},
{bls12_g1msm_analyze, bls12_g1msm_execute}, {bls12_g2add_analyze, bls12_g2add_execute},
{bls12_g2mul_analyze, bls12_g2mul_execute}, {bls12_g2msm_analyze, bls12_g2msm_execute},
{bls12_pairing_check_analyze, bls12_pairing_check_execute},
{bls12_map_fp_to_g1_analyze, bls12_map_fp_to_g1_execute},
{bls12_map_fp2_to_g2_analyze, bls12_map_fp2_to_g2_execute}}};
#ifdef EVMONE_PRECOMPILES_SILKPRE
// tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
// tbl[static_cast<size_t>(PrecompileId::sha256)].execute = silkpre_sha256_execute;
Expand Down Expand Up @@ -348,6 +446,9 @@ bool is_precompile(evmc_revision rev, const evmc::address& addr) noexcept
if (rev < EVMC_CANCUN && id >= stdx::to_underlying(PrecompileId::since_cancun))
return false;

if (rev < EVMC_PRAGUE && id >= stdx::to_underlying(PrecompileId::since_prague))
return false;

return true;
}

Expand Down
12 changes: 11 additions & 1 deletion test/state/precompiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ enum class PrecompileId : uint8_t
ecpairing = 0x08,
blake2bf = 0x09,
point_evaluation = 0x0a,
bls12_g1add = 0x0b,
bls12_g1mul = 0x0c,
bls12_g1msm = 0x0d,
bls12_g2add = 0x0e,
bls12_g2mul = 0x0f,
bls12_g2msm = 0x10,
bls12_pairing_check = 0x11,
bls12_map_fp_to_g1 = 0x12,
bls12_map_fp2_to_g2 = 0x13,

since_byzantium = expmod, ///< The first precompile introduced in Byzantium.
since_istanbul = blake2bf, ///< The first precompile introduced in Istanbul.
since_cancun = point_evaluation, ///< The first precompile introduced in Cancun.
latest = point_evaluation ///< The latest introduced precompile (highest address).
since_prague = bls12_g1add, ///< The first precompile introduced in Prague.
latest = bls12_map_fp2_to_g2 ///< The latest introduced precompile (highest address).
};

/// The total number of known precompiles ids, including 0.
Expand Down
27 changes: 27 additions & 0 deletions test/state/precompiles_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ PrecompileAnalysis ecmul_analyze(evmc::bytes_view input, evmc_revision rev) noex
PrecompileAnalysis ecpairing_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis blake2bf_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis point_evaluation_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_g1add_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_g1mul_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_g1msm_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_g2add_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_g2mul_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_g2msm_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_pairing_check_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_map_fp_to_g1_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
PrecompileAnalysis bls12_map_fp2_to_g2_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;

ExecutionResult ecrecover_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
Expand All @@ -44,4 +53,22 @@ ExecutionResult ecmul_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult blake2bf_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_g1add_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_g1mul_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_g1msm_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_g2add_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_g2mul_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_g2msm_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_pairing_check_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_map_fp_to_g1_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult bls12_map_fp2_to_g2_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
} // namespace evmone::state
20 changes: 15 additions & 5 deletions test/unittests/state_precompiles_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ TEST(state_precompiles, is_precompile)
// Cancun:
EXPECT_EQ(is_precompile(rev, 0x0a_address), rev >= EVMC_CANCUN);

// Prague:
EXPECT_EQ(is_precompile(rev, 0x0b_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x0c_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x0d_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x0e_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x0f_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x10_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x11_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x12_address), rev >= EVMC_PRAGUE);
EXPECT_EQ(is_precompile(rev, 0x13_address), rev >= EVMC_PRAGUE);

// Future?
EXPECT_FALSE(is_precompile(rev, 0x0b_address));
EXPECT_FALSE(is_precompile(rev, 0x0c_address));
EXPECT_FALSE(is_precompile(rev, 0x0d_address));
EXPECT_FALSE(is_precompile(rev, 0x0e_address));
EXPECT_FALSE(is_precompile(rev, 0x0f_address));
EXPECT_FALSE(is_precompile(rev, 0x14_address));
EXPECT_FALSE(is_precompile(rev, 0x15_address));
EXPECT_FALSE(is_precompile(rev, 0x16_address));
EXPECT_FALSE(is_precompile(rev, 0x17_address));
}
}

0 comments on commit 83bc986

Please sign in to comment.