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

Add BIP44 Key Generation with HD Mint path #392

Merged
merged 47 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b9bb7b7
HD mint for Sigma - base infrastructure
riordant Apr 12, 2019
1d1ee64
Update to zerocoin v3
riordant Apr 16, 2019
0f39b43
Update all Sigma spend functions, rename deterministic mint class
riordant Apr 22, 2019
c5e2299
Fixes
riordant Apr 22, 2019
8817049
Further fixes
riordant Apr 23, 2019
cae518f
Merge branch 'sigma' of https://github.com/zcoinofficial/zcoin into h…
riordant Apr 23, 2019
3fe9ff2
Zerocoin function fixes + renaming
riordant Apr 25, 2019
6b47151
Merge branch 'hdmint' of https://github.com/zcoinofficial/zcoin-sigma…
riordant Apr 30, 2019
eab2484
Some reorg rpcwallet functions
riordant Apr 30, 2019
92453f7
Fix linux build + modify tests
May 1, 2019
c95f4cb
Merge remote-tracking branch 'origin/sigma' into hdmint
riordant May 2, 2019
75455a1
More test fixes
riordant May 3, 2019
9d1fb15
All tests passing
riordant May 6, 2019
00b9864
BIP44 upgrade w/ HD mint
riordant May 9, 2019
f41c339
Fixes - all tests passing
riordant May 10, 2019
767b967
Merge remote-tracking branch 'origin/reorg-test' into hdmint
riordant May 14, 2019
036a125
Fixes for Python tests
riordant May 14, 2019
6a4a17f
Add header file for Jenkins build
riordant May 15, 2019
afd8d32
Updates to correctly spend non-deterministic coins
riordant May 15, 2019
e89954e
Merge branch 'nondeterministic' into hdmint
riordant May 15, 2019
6e791ef
Further updates to include non-deterministic coins
riordant May 16, 2019
bcb4ad0
Test fixes
riordant May 17, 2019
6efbb34
Fix formatting errors for Jenkins build
riordant May 17, 2019
ae7bd96
Merge remote-tracking branch 'origin/sigma' into hdmint
riordant May 20, 2019
b823489
Merge remote-tracking branch 'origin/reorg-test' into hdmint
riordant May 21, 2019
d06564e
Add testnet path, Update change and address_index to be non-hardened
riordant May 21, 2019
992dcba
Fix wallet upgrade issue
riordant May 22, 2019
80e5183
Merge remote-tracking branch 'origin/add-spends-limit' into hdmint
riordant May 22, 2019
ad9b33c
Fix parameter error
riordant May 22, 2019
06b1cd7
Fix failing test after merge
riordant May 23, 2019
f3b18ec
- Fix RPC functions and make available
riordant May 24, 2019
4fc8b15
Merge branch 'hdmint' of https://github.com/zcoinofficial/zcoin into …
riordant May 24, 2019
2c493c2
Merge remote-tracking branch 'origin/sigma-fix-rpc' into hdmint
riordant May 24, 2019
9813e59
Merge remote-tracking branch 'origin/add-spends-limit' into hdmint
riordant May 24, 2019
40cd000
Merge remote-tracking branch 'origin/fix-floating-tests' into hdmint
riordant May 29, 2019
c8d4645
Merge remote-tracking branch 'origin/sigma' into HEAD
riordant Jun 1, 2019
ac93492
Fixes from comments
riordant Jun 5, 2019
a976f98
Merge remote-tracking branch 'origin/sigma' into HEAD
riordant Jun 5, 2019
4017ef3
Fix error
riordant Jun 6, 2019
befae64
Fix Scalar functions
riordant Jun 6, 2019
fb150b9
Add tests
riordant Jun 10, 2019
a0e9fa4
Include test in build
riordant Jun 10, 2019
4c2c50d
Qt issue fixes
riordant Jun 10, 2019
c1fa037
Merge remote-tracking branch 'origin/sigma' into hdmint
riordant Jun 14, 2019
69eb9f6
Fix for failing test
riordant Jun 15, 2019
dad0bd2
Try new count should seed fail at pubkey creation
riordant Jun 17, 2019
da4f1f3
Fix issue with count updating
riordant Jun 18, 2019
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
10 changes: 10 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ BITCOIN_CORE_H = \
policy/rbf.h \
fixed.h \
pow.h \
hdmint/hdmint.h \
primitives/zerocoin.h \
protocol.h \
random.h \
reverselock.h \
Expand Down Expand Up @@ -207,6 +209,9 @@ BITCOIN_CORE_H = \
zmq/zmqconfig.h\
zmq/zmqnotificationinterface.h \
zmq/zmqpublishnotifier.h \
hdmint/mintpool.h \
hdmint/tracker.h \
hdmint/wallet.h \
zerocoin.h \
zerocoin_v3.h \
hash_functions.h \
Expand Down Expand Up @@ -284,12 +289,15 @@ libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_wallet_a_SOURCES = \
activeznode.cpp \
darksend.cpp \
hdmint/hdmint.cpp \
znode.cpp \
instantx.cpp \
znode-payments.cpp \
znode-sync.cpp \
znodeconfig.cpp \
znodeman.cpp \
hdmint/mintpool.cpp \
hdmint/wallet.cpp \
zerocoin_v3.cpp \
wallet/crypter.cpp \
wallet/db.cpp \
Expand All @@ -301,6 +309,7 @@ libbitcoin_wallet_a_SOURCES = \
wallet/wallet.cpp \
wallet/walletdb.cpp \
wallet/authhelper.cpp \
hdmint/tracker.cpp \
policy/rbf.cpp \
$(BITCOIN_CORE_H)

