From ea9178d73b606cf66aa3f5b984afbff735de1035 Mon Sep 17 00:00:00 2001 From: alex v Date: Thu, 16 Feb 2023 17:27:50 +0100 Subject: [PATCH 01/73] tx fields blsct --- src/Makefile.am | 13 ++- src/Makefile.test_util.include | 2 + src/blsct/arith/elements.cpp | 19 +++- src/blsct/arith/elements.h | 28 +++++ src/blsct/arith/mcl/atomic_mcl_init.h | 6 +- src/blsct/arith/mcl/mcl_g1point.cpp | 8 +- src/blsct/arith/mcl/mcl_g1point.h | 2 - src/blsct/arith/mcl/mcl_scalar.cpp | 45 ++++---- src/blsct/arith/mcl/mcl_scalar.h | 11 +- src/blsct/range_proof/range_proof.cpp | 57 ++++++++++ src/blsct/range_proof/range_proof.h | 20 +++- src/blsct/signature.cpp | 23 +--- src/blsct/signature.h | 22 +++- src/crypto/muhash.cpp | 50 +++++---- src/crypto/muhash.h | 6 +- src/ctokens/tokenid.h | 28 ++--- src/primitives/transaction.cpp | 28 ++--- src/primitives/transaction.h | 150 +++++++++++++++++++++----- src/test/util/setup_common.h | 5 +- 19 files changed, 377 insertions(+), 146 deletions(-) create mode 100644 src/blsct/range_proof/range_proof.cpp diff --git a/src/Makefile.am b/src/Makefile.am index cdf32dbaac285..d4b50ce753535 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -421,6 +421,7 @@ libbitcoin_node_a_SOURCES = \ blsct/range_proof/generators.cpp \ blsct/range_proof/lazy_g1point.cpp \ blsct/range_proof/range_proof_logic.cpp \ + blsct/range_proof/range_proof.cpp \ blsct/range_proof/range_proof_with_transcript.cpp \ blsct/signature.cpp \ chain.cpp \ @@ -661,6 +662,11 @@ libbitcoin_consensus_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_consensus_a_SOURCES = \ arith_uint256.cpp \ arith_uint256.h \ + blsct/arith/elements.cpp \ + blsct/arith/mcl/mcl_g1point.cpp \ + blsct/arith/mcl/mcl_scalar.cpp \ + blsct/range_proof/range_proof.cpp \ + blsct/signature.cpp \ consensus/amount.h \ consensus/merkle.cpp \ consensus/merkle.h \ @@ -685,6 +691,7 @@ libbitcoin_consensus_a_SOURCES = \ script/script_error.h \ serialize.h \ span.h \ + streams.h \ tinyformat.h \ uint256.cpp \ uint256.h \ @@ -699,6 +706,7 @@ libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_common_a_SOURCES = \ base58.cpp \ bech32.cpp \ + blsct/arith/elements.cpp \ chainparams.cpp \ coins.cpp \ common/bloom.cpp \ @@ -745,6 +753,7 @@ endif libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_util_a_SOURCES = \ + blsct/arith/elements.cpp \ support/lockedpool.cpp \ chainparamsbase.cpp \ clientversion.cpp \ @@ -971,6 +980,8 @@ libbitcoinkernel_la_SOURCES = \ blsct/private_key.cpp \ blsct/public_key.cpp \ blsct/public_keys.cpp \ + blsct/range_proof/range_proof.cpp \ + blsct/signature.cpp \ chain.cpp \ chainparamsbase.cpp \ chainparams.cpp \ @@ -1065,7 +1076,7 @@ libbitcoinconsensus_la_LIBADD = \ $(LIBBLS) \ $(LIBMCL) \ $(LIBSECP256K1) -libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL +libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include $(BLS_INCLUDES) -DBUILD_BITCOIN_INTERNAL libbitcoinconsensus_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) endif diff --git a/src/Makefile.test_util.include b/src/Makefile.test_util.include index 80bb083eb29d8..ede84fa10d3c1 100644 --- a/src/Makefile.test_util.include +++ b/src/Makefile.test_util.include @@ -17,6 +17,7 @@ TEST_UTIL_H = \ blsct/private_key.h \ blsct/public_key.h \ blsct/public_keys.h \ + blsct/signature.h \ test/util/blockfilter.h \ test/util/chainstate.h \ test/util/logging.h \ @@ -44,6 +45,7 @@ libtest_util_a_SOURCES = \ blsct/private_key.cpp \ blsct/public_key.cpp \ blsct/public_keys.cpp \ + blsct/signature.h \ test/util/blockfilter.cpp \ test/util/logging.cpp \ test/util/mining.cpp \ diff --git a/src/blsct/arith/elements.cpp b/src/blsct/arith/elements.cpp index 7f0d7647f08f1..145d3eefea575 100644 --- a/src/blsct/arith/elements.cpp +++ b/src/blsct/arith/elements.cpp @@ -3,8 +3,8 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include #include +#include #include template @@ -36,8 +36,8 @@ Elements::Elements(const Elements& other) { m_vec = other.m_vec; } -template Elements::Elements(const Elements &x); -template Elements::Elements(const Elements &x); +template Elements::Elements(const Elements& x); +template Elements::Elements(const Elements& x); template bool Elements::Empty() const @@ -60,7 +60,8 @@ template MclScalar Elements::Sum() const; template MclG1Point Elements::Sum() const; template -void Elements::ConfirmIndexInsideRange(const uint32_t& index) const { +void Elements::ConfirmIndexInsideRange(const uint32_t& index) const +{ if (index >= m_vec.size()) { auto s = strprintf("index %d is out of range [0..%d]", index, m_vec.size() - 1ul); throw std::runtime_error(s); @@ -103,6 +104,14 @@ void Elements::Add(const T& x) template void Elements::Add(const MclScalar&); template void Elements::Add(const MclG1Point&); +template +void Elements::Clear() +{ + m_vec.clear(); +} +template void Elements::Clear(); +template void Elements::Clear(); + template inline void Elements::ConfirmSizesMatch(const size_t& other_size) const { @@ -279,7 +288,7 @@ template Elements Elements::Negate() const { Elements ret; - for(auto& x: m_vec) { + for (auto& x : m_vec) { ret.Add(x.Negate()); } return ret; diff --git a/src/blsct/arith/elements.h b/src/blsct/arith/elements.h index 9651dae6e8397..c94feb5939054 100644 --- a/src/blsct/arith/elements.h +++ b/src/blsct/arith/elements.h @@ -9,6 +9,8 @@ #ifndef NAVCOIN_BLSCT_ARITH_ELEMENTS_H #define NAVCOIN_BLSCT_ARITH_ELEMENTS_H +#include + #include #include #include @@ -27,6 +29,7 @@ class Elements T operator[](const size_t& index) const; size_t Size() const; void Add(const T& x); + void Clear(); bool Empty() const; void ConfirmIndexInsideRange(const uint32_t& index) const; @@ -86,6 +89,31 @@ class Elements */ Elements Negate() const; + template + void Serialize(Stream& s) const + { + ::WriteCompactSize(s, m_vec.size()); + for (auto& it : m_vec) { + ::Serialize(s, it.GetVch()); + } + } + + template + void Unserialize(Stream& s) + { + size_t v_size; + v_size = ::ReadCompactSize(s); + m_vec.resize(v_size); + Clear(); + for (auto i = 0; i < v_size; i++) { + T n; + std::vector v; + ::Unserialize(s, v); + n.SetVch(v); + Add(n); + } + } + std::vector m_vec; }; diff --git a/src/blsct/arith/mcl/atomic_mcl_init.h b/src/blsct/arith/mcl/atomic_mcl_init.h index bb2a630595f2b..124cecb060167 100644 --- a/src/blsct/arith/mcl/atomic_mcl_init.h +++ b/src/blsct/arith/mcl/atomic_mcl_init.h @@ -7,18 +7,19 @@ #define BLS_ETH 1 #include -#include #include #include +#include +#include class AtomicMclInit { public: AtomicMclInit() { - boost::lock_guard lock(m_init_mutex); static bool is_initialized = false; if (is_initialized) return; + boost::lock_guard lock(m_init_mutex); if (blsInit(MCL_BLS12_381, MCLBN_COMPILED_TIME_VAR) != 0) { throw std::runtime_error("blsInit failed"); @@ -27,6 +28,7 @@ class AtomicMclInit is_initialized = true; } + private: inline static boost::mutex m_init_mutex; }; diff --git a/src/blsct/arith/mcl/mcl_g1point.cpp b/src/blsct/arith/mcl/mcl_g1point.cpp index ce772fe2ccae5..47f4472c3810b 100644 --- a/src/blsct/arith/mcl/mcl_g1point.cpp +++ b/src/blsct/arith/mcl/mcl_g1point.cpp @@ -6,6 +6,8 @@ #include #include +static volatile StaticMclInit for_side_effect_only; + mclBnG1 MclG1Point::m_g = MclG1Point("1 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"s).m_p; MclG1Point::MclG1Point() @@ -84,8 +86,8 @@ std::vector MclG1Point::operator*(const std::vector& ss) std::vector ret; MclG1Point p = *this; - for(size_t i = 0; i < ss.size(); ++i) { - MclG1Point q = p * ss[i]; + for (size_t i = 0; i < ss.size(); ++i) { + MclG1Point q = p * ss[i]; ret.push_back(q); } return ret; @@ -200,7 +202,7 @@ size_t MclG1Point::GetSerializeSize() const MclScalar MclG1Point::GetHashWithSalt(const uint64_t salt) const { - CHashWriter hasher(0,0); + CHashWriter hasher(0, 0); hasher << *this; hasher << salt; MclScalar hash(hasher.GetHash()); diff --git a/src/blsct/arith/mcl/mcl_g1point.h b/src/blsct/arith/mcl/mcl_g1point.h index b8abc1aa66db7..f3e00abc26f02 100644 --- a/src/blsct/arith/mcl/mcl_g1point.h +++ b/src/blsct/arith/mcl/mcl_g1point.h @@ -17,8 +17,6 @@ #include #include -static volatile StaticMclInit for_side_effect_only; - class MclG1Point { public: diff --git a/src/blsct/arith/mcl/mcl_scalar.cpp b/src/blsct/arith/mcl/mcl_scalar.cpp index 6cdaec94955f7..7f2a9aaac9353 100644 --- a/src/blsct/arith/mcl/mcl_scalar.cpp +++ b/src/blsct/arith/mcl/mcl_scalar.cpp @@ -6,21 +6,21 @@ MclScalar::MclScalar(const int64_t& n) { - mclBnFr_setInt(&m_fr, n); // this takes int64_t + mclBnFr_setInt(&m_fr, n); // this takes int64_t } -MclScalar::MclScalar(const std::vector &v) +MclScalar::MclScalar(const std::vector& v) { MclScalar::SetVch(v); } template -MclScalar::MclScalar(const std::array &a) +MclScalar::MclScalar(const std::array& a) { std::vector v(a.cbegin(), a.cend()); MclScalar::SetVch(v); } -template MclScalar::MclScalar(const std::array &v); +template MclScalar::MclScalar(const std::array& v); MclScalar::MclScalar(const mclBnFr& other_fr) { @@ -41,28 +41,28 @@ MclScalar::MclScalar(const std::string& s, int radix) } } -MclScalar MclScalar::operator+(const MclScalar &rhs) const +MclScalar MclScalar::operator+(const MclScalar& rhs) const { MclScalar ret; mclBnFr_add(&ret.m_fr, &m_fr, &rhs.m_fr); return ret; } -MclScalar MclScalar::operator-(const MclScalar &rhs) const +MclScalar MclScalar::operator-(const MclScalar& rhs) const { MclScalar ret; mclBnFr_sub(&ret.m_fr, &m_fr, &rhs.m_fr); return ret; } -MclScalar MclScalar::operator*(const MclScalar &rhs) const +MclScalar MclScalar::operator*(const MclScalar& rhs) const { MclScalar ret; mclBnFr_mul(&ret.m_fr, &m_fr, &rhs.m_fr); return ret; } -MclScalar MclScalar::operator/(const MclScalar &rhs) const +MclScalar MclScalar::operator/(const MclScalar& rhs) const { MclScalar ret; mclBnFr_div(&ret.m_fr, &m_fr, &rhs.m_fr); @@ -70,7 +70,7 @@ MclScalar MclScalar::operator/(const MclScalar &rhs) const } MclScalar MclScalar::ApplyBitwiseOp(const MclScalar& a, const MclScalar& b, - std::function op) const + std::function op) const { MclScalar ret; auto a_vec = a.GetVch(); @@ -98,19 +98,19 @@ MclScalar MclScalar::ApplyBitwiseOp(const MclScalar& a, const MclScalar& b, return ret; } -MclScalar MclScalar::operator|(const MclScalar &rhs) const +MclScalar MclScalar::operator|(const MclScalar& rhs) const { auto op = [](uint8_t a, uint8_t b) -> uint8_t { return a | b; }; return ApplyBitwiseOp(*this, rhs, op); } -MclScalar MclScalar::operator^(const MclScalar &rhs) const +MclScalar MclScalar::operator^(const MclScalar& rhs) const { auto op = [](uint8_t a, uint8_t b) -> uint8_t { return a ^ b; }; return ApplyBitwiseOp(*this, rhs, op); } -MclScalar MclScalar::operator&(const MclScalar &rhs) const +MclScalar MclScalar::operator&(const MclScalar& rhs) const { auto op = [](uint8_t a, uint8_t b) -> uint8_t { return a & b; }; return ApplyBitwiseOp(*this, rhs, op); @@ -120,7 +120,7 @@ MclScalar MclScalar::operator~() const { // Getting complement of lower 8 bytes only since when 32-byte buffer is fully complemented, // mclBrFr_deserialize returns undesired result - const int64_t n_complement_scalar = (int64_t) ~GetUint64(); + const int64_t n_complement_scalar = (int64_t)~GetUint64(); MclScalar ret(n_complement_scalar); return ret; @@ -172,17 +172,17 @@ bool MclScalar::operator==(const int32_t& rhs) const return mclBnFr_isEqual(&m_fr, &temp.m_fr); } -bool MclScalar::operator==(const MclScalar &rhs) const +bool MclScalar::operator==(const MclScalar& rhs) const { return mclBnFr_isEqual(&m_fr, &rhs.m_fr); } -bool MclScalar::operator!=(const int &b) const +bool MclScalar::operator!=(const int& b) const { return !operator==(b); } -bool MclScalar::operator!=(const MclScalar &b) const +bool MclScalar::operator!=(const MclScalar& b) const { return !operator==(b); } @@ -269,7 +269,7 @@ uint64_t MclScalar::GetUint64() const uint64_t ret = 0; std::vector vch = GetVch(); for (auto i = 0; i < 8; ++i) { - ret |= (uint64_t) vch[vch.size() - 1 - i] << i * 8; + ret |= (uint64_t)vch[vch.size() - 1 - i] << i * 8; } return ret; } @@ -286,14 +286,14 @@ std::vector MclScalar::GetVch(const bool trim_preceeding_zeros) const std::vector trimmed_vec; bool take_char = false; - for (auto c: vec) { + for (auto c : vec) { if (!take_char && c != '\0') take_char = true; if (take_char) trimmed_vec.push_back(c); } return trimmed_vec; } -void MclScalar::SetVch(const std::vector &v) +void MclScalar::SetVch(const std::vector& v) { if (v.size() == 0) { mclBnFr x; @@ -320,7 +320,7 @@ void MclScalar::SetPow2(const uint32_t& n) uint256 MclScalar::GetHashWithSalt(const uint64_t& salt) const { - CHashWriter hasher(0,0); + CHashWriter hasher(0, 0); hasher << *this; hasher << salt; return hasher.GetHash(); @@ -354,7 +354,7 @@ bool MclScalar::GetSeriBit(const uint8_t& n) const std::vector vch = GetVch(); assert(vch.size() == SERIALIZATION_SIZE); - const uint8_t vchIdx = 31 - n / 8; // vch is little-endian + const uint8_t vchIdx = 31 - n / 8; // vch is little-endian const uint8_t bitIdx = n % 8; const uint8_t mask = 1 << bitIdx; const bool bit = (vch[vchIdx] & mask) != 0; @@ -372,7 +372,9 @@ void MclScalar::Serialize(Stream& s) const { ::Serialize(s, GetVch()); } + template void MclScalar::Serialize(CHashWriter& s) const; +template void MclScalar::Serialize(CDataStream& s) const; template void MclScalar::Unserialize(Stream& s) @@ -381,3 +383,4 @@ void MclScalar::Unserialize(Stream& s) ::Unserialize(s, vch); SetVch(vch); } +template void MclScalar::Unserialize(CDataStream& s); diff --git a/src/blsct/arith/mcl/mcl_scalar.h b/src/blsct/arith/mcl/mcl_scalar.h index 44111d87418dc..058a33f0dcc71 100644 --- a/src/blsct/arith/mcl/mcl_scalar.h +++ b/src/blsct/arith/mcl/mcl_scalar.h @@ -9,17 +9,19 @@ #ifndef NAVCOIN_BLSCT_ARITH_MCL_MCL_SCALAR_H #define NAVCOIN_BLSCT_ARITH_MCL_MCL_SCALAR_H +#include #include #include #include #include -#include #define BLS_ETH 1 #include +#include #include #include #include +#include #include #include @@ -30,15 +32,16 @@ class MclScalar public: MclScalar(const int64_t& n = 0); MclScalar(const std::vector& v); - template MclScalar(const std::array& a); + template + MclScalar(const std::array& a); MclScalar(const mclBnFr& n_fr); MclScalar(const uint256& n); MclScalar(const std::string& s, int radix); MclScalar ApplyBitwiseOp(const MclScalar& a, const MclScalar& b, - std::function op) const; + std::function op) const; - void operator=(const int64_t& n); // using int64_t instead of uint64_t since underlying mcl lib takes int64_t + void operator=(const int64_t& n); // using int64_t instead of uint64_t since underlying mcl lib takes int64_t MclScalar operator+(const MclScalar& b) const; MclScalar operator-(const MclScalar& b) const; diff --git a/src/blsct/range_proof/range_proof.cpp b/src/blsct/range_proof/range_proof.cpp new file mode 100644 index 0000000000000..3c368d4d75b87 --- /dev/null +++ b/src/blsct/range_proof/range_proof.cpp @@ -0,0 +1,57 @@ +// 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 + +template +std::vector SerializeRangeProof(const RangeProof& proof) +{ + CDataStream s(0, 0); + + s << proof.Vs; + s << proof.Ls; + s << proof.Rs; + s << proof.A; + s << proof.S; + s << proof.T1; + s << proof.T2; + s << proof.tau_x; + s << proof.mu; + s << proof.a; + s << proof.b; + s << proof.t_hat; + + Span spanStream(s); + std::vector vRet(spanStream.size()); + memcpy(&vRet[0], spanStream.begin(), spanStream.size()); + + return vRet; +} +template std::vector SerializeRangeProof(const RangeProof& proof); + +template +RangeProof UnserializeRangeProof(const std::vector& vecIn) +{ + using Point = typename T::Point; + using Scalar = typename T::Scalar; + + RangeProof retProof; + CDataStream s(vecIn, 0, 0); + + s >> retProof.Vs; + s >> retProof.Ls; + s >> retProof.Rs; + s >> retProof.A; + s >> retProof.S; + s >> retProof.T1; + s >> retProof.T2; + s >> retProof.tau_x; + s >> retProof.mu; + s >> retProof.a; + s >> retProof.b; + s >> retProof.t_hat; + + return retProof; +} +template RangeProof UnserializeRangeProof(const std::vector& vecIn); diff --git a/src/blsct/range_proof/range_proof.h b/src/blsct/range_proof/range_proof.h index c11e44e47521a..09ff03bc68400 100644 --- a/src/blsct/range_proof/range_proof.h +++ b/src/blsct/range_proof/range_proof.h @@ -6,10 +6,14 @@ #define NAVCOIN_BLSCT_RANGE_PROOF_RANGE_PROOF_H #include +#include +#include +#include +#include +#include template -struct RangeProof -{ +struct RangeProof { using Point = typename T::Point; using Scalar = typename T::Scalar; using Points = Elements; @@ -26,9 +30,15 @@ struct RangeProof Points Rs; // proof results - Scalar t_hat; // inner product of l and r - Scalar a; // result of inner product argument - Scalar b; // result of inner product argument + Scalar t_hat; // inner product of l and r + Scalar a; // result of inner product argument + Scalar b; // result of inner product argument }; +template +std::vector SerializeRangeProof(const RangeProof& proof); + +template +RangeProof UnserializeRangeProof(const std::vector& vecIn); + #endif // NAVCOIN_BLSCT_RANGE_PROOF_RANGE_PROOF_H diff --git a/src/blsct/signature.cpp b/src/blsct/signature.cpp index ecb92c9e0a936..b8d54d47e171b 100644 --- a/src/blsct/signature.cpp +++ b/src/blsct/signature.cpp @@ -23,27 +23,6 @@ Signature Signature::Aggregate(const std::vector& sigs) return aggr_sig; } -size_t Signature::GetSerializeSize(int nVersion) const -{ - return ::GetSerializeSize(GetVch(), nVersion); -} - -template -void Signature::Serialize(Stream& s) const -{ - ::Serialize(s, GetVch()); -} -template void Signature::Serialize(CDataStream& s) const; - -template -void Signature::Unserialize(Stream& s) -{ - std::vector vch; - ::Unserialize(s, vch); - SetVch(vch); -} -template void Signature::Unserialize(CDataStream& s); - std::vector Signature::GetVch() const { size_t ser_size = mclBn_getFpByteSize() * 2; @@ -67,4 +46,4 @@ void Signature::SetVch(const std::vector& buf) } } -} // namespace blsct +} // namespace blsct diff --git a/src/blsct/signature.h b/src/blsct/signature.h index 39bbf0c0c3b37..ad83ccd97275d 100644 --- a/src/blsct/signature.h +++ b/src/blsct/signature.h @@ -10,6 +10,7 @@ #define BLS_ETH 1 #include +#include #include #include #include @@ -23,17 +24,28 @@ class Signature std::vector GetVch() const; void SetVch(const std::vector& b); - size_t GetSerializeSize(int nVersion = PROTOCOL_VERSION) const; + size_t GetSerializeSize(int nVersion = PROTOCOL_VERSION) const + { + return ::GetSerializeSize(GetVch(), nVersion); + } template - void Serialize(Stream& s) const; + void Serialize(Stream& s) const + { + s << GetVch(); + } template - void Unserialize(Stream& s); + void Unserialize(Stream& s) + { + std::vector vch; + s >> vch; + SetVch(vch); + } blsSignature m_data; }; -} // namespace blsct +} // namespace blsct -#endif // NAVCOIN_BLSCT_SIGNATURE_H +#endif // NAVCOIN_BLSCT_SIGNATURE_H diff --git a/src/crypto/muhash.cpp b/src/crypto/muhash.cpp index 26f024866390f..17b92732368e5 100644 --- a/src/crypto/muhash.cpp +++ b/src/crypto/muhash.cpp @@ -116,7 +116,8 @@ inline void addnextract2(limb_t& c0, limb_t& c1, const limb_t& a, limb_t& n) /** in_out = in_out^(2^sq) * mul */ inline void square_n_mul(Num3072& in_out, const int sq, const Num3072& mul) { - for (int j = 0; j < sq; ++j) in_out.Square(); + for (int j = 0; j < sq; ++j) + in_out.Square(); in_out.Multiply(mul); } @@ -154,7 +155,8 @@ Num3072 Num3072::GetInverse() const for (int i = 0; i < 11; ++i) { p[i + 1] = p[i]; - for (int j = 0; j < (1 << i); ++j) p[i + 1].Square(); + for (int j = 0; j < (1 << i); ++j) + p[i + 1].Square(); p[i + 1].Multiply(p[i]); } @@ -187,15 +189,18 @@ void Num3072::Multiply(const Num3072& a) for (int j = 0; j < LIMBS - 1; ++j) { limb_t d0 = 0, d1 = 0, d2 = 0; mul(d0, d1, this->limbs[1 + j], a.limbs[LIMBS + j - (1 + j)]); - for (int i = 2 + j; i < LIMBS; ++i) muladd3(d0, d1, d2, this->limbs[i], a.limbs[LIMBS + j - i]); + for (int i = 2 + j; i < LIMBS; ++i) + muladd3(d0, d1, d2, this->limbs[i], a.limbs[LIMBS + j - i]); mulnadd3(c0, c1, c2, d0, d1, d2, MAX_PRIME_DIFF); - for (int i = 0; i < j + 1; ++i) muladd3(c0, c1, c2, this->limbs[i], a.limbs[j - i]); + for (int i = 0; i < j + 1; ++i) + muladd3(c0, c1, c2, this->limbs[i], a.limbs[j - i]); extract3(c0, c1, c2, tmp.limbs[j]); } /* Compute limb N-1 of a*b into tmp. */ assert(c2 == 0); - for (int i = 0; i < LIMBS; ++i) muladd3(c0, c1, c2, this->limbs[i], a.limbs[LIMBS - 1 - i]); + for (int i = 0; i < LIMBS; ++i) + muladd3(c0, c1, c2, this->limbs[i], a.limbs[LIMBS - 1 - i]); extract3(c0, c1, c2, tmp.limbs[LIMBS - 1]); /* Perform a second reduction. */ @@ -223,16 +228,19 @@ void Num3072::Square() /* Compute limbs 0..N-2 of this*this into tmp, including one reduction. */ for (int j = 0; j < LIMBS - 1; ++j) { limb_t d0 = 0, d1 = 0, d2 = 0; - for (int i = 0; i < (LIMBS - 1 - j) / 2; ++i) muldbladd3(d0, d1, d2, this->limbs[i + j + 1], this->limbs[LIMBS - 1 - i]); + for (int i = 0; i < (LIMBS - 1 - j) / 2; ++i) + muldbladd3(d0, d1, d2, this->limbs[i + j + 1], this->limbs[LIMBS - 1 - i]); if ((j + 1) & 1) muladd3(d0, d1, d2, this->limbs[(LIMBS - 1 - j) / 2 + j + 1], this->limbs[LIMBS - 1 - (LIMBS - 1 - j) / 2]); mulnadd3(c0, c1, c2, d0, d1, d2, MAX_PRIME_DIFF); - for (int i = 0; i < (j + 1) / 2; ++i) muldbladd3(c0, c1, c2, this->limbs[i], this->limbs[j - i]); + for (int i = 0; i < (j + 1) / 2; ++i) + muldbladd3(c0, c1, c2, this->limbs[i], this->limbs[j - i]); if ((j + 1) & 1) muladd3(c0, c1, c2, this->limbs[(j + 1) / 2], this->limbs[j - (j + 1) / 2]); extract3(c0, c1, c2, tmp.limbs[j]); } assert(c2 == 0); - for (int i = 0; i < LIMBS / 2; ++i) muldbladd3(c0, c1, c2, this->limbs[i], this->limbs[LIMBS - 1 - i]); + for (int i = 0; i < LIMBS / 2; ++i) + muldbladd3(c0, c1, c2, this->limbs[i], this->limbs[LIMBS - 1 - i]); extract3(c0, c1, c2, tmp.limbs[LIMBS - 1]); /* Perform a second reduction. */ @@ -255,7 +263,8 @@ void Num3072::Square() void Num3072::SetToOne() { this->limbs[0] = 1; - for (int i = 1; i < LIMBS; ++i) this->limbs[i] = 0; + for (int i = 1; i < LIMBS; ++i) + this->limbs[i] = 0; } void Num3072::Divide(const Num3072& a) @@ -275,7 +284,8 @@ void Num3072::Divide(const Num3072& a) if (this->IsOverflow()) this->FullReduce(); } -Num3072::Num3072(const unsigned char (&data)[BYTE_SIZE]) { +Num3072::Num3072(const unsigned char (&data)[SIZE_BYTE]) +{ for (int i = 0; i < LIMBS; ++i) { if (sizeof(limb_t) == 4) { this->limbs[i] = ReadLE32(data + 4 * i); @@ -285,7 +295,8 @@ Num3072::Num3072(const unsigned char (&data)[BYTE_SIZE]) { } } -void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) { +void Num3072::ToBytes(unsigned char (&out)[SIZE_BYTE]) +{ for (int i = 0; i < LIMBS; ++i) { if (sizeof(limb_t) == 4) { WriteLE32(out + i * 4, this->limbs[i]); @@ -295,11 +306,12 @@ void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) { } } -Num3072 MuHash3072::ToNum3072(Span in) { - unsigned char tmp[Num3072::BYTE_SIZE]; +Num3072 MuHash3072::ToNum3072(Span in) +{ + unsigned char tmp[Num3072::SIZE_BYTE]; uint256 hashed_in{(HashWriter{} << in).GetSHA256()}; - ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::BYTE_SIZE); + ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::SIZE_BYTE); Num3072 out{tmp}; return out; @@ -313,9 +325,9 @@ MuHash3072::MuHash3072(Span in) noexcept void MuHash3072::Finalize(uint256& out) noexcept { m_numerator.Divide(m_denominator); - m_denominator.SetToOne(); // Needed to keep the MuHash object valid + m_denominator.SetToOne(); // Needed to keep the MuHash object valid - unsigned char data[Num3072::BYTE_SIZE]; + unsigned char data[Num3072::SIZE_BYTE]; m_numerator.ToBytes(data); out = (HashWriter{} << data).GetSHA256(); @@ -335,12 +347,14 @@ MuHash3072& MuHash3072::operator/=(const MuHash3072& div) noexcept return *this; } -MuHash3072& MuHash3072::Insert(Span in) noexcept { +MuHash3072& MuHash3072::Insert(Span in) noexcept +{ m_numerator.Multiply(ToNum3072(in)); return *this; } -MuHash3072& MuHash3072::Remove(Span in) noexcept { +MuHash3072& MuHash3072::Remove(Span in) noexcept +{ m_denominator.Multiply(ToNum3072(in)); return *this; } diff --git a/src/crypto/muhash.h b/src/crypto/muhash.h index 53c5a91a03d9a..b56efa97b9ffd 100644 --- a/src/crypto/muhash.h +++ b/src/crypto/muhash.h @@ -22,7 +22,7 @@ class Num3072 Num3072 GetInverse() const; public: - static constexpr size_t BYTE_SIZE = 384; + static constexpr size_t SIZE_BYTE = 384; #ifdef __SIZEOF_INT128__ typedef unsigned __int128 double_limb_t; @@ -49,10 +49,10 @@ class Num3072 void Divide(const Num3072& a); void SetToOne(); void Square(); - void ToBytes(unsigned char (&out)[BYTE_SIZE]); + void ToBytes(unsigned char (&out)[SIZE_BYTE]); Num3072() { this->SetToOne(); }; - Num3072(const unsigned char (&data)[BYTE_SIZE]); + Num3072(const unsigned char (&data)[SIZE_BYTE]); SERIALIZE_METHODS(Num3072, obj) { diff --git a/src/ctokens/tokenid.h b/src/ctokens/tokenid.h index 5e9ef345bbcae..86efa6c5d547e 100644 --- a/src/ctokens/tokenid.h +++ b/src/ctokens/tokenid.h @@ -6,8 +6,9 @@ #ifndef BITCOIN_CTOKENS_TOKENID_H #define BITCOIN_CTOKENS_TOKENID_H -#include #include +#include +#include class TokenId { @@ -15,17 +16,23 @@ class TokenId uint256 token; uint64_t subid; - TokenId(const uint256& t = uint256(), const uint64_t& i = std::numeric_limits::max()) : token(t), subid(i){} + TokenId(const uint256& t = uint256(), const uint64_t& i = std::numeric_limits::max()) : token(t), subid(i) {} - void SetNull() { token = uint256(); subid = -1; } + void SetNull() + { + token = uint256(); + subid = std::numeric_limits::max(); + } bool IsNull() const { return token == uint256() && subid == std::numeric_limits::max(); } + std::string ToString() const { return strprintf("%s%s", token.ToString(), subid == std::numeric_limits::max() ? "" : strprintf("#%d", subid)); } + friend bool operator==(const TokenId& a, const TokenId& b) { return a.token == b.token && a.subid == b.subid; } - friend bool operator<(const TokenId& a, const TokenId& b) { - if (a.token == b.token) - { + friend bool operator<(const TokenId& a, const TokenId& b) + { + if (a.token == b.token) { return a.subid < b.subid; } return a.token < b.token; @@ -33,14 +40,7 @@ class TokenId friend bool operator!=(const TokenId& a, const TokenId& b) { return !(a == b); } - // TODO add definition of this and uncomment - // ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - READWRITE(token); - READWRITE(subid); - } + SERIALIZE_METHODS(TokenId, obj) { READWRITE(obj.token, obj.subid); } }; #endif // BITCOIN_CTOKENS_TOKENID_H diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index 3060746909e2d..56876374d83d9 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -19,7 +19,7 @@ std::string COutPoint::ToString() const { - return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n); + return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0, 10), n); } CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) @@ -51,19 +51,20 @@ std::string CTxIn::ToString() const return str; } -CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn) +CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn, TokenId tokenIdIn) { nValue = nValueIn; scriptPubKey = scriptPubKeyIn; + tokenId = tokenIdIn; } std::string CTxOut::ToString() const { - return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30)); + return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30), tokenId.IsNull() ? "" : strprintf(", tokenId=%s", tokenId.ToString())); } CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} -CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {} +CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), balanceSig(tx.balanceSig), txSig(tx.txSig) {} uint256 CMutableTransaction::GetHash() const { @@ -83,8 +84,8 @@ uint256 CTransaction::ComputeWitnessHash() const return SerializeHash(*this, SER_GETHASH, 0); } -CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} -CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} +CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), balanceSig(tx.balanceSig), txSig(tx.txSig), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} +CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), balanceSig(tx.balanceSig), txSig(tx.txSig), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} CAmount CTransaction::GetValueOut() const { @@ -103,15 +104,16 @@ unsigned int CTransaction::GetTotalSize() const return ::GetSerializeSize(*this, PROTOCOL_VERSION); } -std::string CTransaction::ToString() const +std::string CTransaction::ToString(bool fIncludeSignatures) const { std::string str; - str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n", - GetHash().ToString().substr(0,10), - nVersion, - vin.size(), - vout.size(), - nLockTime); + str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u%s)\n", + GetHash().ToString().substr(0, 10), + nVersion, + vin.size(), + vout.size(), + nLockTime, + fIncludeSignatures ? strprintf(", balanceSig=%s, txSig=%s", HexStr(balanceSig.GetVch()), HexStr(txSig.GetVch())) : ""); for (const auto& tx_in : vin) str += " " + tx_in.ToString() + "\n"; for (const auto& tx_in : vin) diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index bd7eb16becf6a..a6ca9cb6409ec 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -6,7 +6,11 @@ #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H #define BITCOIN_PRIMITIVES_TRANSACTION_H +#include +#include +#include #include +#include #include #include