From b0efef8ef1456a6eea6bbcfdf1004934b6849674 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sat, 26 Oct 2024 14:28:27 +0400 Subject: [PATCH] temporary disabled secp256k1 library --- CMakeLists.txt | 1 + contrib/CMakeLists.txt | 26 ++++--- src/CMakeLists.txt | 9 ++- src/crypto/eth_signature.cpp | 101 ++++++++++++++++++-------- src/crypto/eth_signature.h | 2 +- tests/core_tests/multiassets_test.cpp | 2 +- 6 files changed, 96 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b07daa1d..099c1fc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(USE_PCH FALSE CACHE BOOL "Use shared precompiled headers") set(DISABLE_TOR FALSE CACHE BOOL "Disable TOR library(and related tor-connect submodule)") set(TESTNET FALSE CACHE BOOL "Compile for testnet") set(BUILD_GUI FALSE CACHE BOOL "Build qt-daemon") +set(USE_BITCOIN_SECP256K1_FOR_ECDSA FALSE CACHE BOOL "Use bitcoin-secp256k1 library for validating ECDSA(instead of OpenSSL)") include_directories(src contrib/eos_portable_archive contrib contrib/epee/include contrib/jwt-cpp/include ${OPENSSL_INCLUDE_DIR} "${CMAKE_BINARY_DIR}/version" "${CMAKE_BINARY_DIR}/contrib/zlib") diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt index 122b0c89..842f905c 100644 --- a/contrib/CMakeLists.txt +++ b/contrib/CMakeLists.txt @@ -5,18 +5,22 @@ add_subdirectory(zlib) add_subdirectory(db) add_subdirectory(ethereum) -option(SECP256K1_BUILD_BENCHMARK "Build benchmarks." OFF) -option(SECP256K1_BUILD_TESTS "Build tests." OFF) -option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." OFF) -option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." OFF) -option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF) -set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1) +if(USE_BITCOIN_SECP256K1_FOR_ECDSA) + option(SECP256K1_BUILD_BENCHMARK "Build benchmarks." OFF) + option(SECP256K1_BUILD_TESTS "Build tests." OFF) + option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." OFF) + option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." OFF) + option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF) + set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1) -if(STATIC) - set(SECP256K1_DISABLE_SHARED ON CACHE BOOL "Disable shared library for secp256k1") - set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build static libraries by default" FORCE) + if(STATIC) + set(SECP256K1_DISABLE_SHARED ON CACHE BOOL "Disable shared library for secp256k1") + set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build static libraries by default" FORCE) + endif() + add_subdirectory(bitcoin-secp256k1) + set_property(TARGET secp256k1 PROPERTY FOLDER "contrib") + set_property(TARGET secp256k1_precomputed PROPERTY FOLDER "contrib") endif() -add_subdirectory(bitcoin-secp256k1) if( NOT DISABLE_TOR) add_subdirectory(tor-connect) @@ -36,8 +40,6 @@ set_property(TARGET libminiupnpc-static PROPERTY FOLDER "contrib") set_property(TARGET zlibstatic PROPERTY FOLDER "contrib") set_property(TARGET mdbx PROPERTY FOLDER "contrib") set_property(TARGET lmdb PROPERTY FOLDER "contrib") -set_property(TARGET secp256k1 PROPERTY FOLDER "contrib") -set_property(TARGET secp256k1_precomputed PROPERTY FOLDER "contrib") if( NOT DISABLE_TOR) set_property(TARGET tor-connect PROPERTY FOLDER "contrib") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 563b4266..4505b8f8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -116,8 +116,13 @@ else() endif() add_library(crypto ${CRYPTO}) -add_dependencies(crypto secp256k1) -target_link_libraries(crypto secp256k1) +if(USE_BITCOIN_SECP256K1_FOR_ECDSA) + add_dependencies(crypto secp256k1) + target_link_libraries(crypto secp256k1) +else() + add_dependencies(crypto OpenSSL::Crypto) + target_link_libraries(crypto OpenSSL::Crypto) +endif() add_library(currency_core ${CURRENCY_CORE}) add_dependencies(currency_core version ${PCH_LIB_NAME}) diff --git a/src/crypto/eth_signature.cpp b/src/crypto/eth_signature.cpp index 5d70b11a..972eab3b 100644 --- a/src/crypto/eth_signature.cpp +++ b/src/crypto/eth_signature.cpp @@ -3,25 +3,25 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "eth_signature.h" #include "crypto.h" -#include "bitcoin-secp256k1/include/secp256k1.h" +#ifndef USE_OPEN_SSL_FOR_ECDSA + #include "bitcoin-secp256k1/include/secp256k1.h" +#endif #include "random.h" #include "misc_language.h" #include -#define USE_OPEN_SSL_FOR_ETH +#define USE_OPEN_SSL_FOR_ECDSA -#ifdef USE_OPEN_SSL_FOR_ETH -#include -#include -#include -#include +#ifdef USE_OPEN_SSL_FOR_ECDSA + #include + #include + #include + #include + #include #endif - - - // Function to create EC_KEY from raw 32 - byte private key EC_KEY * create_ec_key_from_private_key(const unsigned char* private_key) { EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1); @@ -100,24 +100,10 @@ bool generate_ethereum_signature(const unsigned char* hash, const unsigned char* BIGNUM* s_canonical = BN_dup(s); ensure_canonical_s(s_canonical, EC_KEY_get0_group(ec_key)); - //std::vector r_bytes(32); - //std::vector s_bytes(32); - - //BN_bn2binpad(r, r_bytes.data(), 32); - //BN_bn2binpad(s_canonical, s_bytes.data(), 32); BN_bn2binpad(r, (unsigned char* )&sig_res.data[0], 32); BN_bn2binpad(s_canonical, (unsigned char*)&sig_res.data[32], 32); - - // To determine the recovery ID (v), you'd need to use custom logic to determine this. - //unsigned char v = 27; // Placeholder - - //std::vector eth_signature(65); - //std::copy(r_bytes.begin(), r_bytes.end(), eth_signature.begin()); - //std::copy(s_bytes.begin(), s_bytes.end(), eth_signature.begin() + 32); - //eth_signature[64] = v; - ECDSA_SIG_free(sig); BN_free(s_canonical); EC_KEY_free(ec_key); @@ -192,19 +178,72 @@ bool verify_ethereum_signature(const crypto::hash& m, const crypto::eth_signatur ECDSA_SIG_free(sig); EC_KEY_free(ec_key); - //BN_free(bn_r); - //BN_free(bn_s); + return verification_result == 1; } +// +// struct KeyPair { +// std::vector private_key; // 32 bytes +// std::vector public_key; // 33 bytes (compressed format) +// }; + +// Function to generate an Ethereum-compatible key pair +bool generate_ethereum_key_pair(crypto::eth_secret_key& sec_key, crypto::eth_public_key& pub_key) { + /*KeyPair keypair;*/ + + // Create a new EC_KEY object with the secp256k1 curve + EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1); + if (!key) { + throw std::runtime_error("Failed to create new EC_KEY object"); + } + + // Generate the key pair + if (EC_KEY_generate_key(key) == 0) { + EC_KEY_free(key); + throw std::runtime_error("Failed to generate key pair"); + } + + // Extract the private key + const BIGNUM* priv_bn = EC_KEY_get0_private_key(key); + if (!priv_bn) { + EC_KEY_free(key); + throw std::runtime_error("Failed to get private key"); + } + + BN_bn2binpad(priv_bn, (unsigned char*)&sec_key.data[0], 32); + + // Extract the public key in compressed format + const EC_POINT* pub_point = EC_KEY_get0_public_key(key); + if (!pub_point) { + EC_KEY_free(key); + throw std::runtime_error("Failed to get public key"); + } + + //keypair.public_key.resize(33); // Compressed format + if (EC_POINT_point2oct(EC_KEY_get0_group(key), pub_point, POINT_CONVERSION_COMPRESSED, + (unsigned char*)&pub_key.data[0], sizeof(pub_key.data), nullptr) == 0) { + EC_KEY_free(key); + throw std::runtime_error("Failed to convert public key to compressed format"); + } + + EC_KEY_free(key); + return true; +} + + + + + namespace crypto { bool generate_eth_key_pair(eth_secret_key& sec_key, eth_public_key& pub_key) noexcept { try { +#ifndef USE_OPEN_SSL_FOR_ECDSA secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE); auto slh = epee::misc_utils::create_scope_leave_handler([&ctx](){ secp256k1_context_destroy(ctx); @@ -234,6 +273,9 @@ namespace crypto return false; return true; +#else + return generate_ethereum_key_pair(sec_key, pub_key); +#endif } catch(...) { @@ -241,6 +283,7 @@ namespace crypto } } +#ifndef USE_OPEN_SSL_FOR_ECDSA bool eth_secret_key_to_public_key(const eth_secret_key& sec_key, eth_public_key& pub_key) noexcept { try @@ -267,13 +310,13 @@ namespace crypto return false; } } - +#endif // generates secp256k1 ECDSA signature bool generate_eth_signature(const hash& m, const eth_secret_key& sec_key, eth_signature& sig) noexcept { try { -#ifndef USE_OPEN_SSL_FOR_ETH___ +#ifndef USE_OPEN_SSL_FOR_ECDSA secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE); auto slh = epee::misc_utils::create_scope_leave_handler([&ctx](){ secp256k1_context_destroy(ctx); @@ -309,7 +352,7 @@ namespace crypto try { // TODO (performance) consider using secp256k1_context_static for verification -- sowle -#ifndef USE_OPEN_SSL_FOR_ETH +#ifndef USE_OPEN_SSL_FOR_ECDSA secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE); auto slh = epee::misc_utils::create_scope_leave_handler([&ctx](){ secp256k1_context_destroy(ctx); diff --git a/src/crypto/eth_signature.h b/src/crypto/eth_signature.h index b12d41fe..4db0b8e7 100644 --- a/src/crypto/eth_signature.h +++ b/src/crypto/eth_signature.h @@ -31,7 +31,7 @@ namespace crypto bool generate_eth_key_pair(eth_secret_key& sec_key, eth_public_key& pub_key) noexcept; // converts eth_secret_key to eth_public_key - bool eth_secret_key_to_public_key(const eth_secret_key& sec_key, eth_public_key& pub_key) noexcept; + //bool _eth_secret_key_to_public_key(const eth_secret_key& sec_key, eth_public_key& pub_key) noexcept; // generates secp256k1 ECDSA signature bool generate_eth_signature(const hash& m, const eth_secret_key& sec_key, eth_signature& sig) noexcept; diff --git a/tests/core_tests/multiassets_test.cpp b/tests/core_tests/multiassets_test.cpp index f8948e08..a82e2afc 100644 --- a/tests/core_tests/multiassets_test.cpp +++ b/tests/core_tests/multiassets_test.cpp @@ -1490,7 +1490,7 @@ bool eth_signed_asset_basics::c1(currency::core& c, size_t ev_index, const std:: CHECK_AND_ASSERT_MES(r, false, "generate_eth_signature failed"); r = crypto::verify_eth_signature(ft.tx_id, eth_pk, eth_sig); CHECK_AND_ASSERT_MES(r, false, "generate_eth_signature self validation failed"); - + transaction emit_tx{}; bool transfers_unlocked = false; miner_wlt->submit_externally_signed_asset_tx(ft, eth_sig, true, emit_tx, transfers_unlocked);