6
6
7
7
#include " crypto/sha2.h"
8
8
#include " eccryptoverify.h"
9
+ #include " pubkey.h"
9
10
#include " random.h"
10
11
11
12
#ifdef USE_SECP256K1
@@ -167,76 +168,6 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
167
168
return true ;
168
169
}
169
170
170
- bool CPubKey::Verify (const uint256 &hash, const std::vector<unsigned char >& vchSig) const {
171
- if (!IsValid ())
172
- return false ;
173
- #ifdef USE_SECP256K1
174
- if (secp256k1_ecdsa_verify ((const unsigned char *)&hash, 32 , &vchSig[0 ], vchSig.size (), begin (), size ()) != 1 )
175
- return false ;
176
- #else
177
- CECKey key;
178
- if (!key.SetPubKey (begin (), size ()))
179
- return false ;
180
- if (!key.Verify (hash, vchSig))
181
- return false ;
182
- #endif
183
- return true ;
184
- }
185
-
186
- bool CPubKey::RecoverCompact (const uint256 &hash, const std::vector<unsigned char >& vchSig) {
187
- if (vchSig.size () != 65 )
188
- return false ;
189
- int recid = (vchSig[0 ] - 27 ) & 3 ;
190
- bool fComp = ((vchSig[0 ] - 27 ) & 4 ) != 0 ;
191
- #ifdef USE_SECP256K1
192
- int pubkeylen = 65 ;
193
- if (!secp256k1_ecdsa_recover_compact ((const unsigned char *)&hash, 32 , &vchSig[1 ], (unsigned char *)begin (), &pubkeylen, fComp , recid))
194
- return false ;
195
- assert ((int )size () == pubkeylen);
196
- #else
197
- CECKey key;
198
- if (!key.Recover (hash, &vchSig[1 ], recid))
199
- return false ;
200
- std::vector<unsigned char > pubkey;
201
- key.GetPubKey (pubkey, fComp );
202
- Set (pubkey.begin (), pubkey.end ());
203
- #endif
204
- return true ;
205
- }
206
-
207
- bool CPubKey::IsFullyValid () const {
208
- if (!IsValid ())
209
- return false ;
210
- #ifdef USE_SECP256K1
211
- if (!secp256k1_ecdsa_pubkey_verify (begin (), size ()))
212
- return false ;
213
- #else
214
- CECKey key;
215
- if (!key.SetPubKey (begin (), size ()))
216
- return false ;
217
- #endif
218
- return true ;
219
- }
220
-
221
- bool CPubKey::Decompress () {
222
- if (!IsValid ())
223
- return false ;
224
- #ifdef USE_SECP256K1
225
- int clen = size ();
226
- int ret = secp256k1_ecdsa_pubkey_decompress ((unsigned char *)begin (), &clen);
227
- assert (ret);
228
- assert (clen == (int )size ());
229
- #else
230
- CECKey key;
231
- if (!key.SetPubKey (begin (), size ()))
232
- return false ;
233
- std::vector<unsigned char > pubkey;
234
- key.GetPubKey (pubkey, false );
235
- Set (pubkey.begin (), pubkey.end ());
236
- #endif
237
- return true ;
238
- }
239
-
240
171
bool CKey::Derive (CKey& keyChild, unsigned char ccChild[32 ], unsigned int nChild, const unsigned char cc[32 ]) const {
241
172
assert (IsValid ());
242
173
assert (IsCompressed ());
@@ -263,27 +194,6 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
263
194
return ret;
264
195
}
265
196
266
- bool CPubKey::Derive (CPubKey& pubkeyChild, unsigned char ccChild[32 ], unsigned int nChild, const unsigned char cc[32 ]) const {
267
- assert (IsValid ());
268
- assert ((nChild >> 31 ) == 0 );
269
- assert (begin () + 33 == end ());
270
- unsigned char out[64 ];
271
- BIP32Hash (cc, nChild, *begin (), begin ()+1 , out);
272
- memcpy (ccChild, out+32 , 32 );
273
- #ifdef USE_SECP256K1
274
- pubkeyChild = *this ;
275
- bool ret = secp256k1_ecdsa_pubkey_tweak_add ((unsigned char *)pubkeyChild.begin (), pubkeyChild.size (), out);
276
- #else
277
- CECKey key;
278
- bool ret = key.SetPubKey (begin (), size ());
279
- ret &= key.TweakPublic (out);
280
- std::vector<unsigned char > pubkey;
281
- key.GetPubKey (pubkey, true );
282
- pubkeyChild.Set (pubkey.begin (), pubkey.end ());
283
- #endif
284
- return ret;
285
- }
286
-
287
197
bool CExtKey::Derive (CExtKey &out, unsigned int nChild) const {
288
198
out.nDepth = nDepth + 1 ;
289
199
CKeyID id = key.GetPubKey ().GetID ();
@@ -334,32 +244,6 @@ void CExtKey::Decode(const unsigned char code[74]) {
334
244
key.Set (code+42 , code+74 , true );
335
245
}
336
246
337
- void CExtPubKey::Encode (unsigned char code[74 ]) const {
338
- code[0 ] = nDepth;
339
- memcpy (code+1 , vchFingerprint, 4 );
340
- code[5 ] = (nChild >> 24 ) & 0xFF ; code[6 ] = (nChild >> 16 ) & 0xFF ;
341
- code[7 ] = (nChild >> 8 ) & 0xFF ; code[8 ] = (nChild >> 0 ) & 0xFF ;
342
- memcpy (code+9 , vchChainCode, 32 );
343
- assert (pubkey.size () == 33 );
344
- memcpy (code+41 , pubkey.begin (), 33 );
345
- }
346
-
347
- void CExtPubKey::Decode (const unsigned char code[74 ]) {
348
- nDepth = code[0 ];
349
- memcpy (vchFingerprint, code+1 , 4 );
350
- nChild = (code[5 ] << 24 ) | (code[6 ] << 16 ) | (code[7 ] << 8 ) | code[8 ];
351
- memcpy (vchChainCode, code+9 , 32 );
352
- pubkey.Set (code+41 , code+74 );
353
- }
354
-
355
- bool CExtPubKey::Derive (CExtPubKey &out, unsigned int nChild) const {
356
- out.nDepth = nDepth + 1 ;
357
- CKeyID id = pubkey.GetID ();
358
- memcpy (&out.vchFingerprint [0 ], &id, 4 );
359
- out.nChild = nChild;
360
- return pubkey.Derive (out.pubkey , out.vchChainCode , nChild, vchChainCode);
361
- }
362
-
363
247
bool ECC_InitSanityCheck () {
364
248
#ifdef USE_SECP256K1
365
249
return true ;
0 commit comments