Skip to content

Commit

Permalink
Merge pull request #62 from navcoin/blsct-keys-classes
Browse files Browse the repository at this point in the history
Add blsct::{PublicKey,DoublePublicKey,PrivateKey} classes
  • Loading branch information
alex v authored Sep 2, 2022
2 parents 4770ec6 + 5d4ec53 commit 4a1596e
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ BITCOIN_CORE_H = \
blsct/arith/g1point.h \
blsct/arith/mcl_initializer.h \
blsct/arith/scalar.h \
blsct/keys.h \
chain.h \
chainparams.h \
chainparamsbase.h \
Expand Down Expand Up @@ -387,6 +388,7 @@ libbitcoin_node_a_SOURCES = \
blsct/arith/g1point.cpp \
blsct/arith/mcl_initializer.cpp \
blsct/arith/scalar.cpp \
blsct/keys.cpp \
chain.cpp \
consensus/tx_verify.cpp \
dbwrapper.cpp \
Expand Down Expand Up @@ -911,6 +913,7 @@ libbitcoinkernel_la_SOURCES = \
blsct/arith/g1point.cpp \
blsct/arith/mcl_initializer.cpp \
blsct/arith/scalar.cpp \
blsct/keys.cpp \
chain.cpp \
chainparamsbase.cpp \
chainparams.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ BITCOIN_TESTS =\
test/blsct/arith/g1point_tests.cpp \
test/blsct/arith/scalar_tests.cpp \
test/blsct/arith/bls_arith_integration_tests.cpp \
test/blsct/keys_tests.cpp \
test/bswap_tests.cpp \
test/checkqueue_tests.cpp \
test/coins_tests.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.test_util.include
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TEST_UTIL_H = \
blsct/arith/g1point.h \
blsct/arith/mcl_initializer.h \
blsct/arith/scalar.h \
blsct/keys.h \
test/util/chainstate.h \
test/util/logging.h \
test/util/mining.h \
Expand All @@ -30,6 +31,7 @@ libtest_util_a_SOURCES = \
blsct/arith/g1point.cpp \
blsct/arith/mcl_initializer.cpp \
blsct/arith/scalar.cpp \
blsct/keys.cpp \
test/util/blockfilter.cpp \
test/util/logging.cpp \
test/util/mining.cpp \
Expand Down
5 changes: 5 additions & 0 deletions src/blsct/arith/g1point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ G1Point G1Point::Rand()
return g * Scalar::Rand();
}

bool G1Point::IsValid() const
{
return mclBnG1_isValid(&m_p) == 1;
}

bool G1Point::IsUnity() const
{
return mclBnG1_isZero(&m_p);
Expand Down
1 change: 1 addition & 0 deletions src/blsct/arith/g1point.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class G1Point
bool operator==(const G1Point& b) const;
bool operator!=(const G1Point& b) const;

bool IsValid() const;
bool IsUnity() const;

std::vector<uint8_t> GetVch() const;
Expand Down
5 changes: 5 additions & 0 deletions src/blsct/arith/scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ bool Scalar::operator!=(const Scalar &b) const
return !operator==(b);
}

bool Scalar::IsValid() const
{
return mclBnFr_isValid(&m_fr) == 1;
}

Scalar Scalar::Invert() const
{
if (mclBnFr_isZero(&m_fr) == 1) {
Expand Down
2 changes: 2 additions & 0 deletions src/blsct/arith/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Scalar {
bool operator!=(const Scalar& b) const;
bool operator!=(const int& b) const;

bool IsValid() const;

Scalar Invert() const;
Scalar Negate() const;
Scalar Square() const;
Expand Down
163 changes: 163 additions & 0 deletions src/blsct/keys.cpp
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
122 changes: 122 additions & 0 deletions src/blsct/keys.h
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
Loading

0 comments on commit 4a1596e

Please sign in to comment.