55#include " pubkey.h"
66
77#include < secp256k1.h>
8+ #include < secp256k1_rangeproof.h>
9+ #include < secp256k1_schnorr.h>
810
911secp256k1_context_t * secp256k1_bitcoin_verify_context = NULL ;
1012static secp256k1_context_t *& secp256k1_context = secp256k1_bitcoin_verify_context;
@@ -14,7 +16,10 @@ bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchS
1416 return false ;
1517 if (vchSig.size () != 64 )
1618 return false ;
17- if (secp256k1_schnorr_verify (secp256k1_context, (const unsigned char *)&hash, &vchSig[0 ], begin (), size ()) != 1 )
19+ secp256k1_pubkey_t pubkey;
20+ if (!secp256k1_ec_pubkey_parse (secp256k1_context, &pubkey, begin (), size ()))
21+ return false ;
22+ if (secp256k1_schnorr_verify (secp256k1_context, (const unsigned char *)&hash, &vchSig[0 ], &pubkey) != 1 )
1823 return false ;
1924 return true ;
2025}
@@ -24,28 +29,41 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha
2429 return false ;
2530 int recid = (vchSig[0 ] - 27 ) & 3 ;
2631 bool fComp = ((vchSig[0 ] - 27 ) & 4 ) != 0 ;
27- int pubkeylen = 65 ;
28- if (!secp256k1_ecdsa_recover_compact (secp256k1_context, (const unsigned char *)&hash, &vchSig[1 ], (unsigned char *)begin (), &pubkeylen, fComp , recid))
32+ secp256k1_pubkey_t pubkey;
33+ secp256k1_ecdsa_signature_t sig;
34+ if (!secp256k1_ecdsa_signature_parse_compact (secp256k1_context, &sig, &vchSig[1 ], recid)) {
35+ return false ;
36+ }
37+ if (!secp256k1_ecdsa_recover (secp256k1_context, hash.begin (), &sig, &pubkey)) {
2938 return false ;
30- assert ((int )size () == pubkeylen);
39+ }
40+ unsigned char pub[65 ];
41+ int publen = 0 ;
42+ secp256k1_ec_pubkey_serialize (secp256k1_context, pub, &publen, &pubkey, fComp );
43+ Set (pub, pub + publen);
3144 return true ;
3245}
3346
3447bool CPubKey::IsFullyValid () const {
3548 if (!IsValid ())
3649 return false ;
37- if (!secp256k1_ec_pubkey_verify (secp256k1_context, begin (), size ()))
50+ secp256k1_pubkey_t pubkey;
51+ if (!secp256k1_ec_pubkey_parse (secp256k1_context, &pubkey, begin (), size ()))
3852 return false ;
3953 return true ;
4054}
4155
4256bool CPubKey::Decompress () {
4357 if (!IsValid ())
4458 return false ;
45- int clen = size ();
46- int ret = secp256k1_ec_pubkey_decompress (secp256k1_context, (unsigned char *)begin (), &clen);
47- assert (ret);
48- assert (clen == (int )size ());
59+ secp256k1_pubkey_t pubkey;
60+ if (!secp256k1_ec_pubkey_parse (secp256k1_context, &pubkey, &(*this )[0 ], size ())) {
61+ return false ;
62+ }
63+ unsigned char pub[65 ];
64+ int publen = 0 ;
65+ secp256k1_ec_pubkey_serialize (secp256k1_context, pub, &publen, &pubkey, false );
66+ Set (pub, pub + publen);
4967 return true ;
5068}
5169
@@ -56,9 +74,18 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned i
5674 unsigned char out[64 ];
5775 BIP32Hash (cc, nChild, *begin (), begin ()+1 , out);
5876 memcpy (ccChild, out+32 , 32 );
59- pubkeyChild = *this ;
60- bool ret = secp256k1_ec_pubkey_tweak_add (secp256k1_context, (unsigned char *)pubkeyChild.begin (), pubkeyChild.size (), out);
61- return ret;
77+ secp256k1_pubkey_t pubkey;
78+ if (!secp256k1_ec_pubkey_parse (secp256k1_context, &pubkey, &(*this )[0 ], size ())) {
79+ return false ;
80+ }
81+ if (!secp256k1_ec_pubkey_tweak_add (secp256k1_context, &pubkey, out)) {
82+ return false ;
83+ }
84+ unsigned char pub[33 ];
85+ int publen = 0 ;
86+ secp256k1_ec_pubkey_serialize (secp256k1_context, pub, &publen, &pubkey, 1 );
87+ pubkeyChild.Set (pub, pub + publen);
88+ return true ;
6289}
6390
6491void CExtPubKey::Encode (unsigned char code[74 ]) const {
@@ -90,8 +117,10 @@ bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
90117void ECC_Verify_Start () {
91118 assert (secp256k1_context == NULL );
92119
93- secp256k1_context_t *ctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_COMMIT | SECP256K1_CONTEXT_RANGEPROOF );
120+ secp256k1_context_t *ctx = secp256k1_context_create (SECP256K1_CONTEXT_VERIFY);
94121 assert (ctx != NULL );
122+ secp256k1_pedersen_context_initialize (ctx);
123+ secp256k1_rangeproof_context_initialize (ctx);
95124
96125 secp256k1_context = ctx;
97126}
0 commit comments