Skip to content

Commit c15046c

Browse files
authored
Merge pull request gridcoin-community#2655 from div72/update-libsecp256k1
build: update libsecp256k1 to v0.3.0
2 parents db0d3aa + c76ceab commit c15046c

File tree

98 files changed

+4958
-2456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4958
-2456
lines changed

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ PKGCONFIG_LIBDIR_TEMP="$PKG_CONFIG_LIBDIR"
13241324
unset PKG_CONFIG_LIBDIR
13251325
PKG_CONFIG_LIBDIR="$PKGCONFIG_LIBDIR_TEMP"
13261326

1327-
ac_configure_args="${ac_configure_args} --disable-shared --with-pic --with-bignum=no --enable-module-recovery"
1327+
ac_configure_args="${ac_configure_args} --disable-shared --with-pic --with-bignum=no --enable-module-recovery --disable-module-ecdh"
13281328

13291329
ADDITIONAL_BDB_FLAGS=""
13301330

src/init.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ extern constexpr int DEFAULT_WAIT_CLIENT_TIMEOUT = 0;
5757

5858
std::unique_ptr<BanMan> g_banman;
5959

60-
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
61-
6260
/**
6361
* The PID file facilities.
6462
*/
@@ -166,7 +164,6 @@ void Shutdown(void* parg)
166164
// This causes issues on daemons where it tries to create a second
167165
// lock file.
168166
//CTxDB().Close();
169-
globalVerifyHandle.reset();
170167
ECC_Stop();
171168
UninterruptibleSleep(std::chrono::milliseconds{50});
172169
LogPrintf("Gridcoin exited");
@@ -969,7 +966,6 @@ bool AppInit2(ThreadHandlerPtr threads)
969966
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
970967
RandomInit();
971968
ECC_Start();
972-
globalVerifyHandle.reset(new ECCVerifyHandle());
973969

974970
// Sanity check
975971
if (!InitSanityCheck())

src/key.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool gr
239239
secp256k1_pubkey pk;
240240
ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pk, begin());
241241
assert(ret);
242-
ret = secp256k1_ecdsa_verify(GetVerifyContext(), &sig, hash.begin(), &pk);
242+
ret = secp256k1_ecdsa_verify(secp256k1_context_static, &sig, hash.begin(), &pk);
243243
assert(ret);
244244
return true;
245245
}
@@ -274,9 +274,9 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
274274
secp256k1_pubkey epk, rpk;
275275
ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &epk, begin());
276276
assert(ret);
277-
ret = secp256k1_ecdsa_recover(GetVerifyContext(), &rpk, &rsig, hash.begin());
277+
ret = secp256k1_ecdsa_recover(secp256k1_context_static, &rpk, &rsig, hash.begin());
278278
assert(ret);
279-
ret = secp256k1_ec_pubkey_cmp(GetVerifyContext(), &epk, &rpk);
279+
ret = secp256k1_ec_pubkey_cmp(secp256k1_context_static, &epk, &rpk);
280280
assert(ret == 0);
281281
return true;
282282
}
@@ -372,7 +372,7 @@ bool ECC_InitSanityCheck() {
372372
void ECC_Start() {
373373
assert(secp256k1_context_sign == nullptr);
374374

375-
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
375+
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
376376
assert(ctx != nullptr);
377377

378378
{

src/pubkey.cpp

+28-54
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
#include <secp256k1_extrakeys.h>
1010
#include <secp256k1_recovery.h>
1111

12-
namespace
12+
namespace {
13+
14+
struct Secp256k1SelfTester
1315
{
14-
/* Global secp256k1_context object used for verification. */
15-
secp256k1_context* secp256k1_context_verify = nullptr;
16+
Secp256k1SelfTester() {
17+
/* Run libsecp256k1 self-test before using the secp256k1_context_static. */
18+
secp256k1_selftest();
19+
}
20+
} SECP256K1_SELFTESTER;
21+
1622
} // namespace
1723

1824
/** This function is taken from the libsecp256k1 distribution and implements
@@ -25,15 +31,15 @@ secp256k1_context* secp256k1_context_verify = nullptr;
2531
* strict DER before being passed to this module, and we know it supports all
2632
* violations present in the blockchain before that point.
2733
*/
28-
int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
34+
int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
2935
size_t rpos, rlen, spos, slen;
3036
size_t pos = 0;
3137
size_t lenbyte;
3238
unsigned char tmpsig[64] = {0};
3339
int overflow = 0;
3440

3541
/* Hack to initialize sig with a correctly-parsed but invalid signature. */
36-
secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
42+
secp256k1_ecdsa_signature_parse_compact(secp256k1_context_static, sig, tmpsig);
3743

3844
/* Sequence tag byte */
3945
if (pos == inputlen || input[pos] != 0x30) {
@@ -156,13 +162,13 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
156162
}
157163

158164
if (!overflow) {
159-
overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
165+
overflow = !secp256k1_ecdsa_signature_parse_compact(secp256k1_context_static, sig, tmpsig);
160166
}
161167
if (overflow) {
162168
/* Overwrite the result again with a correctly-parsed but invalid
163169
signature if parsing failed. */
164170
memset(tmpsig, 0, 64);
165-
secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
171+
secp256k1_ecdsa_signature_parse_compact(secp256k1_context_static, sig, tmpsig);
166172
}
167173
return 1;
168174
}
@@ -172,18 +178,17 @@ bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchS
172178
return false;
173179
secp256k1_pubkey pubkey;
174180
secp256k1_ecdsa_signature sig;
175-
assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
176-
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
181+
if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size())) {
177182
return false;
178183
}
179-
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
184+
if (!ecdsa_signature_parse_der_lax(&sig, vchSig.data(), vchSig.size())) {
180185
return false;
181186
}
182187
/* libsecp256k1's ECDSA verification requires lower-S signatures, which have
183188
* not historically been enforced in Bitcoin, so normalize them first. */
184189
// This however is not the case with Gridcoin.
185-
// secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, &sig, &sig);
186-
return secp256k1_ecdsa_verify(secp256k1_context_verify, &sig, hash.begin(), &pubkey);
190+
// secp256k1_ecdsa_signature_normalize(secp256k1_context_static, &sig, &sig);
191+
return secp256k1_ecdsa_verify(secp256k1_context_static, &sig, hash.begin(), &pubkey);
187192
}
188193