Expand Down Expand Up @@ -400,6 +409,7 @@ libbitcoin_common_a_SOURCES = \
compressor.cpp \
core_read.cpp \
core_write.cpp \
hdmint/hdmint.cpp \
key.cpp \
keystore.cpp \
netbase.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 @@ -76,6 +76,7 @@ BITCOIN_TESTS =\
test/DoS_tests.cpp \
test/getarg_tests.cpp \
test/hash_tests.cpp \
test/hdmint_tests.cpp \
test/key_tests.cpp \
test/limitedmap_tests.cpp \
test/dbwrapper_tests.cpp \
Expand Down
58 changes: 56 additions & 2 deletions src/arith_uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ double base_uint<BITS>::getdouble() const
template <unsigned int BITS>
std::string base_uint<BITS>::GetHex() const
{
return ArithToUint256(*this).GetHex();
return ArithToUint(*this).GetHex();
}

template <unsigned int BITS>
void base_uint<BITS>::SetHex(const char* psz)
{
*this = UintToArith256(uint256S(psz));
base_blob<BITS> a;
*this = UintToArith(a.uintS(psz));
}

template <unsigned int BITS>
Expand Down Expand Up @@ -183,6 +184,24 @@ unsigned int base_uint<BITS>::bits() const
return 0;
}

template <unsigned int BITS>
base_uint<BITS> base_uint<BITS>::UintToArith(const base_blob<BITS>& a) const
{
base_uint<BITS> b;
for(int x=0; x<b.WIDTH; ++x)
b.pn[x] = ReadLE32(a.begin() + x*4);
return b;
}

template <unsigned int BITS>
base_blob<BITS> base_uint<BITS>::ArithToUint(const base_uint<BITS>& a) const
{
base_blob<BITS> b;
for(int x=0; x<a.WIDTH; ++x)
WriteLE32(b.begin() + x*4, a.pn[x]);
return b;
}

// Explicit instantiations for base_uint<256>
template base_uint<256>::base_uint(const std::string&);
template base_uint<256>& base_uint<256>::operator<<=(unsigned int);
Expand All @@ -198,6 +217,8 @@ template std::string base_uint<256>::ToString() const;
template void base_uint<256>::SetHex(const char*);
template void base_uint<256>::SetHex(const std::string&);
template unsigned int base_uint<256>::bits() const;
template base_uint<256> base_uint<256>::UintToArith(const base_blob<256>& a) const;
template base_blob<256> base_uint<256>::ArithToUint(const base_uint<256>& a) const;

// This implementation directly uses shifts instead of going
// through an intermediate MPI representation.
Expand Down Expand Up @@ -258,3 +279,36 @@ arith_uint256 UintToArith256(const uint256 &a)
b.pn[x] = ReadLE32(a.begin() + x*4);
return b;
}

