4
4
5
5
#include " precompiles.hpp"
6
6
#include " precompiles_cache.hpp"
7
+ #include < evmone_precompiles/secp256k1.hpp>
7
8
#include < intx/intx.hpp>
8
9
#include < bit>
9
10
#include < cassert>
@@ -142,6 +143,37 @@ PrecompileAnalysis expmod_analyze(bytes_view input, evmc_revision rev) noexcept
142
143
static_cast <size_t >(mod_len)};
143
144
}
144
145
146
+ ExecutionResult ecrecover_execute (const uint8_t * input, size_t input_size, uint8_t * output,
147
+ [[maybe_unused]] size_t output_size) noexcept
148
+ {
149
+ assert (output_size >= 32 );
150
+
151
+ uint8_t input_buffer[128 ]{};
152
+ if (input_size != 0 )
153
+ std::memcpy (input_buffer, input, std::min (input_size, std::size (input_buffer)));
154
+
155
+ ethash::hash256 h{};
156
+ std::memcpy (h.bytes , input_buffer, sizeof (h));
157
+
158
+ const auto v = intx::be::unsafe::load<intx::uint256>(input_buffer + 32 );
159
+ if (v != 27 && v != 28 )
160
+ return {EVMC_SUCCESS, 0 };
161
+ const bool parity = v == 28 ;
162
+
163
+ const auto r = intx::be::unsafe::load<intx::uint256>(input_buffer + 64 );
164
+ const auto s = intx::be::unsafe::load<intx::uint256>(input_buffer + 96 );
165
+
166
+ const auto res = evmmax::secp256k1::ecrecover (h, r, s, parity);
167
+ if (res)
168
+ {
169
+ std::memset (output, 0 , 12 );
170
+ std::memcpy (output + 12 , res->bytes , 20 );
171
+ return {EVMC_SUCCESS, 32 };
172
+ }
173
+ else
174
+ return {EVMC_SUCCESS, 0 };
175
+ }
176
+
145
177
ExecutionResult identity_execute (const uint8_t * input, size_t input_size, uint8_t * output,
146
178
[[maybe_unused]] size_t output_size) noexcept
147
179
{
@@ -166,7 +198,7 @@ ExecutionResult dummy_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
166
198
inline constexpr auto traits = []() noexcept {
167
199
std::array<PrecompileTraits, NumPrecompiles> tbl{{
168
200
{}, // undefined for 0
169
- {ecrecover_analyze, dummy_execute<PrecompileId::ecrecover> },
201
+ {ecrecover_analyze, ecrecover_execute },
170
202
{sha256_analyze, dummy_execute<PrecompileId::sha256>},
171
203
{ripemd160_analyze, dummy_execute<PrecompileId::ripemd160>},
172
204
{identity_analyze, identity_execute},
@@ -177,7 +209,7 @@ inline constexpr auto traits = []() noexcept {
177
209
{blake2bf_analyze, dummy_execute<PrecompileId::blake2bf>},
178
210
}};
179
211
#ifdef EVMONE_PRECOMPILES_SILKPRE
180
- tbl[static_cast <size_t >(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
212
+ // tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
181
213
tbl[static_cast <size_t >(PrecompileId::sha256)].execute = silkpre_sha256_execute;
182
214
tbl[static_cast <size_t >(PrecompileId::ripemd160)].execute = silkpre_ripemd160_execute;
183
215
tbl[static_cast <size_t >(PrecompileId::expmod)].execute = silkpre_expmod_execute;
0 commit comments