forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from navcoin/blsct-keys-classes
Add blsct::{PublicKey,DoublePublicKey,PrivateKey} classes
- Loading branch information
Showing
10 changed files
with
434 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright (c) 2022 The Navcoin developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <blsct/keys.h> | ||
|
||
namespace blsct { | ||
|
||
PublicKey PublicKey::Aggregate(std::vector<PublicKey> vPk) | ||
{ | ||
auto retPoint = G1Point(); | ||
bool isZero = true; | ||
|
||
for (auto& pk : vPk) { | ||
G1Point pkG1; | ||
|
||
bool success = pk.GetG1Point(pkG1); | ||
if (!success) | ||
throw std::runtime_error(strprintf("%s: Vector of public keys has an invalid element", __func__)); | ||
|
||
retPoint = isZero ? pkG1 : retPoint + pkG1; | ||
isZero = false; | ||
} | ||
|
||
return PublicKey(retPoint); | ||
} | ||
|
||
uint256 PublicKey::GetHash() const | ||
{ | ||
CHashWriter ss(SER_GETHASH, 0); | ||
ss << data; | ||
return ss.GetHash(); | ||
} | ||
|
||
CKeyID PublicKey::GetID() const | ||
{ | ||
return CKeyID(Hash160(data)); | ||
} | ||
|
||
bool PublicKey::GetG1Point(G1Point& ret) const | ||
{ | ||
try { | ||
ret = G1Point(data); | ||
} catch (...) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
std::string PublicKey::ToString() const | ||
{ | ||
return HexStr(GetVch()); | ||
}; | ||
|
||
bool PublicKey::operator==(const PublicKey& rhs) const | ||
{ | ||
return GetVch() == rhs.GetVch(); | ||
} | ||
|
||
bool PublicKey::IsValid() const | ||
{ | ||
if (data.size() == 0) return false; | ||
|
||
G1Point g1; | ||
|
||
if (!GetG1Point(g1)) return false; | ||
|
||
return g1.IsValid(); | ||
} | ||
|
||
std::vector<unsigned char> PublicKey::GetVch() const | ||
{ | ||
return data; | ||
} | ||
|
||
CKeyID DoublePublicKey::GetID() const | ||
{ | ||
return CKeyID(Hash160(GetVch())); | ||
} | ||
|
||
bool DoublePublicKey::GetViewKey(G1Point& ret) const | ||
{ | ||
try { | ||
ret = G1Point(vk.GetVch()); | ||
} catch (...) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool DoublePublicKey::GetSpendKey(G1Point& ret) const | ||
{ | ||
try { | ||
ret = G1Point(sk.GetVch()); | ||
} catch (...) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool DoublePublicKey::operator==(const DoublePublicKey& rhs) const | ||
{ | ||
return vk == rhs.vk && sk == rhs.sk; | ||
} | ||
|
||
bool DoublePublicKey::IsValid() const | ||
{ | ||
return vk.IsValid() && sk.IsValid(); | ||
} | ||
|
||
std::vector<unsigned char> DoublePublicKey::GetVkVch() const | ||
{ | ||
return vk.GetVch(); | ||
} | ||
|
||
std::vector<unsigned char> DoublePublicKey::GetSkVch() const | ||
{ | ||
return sk.GetVch(); | ||
} | ||
|
||
std::vector<unsigned char> DoublePublicKey::GetVch() const | ||
{ | ||
auto ret = vk.GetVch(); | ||
auto toAppend = sk.GetVch(); | ||
|
||
ret.insert(ret.end(), toAppend.begin(), toAppend.end()); | ||
|
||
return ret; | ||
} | ||
|
||
bool PrivateKey::operator==(const PrivateKey& rhs) const | ||
{ | ||
return k == rhs.k; | ||
} | ||
|
||
G1Point PrivateKey::GetG1Point() const | ||
{ | ||
return G1Point::GetBasePoint() * Scalar(std::vector<unsigned char>(k.begin(), k.end())); | ||
} | ||
|
||
PublicKey PrivateKey::GetPublicKey() const | ||
{ | ||
return PublicKey(GetG1Point()); | ||
} | ||
|
||
Scalar PrivateKey::GetScalar() const | ||
{ | ||
return Scalar(std::vector<unsigned char>(k.begin(), k.end())); | ||
} | ||
|
||
bool PrivateKey::IsValid() const | ||
{ | ||
if (k.size() == 0) return false; | ||
return GetScalar().IsValid(); | ||
} | ||
|
||
void PrivateKey::SetToZero() | ||
{ | ||
k.clear(); | ||
} | ||
} // namespace blsct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) 2022 The Navcoin developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef KEY_H | ||
#define KEY_H | ||
|
||
#include <blsct/arith/g1point.h> | ||
#include <blsct/arith/scalar.h> | ||
#include <hash.h> | ||
#include <key.h> | ||
#include <serialize.h> | ||
#include <streams.h> | ||
#include <tinyformat.h> | ||
#include <uint256.h> | ||
#include <util/strencodings.h> | ||
#include <version.h> | ||
|
||
namespace blsct { | ||
static const std::string subAddressHeader = "SubAddress\0"; | ||
|
||
class PublicKey | ||
{ | ||
private: | ||
std::vector<unsigned char> data; | ||
|
||
public: | ||
static constexpr size_t SIZE = 48; | ||
|
||
PublicKey() { data.clear(); } | ||
PublicKey(const G1Point& pk) : data(pk.GetVch()) {} | ||
PublicKey(const std::vector<unsigned char>& pk) : data(pk) {} | ||
|
||
SERIALIZE_METHODS(PublicKey, obj) { READWRITE(obj.data); } | ||
|
||
static PublicKey Aggregate(std::vector<PublicKey> vPk); | ||
|
||
uint256 GetHash() const; | ||
CKeyID GetID() const; | ||
|
||
std::string ToString() const; | ||
|
||
bool operator==(const PublicKey& rhs) const; | ||
|
||
bool IsValid() const; | ||
|
||
bool GetG1Point(G1Point& ret) const; | ||
std::vector<unsigned char> GetVch() const; | ||
}; | ||
|
||
class DoublePublicKey | ||
{ | ||
private: | ||
PublicKey vk; | ||
PublicKey sk; | ||
|
||
public: | ||
static constexpr size_t SIZE = 48 * 2; | ||
|
||
DoublePublicKey() {} | ||
DoublePublicKey(const G1Point& vk_, const G1Point& sk_) : vk(vk_.GetVch()), sk(sk_.GetVch()) {} | ||
DoublePublicKey(const std::vector<unsigned char>& vk_, const std::vector<unsigned char>& sk_) : vk(vk_), sk(sk_) {} | ||
|
||
SERIALIZE_METHODS(DoublePublicKey, obj) { READWRITE(obj.vk.GetVch(), obj.sk.GetVch()); } | ||
|
||
uint256 GetHash() const; | ||
CKeyID GetID() const; | ||
|
||
bool GetViewKey(G1Point& ret) const; | ||
bool GetSpendKey(G1Point& ret) const; | ||
|
||
bool operator==(const DoublePublicKey& rhs) const; | ||
|
||
bool IsValid() const; | ||
|
||
std::vector<unsigned char> GetVkVch() const; | ||
std::vector<unsigned char> GetSkVch() const; | ||
std::vector<unsigned char> GetVch() const; | ||
}; | ||
|
||
class PrivateKey | ||
{ | ||
private: | ||
CPrivKey k; | ||
|
||
public: | ||
static constexpr size_t SIZE = 32; | ||
|
||
PrivateKey() { k.clear(); } | ||
|
||
PrivateKey(Scalar k_) | ||
{ | ||
k.resize(PrivateKey::SIZE); | ||
std::vector<unsigned char> v = k_.GetVch(); | ||
memcpy(k.data(), &v.front(), k.size()); | ||
} | ||
|
||
PrivateKey(CPrivKey k_) | ||
{ | ||
k.resize(PrivateKey::SIZE); | ||
memcpy(k.data(), &k_.front(), k.size()); | ||
} | ||
|
||
SERIALIZE_METHODS(PrivateKey, obj) { READWRITE(std::vector<unsigned char>(obj.k.begin(), obj.k.end())); } | ||
|
||
bool operator==(const PrivateKey& rhs) const; | ||
|
||
G1Point GetG1Point() const; | ||
PublicKey GetPublicKey() const; | ||
Scalar GetScalar() const; | ||
|
||
bool IsValid() const; | ||
|
||
void SetToZero(); | ||
|
||
friend class CCryptoKeyStore; | ||
friend class CBasicKeyStore; | ||
}; | ||
|
||
} // namespace blsct | ||
|
||
#endif // KEY_H |
Oops, something went wrong.