// Explicit instantiations for base_uint<512>
template base_uint<512>::base_uint(const std::string&);
template base_uint<512>& base_uint<512>::operator<<=(unsigned int);
template base_uint<512>& base_uint<512>::operator>>=(unsigned int);
template base_uint<512>& base_uint<512>::operator*=(uint32_t b32);
template base_uint<512>& base_uint<512>::operator*=(const base_uint<512>& b);
template base_uint<512>& base_uint<512>::operator/=(const base_uint<512>& b);
template int base_uint<512>::CompareTo(const base_uint<512>&) const;
template bool base_uint<512>::EqualTo(uint64_t) const;
template double base_uint<512>::getdouble() const;
template std::string base_uint<512>::GetHex() const;
template std::string base_uint<512>::ToString() const;
template void base_uint<512>::SetHex(const char*);
template void base_uint<512>::SetHex(const std::string&);
template unsigned int base_uint<512>::bits() const;
template base_uint<512> base_uint<512>::UintToArith(const base_blob<512>& a) const;
template base_blob<512> base_uint<512>::ArithToUint(const base_uint<512>& a) const;

uint512 ArithToUint512(const arith_uint512 &a)
{
uint512 b;
for(int x=0; x<a.WIDTH; ++x)
WriteLE32(b.begin() + x*4, a.pn[x]);
return b;
}
arith_uint512 UintToArith512(const uint512 &a)
{
arith_uint512 b;
for(int x=0; x<b.WIDTH; ++x)
b.pn[x] = ReadLE32(a.begin() + x*4);
return b;
}
18 changes: 18 additions & 0 deletions src/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdint.h>
#include <string>
#include <vector>
#include <uint256.h>

class uint256;

Expand Down Expand Up @@ -236,6 +237,9 @@ class base_uint
return sizeof(pn);
}

base_uint<BITS> UintToArith(const base_blob<BITS>& a) const;
base_blob<BITS> ArithToUint(const base_uint<BITS>& a) const;

/**
* Returns the position of the highest bit set plus one, or zero if the
* value is zero.
Expand Down Expand Up @@ -295,7 +299,21 @@ class arith_uint256 : public base_uint<256> {
friend arith_uint256 UintToArith256(const uint256 &);
};

/** 512-bit unsigned big integer. */
thebevrishot marked this conversation as resolved.
Show resolved Hide resolved
class arith_uint512 : public base_uint<512> {
public:
arith_uint512() {}
arith_uint512(const base_uint<512>& b) : base_uint<512>(b) {}
arith_uint512(uint64_t b) : base_uint<512>(b) {}
explicit arith_uint512(const std::string& str) : base_uint<512>(str) {}
friend uint512 ArithToUint512(const arith_uint512 &);
friend arith_uint512 UintToArith512(const uint512 &);
};

uint256 ArithToUint256(const arith_uint256 &);
arith_uint256 UintToArith256(const uint256 &);

uint512 ArithToUint512(const arith_uint512 &);
arith_uint512 UintToArith512(const uint512 &);

#endif // BITCOIN_ARITH_UINT256_H
48 changes: 48 additions & 0 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "crypto/ripemd160.h"
#include "crypto/sha256.h"
#include "crypto/sha512.h"
#include "prevector.h"
#include "serialize.h"
#include "uint256.h"
Expand Down Expand Up @@ -66,6 +67,53 @@ class CHash160 {
}
};

class CHash512
{
private:
CSHA512 sha;

public:
static const size_t OUTPUT_SIZE = CSHA512::OUTPUT_SIZE;

void Finalize(unsigned char hash[OUTPUT_SIZE])
{
unsigned char buf[CSHA512::OUTPUT_SIZE];
sha.Finalize(buf);
sha.Reset().Write(buf, CSHA512::OUTPUT_SIZE).Finalize(hash);
}

CHash512& Write(const unsigned char* data, size_t len)
{
sha.Write(data, len);
return *this;
}

CHash512& Reset()
{
sha.Reset();
return *this;
}
};


