diff options
| author | Bitflash-sh <btf01@proton.me> | 2026-07-24 11:18:11 -0300 |
|---|---|---|
| committer | Bitflash-sh <btf01@proton.me> | 2026-07-24 11:18:11 -0300 |
| commit | 51d617e70f90a972185917ea0096c1ecef2bfa8b (patch) | |
| tree | 9fbbcd867de9de9d0776afdd02f594ba312c46eb /src/key.h | |
Bitflash: fair-launch release
Satoshi's bitcoin-0.1.0 reborn with RandomX CPU proof of work, anonymous .btf
addressing over Nostr with CGNAT rendezvous, automatic peer discovery, a
headless Linux node, and Windows GUI wallet. Fair launch, no premine, 21M cap.
Diffstat (limited to 'src/key.h')
| -rw-r--r-- | src/key.h | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/key.h b/src/key.h new file mode 100644 index 0000000..26e1ae2 --- /dev/null +++ b/src/key.h | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | // Copyright (c) 2009 Satoshi Nakamoto | ||
| 2 | // Distributed under the MIT/X11 software license, see the accompanying | ||
| 3 | // file license.txt or http://www.opensource.org/licenses/mit-license.php. | ||
| 4 | |||
| 5 | |||
| 6 | // secp160k1 | ||
| 7 | // const unsigned int PRIVATE_KEY_SIZE = 192; | ||
| 8 | // const unsigned int PUBLIC_KEY_SIZE = 41; | ||
| 9 | // const unsigned int SIGNATURE_SIZE = 48; | ||
| 10 | // | ||
| 11 | // secp192k1 | ||
| 12 | // const unsigned int PRIVATE_KEY_SIZE = 222; | ||
| 13 | // const unsigned int PUBLIC_KEY_SIZE = 49; | ||
| 14 | // const unsigned int SIGNATURE_SIZE = 57; | ||
| 15 | // | ||
| 16 | // secp224k1 | ||
| 17 | // const unsigned int PRIVATE_KEY_SIZE = 250; | ||
| 18 | // const unsigned int PUBLIC_KEY_SIZE = 57; | ||
| 19 | // const unsigned int SIGNATURE_SIZE = 66; | ||
| 20 | // | ||
| 21 | // secp256k1: | ||
| 22 | // const unsigned int PRIVATE_KEY_SIZE = 279; | ||
| 23 | // const unsigned int PUBLIC_KEY_SIZE = 65; | ||
| 24 | // const unsigned int SIGNATURE_SIZE = 72; | ||
| 25 | // | ||
| 26 | // see www.keylength.com | ||
| 27 | // script supports up to 75 for single byte push | ||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | class key_error : public std::runtime_error | ||
| 32 | { | ||
| 33 | public: | ||
| 34 | explicit key_error(const std::string& str) : std::runtime_error(str) {} | ||
| 35 | }; | ||
| 36 | |||
| 37 | |||
| 38 | // secure_allocator is defined is serialize.h | ||
| 39 | typedef vector<unsigned char, secure_allocator<unsigned char> > CPrivKey; | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | class CKey | ||
| 44 | { | ||
| 45 | protected: | ||
| 46 | EC_KEY* pkey; | ||
| 47 | |||
| 48 | public: | ||
| 49 | CKey() | ||
| 50 | { | ||
| 51 | pkey = EC_KEY_new_by_curve_name(NID_secp256k1); | ||
| 52 | if (pkey == NULL) | ||
| 53 | throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed"); | ||
| 54 | } | ||
| 55 | |||
| 56 | CKey(const CKey& b) | ||
| 57 | { | ||
| 58 | pkey = EC_KEY_dup(b.pkey); | ||
| 59 | if (pkey == NULL) | ||
| 60 | throw key_error("CKey::CKey(const CKey&) : EC_KEY_dup failed"); | ||
| 61 | } | ||
| 62 | |||
| 63 | CKey& operator=(const CKey& b) | ||
| 64 | { | ||
| 65 | if (!EC_KEY_copy(pkey, b.pkey)) | ||
| 66 | throw key_error("CKey::operator=(const CKey&) : EC_KEY_copy failed"); | ||
| 67 | return (*this); | ||
| 68 | } | ||
| 69 | |||
| 70 | ~CKey() | ||
| 71 | { | ||
| 72 | EC_KEY_free(pkey); | ||
| 73 | } | ||
| 74 | |||
| 75 | void MakeNewKey() | ||
| 76 | { | ||
| 77 | if (!EC_KEY_generate_key(pkey)) | ||
| 78 | throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed"); | ||
| 79 | } | ||
| 80 | |||
| 81 | bool SetPrivKey(const CPrivKey& vchPrivKey) | ||
| 82 | { | ||
| 83 | const unsigned char* pbegin = &vchPrivKey[0]; | ||
| 84 | if (!d2i_ECPrivateKey(&pkey, &pbegin, vchPrivKey.size())) | ||
| 85 | return false; | ||
| 86 | return true; | ||
| 87 | } | ||
| 88 | |||
| 89 | CPrivKey GetPrivKey() const | ||
| 90 | { | ||
| 91 | unsigned int nSize = i2d_ECPrivateKey(pkey, NULL); | ||
| 92 | if (!nSize) | ||
| 93 | throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey failed"); | ||
| 94 | CPrivKey vchPrivKey(nSize, 0); | ||
| 95 | unsigned char* pbegin = &vchPrivKey[0]; | ||
| 96 | if (i2d_ECPrivateKey(pkey, &pbegin) != nSize) | ||
| 97 | throw key_error("CKey::GetPrivKey() : i2d_ECPrivateKey returned unexpected size"); | ||
| 98 | return vchPrivKey; | ||
| 99 | } | ||
| 100 | |||
| 101 | bool SetPubKey(const vector<unsigned char>& vchPubKey) | ||
| 102 | { | ||
| 103 | const unsigned char* pbegin = &vchPubKey[0]; | ||
| 104 | if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size())) | ||
| 105 | return false; | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | |||
| 109 | vector<unsigned char> GetPubKey() const | ||
| 110 | { | ||
| 111 | unsigned int nSize = i2o_ECPublicKey(pkey, NULL); | ||
| 112 | if (!nSize) | ||
| 113 | throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed"); | ||
| 114 | vector<unsigned char> vchPubKey(nSize, 0); | ||
| 115 | unsigned char* pbegin = &vchPubKey[0]; | ||
| 116 | if (i2o_ECPublicKey(pkey, &pbegin) != nSize) | ||
| 117 | throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size"); | ||
| 118 | return vchPubKey; | ||
| 119 | } | ||
| 120 | |||
| 121 | bool Sign(uint256 hash, vector<unsigned char>& vchSig) | ||
| 122 | { | ||
| 123 | vchSig.clear(); | ||
| 124 | unsigned char pchSig[10000]; | ||
| 125 | unsigned int nSize = 0; | ||
| 126 | if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey)) | ||
| 127 | return false; | ||
| 128 | vchSig.resize(nSize); | ||
| 129 | memcpy(&vchSig[0], pchSig, nSize); | ||
| 130 | return true; | ||
| 131 | } | ||
| 132 | |||
| 133 | bool Verify(uint256 hash, const vector<unsigned char>& vchSig) | ||
| 134 | { | ||
| 135 | // -1 = error, 0 = bad sig, 1 = good | ||
| 136 | if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1) | ||
| 137 | return false; | ||
| 138 | return true; | ||
| 139 | } | ||
| 140 | |||
| 141 | static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, vector<unsigned char>& vchSig) | ||
| 142 | { | ||
| 143 | CKey key; | ||
| 144 | if (!key.SetPrivKey(vchPrivKey)) | ||
| 145 | return false; | ||
| 146 | return key.Sign(hash, vchSig); | ||
| 147 | } | ||
| 148 | |||
| 149 | static bool Verify(const vector<unsigned char>& vchPubKey, uint256 hash, const vector<unsigned char>& vchSig) | ||
| 150 | { | ||
| 151 | CKey key; | ||
| 152 | if (!key.SetPubKey(vchPubKey)) | ||
| 153 | return false; | ||
| 154 | return key.Verify(hash, vchSig); | ||
| 155 | } | ||
| 156 | }; | ||