Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLSCT Wallet KeyManager #120

Merged
merged 26 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ BITCOIN_CORE_H = \
blsct/set_mem_proof/set_mem_proof.h \
blsct/set_mem_proof/set_mem_proof_prover.h \
blsct/signature.h \
blsct/wallet/address.h \
blsct/wallet/hdchain.h \
blsct/wallet/keyman.h \
blsct/wallet/keyring.h \
dandelion.h \
chain.h \
chainparams.h \
Expand Down Expand Up @@ -556,6 +560,12 @@ endif
libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS) $(BDB_CPPFLAGS) $(SQLITE_CFLAGS)
libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_wallet_a_SOURCES = \
blsct/eip_2333/bls12_381_keygen.cpp \
blsct/private_key.cpp \
blsct/public_key.cpp \
blsct/wallet/address.cpp \
blsct/wallet/keyman.cpp \
blsct/wallet/keyring.cpp \
wallet/coincontrol.cpp \
wallet/context.cpp \
wallet/crypter.cpp \
Expand Down Expand Up @@ -736,6 +746,8 @@ libbitcoin_common_a_SOURCES = \
base58.cpp \
bech32.cpp \
blsct/arith/elements.cpp \
blsct/double_public_key.cpp \
blsct/public_key.cpp \
chainparams.cpp \
coins.cpp \
common/args.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-descriptors", "Create descriptors wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blsct", "Create blsct wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-legacy", "Create legacy wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
Expand Down
5 changes: 3 additions & 2 deletions src/blsct/arith/mcl/mcl_g1point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ std::vector<uint8_t> MclG1Point::GetVch() const
{
std::vector<uint8_t> b(SERIALIZATION_SIZE);
if (mclBnG1_serialize(&b[0], SERIALIZATION_SIZE, &m_p) == 0) {
MclG1Point ret;
return ret.GetVch();
MclG1Point unity;
auto ret = unity.GetVch();
return ret;
mxaddict marked this conversation as resolved.
Show resolved Hide resolved
}
return b;
}
Expand Down
28 changes: 28 additions & 0 deletions src/blsct/double_public_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@

