14
14
#include < secp256k1.h>
15
15
#include " ecwrapper.h"
16
16
17
- // ! anonymous namespace
18
- namespace {
19
-
20
- class CSecp256k1Init {
21
- public:
22
- CSecp256k1Init () {
23
- secp256k1_start (SECP256K1_START_SIGN);
24
- }
25
- ~CSecp256k1Init () {
26
- secp256k1_stop ();
27
- }
28
- };
29
- static CSecp256k1Init instance_of_csecp256k1;
30
-
31
- } // anon namespace
17
+ static secp256k1_context_t * secp256k1_context = NULL ;
32
18
33
19
bool CKey::Check (const unsigned char *vch) {
34
20
return eccrypto::Check (vch);
@@ -44,7 +30,7 @@ void CKey::MakeNewKey(bool fCompressedIn) {
44
30
}
45
31
46
32
bool CKey::SetPrivKey (const CPrivKey &privkey, bool fCompressedIn ) {
47
- if (!secp256k1_ec_privkey_import ((unsigned char *)begin (), &privkey[0 ], privkey.size ()))
33
+ if (!secp256k1_ec_privkey_import (secp256k1_context, (unsigned char *)begin (), &privkey[0 ], privkey.size ()))
48
34
return false ;
49
35
fCompressed = fCompressedIn ;
50
36
fValid = true ;
@@ -57,7 +43,7 @@ CPrivKey CKey::GetPrivKey() const {
57
43
int privkeylen, ret;
58
44
privkey.resize (279 );
59
45
privkeylen = 279 ;
60
- ret = secp256k1_ec_privkey_export (begin (), (unsigned char *)&privkey[0 ], &privkeylen, fCompressed );
46
+ ret = secp256k1_ec_privkey_export (secp256k1_context, begin (), (unsigned char *)&privkey[0 ], &privkeylen, fCompressed );
61
47
assert (ret);
62
48
privkey.resize (privkeylen);
63
49
return privkey;
@@ -67,7 +53,7 @@ CPubKey CKey::GetPubKey() const {
67
53
assert (fValid );
68
54
CPubKey result;
69
55
int clen = 65 ;
70
- int ret = secp256k1_ec_pubkey_create ((unsigned char *)result.begin (), &clen, begin (), fCompressed );
56
+ int ret = secp256k1_ec_pubkey_create (secp256k1_context, (unsigned char *)result.begin (), &clen, begin (), fCompressed );
71
57
assert ((int )result.size () == clen);
72
58
assert (ret);
73
59
assert (result.IsValid ());
@@ -81,7 +67,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_
81
67
int nSigLen = 72 ;
82
68
unsigned char extra_entropy[32 ] = {0 };
83
69
WriteLE32 (extra_entropy, test_case);
84
- int ret = secp256k1_ecdsa_sign (hash.begin (), (unsigned char *)&vchSig[0 ], &nSigLen, begin (), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL );
70
+ int ret = secp256k1_ecdsa_sign (secp256k1_context, hash.begin (), (unsigned char *)&vchSig[0 ], &nSigLen, begin (), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL );
85
71
assert (ret);
86
72
vchSig.resize (nSigLen);
87
73
return true ;
@@ -106,15 +92,15 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
106
92
return false ;
107
93
vchSig.resize (65 );
108
94
int rec = -1 ;
109
- int ret = secp256k1_ecdsa_sign_compact (hash.begin (), &vchSig[1 ], begin (), secp256k1_nonce_function_rfc6979, NULL , &rec);
95
+ int ret = secp256k1_ecdsa_sign_compact (secp256k1_context, hash.begin (), &vchSig[1 ], begin (), secp256k1_nonce_function_rfc6979, NULL , &rec);
110
96
assert (ret);
111
97
assert (rec != -1 );
112
98
vchSig[0 ] = 27 + rec + (fCompressed ? 4 : 0 );
113
99
return true ;
114
100
}
115
101
116
102
bool CKey::Load (CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck =false ) {
117
- if (!secp256k1_ec_privkey_import ((unsigned char *)begin (), &privkey[0 ], privkey.size ()))
103
+ if (!secp256k1_ec_privkey_import (secp256k1_context, (unsigned char *)begin (), &privkey[0 ], privkey.size ()))
118
104
return false ;
119
105
fCompressed = vchPubKey.IsCompressed ();
120
106
fValid = true ;
@@ -140,7 +126,7 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
140
126
}
141
127
memcpy (ccChild, out+32 , 32 );
142
128
memcpy ((unsigned char *)keyChild.begin (), begin (), 32 );
143
- bool ret = secp256k1_ec_privkey_tweak_add ((unsigned char *)keyChild.begin (), out);
129
+ bool ret = secp256k1_ec_privkey_tweak_add (secp256k1_context, (unsigned char *)keyChild.begin (), out);
144
130
UnlockObject (out);
145
131
keyChild.fCompressed = true ;
146
132
keyChild.fValid = ret;
@@ -206,3 +192,32 @@ bool ECC_InitSanityCheck() {
206
192
CPubKey pubkey = key.GetPubKey ();
207
193
return key.VerifyPubKey (pubkey);
208
194
}
195
+
196
+
197
+ void ECC_Start () {
198
+ assert (secp256k1_context == NULL );
199
+
200
+ secp256k1_context_t *ctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN);
201
+ assert (ctx != NULL );
202
+
203
+ {
204
+ // Pass in a random blinding seed to the secp256k1 context.
205
+ unsigned char seed[32 ];
206
+ LockObject (seed);
207
+ GetRandBytes (seed, 32 );
208
+ bool ret = secp256k1_context_randomize (ctx, seed);
209
+ assert (ret);
210
+ UnlockObject (seed);
211
+ }
212
+
213
+ secp256k1_context = ctx;
214
+ }
215
+
216
+ void ECC_Stop () {
217
+ secp256k1_context_t *ctx = secp256k1_context;
218
+ secp256k1_context = NULL ;
219
+
220
+ if (ctx) {
221
+ secp256k1_context_destroy (ctx);
222
+ }
223
+ }
0 commit comments