From d8d1347863d8a9abeef8f3173fa53b61ec915c05 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 7 Jun 2023 00:15:31 +0800 Subject: [PATCH] conditionally link against geth's libsecp256k1 --- restricted/crypto/secp256k1/ext.h | 4 + .../secp256k1/libsecp256k1/src/group_impl.h | 2 +- .../secp256k1/libsecp256k1/src/secp256k1.c | 64 +--------------- .../libsecp256k1/src/secp256k1_impl.h | 74 +++++++++++++++++++ restricted/crypto/secp256k1/panic_cb.go | 4 +- restricted/crypto/secp256k1/secp256.go | 13 +++- .../crypto/secp256k1/secp256_linkgeth.go | 14 ++++ 7 files changed, 105 insertions(+), 70 deletions(-) create mode 100644 restricted/crypto/secp256k1/libsecp256k1/src/secp256k1_impl.h create mode 100644 restricted/crypto/secp256k1/secp256_linkgeth.go diff --git a/restricted/crypto/secp256k1/ext.h b/restricted/crypto/secp256k1/ext.h index e422fe4..3f17dc9 100644 --- a/restricted/crypto/secp256k1/ext.h +++ b/restricted/crypto/secp256k1/ext.h @@ -98,6 +98,9 @@ static int secp256k1_ext_reencode_pubkey( // In: point: pointer to a 64-byte public point, // encoded as two 256bit big-endian numbers. // scalar: a 32-byte scalar with which to multiply the point +int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar); + +#ifndef PLUGETH_LINK_GETH int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) { int ret = 0; int overflow = 0; @@ -128,3 +131,4 @@ int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, secp256k1_scalar_clear(&s); return ret; } +#endif diff --git a/restricted/crypto/secp256k1/libsecp256k1/src/group_impl.h b/restricted/crypto/secp256k1/libsecp256k1/src/group_impl.h index 7d72353..dec54e4 100644 --- a/restricted/crypto/secp256k1/libsecp256k1/src/group_impl.h +++ b/restricted/crypto/secp256k1/libsecp256k1/src/group_impl.h @@ -68,7 +68,7 @@ static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST( 0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL ); -const int CURVE_B = 7; +static const int CURVE_B = 7; #endif static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) { diff --git a/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1.c b/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1.c index 7d637bf..794552e 100644 --- a/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1.c +++ b/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1.c @@ -4,26 +4,7 @@ * file COPYING or http://www.opensource.org/licenses/mit-license.php.* **********************************************************************/ -#include "include/secp256k1.h" - -#include "util.h" -#include "num_impl.h" -#include "field_impl.h" -#include "scalar_impl.h" -#include "group_impl.h" -#include "ecmult_impl.h" -#include "ecmult_const_impl.h" -#include "ecmult_gen_impl.h" -#include "ecdsa_impl.h" -#include "eckey_impl.h" -#include "hash_impl.h" - -#define ARG_CHECK(cond) do { \ - if (EXPECT(!(cond), 0)) { \ - secp256k1_callback_call(&ctx->illegal_callback, #cond); \ - return 0; \ - } \ -} while(0) +#include "secp256k1_impl.h" static void default_illegal_callback_fn(const char* str, void* data) { fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str); @@ -46,13 +27,6 @@ static const secp256k1_callback default_error_callback = { }; -struct secp256k1_context_struct { - secp256k1_ecmult_context ecmult_ctx; - secp256k1_ecmult_gen_context ecmult_gen_ctx; - secp256k1_callback illegal_callback; - secp256k1_callback error_callback; -}; - secp256k1_context* secp256k1_context_create(unsigned int flags) { secp256k1_context* ret = (secp256k1_context*)checked_malloc(&default_error_callback, sizeof(secp256k1_context)); ret->illegal_callback = default_illegal_callback; @@ -131,20 +105,6 @@ static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, return 1; } -static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) { - if (sizeof(secp256k1_ge_storage) == 64) { - secp256k1_ge_storage s; - secp256k1_ge_to_storage(&s, ge); - memcpy(&pubkey->data[0], &s, 64); - } else { - VERIFY_CHECK(!secp256k1_ge_is_infinity(ge)); - secp256k1_fe_normalize_var(&ge->x); - secp256k1_fe_normalize_var(&ge->y); - secp256k1_fe_get_b32(pubkey->data, &ge->x); - secp256k1_fe_get_b32(pubkey->data + 32, &ge->y); - } -} - int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) { secp256k1_ge Q; @@ -197,16 +157,6 @@ static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256 } } -static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) { - if (sizeof(secp256k1_scalar) == 32) { - memcpy(&sig->data[0], r, 32); - memcpy(&sig->data[32], s, 32); - } else { - secp256k1_scalar_get_b32(&sig->data[0], r); - secp256k1_scalar_get_b32(&sig->data[32], s); - } -} - int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) { secp256k1_scalar r, s; @@ -545,15 +495,3 @@ int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey * secp256k1_pubkey_save(pubnonce, &Q); return 1; } - -#ifdef ENABLE_MODULE_ECDH -# include "modules/ecdh/main_impl.h" -#endif - -#ifdef ENABLE_MODULE_SCHNORR -# include "modules/schnorr/main_impl.h" -#endif - -#ifdef ENABLE_MODULE_RECOVERY -# include "modules/recovery/main_impl.h" -#endif diff --git a/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1_impl.h b/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1_impl.h new file mode 100644 index 0000000..137fc08 --- /dev/null +++ b/restricted/crypto/secp256k1/libsecp256k1/src/secp256k1_impl.h @@ -0,0 +1,74 @@ +/********************************************************************** + * Copyright (c) 2013-2015 Pieter Wuille * + * Distributed under the MIT software license, see the accompanying * + * file COPYING or http://www.opensource.org/licenses/mit-license.php.* + **********************************************************************/ + +#ifndef _SECP256K1_IMPL_H_ +#define _SECP256K1_IMPL_H_ + +#include "include/secp256k1.h" + +#include "util.h" +#include "num_impl.h" +#include "field_impl.h" +#include "scalar_impl.h" +#include "group_impl.h" +#include "ecmult_impl.h" +#include "ecmult_const_impl.h" +#include "ecmult_gen_impl.h" +#include "ecdsa_impl.h" +#include "eckey_impl.h" +#include "hash_impl.h" + +#define ARG_CHECK(cond) do { \ + if (EXPECT(!(cond), 0)) { \ + secp256k1_callback_call(&ctx->illegal_callback, #cond); \ + return 0; \ + } \ +} while(0) + +struct secp256k1_context_struct { + secp256k1_ecmult_context ecmult_ctx; + secp256k1_ecmult_gen_context ecmult_gen_ctx; + secp256k1_callback illegal_callback; + secp256k1_callback error_callback; +}; + +static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) { + if (sizeof(secp256k1_scalar) == 32) { + memcpy(&sig->data[0], r, 32); + memcpy(&sig->data[32], s, 32); + } else { + secp256k1_scalar_get_b32(&sig->data[0], r); + secp256k1_scalar_get_b32(&sig->data[32], s); + } +} + +static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) { + if (sizeof(secp256k1_ge_storage) == 64) { + secp256k1_ge_storage s; + secp256k1_ge_to_storage(&s, ge); + memcpy(&pubkey->data[0], &s, 64); + } else { + VERIFY_CHECK(!secp256k1_ge_is_infinity(ge)); + secp256k1_fe_normalize_var(&ge->x); + secp256k1_fe_normalize_var(&ge->y); + secp256k1_fe_get_b32(pubkey->data, &ge->x); + secp256k1_fe_get_b32(pubkey->data + 32, &ge->y); + } +} + +#ifdef ENABLE_MODULE_ECDH +# include "modules/ecdh/main_impl.h" +#endif + +#ifdef ENABLE_MODULE_SCHNORR +# include "modules/schnorr/main_impl.h" +#endif + +#ifdef ENABLE_MODULE_RECOVERY +# include "modules/recovery/main_impl.h" +#endif + +#endif diff --git a/restricted/crypto/secp256k1/panic_cb.go b/restricted/crypto/secp256k1/panic_cb.go index 5da2bea..729e71a 100644 --- a/restricted/crypto/secp256k1/panic_cb.go +++ b/restricted/crypto/secp256k1/panic_cb.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be found in // the LICENSE file. -// +build !gofuzz -// +build cgo +//go:build !gofuzz && cgo && !linkgeth +// +build !gofuzz,cgo,!linkgeth package secp256k1 diff --git a/restricted/crypto/secp256k1/secp256.go b/restricted/crypto/secp256k1/secp256.go index a1bcf77..e395b7d 100644 --- a/restricted/crypto/secp256k1/secp256.go +++ b/restricted/crypto/secp256k1/secp256.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be found in // the LICENSE file. -// +build !gofuzz -// +build cgo +//go:build !gofuzz && cgo +// +build !gofuzz,cgo // Package secp256k1 wraps the bitcoin secp256k1 C library. package secp256k1 @@ -26,8 +26,13 @@ package secp256k1 #define USE_FIELD_INV_BUILTIN #define USE_SCALAR_INV_BUILTIN #define NDEBUG -#include "./libsecp256k1/src/secp256k1.c" -#include "./libsecp256k1/src/modules/recovery/main_impl.h" +#ifdef PLUGETH_LINK_GETH +# include "./libsecp256k1/src/secp256k1_impl.h" +# include "./libsecp256k1/include/secp256k1_recovery.h" +#else +# include "./libsecp256k1/src/secp256k1.c" +# include "./libsecp256k1/src/modules/recovery/main_impl.h" +#endif #include "ext.h" typedef void (*callbackFunc) (const char* msg, void* data); diff --git a/restricted/crypto/secp256k1/secp256_linkgeth.go b/restricted/crypto/secp256k1/secp256_linkgeth.go new file mode 100644 index 0000000..2e7cf4f --- /dev/null +++ b/restricted/crypto/secp256k1/secp256_linkgeth.go @@ -0,0 +1,14 @@ +// Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be found in +// the LICENSE file. + +//go:build !gofuzz && cgo && linkgeth +// +build !gofuzz,cgo,linkgeth + +// Package secp256k1 wraps the bitcoin secp256k1 C library. +package secp256k1 + +/* +#cgo CFLAGS: -DPLUGETH_LINK_GETH +*/ +import "C"