namespace blsct {

DoublePublicKey::DoublePublicKey(const std::vector<unsigned char>& keys) {
if (keys.size() != SIZE) return;
std::vector<unsigned char> vkData(SIZE/2);
std::vector<unsigned char> skData(SIZE/2);
std::copy(keys.begin(), keys.begin()+SIZE/2, vkData.begin());
std::copy(keys.begin()+SIZE/2, keys.end(), skData.begin());
vk = vkData;
sk = skData;
}

CKeyID DoublePublicKey::GetID() const
{
return CKeyID(Hash160(GetVch()));
}

bool DoublePublicKey::GetViewKey(PublicKey& ret) const
{
ret = vk;
return true;
}

bool DoublePublicKey::GetViewKey(Point& ret) const
{
try {
Expand All @@ -22,6 +38,12 @@ bool DoublePublicKey::GetViewKey(Point& ret) const
return true;
}

bool DoublePublicKey::GetSpendKey(PublicKey& ret) const
{
ret = sk;
return true;
}

bool DoublePublicKey::GetSpendKey(Point& ret) const
{
try {
Expand All @@ -38,6 +60,12 @@ bool DoublePublicKey::operator==(const DoublePublicKey& rhs) const
return vk == rhs.vk && sk == rhs.sk;
}

bool DoublePublicKey::operator<(const DoublePublicKey& rhs) const
{
return this->GetVkVch() == rhs.GetVkVch() ? this->GetSkVch() < rhs.GetSkVch() : this->GetVkVch() < rhs.GetVkVch();
};


bool DoublePublicKey::IsValid() const
{
return vk.IsValid() && sk.IsValid();
Expand Down
6 changes: 6 additions & 0 deletions src/blsct/double_public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class DoublePublicKey
static constexpr size_t SIZE = 48 * 2;

DoublePublicKey() {}
DoublePublicKey(const PublicKey& vk_, const PublicKey& sk_) : vk(vk_.GetVch()), sk(sk_.GetVch()) {}
DoublePublicKey(const Point& vk_, const Point& sk_) : vk(vk_.GetVch()), sk(sk_.GetVch()) {}
DoublePublicKey(const std::vector<unsigned char>& vk_, const std::vector<unsigned char>& sk_) : vk(vk_), sk(sk_) {}
DoublePublicKey(const std::vector<unsigned char>& keys);

SERIALIZE_METHODS(DoublePublicKey, obj) { READWRITE(obj.vk.GetVch(), obj.sk.GetVch()); }

Expand All @@ -35,7 +37,11 @@ class DoublePublicKey
bool GetViewKey(Point& ret) const;
bool GetSpendKey(Point& ret) const;

bool GetViewKey(PublicKey& ret) const;
bool GetSpendKey(PublicKey& ret) const;

bool operator==(const DoublePublicKey& rhs) const;
bool operator<(const DoublePublicKey& rhs) const;

bool IsValid() const;

Expand Down
13 changes: 8 additions & 5 deletions src/blsct/private_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace blsct {

PrivateKey::PrivateKey(Scalar k_)
{
if (k_.IsZero()) {
throw std::runtime_error("Private key needs to be a non-zero scalar");
}
mxaddict marked this conversation as resolved.
Show resolved Hide resolved
k.resize(PrivateKey::SIZE);
std::vector<unsigned char> v = k_.GetVch();
memcpy(k.data(), &v.front(), k.size());
Expand All @@ -36,12 +33,14 @@ PrivateKey::Point PrivateKey::GetPoint() const

PublicKey PrivateKey::GetPublicKey() const
{
return PublicKey(GetPoint());
auto point = GetPoint();
return point;
}

PrivateKey::Scalar PrivateKey::GetScalar() const
{
return Scalar(std::vector<unsigned char>(k.begin(), k.end()));
auto ret = std::vector<unsigned char>(k.begin(), k.end());
return ret;
}

bool PrivateKey::IsValid() const
Expand Down Expand Up @@ -78,4 +77,8 @@ Signature PrivateKey::Sign(const Message& msg) const
return sig;
}

bool PrivateKey::VerifyPubKey(const PublicKey& pk) const {
return GetPublicKey() == pk;
}

} // namespace blsct
22 changes: 18 additions & 4 deletions src/blsct/private_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ class PrivateKey
public:
static constexpr size_t SIZE = 32;

PrivateKey() { k.clear(); }
PrivateKey() { k.resize(SIZE); }
PrivateKey(Scalar k_);
PrivateKey(CPrivKey k_);

SERIALIZE_METHODS(PrivateKey, obj) { READWRITE(std::vector<unsigned char>(obj.k.begin(), obj.k.end())); }
template <typename Stream>
void Serialize(Stream& s) const
{
s.write(MakeByteSpan(k));
}

template <typename Stream>
void Unserialize(Stream& s)
{
s.read(MakeWritableByteSpan(k));
}

bool operator==(const PrivateKey& rhs) const;

Expand All @@ -38,6 +48,7 @@ class PrivateKey
Scalar GetScalar() const;
bool IsValid() const;
void SetToZero();
bool VerifyPubKey(const PublicKey& pk) const;

// Basic scheme
Signature SignBalance() const;
Expand All @@ -48,8 +59,11 @@ class PrivateKey
// Core operations
Signature CoreSign(const Message& msg) const;

friend class CCryptoKeyStore;
friend class CBasicKeyStore;
//! Simple read-only vector-like interface.
unsigned int size() const { return (IsValid() ? k.size() : 0); }
const std::byte* data() const { return reinterpret_cast<const std::byte*>(k.data()); }
const unsigned char* begin() const { return k.data(); }
const unsigned char* end() const { return k.data() + size(); }
};

}
Expand Down
5 changes: 5 additions & 0 deletions src/blsct/public_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ bool PublicKey::operator==(const PublicKey& rhs) const
return GetVch() == rhs.GetVch();
}

bool PublicKey::operator!=(const PublicKey& rhs) const
{
return GetVch() != rhs.GetVch();
}

bool PublicKey::IsValid() const
{
if (data.size() == 0) return false;
Expand Down
3 changes: 3 additions & 0 deletions src/blsct/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ class PublicKey
std::string ToString() const;

bool operator==(const PublicKey& rhs) const;
bool operator!=(const PublicKey& rhs) const;

bool IsValid() const;

bool GetG1Point(Point& ret) const;
std::vector<unsigned char> GetVch() const;

bool operator<(const PublicKey& b) const { return this->GetVch() < b.GetVch(); };

blsPublicKey ToBlsPublicKey() const;
std::vector<uint8_t> AugmentMessage(const Message& msg) const;

Expand Down
54 changes: 54 additions & 0 deletions src/blsct/wallet/address.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2023 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/wallet/address.h>

namespace blsct {
SubAddress::SubAddress(const PrivateKey &viewKey, const PublicKey &spendKey, const SubAddressIdentifier &subAddressId)
{
if(!viewKey.IsValid() || !spendKey.IsValid())
return;

CHashWriter string(SER_GETHASH, 0);

string << std::vector<unsigned char>(subAddressHeader.begin(), subAddressHeader.end());
string << viewKey;
string << subAddressId.account;
string << subAddressId.address;

// m = Hs(a || i)
// M = m*G
// D = B + M
// C = a*D
MclScalar m{string.GetHash()};
MclG1Point M, B;

if (!PrivateKey(m).GetPublicKey().GetG1Point(M))
return;

if (!spendKey.GetG1Point(B))
return;

MclG1Point D = M + B;
auto C = D * viewKey.GetScalar();
pk = DoublePublicKey(C, D);
}

std::string SubAddress::GetString() const
{
return EncodeDestination(pk);
}

CTxDestination SubAddress::GetDestination() const
{
if (!IsValid())
return CNoDestination();
return pk;
}

bool SubAddress::IsValid() const
{
return pk.IsValid();
}
}
37 changes: 37 additions & 0 deletions src/blsct/wallet/address.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023 The Navcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef NAVCOIN_BLSCT_ADDRESS_H
#define NAVCOIN_BLSCT_ADDRESS_H

#include <blsct/double_public_key.h>
#include <blsct/private_key.h>
#include <blsct/public_key.h>
#include <key_io.h>

namespace blsct {
static const std::string subAddressHeader = "SubAddress\0";

struct SubAddressIdentifier {
uint64_t account;
uint64_t address;
};

class SubAddress
{
private:
DoublePublicKey pk;
public:
SubAddress(const PrivateKey &viewKey, const PublicKey &spendKey, const SubAddressIdentifier &subAddressId);
SubAddress(const DoublePublicKey& pk) : pk(pk) {};

bool IsValid() const;

std::string GetString() const;
CTxDestination GetDestination() const;
DoublePublicKey GetKeys() const { return pk; };
};
}

#endif // NAVCOIN_BLSCT_ADDRESS_H
48 changes: 48 additions & 0 deletions src/blsct/wallet/hdchain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2023 The Navcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BLSCTHDCHAIN_H
#define BLSCTHDCHAIN_H

#include <wallet/db.h>
#include <wallet/walletutil.h>

/* simple HD chain data model */
namespace blsct {
class HDChain
{
public:
CKeyID seed_id; //!< seed hash160
CKeyID spend_id; //!< spend hash160
CKeyID view_id; //!< view hash160
CKeyID token_id; //!< token hash160

static const int VERSION_HD_BASE = 1;
static const int CURRENT_VERSION = VERSION_HD_BASE;
int nVersion;

HDChain() { SetNull(); }

SERIALIZE_METHODS(HDChain, obj)
{
READWRITE(obj.nVersion, obj.seed_id, obj.spend_id, obj.view_id, obj.token_id);
}

void SetNull()
{
nVersion = HDChain::CURRENT_VERSION;
seed_id.SetNull();
spend_id.SetNull();
view_id.SetNull();
token_id.SetNull();
}

bool operator==(const HDChain& chain) const
{
return seed_id == chain.seed_id && spend_id == chain.spend_id && view_id == chain.view_id && token_id == chain.token_id;
}
};
}

#endif // BLSCTHDCHAIN_H
Loading