From 20eb80ca9d1783e484566324254f69ce26e5b6f8 Mon Sep 17 00:00:00 2001 From: timemarkovqtum Date: Thu, 26 Sep 2024 16:03:12 +0200 Subject: [PATCH] Add point evaluation --- src/Makefile.am | 2 ++ src/eth_client/libdevcrypto/LibKzg.cpp | 31 ++++++++++++++++++++++ src/eth_client/libdevcrypto/LibKzg.h | 13 +++++++++ src/eth_client/libethcore/Precompiled.cpp | 12 +++++++++ src/eth_client/libethereum/ChainParams.cpp | 2 ++ 5 files changed, 60 insertions(+) create mode 100644 src/eth_client/libdevcrypto/LibKzg.cpp create mode 100644 src/eth_client/libdevcrypto/LibKzg.h diff --git a/src/Makefile.am b/src/Makefile.am index 776cfd0341..c4cbe6d081 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -862,6 +862,8 @@ libbitcoin_common_a_SOURCES = \ eth_client/libdevcrypto/Hash.h \ eth_client/libdevcrypto/LibSnark.cpp \ eth_client/libdevcrypto/LibSnark.h \ + eth_client/libdevcrypto/LibKzg.cpp \ + eth_client/libdevcrypto/LibKzg.h \ eth_client/libethashseal/GenesisInfo.cpp \ eth_client/libethashseal/GenesisInfo.h \ eth_client/libethashseal/genesis/qtumNetwork.cpp \ diff --git a/src/eth_client/libdevcrypto/LibKzg.cpp b/src/eth_client/libdevcrypto/LibKzg.cpp new file mode 100644 index 0000000000..c04a66cc0d --- /dev/null +++ b/src/eth_client/libdevcrypto/LibKzg.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; +using namespace dev; +using namespace dev::crypto; + +pair dev::crypto::point_evaluation_execute(dev::bytesConstRef input) +{ + bytes output(64, 0); + size_t output_size = output.size(); + size_t input_size = input.size(); + assert(output_size >= 64); + if (input_size != 192) + return {false, bytes{}}; + + const auto r = evmone::crypto::kzg_verify_proof(reinterpret_cast(&input[0]), + reinterpret_cast(&input[32]), + reinterpret_cast(&input[64]), + reinterpret_cast(&input[96]), + reinterpret_cast(&input[96 + 48])); + + if (!r) + return {false, bytes{}}; + + // Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values + // as required by the EIP-4844. + intx::be::unsafe::store(output.data(), evmone::crypto::FIELD_ELEMENTS_PER_BLOB); + intx::be::unsafe::store(output.data() + 32, evmone::crypto::BLS_MODULUS); + return {true, output}; +} diff --git a/src/eth_client/libdevcrypto/LibKzg.h b/src/eth_client/libdevcrypto/LibKzg.h new file mode 100644 index 0000000000..6d2d76e09b --- /dev/null +++ b/src/eth_client/libdevcrypto/LibKzg.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace dev +{ +namespace crypto +{ + +std::pair point_evaluation_execute(bytesConstRef _in); + +} +} diff --git a/src/eth_client/libethcore/Precompiled.cpp b/src/eth_client/libethcore/Precompiled.cpp index dcc2c19bd2..099ae889c8 100644 --- a/src/eth_client/libethcore/Precompiled.cpp +++ b/src/eth_client/libethcore/Precompiled.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include using namespace std; @@ -320,4 +321,15 @@ ETH_REGISTER_PRECOMPILED_PRICER(blake2_compression) auto const rounds = fromBigEndian(_in.cropped(0, 4)); return rounds; } + +ETH_REGISTER_PRECOMPILED_PRICER(point_evaluation) +(bytesConstRef /*_in*/, ChainOperationParams const& /*_chainParams*/, u256 const& /*_blockNumber*/) +{ + return 50000; +} + +ETH_REGISTER_PRECOMPILED(point_evaluation)(bytesConstRef _in) +{ + return dev::crypto::point_evaluation_execute(_in); +} } diff --git a/src/eth_client/libethereum/ChainParams.cpp b/src/eth_client/libethereum/ChainParams.cpp index 2367a102a9..b7507024ce 100644 --- a/src/eth_client/libethereum/ChainParams.cpp +++ b/src/eth_client/libethereum/ChainParams.cpp @@ -166,6 +166,8 @@ void ChainParams::loadConfig( {Address{0x8}, PrecompiledContract{"alt_bn128_pairing_product", byzantiumForkBlock}}); precompiled.insert( {Address{0x9}, PrecompiledContract{"blake2_compression", istanbulForkBlock}}); + precompiled.insert( + {Address{0xa}, PrecompiledContract{"point_evaluation", cancunForkBlock}}); precompiled.insert({Address{0x85}, PrecompiledContract{"btc_ecrecover", qip6ForkBlock}}); stateRoot = _stateRoot ? _stateRoot : calculateStateRoot(true);