@@ -159,21 +159,21 @@ bool CKey::Check(const unsigned char *vch) {
159
159
}
160
160
161
161
void CKey::MakeNewKey (bool fCompressedIn ) {
162
+ MakeKeyData ();
162
163
do {
163
- GetStrongRandBytes (keydata);
164
- } while (!Check (keydata.data ()));
165
- fValid = true ;
164
+ GetStrongRandBytes (*keydata);
165
+ } while (!Check (keydata->data ()));
166
166
fCompressed = fCompressedIn ;
167
167
}
168
168
169
169
bool CKey::Negate ()
170
170
{
171
- assert (fValid );
172
- return secp256k1_ec_seckey_negate (secp256k1_context_sign, keydata. data ());
171
+ assert (keydata );
172
+ return secp256k1_ec_seckey_negate (secp256k1_context_sign, keydata-> data ());
173
173
}
174
174
175
175
CPrivKey CKey::GetPrivKey () const {
176
- assert (fValid );
176
+ assert (keydata );
177
177
CPrivKey seckey;
178
178
int ret;
179
179
size_t seckeylen;
@@ -186,7 +186,7 @@ CPrivKey CKey::GetPrivKey() const {
186
186
}
187
187
188
188
CPubKey CKey::GetPubKey () const {
189
- assert (fValid );
189
+ assert (keydata );
190
190
secp256k1_pubkey pubkey;
191
191
size_t clen = CPubKey::SIZE;
192
192
CPubKey result;
@@ -212,7 +212,7 @@ bool SigHasLowR(const secp256k1_ecdsa_signature* sig)
212
212
}
213
213
214
214
bool CKey::Sign (const uint256 &hash, std::vector<unsigned char >& vchSig, bool grind, uint32_t test_case) const {
215
- if (!fValid )
215
+ if (!keydata )
216
216
return false ;
217
217
vchSig.resize (CPubKey::SIGNATURE_SIZE);
218
218
size_t nSigLen = CPubKey::SIGNATURE_SIZE;
@@ -253,7 +253,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
253
253
}
254
254
255
255
bool CKey::SignCompact (const uint256 &hash, std::vector<unsigned char >& vchSig) const {
256
- if (!fValid )
256
+ if (!keydata )
257
257
return false ;
258
258
vchSig.resize (CPubKey::COMPACT_SIGNATURE_SIZE);
259
259
int rec = -1 ;
@@ -301,10 +301,12 @@ bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint2
301
301
}
302
302
303
303
bool CKey::Load (const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck =false ) {
304
- if (!ec_seckey_import_der (secp256k1_context_sign, (unsigned char *)begin (), seckey.data (), seckey.size ()))
304
+ MakeKeyData ();
305
+ if (!ec_seckey_import_der (secp256k1_context_sign, (unsigned char *)begin (), seckey.data (), seckey.size ())) {
306
+ ClearKeyData ();
305
307
return false ;
308
+ }
306
309
fCompressed = vchPubKey.IsCompressed ();
307
- fValid = true ;
308
310
309
311
if (fSkipCheck )
310
312
return true ;
@@ -325,22 +327,21 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
325
327
BIP32Hash (cc, nChild, 0 , begin (), vout.data ());
326
328
}
327
329
memcpy (ccChild.begin (), vout.data ()+32 , 32 );
328
- memcpy (( unsigned char *) keyChild.begin (), begin (), 32 );
330
+ keyChild.Set ( begin (), begin () + 32 , true );
329
331
bool ret = secp256k1_ec_seckey_tweak_add (secp256k1_context_sign, (unsigned char *)keyChild.begin (), vout.data ());
330
- keyChild.fCompressed = true ;
331
- keyChild.fValid = ret;
332
+ if (!ret) keyChild.ClearKeyData ();
332
333
return ret;
333
334
}
334
335
335
336
EllSwiftPubKey CKey::EllSwiftCreate (Span<const std::byte> ent32) const
336
337
{
337
- assert (fValid );
338
+ assert (keydata );
338
339
assert (ent32.size () == 32 );
339
340
std::array<std::byte, EllSwiftPubKey::size ()> encoded_pubkey;
340
341
341
342
auto success = secp256k1_ellswift_create (secp256k1_context_sign,
342
343
UCharCast (encoded_pubkey.data ()),
343
- keydata. data (),
344
+ keydata-> data (),
344
345
UCharCast (ent32.data ()));
345
346
346
347
// Should always succeed for valid keys (asserted above).
@@ -350,7 +351,7 @@ EllSwiftPubKey CKey::EllSwiftCreate(Span<const std::byte> ent32) const
350
351
351
352
ECDHSecret CKey::ComputeBIP324ECDHSecret (const EllSwiftPubKey& their_ellswift, const EllSwiftPubKey& our_ellswift, bool initiating) const
352
353
{
353
- assert (fValid );
354
+ assert (keydata );
354
355
355
356
ECDHSecret output;
356
357
// BIP324 uses the initiator as party A, and the responder as party B. Remap the inputs
@@ -359,7 +360,7 @@ ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, c
359
360
UCharCast (output.data ()),
360
361
UCharCast (initiating ? our_ellswift.data () : their_ellswift.data ()),
361
362
UCharCast (initiating ? their_ellswift.data () : our_ellswift.data ()),
362
- keydata. data (),
363
+ keydata-> data (),
363
364
initiating ? 0 : 1 ,
364
365
secp256k1_ellswift_xdh_hash_function_bip324,
365
366
nullptr );
0 commit comments