/** Compute the 512-bit hash of an object. */
template <typename T1>
inline uint512 Hash512(const T1 pbegin, const T1 pend)
{
static const unsigned char pblank[1] = {};
uint512 result;
CHash512().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])).Finalize((unsigned char*)&result);
return result;
}
template <typename T1, typename T2>
inline uint512 Hash512(const T1 p1begin, const T1 p1end, const T2 p2begin, const T2 p2end)
{
static const unsigned char pblank[1] = {};
uint512 result;
CHash512().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])).Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])).Finalize((unsigned char*)&result);
return result;
}

/** Compute the 256-bit hash of an object. */
template<typename T1>
inline uint256 Hash(const T1 pbegin, const T1 pend)
Expand Down
38 changes: 38 additions & 0 deletions src/hdmint/hdmint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2019 The Zcoin Core Developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <tinyformat.h>
#include "hdmint.h"

CHDMint::CHDMint()
{
SetNull();
}

CHDMint::CHDMint(const uint32_t& nCount, const CKeyID& seedId, const uint256& hashSerial, const GroupElement& pubCoinValue)
{
SetNull();
this->nCount = nCount;
this->seedId = seedId;
this->hashSerial = hashSerial;
this->pubCoinValue = pubCoinValue;
}

void CHDMint::SetNull()
{
nCount = 0;
seedId.SetNull();
hashSerial.SetNull();
txid.SetNull();
nHeight = -1;
nId = -1;
DenominationToInteger(sigma::CoinDenomination::SIGMA_ERROR, denom);
isUsed = false;
}

std::string CHDMint::ToString() const
{
return strprintf(" HDMint:\n count=%d\n seedId=%s\n hashSerial=%s\n hashPubCoinValue=%s\n txid=%s\n height=%d\n id=%d\n denom=%d\n isUsed=%d\n",
nCount, seedId.ToString(), hashSerial.GetHex(), GetPubCoinHash().GetHex(), txid.GetHex(), nHeight, nId, denom, isUsed);
}
78 changes: 78 additions & 0 deletions src/hdmint/hdmint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2019 The Zcoin Core Developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef ZCOIN_HDMINT_H
#define ZCOIN_HDMINT_H

#include "primitives/zerocoin.h"
#include "zerocoin_v3.h"

//struct that is safe to store essential mint data, without holding any information that allows for actual spending (serial, randomness, private key)
class CHDMint
{
private:
uint32_t nCount;
CKeyID seedId;
uint256 hashSerial;
GroupElement pubCoinValue;
uint256 txid;
int nHeight;
int nId;
int64_t denom;
bool isUsed;

public:
CHDMint();
CHDMint(const uint32_t& nCount, const CKeyID& seedId, const uint256& hashSerial, const GroupElement& pubCoinValue);

sigma::CoinDenomination GetDenomination() const {
sigma::CoinDenomination value;
IntegerToDenomination(denom, value);
return value;
}
int64_t GetDenominationValue() const {
return denom;
}
uint32_t GetCount() const { return nCount; }
int GetHeight() const { return nHeight; }
int GetId() const { return nId; }
CKeyID GetSeedId() const { return seedId; }
uint256 GetSerialHash() const { return hashSerial; }
GroupElement GetPubcoinValue() const { return pubCoinValue; }
uint256 GetPubCoinHash() const { return sigma::GetPubCoinValueHash(pubCoinValue); }
uint256 GetTxHash() const { return txid; }
bool IsUsed() const { return isUsed; }
void SetDenomination(const sigma::CoinDenomination value) {
int64_t denom;
DenominationToInteger(value, denom);
this->denom = denom;
};
void SetDenominationValue(const int64_t& denom) { this->denom = denom; }
void SetHeight(const int& nHeight) { this->nHeight = nHeight; }
void SetId(const int& nId) { this->nId = nId; }
void SetNull();
void SetTxHash(const uint256& txid) { this->txid = txid; }
void SetUsed(const bool isUsed) { this->isUsed = isUsed; }
void SetPubcoinValue(const GroupElement pubCoinValue) { this->pubCoinValue = pubCoinValue; }
std::string ToString() const;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(nCount);
READWRITE(seedId);
READWRITE(hashSerial);
READWRITE(pubCoinValue);
READWRITE(txid);
READWRITE(nHeight);
READWRITE(nId);
READWRITE(denom);
READWRITE(isUsed);
};
};

#endif //ZCOIN_HDMINT_H

Loading