2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (C) 2009-2015 The Bitcoin Core developers
|
2023-11-24 21:36:54 +01:00
|
|
|
* Copyright (C) 2019-2023 Tom Zander <tom@flowee.org>
|
2017-11-09 19:34:51 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2014-10-28 17:47:18 -04:00
|
|
|
|
2023-11-24 18:01:36 +01:00
|
|
|
#ifndef FLOWEE_PUBLICKEY_H
|
|
|
|
|
#define FLOWEE_PUBLICKEY_H
|
2014-10-28 17:47:18 -04:00
|
|
|
|
2023-11-24 18:01:36 +01:00
|
|
|
#include "PublicKeyUtils.h"
|
2014-10-28 17:47:18 -04:00
|
|
|
#include "serialize.h"
|
|
|
|
|
#include "uint256.h"
|
2023-11-24 21:36:54 +01:00
|
|
|
#include "streaming/ConstBuffer.h"
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* secp256k1:
|
|
|
|
|
* const unsigned int PRIVATE_KEY_SIZE = 279;
|
|
|
|
|
* const unsigned int PUBLIC_KEY_SIZE = 65;
|
|
|
|
|
* const unsigned int SIGNATURE_SIZE = 72;
|
|
|
|
|
*
|
|
|
|
|
* see www.keylength.com
|
|
|
|
|
* script supports up to 75 for single byte push
|
|
|
|
|
*/
|
|
|
|
|
|
2022-07-06 21:52:47 +02:00
|
|
|
/** A reference to a Key: the Hash160 of its serialized public key */
|
|
|
|
|
class KeyId : public uint160
|
2014-10-28 17:47:18 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2022-07-06 21:52:47 +02:00
|
|
|
KeyId() : uint160() {}
|
|
|
|
|
KeyId(const uint160& in) : uint160(in) {}
|
|
|
|
|
explicit KeyId(const char *d) : uint160(d) {}
|
2014-10-28 17:47:18 -04:00
|
|
|
};
|
|
|
|
|
|
2015-04-21 18:09:37 -04:00
|
|
|
typedef uint256 ChainCode;
|
2014-01-25 20:05:00 +01:00
|
|
|
|
2014-10-28 17:47:18 -04:00
|
|
|
/** An encapsulated public key. */
|
2022-07-06 21:56:34 +02:00
|
|
|
class PublicKey
|
2014-10-28 17:47:18 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2019-10-10 15:13:34 +02:00
|
|
|
/**
|
|
|
|
|
* secp256k1:
|
|
|
|
|
*/
|
|
|
|
|
static constexpr unsigned int PUBLIC_KEY_SIZE = 65;
|
|
|
|
|
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
|
|
|
|
|
static constexpr unsigned int SIGNATURE_SIZE = 72;
|
|
|
|
|
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
|
|
|
|
|
/**
|
|
|
|
|
* see www.keylength.com
|
|
|
|
|
* script supports up to 75 for single byte push
|
|
|
|
|
*/
|
|
|
|
|
static_assert(PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
|
|
|
|
|
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
|
|
|
|
|
|
2014-10-28 17:47:18 -04:00
|
|
|
//! Construct an invalid public key.
|
2022-07-06 21:56:34 +02:00
|
|
|
PublicKey() {
|
2021-04-19 11:58:00 +02:00
|
|
|
invalidate();
|
2014-10-28 17:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Initialize a public key using begin/end iterators to byte data.
|
|
|
|
|
template <typename T>
|
2021-04-19 11:58:00 +02:00
|
|
|
void setData(const T pbegin, const T pend)
|
2014-10-28 17:47:18 -04:00
|
|
|
{
|
2023-11-24 18:01:36 +01:00
|
|
|
int len = pend == pbegin ? 0 : PublicKeyUtils::keyLength(pbegin[0]);
|
2014-10-28 17:47:18 -04:00
|
|
|
if (len && len == (pend - pbegin))
|
|
|
|
|
memcpy(vch, (unsigned char*)&pbegin[0], len);
|
|
|
|
|
else
|
2021-04-19 11:58:00 +02:00
|
|
|
invalidate();
|
2014-10-28 17:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Construct a public key using begin/end iterators to byte data.
|
|
|
|
|
template <typename T>
|
2022-07-06 21:56:34 +02:00
|
|
|
PublicKey(const T pbegin, const T pend)
|
2014-10-28 17:47:18 -04:00
|
|
|
{
|
2021-04-19 11:58:00 +02:00
|
|
|
setData(pbegin, pend);
|
2014-10-28 17:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Construct a public key from a byte vector.
|
2023-07-15 12:24:40 +02:00
|
|
|
PublicKey(const std::vector<unsigned char>& vch) { setData(vch.begin(), vch.end()); }
|
|
|
|
|
PublicKey(const std::vector<char> &vch) { setData(vch.begin(), vch.end()); }
|
2023-11-24 21:36:54 +01:00
|
|
|
PublicKey(const Streaming::ConstBuffer &buf) { setData(buf.begin(), buf.end()); }
|
2023-07-15 12:24:40 +02:00
|
|
|
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
//! Simple read-only vector-like interface to the pubkey data.
|
2023-11-24 18:01:36 +01:00
|
|
|
unsigned int size() const { return PublicKeyUtils::keyLength(vch[0]); }
|
2014-10-28 17:47:18 -04:00
|
|
|
const unsigned char* begin() const { return vch; }
|
|
|
|
|
const unsigned char* end() const { return vch + size(); }
|
|
|
|
|
const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
|
|
|
|
|
|
|
|
|
|
//! Comparator implementation.
|
2022-07-06 21:56:34 +02:00
|
|
|
friend bool operator==(const PublicKey& a, const PublicKey& b) {
|
2019-11-13 15:11:39 +01:00
|
|
|
return a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) == 0;
|
2014-10-28 17:47:18 -04:00
|
|
|
}
|
2019-11-13 15:11:39 +01:00
|
|
|
|
2022-07-06 21:56:34 +02:00
|
|
|
friend bool operator!=(const PublicKey& a, const PublicKey& b) {
|
2014-10-28 17:47:18 -04:00
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2019-11-13 15:11:39 +01:00
|
|
|
|
2022-07-06 21:56:34 +02:00
|
|
|
friend bool operator<(const PublicKey& a, const PublicKey& b) {
|
2014-10-28 17:47:18 -04:00
|
|
|
return a.vch[0] < b.vch[0] ||
|
|
|
|
|
(a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Implement serialization, as if this was a byte vector.
|
2021-04-19 11:58:00 +02:00
|
|
|
unsigned int serializedSize(int nType, int nVersion) const {
|
2014-10-28 17:47:18 -04:00
|
|
|
return size() + 1;
|
|
|
|
|
}
|
|
|
|
|
template <typename Stream>
|
2019-11-13 15:11:39 +01:00
|
|
|
void Serialize(Stream& s, int nType, int nVersion) const {
|
2014-10-28 17:47:18 -04:00
|
|
|
unsigned int len = size();
|
|
|
|
|
::WriteCompactSize(s, len);
|
|
|
|
|
s.write((char*)vch, len);
|
|
|
|
|
}
|
2019-11-13 15:11:39 +01:00
|
|
|
|
2014-10-28 17:47:18 -04:00
|
|
|
template <typename Stream>
|
2019-11-13 15:11:39 +01:00
|
|
|
void Unserialize(Stream& s, int nType, int nVersion) {
|
2026-05-18 09:29:26 +02:00
|
|
|
unsigned int len = ::ReadCompactSizeAsUnsignedInt(s);
|
2014-10-28 17:47:18 -04:00
|
|
|
if (len <= 65) {
|
|
|
|
|
s.read((char*)vch, len);
|
|
|
|
|
} else {
|
|
|
|
|
// invalid pubkey, skip available data
|
|
|
|
|
char dummy;
|
|
|
|
|
while (len--)
|
|
|
|
|
s.read(&dummy, 1);
|
2021-04-19 11:58:00 +02:00
|
|
|
invalidate();
|
2014-10-28 17:47:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Get the KeyID of this public key (hash of its serialization)
|
2022-07-06 21:52:47 +02:00
|
|
|
KeyId getKeyId() const;
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
//! Get the 256-bit hash of this public key.
|
2021-04-19 11:58:00 +02:00
|
|
|
uint256 createHash() const;
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check syntactic correctness.
|
|
|
|
|
*
|
|
|
|
|
* Note that this is consensus critical as CheckSig() calls it!
|
|
|
|
|
*/
|
2021-04-19 11:58:00 +02:00
|
|
|
bool isValid() const;
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
//! fully validate whether this is a valid public key (more expensive than IsValid())
|
2021-04-19 11:58:00 +02:00
|
|
|
bool isFullyValid() const;
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
//! Check whether this is a compressed public key.
|
2021-04-19 11:58:00 +02:00
|
|
|
bool isCompressed() const;
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify a DER signature (~72 bytes).
|
|
|
|
|
* If this public key is not fully valid, the return value will be false.
|
|
|
|
|
*/
|
2019-04-17 23:21:14 +02:00
|
|
|
bool verifyECDSA(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
|
|
|
|
|
|
2025-10-14 22:38:54 +02:00
|
|
|
bool verifyCompact(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
|
|
|
|
|
|
2019-04-17 23:21:14 +02:00
|
|
|
/**
|
|
|
|
|
* Verify a Schnorr signature (=64 bytes).
|
|
|
|
|
* If this public key is not fully valid, the return value will be false.
|
|
|
|
|
*/
|
|
|
|
|
bool verifySchnorr(const uint256 &hash, const std::vector<uint8_t> &vchSig) const;
|
2014-10-28 17:47:18 -04:00
|
|
|
|
2015-07-28 20:11:20 +02:00
|
|
|
/**
|
|
|
|
|
* Check whether a signature is normalized (lower-S).
|
|
|
|
|
*/
|
2021-04-19 11:58:00 +02:00
|
|
|
static bool checkLowS(const std::vector<unsigned char>& vchSig);
|
2015-07-28 20:11:20 +02:00
|
|
|
|
2014-10-28 17:47:18 -04:00
|
|
|
//! Recover a public key from a compact signature.
|
2021-04-19 11:58:00 +02:00
|
|
|
bool recoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
//! Turn this public key into an uncompressed public key.
|
2021-04-19 11:58:00 +02:00
|
|
|
bool decompress();
|
2014-10-28 17:47:18 -04:00
|
|
|
|
|
|
|
|
//! Derive BIP32 child pubkey.
|
2022-07-06 21:56:34 +02:00
|
|
|
bool derive(PublicKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
|
2019-10-10 15:13:34 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* Just store the serialized data.
|
|
|
|
|
* Its length can very cheaply be computed from the first byte.
|
|
|
|
|
*/
|
|
|
|
|
unsigned char vch[65];
|
|
|
|
|
|
|
|
|
|
//! Set this key data to be invalid
|
2021-04-19 11:58:00 +02:00
|
|
|
inline void invalidate() {
|
2019-10-10 15:13:34 +02:00
|
|
|
vch[0] = 0xFF;
|
|
|
|
|
}
|
2014-10-28 17:47:18 -04:00
|
|
|
};
|
|
|
|
|
|
2015-07-28 20:11:20 +02:00
|
|
|
/** Users of this module must hold an ECCVerifyHandle. The constructor and
|
|
|
|
|
* destructor of these are not allowed to run in parallel, though. */
|
|
|
|
|
class ECCVerifyHandle
|
|
|
|
|
{
|
|
|
|
|
static int refcount;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ECCVerifyHandle();
|
|
|
|
|
~ECCVerifyHandle();
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-16 10:47:52 +00:00
|
|
|
#endif
|