1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
11 #include "allocators.h"
14 #include <openssl/ec.h> // for EC_KEY definition
17 // const unsigned int PRIVATE_KEY_SIZE = 192;
18 // const unsigned int PUBLIC_KEY_SIZE = 41;
19 // const unsigned int SIGNATURE_SIZE = 48;
22 // const unsigned int PRIVATE_KEY_SIZE = 222;
23 // const unsigned int PUBLIC_KEY_SIZE = 49;
24 // const unsigned int SIGNATURE_SIZE = 57;
27 // const unsigned int PRIVATE_KEY_SIZE = 250;
28 // const unsigned int PUBLIC_KEY_SIZE = 57;
29 // const unsigned int SIGNATURE_SIZE = 66;
32 // const unsigned int PRIVATE_KEY_SIZE = 279;
33 // const unsigned int PUBLIC_KEY_SIZE = 65;
34 // const unsigned int SIGNATURE_SIZE = 72;
36 // see www.keylength.com
37 // script supports up to 75 for single byte push
39 class key_error : public std::runtime_error
42 explicit key_error(const std::string& str) : std::runtime_error(str) {}
46 // secure_allocator is defined in serialize.h
47 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
48 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
49 // CSecret is a serialization of just the secret parameter (32 bytes)
50 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
52 /** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
58 bool fCompressedPubKey;
60 void SetCompressedPubKey();
69 CKey& operator=(const CKey& b);
74 bool IsCompressed() const;
76 void MakeNewKey(bool fCompressed);
77 bool SetPrivKey(const CPrivKey& vchPrivKey);
78 bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
79 CSecret GetSecret(bool &fCompressed) const;
80 CPrivKey GetPrivKey() const;
81 bool SetPubKey(const std::vector<unsigned char>& vchPubKey);
82 std::vector<unsigned char> GetPubKey() const;
84 bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
86 // create a compact signature (65 bytes), which allows reconstructing the used public key
87 // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
88 // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
89 // 0x1D = second key with even y, 0x1E = second key with odd y
90 bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
92 // reconstruct public key from a compact signature
93 // This is only slightly more CPU intensive than just verifying it.
94 // If this function succeeds, the recovered public key is guaranteed to be valid
95 // (the signature is a valid signature of the given data for that key)
96 bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
98 bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
100 // Verify a compact signature
101 bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);