189194
bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
@@ -193,16 +198,15 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha
193198
bool fComp = ((vchSig[0] - 27) & 4) != 0;
194199
secp256k1_pubkey pubkey;
195200
secp256k1_ecdsa_recoverable_signature sig;
196-
assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
197-
if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_context_verify, &sig, &vchSig[1], recid)) {
201+
if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_context_static, &sig, &vchSig[1], recid)) {
198202
return false;
199203
}
200-
if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
204+
if (!secp256k1_ecdsa_recover(secp256k1_context_static, &pubkey, &sig, hash.begin())) {
201205
return false;
202206
}
203207
unsigned char pub[SIZE];
204208
size_t publen = SIZE;
205-
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
209+
secp256k1_ec_pubkey_serialize(secp256k1_context_static, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
206210
Set(pub, pub + publen);
207211
return true;
208212
}
@@ -211,21 +215,19 @@ bool CPubKey::IsFullyValid() const {
211215
if (!IsValid())
212216
return false;
213217
secp256k1_pubkey pubkey;
214-
assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
215-
return secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size());
218+
return secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size());
216219
}
217220

218221
bool CPubKey::Decompress() {
219222
if (!IsValid())
220223
return false;
221224
secp256k1_pubkey pubkey;
222-
assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
223-
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
225+
if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size())) {
224226
return false;
225227
}
226228
unsigned char pub[SIZE];
227229
size_t publen = SIZE;
228-
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
230+
secp256k1_ec_pubkey_serialize(secp256k1_context_static, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
229231
Set(pub, pub + publen);
230232
return true;
231233
}
@@ -238,16 +240,15 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
238240
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
239241
memcpy(ccChild.begin(), out+32, 32);
240242
secp256k1_pubkey pubkey;
241-
assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
242-
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
243+
if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size())) {
243244
return false;
244245
}
245-
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
246+
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_static, &pubkey, out)) {
246247
return false;
247248
}
248249
unsigned char pub[COMPRESSED_SIZE];
249250
size_t publen = COMPRESSED_SIZE;
250-
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
251+
secp256k1_ec_pubkey_serialize(secp256k1_context_static, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
251252
pubkeyChild.Set(pub, pub + publen);
252253
return true;
253254
}
@@ -292,35 +293,8 @@ bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
292293

293294
/* static */ bool CPubKey::CheckLowS(const std::vector<unsigned char>& vchSig) {
294295
secp256k1_ecdsa_signature sig;
295-
assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
296-
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
296+
if (!ecdsa_signature_parse_der_lax(&sig, vchSig.data(), vchSig.size())) {
297297
return false;
298298
}
299-
return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, nullptr, &sig));
300-
}
301-
302-
/* static */ int ECCVerifyHandle::refcount = 0;
303-
304-
ECCVerifyHandle::ECCVerifyHandle()
305-
{
306-
if (refcount == 0) {
307-
assert(secp256k1_context_verify == nullptr);
308-
secp256k1_context_verify = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
309-
assert(secp256k1_context_verify != nullptr);
310-
}
311-
refcount++;
312-
}
313-
314-
ECCVerifyHandle::~ECCVerifyHandle()
315-
{
316-
refcount--;
317-
if (refcount == 0) {
318-
assert(secp256k1_context_verify != nullptr);
319-
secp256k1_context_destroy(secp256k1_context_verify);
320-
secp256k1_context_verify = nullptr;
321-
}
322-
}
323-
324-
const secp256k1_context* GetVerifyContext() {
325-
return secp256k1_context_verify;
299+
return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_static, nullptr, &sig));
326300
}

src/pubkey.h

-17
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,4 @@ struct CExtPubKey {
258258
bool Derive(CExtPubKey& out, unsigned int nChild) const;
259259
};
260260

261-
/** Users of this module must hold an ECCVerifyHandle. The constructor and
262-
* destructor of these are not allowed to run in parallel, though. */
263-
class ECCVerifyHandle
264-
{
265-
static int refcount;
266-
267-
public:
268-
ECCVerifyHandle();
269-
~ECCVerifyHandle();
270-
};
271-
272-
typedef struct secp256k1_context_struct secp256k1_context;
273-
274-
/** Access to the internal secp256k1 context used for verification. Only intended to be used
275-
* by key.cpp. */
276-
const secp256k1_context* GetVerifyContext();
277-
278261
#endif // BITCOIN_PUBKEY_H

0 commit comments

Comments
 (0)