Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace old libsecp256k1 fork with upstream #19

Merged
merged 4 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "secp256k1/upstream"]
path = secp256k1/upstream
url = https://github.com/cryptonomex/secp256k1-zkp.git
[submodule "include/fc/crypto/webauthn_json"]
path = include/fc/crypto/webauthn_json
url = https://github.com/Tencent/rapidjson/
[submodule "secp256k1/secp256k1"]
path = secp256k1/secp256k1
url = https://github.com/bitcoin-core/secp256k1
64 changes: 0 additions & 64 deletions CMakeModules/FindGMP.cmake

This file was deleted.

40 changes: 0 additions & 40 deletions include/fc/crypto/elliptic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace fc {
typedef fc::array<char,65> public_key_point_data; ///< the full non-compressed version of the ECC point
typedef fc::array<char,72> signature;
typedef fc::array<unsigned char,65> compact_signature;
typedef std::vector<char> range_proof_type;
typedef fc::array<char,78> extended_key_data;
typedef fc::sha256 blinded_hash;
typedef fc::sha256 blind_signature;
Expand Down Expand Up @@ -53,11 +52,6 @@ namespace fc {
public_key child( const fc::sha256& offset )const;

bool valid()const;
/** Computes new pubkey = generator * offset + old pubkey ?! */
// public_key mult( const fc::sha256& offset )const;
/** Computes new pubkey = regenerate(offset).pubkey + old pubkey
* = offset * G + 1 * old pubkey ?! */
public_key add( const fc::sha256& offset )const;

public_key( public_key&& pk );
public_key& operator=( public_key&& pk );
Expand Down Expand Up @@ -150,39 +144,6 @@ namespace fc {
fc::fwd<detail::private_key_impl,32> my;
};

struct range_proof_info
{
int exp;
int mantissa;
uint64_t min_value;
uint64_t max_value;
};

commitment_type blind( const blind_factor_type& blind, uint64_t value );
blind_factor_type blind_sum( const std::vector<blind_factor_type>& blinds, uint32_t non_neg );
/** verifies taht commnits + neg_commits + excess == 0 */
bool verify_sum( const std::vector<commitment_type>& commits, const std::vector<commitment_type>& neg_commits, int64_t excess );
bool verify_range( uint64_t& min_val, uint64_t& max_val, const commitment_type& commit, const range_proof_type& proof );

range_proof_type range_proof_sign( uint64_t min_value,
const commitment_type& commit,
const blind_factor_type& commit_blind,
const blind_factor_type& nonce,
int8_t base10_exp,
uint8_t min_bits,
uint64_t actual_value
);

bool verify_range_proof_rewind( blind_factor_type& blind_out,
uint64_t& value_out,
string& message_out,
const blind_factor_type& nonce,
uint64_t& min_val,
uint64_t& max_val,
commitment_type commit,
const range_proof_type& proof );
range_proof_info range_get_info( const range_proof_type& proof );

/**
* Shims
*/
Expand Down Expand Up @@ -273,7 +234,6 @@ namespace fc {

FC_REFLECT_TYPENAME( fc::ecc::private_key )
FC_REFLECT_TYPENAME( fc::ecc::public_key )
FC_REFLECT( fc::ecc::range_proof_info, (exp)(mantissa)(min_value)(max_value) )
FC_REFLECT_DERIVED( fc::ecc::public_key_shim, (fc::crypto::shim<fc::ecc::public_key_data>), BOOST_PP_SEQ_NIL )
FC_REFLECT_DERIVED( fc::ecc::signature_shim, (fc::crypto::shim<fc::ecc::compact_signature>), BOOST_PP_SEQ_NIL )
FC_REFLECT_DERIVED( fc::ecc::private_key_shim, (fc::crypto::shim<fc::ecc::private_key_secret>), BOOST_PP_SEQ_NIL )
1 change: 0 additions & 1 deletion include/fc/crypto/elliptic_r1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace fc {
~public_key();
bool verify( const fc::sha256& digest, const signature& sig );
public_key_data serialize()const;
public_key_point_data serialize_ecc_point()const;

operator public_key_data()const { return serialize(); }

Expand Down
56 changes: 26 additions & 30 deletions secp256k1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
set(GMP_USAGES "On;Auto;Off")
set(GMP_USAGE "On" CACHE STRING "libgmp usage: On: required; Auto: used if present; Off: not used")
set_property(CACHE GMP_USAGE PROPERTY STRINGS ${GMP_USAGES})

if(NOT GMP_USAGE IN_LIST GMP_USAGES)
message(FATAL_ERROR "GMP_USAGE must be set to one of: ${GMP_USAGES}")
endif()

if(GMP_USAGE STREQUAL "On")
find_package(GMP REQUIRED)
elseif(GMP_USAGE)
find_package(GMP)
endif()

if(GMP_FOUND)
set(SECP256K1_CONFIG_INC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config_with_gmp")
else()
set(SECP256K1_CONFIG_INC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config_without_gmp")
unset(GMP_INCLUDE_DIR CACHE)
unset(GMP_LIBRARIES CACHE)
endif()
#the secp256k1-internal INTERFACE library is used to define some include paths & compile defs that are needed not just
# for compiling the library (where PRIVATE would have been fine), but also for the unit tests.
add_library(secp256k1-internal INTERFACE)

target_include_directories(secp256k1-internal
INTERFACE
secp256k1/src
config
)

target_compile_definitions(secp256k1-internal INTERFACE HAVE_CONFIG_H)

add_library(secp256k1 STATIC
upstream/src/secp256k1.c
secp256k1/src/secp256k1.c secp256k1/src/precomputed_ecmult.c secp256k1/src/precomputed_ecmult_gen.c
)

target_include_directories(secp256k1
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/upstream/
${CMAKE_CURRENT_SOURCE_DIR}/upstream/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/upstream/src
${SECP256K1_CONFIG_INC_DIR}
${GMP_INCLUDE_DIR}
secp256k1
secp256k1/include
)

target_compile_definitions(secp256k1 PRIVATE HAVE_CONFIG_H=1)
target_link_libraries(secp256k1 PRIVATE secp256k1-internal)

target_link_libraries(secp256k1 ${GMP_LIBRARIES})
install( TARGETS secp256k1
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
)

add_executable(secp256k1-bench secp256k1/src/bench.c)
target_link_libraries(secp256k1-bench secp256k1 secp256k1-internal)

add_executable(secp256k1-tests secp256k1/src/tests.c)
target_link_libraries(secp256k1-tests secp256k1 secp256k1-internal)
add_test(secp256k1-tests secp256k1-tests)

add_executable(secp256k1-exhaustive-tests secp256k1/src/tests_exhaustive.c)
target_link_libraries(secp256k1-exhaustive-tests secp256k1 secp256k1-internal)
add_test(secp256k1-exhaustive-tests secp256k1-exhaustive-tests)
11 changes: 11 additions & 0 deletions secp256k1/config/libsecp256k1-config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#define ENABLE_MODULE_RECOVERY 1

#define ECMULT_GEN_PREC_BITS 4
#define ECMULT_WINDOW_SIZE 15

//enable asm
#ifdef __x86_64__
#define USE_ASM_X86_64 1
#endif
21 changes: 0 additions & 21 deletions secp256k1/config_with_gmp/libsecp256k1-config.h

This file was deleted.

20 changes: 0 additions & 20 deletions secp256k1/config_without_gmp/libsecp256k1-config.h

This file was deleted.

1 change: 1 addition & 0 deletions secp256k1/secp256k1
Submodule secp256k1 added at 485f60
1 change: 0 additions & 1 deletion secp256k1/upstream
Submodule upstream deleted from bd0679
2 changes: 1 addition & 1 deletion src/crypto/_elliptic_impl_priv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace fc { namespace ecc { namespace detail {


const secp256k1_context_t* _get_context();
const secp256k1_context* _get_context();
void _init_lib();

class private_key_impl
Expand Down
9 changes: 0 additions & 9 deletions src/crypto/elliptic_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,6 @@ namespace fc { namespace ecc {
return public_key(data);
}

public_key public_key::child( const fc::sha256& offset )const
{
fc::sha256::encoder enc;
fc::raw::pack( enc, *this );
fc::raw::pack( enc, offset );

return add( enc.result() );
}

private_key private_key::child( const fc::sha256& offset )const
{
fc::sha256::encoder enc;
Expand Down
20 changes: 13 additions & 7 deletions src/crypto/elliptic_impl_priv.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <fc/fwd_impl.hpp>

#include <secp256k1.h>
#include <secp256k1_recovery.h>

#include "_elliptic_impl_priv.hpp"

Expand Down Expand Up @@ -71,30 +72,35 @@ namespace fc { namespace ecc {
{
FC_ASSERT( my->_key != empty_priv );
public_key_data pub;
unsigned int pk_len;
FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), (unsigned char*) pub.begin(), (int*) &pk_len, (unsigned char*) my->_key.data(), 1 ) );
FC_ASSERT( pk_len == pub.size() );
size_t pub_len = sizeof(pub);
secp256k1_pubkey secp_pub;
FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), &secp_pub, (unsigned char*) my->_key.data() ) );
secp256k1_ec_pubkey_serialize( detail::_get_context(), (unsigned char*)&pub, &pub_len, &secp_pub, SECP256K1_EC_COMPRESSED );
FC_ASSERT( pub_len == pub.size() );
return public_key(pub);
}

static int extended_nonce_function( unsigned char *nonce32, const unsigned char *msg32,
const unsigned char *key32, unsigned int attempt,
const void *data ) {
const unsigned char *key32, const unsigned char* algo16,
void* data, unsigned int attempt ) {
unsigned int* extra = (unsigned int*) data;
(*extra)++;
return secp256k1_nonce_function_default( nonce32, msg32, key32, *extra, nullptr );
return secp256k1_nonce_function_default( nonce32, msg32, key32, algo16, nullptr, *extra );
}

compact_signature private_key::sign_compact( const fc::sha256& digest, bool require_canonical )const
{
FC_ASSERT( my->_key != empty_priv );
compact_signature result;
secp256k1_ecdsa_recoverable_signature secp_sig;
int recid;
unsigned int counter = 0;
do
{
FC_ASSERT( secp256k1_ecdsa_sign_compact( detail::_get_context(), (unsigned char*) digest.data(), (unsigned char*) result.begin() + 1, (unsigned char*) my->_key.data(), extended_nonce_function, &counter, &recid ));
FC_ASSERT( secp256k1_ecdsa_sign_recoverable( detail::_get_context(), &secp_sig, (unsigned char*) digest.data(), (unsigned char*) my->_key.data(), extended_nonce_function, &counter ));
secp256k1_ecdsa_recoverable_signature_serialize_compact( detail::_get_context(), result.data + 1, &recid, &secp_sig);
} while( require_canonical && !public_key::is_canonical( result ) );

result.begin()[0] = 27 + 4 + recid;
return result;
}
Expand Down
9 changes: 0 additions & 9 deletions src/crypto/elliptic_r1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,6 @@ namespace fc { namespace crypto { namespace r1 {
EC_POINT_get_affine_coordinates_GFp( group, pub, self.my->_pub_x.get(), self.my->_pub_y.get(), nullptr );
*/
}
public_key_point_data public_key::serialize_ecc_point()const
{
public_key_point_data dat;
if( !my->_key ) return dat;
EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_UNCOMPRESSED );
char* front = &dat.data[0];
i2o_ECPublicKey( my->_key, (unsigned char**)&front );
return dat;
}

public_key::public_key()
{
Expand Down
Loading