From 105f41006d30382002da552c26542a1ef9619065 Mon Sep 17 00:00:00 2001 From: Daniel Krawisz Date: Thu, 25 Aug 2022 09:45:15 -0500 Subject: [PATCH] Update to latest release of data --- conanfile.py | 2 +- extern/data | 2 +- include/gigamonkey/accounts.hpp | 2 +- include/gigamonkey/hash.hpp | 2 +- include/gigamonkey/number.hpp | 276 +++++++++++----------- include/gigamonkey/satoshi.hpp | 4 +- include/gigamonkey/script/instruction.hpp | 8 +- include/gigamonkey/script/machine.hpp | 4 +- include/gigamonkey/work/target.hpp | 2 +- src/gigamonkey/schema/hd.cpp | 19 +- test/testNumber.cpp | 4 +- test/testScript.cpp | 2 +- 12 files changed, 167 insertions(+), 160 deletions(-) diff --git a/conanfile.py b/conanfile.py index fbb7f59c..a151b996 100644 --- a/conanfile.py +++ b/conanfile.py @@ -15,7 +15,7 @@ class GigamonkeyConan(ConanFile): default_options = {"shared": False, "fPIC": True} generators = "cmake" exports_sources = "*" - requires = "boost/1.76.0", "openssl/1.1.1k", "cryptopp/8.5.0", "nlohmann_json/3.10.0", "gmp/6.2.1", "SECP256K1/0.1@proofofwork/stable", "data/0.2@proofofwork/stable" + requires = "boost/1.76.0", "openssl/1.1.1k", "cryptopp/8.5.0", "nlohmann_json/3.10.0", "gmp/6.2.1", "SECP256K1/0.1@proofofwork/stable", "data/v0.0.5@proofofwork/unstable" def set_version(self): if "CIRCLE_TAG" in environ: diff --git a/extern/data b/extern/data index ab4a389f..0544ae9a 160000 --- a/extern/data +++ b/extern/data @@ -1 +1 @@ -Subproject commit ab4a389f2e06a270decfd1e965b6c69fdc347891 +Subproject commit 0544ae9abcee2171d21765a28aa2694cc4eab1f5 diff --git a/include/gigamonkey/accounts.hpp b/include/gigamonkey/accounts.hpp index b1354585..bca6dc9d 100644 --- a/include/gigamonkey/accounts.hpp +++ b/include/gigamonkey/accounts.hpp @@ -113,7 +113,7 @@ namespace Gigamonkey::Bitcoin { if (txs.empty()) return *this; return data::fold([](const account& a, const event& e) -> account { return a.reduce(e); - }, account{}, txs.values()); + }, account{}, txs); return reduce(txs.first()).reduce(txs.rest()); } }; diff --git a/include/gigamonkey/hash.hpp b/include/gigamonkey/hash.hpp index bf1e9b17..ca2ce4cd 100644 --- a/include/gigamonkey/hash.hpp +++ b/include/gigamonkey/hash.hpp @@ -19,7 +19,7 @@ namespace Gigamonkey { operator bytes_view() const; - explicit operator math::number::N() const; + explicit operator N() const; byte* begin(); byte* end(); diff --git a/include/gigamonkey/number.hpp b/include/gigamonkey/number.hpp index f1ce99af..fe2dda35 100644 --- a/include/gigamonkey/number.hpp +++ b/include/gigamonkey/number.hpp @@ -8,17 +8,10 @@ #include -#include -#include +#include #include -#include -#include -#include -#include - -#include -#include +#include namespace Gigamonkey { @@ -87,14 +80,14 @@ namespace Gigamonkey { template bool operator>(int, const uint&);*/ // a representation of uints of any size. - template struct uint : public base_uint<8 * size> { - static const unsigned int bits = 8 * size; + template struct uint : public base_uint<8 * X> { + static const unsigned int bits = 8 * X; uint(base_uint &&b) : base_uint{b} {} uint(const base_uint &b) : base_uint{b} {} uint(uint64 u) : base_uint(u) {} uint() : uint(0) {} - uint(const slice); + uint(const slice); // valid inputs are a hexidecimal number, which will be written // to the digest in little endian (in other words, reversed @@ -102,16 +95,16 @@ namespace Gigamonkey { // written to the digest as given, without reversing. explicit uint(string_view hex); - explicit uint(const data::math::number::N& n); + explicit uint(const data::N& n); explicit uint(const ::uint256&); - explicit operator data::math::number::N() const; + explicit operator data::N() const; explicit operator float64() const; operator bytes_view() const; - operator slice(); - operator const slice() const; + operator slice(); + operator const slice() const; uint& operator=(uint64_t b); uint& operator=(const base_uint& b); @@ -139,7 +132,7 @@ namespace Gigamonkey { uint& operator-=(const uint &); uint& operator*=(const uint &); - math::division> divide(const uint &) const; + math::division> divide(const uint &) const; uint operator/(const uint &) const; uint operator%(const uint &) const; @@ -165,19 +158,22 @@ namespace Gigamonkey { explicit operator string() const; const byte& operator[](int i) const { - if (i < 0) return operator[](size + i); + if (i < 0) return operator[](X + i); return data()[i]; } byte& operator[](int i) { - if (i < 0) return operator[](size + i); + if (i < 0) return operator[](X + i); return data()[i]; } size_t serialized_size() const; + size_t size() const { + return X; + } - const data::arithmetic::digits digits() const { - return data::arithmetic::digits{data::slice(*this)}; + const data::encoding::words digits() const { + return data::encoding::words{data::slice(*this)}; } }; @@ -236,6 +232,10 @@ namespace Gigamonkey { static bytes minus(bytes_view a, bytes_view b); static bytes times(bytes_view a, bytes_view b); + integer operator&(const integer&) const; + integer operator|(const integer&) const; + integer operator^(const integer&) const; + integer operator-() const; integer operator+(const integer&) const; @@ -266,8 +266,8 @@ namespace Gigamonkey { explicit integer(string_view x); explicit integer(const integer&); - data::arithmetic::digits digits() { - return data::arithmetic::digits{data::slice(*this)}; + data::encoding::words digits() { + return data::encoding::words{data::slice(*this)}; } protected: @@ -279,6 +279,9 @@ namespace Gigamonkey { natural() : integer{} {} + natural operator&(const natural&) const; + natural operator|(const natural&) const; + natural operator+(const natural&) const; natural operator-(const natural&) const; natural operator*(const natural&) const; @@ -319,18 +322,7 @@ namespace Gigamonkey { namespace Gigamonkey::Bitcoin { using N = natural; - using Z = integer; - - using Q = data::math::fraction; - - // Gaussian numbers (complex rationals) - using G = data::math::complex; - - // rational quaternions - using H = data::math::quaternion; - - // rational octonions - using O = data::math::octonion; + using Z = integer; } @@ -353,8 +345,8 @@ namespace Gigamonkey { namespace data::encoding::hexidecimal { template - inline std::string write(const Gigamonkey::uint& n) { - return write((math::number::N)(n)); + inline string write(const Gigamonkey::uint& n) { + return write((data::N)(n)); } template @@ -364,15 +356,15 @@ namespace data::encoding::hexidecimal { } -namespace data::encoding::integer { +namespace data::encoding::decimal { template - inline std::string write(const Gigamonkey::uint& n) { - return write((math::number::N)(n)); + string inline write(const Gigamonkey::uint& n) { + return write((data::math::N)(n)); } template - inline std::ostream& write(std::ostream& o, const Gigamonkey::uint& n) { + std::ostream inline &write(std::ostream& o, const Gigamonkey::uint& n) { return o << write(n); } @@ -380,15 +372,15 @@ namespace data::encoding::integer { namespace Gigamonkey { - template - inline uint::uint(const slice x) { + template + inline uint::uint(const slice x) { std::copy(x.begin(), x.end(), begin()); } - template - uint::operator math::number::N() const { - math::number::N n(0); - int width = size / 4; + template + uint::operator N() const { + N n(0); + int width = X / 4; int i; for (i = width - 1; i > 0; i--) { uint32 step = boost::endian::load_little_u32(data() + 4 * i); @@ -399,39 +391,39 @@ namespace Gigamonkey { return n; } - template - inline uint::uint(string_view hex) : uint(0) { - if (hex.size() != size * 2 + 2) return; + template + inline uint::uint(string_view hex) : uint(0) { + if (hex.size() != X * 2 + 2) return; if (!data::encoding::hexidecimal::valid(hex)) return; ptr read = encoding::hex::read(hex.substr(2)); std::reverse_copy(read->begin(), read->end(), begin()); } - template - uint::uint(const math::number::N& n) : uint(0) { + template + uint::uint(const N& n) : uint(0) { ptr b = encoding::hex::read(encoding::hexidecimal::write(n).substr(2)); std::reverse(b->begin(), b->end()); - if (b->size() > size) std::copy(b->begin(), b->begin() + size, begin()); + if (b->size() > X) std::copy(b->begin(), b->begin() + X, begin()); else std::copy(b->begin(), b->end(), begin()); } - template - inline uint::operator bytes_view() const { - return bytes_view{data(), size}; + template + inline uint::operator bytes_view() const { + return bytes_view{data(), X}; } - template - inline uint::operator slice() { - return slice(data()); + template + inline uint::operator slice() { + return slice(data()); } - template - inline uint::operator const slice() const { - return slice(const_cast(data())); + template + inline uint::operator const slice() const { + return slice(const_cast(data())); } - template - inline uint::operator float64() const { + template + inline uint::operator float64() const { if ((*this) == 0) return 0; // first we have to find the mantissa bis. @@ -466,202 +458,202 @@ namespace Gigamonkey { mantissa >>= 1; } - return ldexp(significand, size * 8 - from_left); + return ldexp(significand, X * 8 - from_left); } - template - inline uint& uint::operator=(uint64_t b) { + template + inline uint& uint::operator=(uint64_t b) { base_uint::operator=(b); return *this; } - template - inline uint& uint::operator=(const base_uint& b) { + template + inline uint& uint::operator=(const base_uint& b) { base_uint::operator=(b); return *this; } - template - inline uint& uint::operator^=(const uint& b) { + template + inline uint& uint::operator^=(const uint& b) { base_uint::operator^=(b); return *this; } - template - inline uint& uint::operator&=(const uint& b) { + template + inline uint& uint::operator&=(const uint& b) { base_uint::operator&=(b); return *this; } - template - inline uint& uint::operator|=(const uint& b) { + template + inline uint& uint::operator|=(const uint& b) { base_uint::operator|=(b); return *this; } - template - inline uint& uint::operator<<=(unsigned int shift) { + template + inline uint& uint::operator<<=(unsigned int shift) { base_uint::operator<<=(shift); return *this; } - template - inline uint& uint::operator>>=(unsigned int shift) { + template + inline uint& uint::operator>>=(unsigned int shift) { base_uint::operator>>=(shift); return *this; } - template - inline uint uint::operator<<(unsigned int shift) const { - return uint(*this) <<= shift; + template + inline uint uint::operator<<(unsigned int shift) const { + return uint(*this) <<= shift; } - template - inline uint uint::operator>>(unsigned int shift) const { - return uint(*this) >>= shift; + template + inline uint uint::operator>>(unsigned int shift) const { + return uint(*this) >>= shift; } - template - inline uint& uint::operator+=(const uint& b) { + template + inline uint& uint::operator+=(const uint& b) { base_uint::operator+=(b); return *this; } - template - inline uint& uint::operator-=(const uint& b) { + template + inline uint& uint::operator-=(const uint& b) { base_uint::operator-=(b); return *this; } - template - inline uint& uint::operator*=(const uint& b) { + template + inline uint& uint::operator*=(const uint& b) { base_uint::operator*=(b); return *this; } - template - inline uint& uint::operator++() { + template + inline uint& uint::operator++() { base_uint::operator++(); return *this; } - template - inline const uint uint::operator++(int) { + template + inline const uint uint::operator++(int) { // postfix operator const uint ret = *this; ++(*this); return ret; } - template - inline uint& uint::operator--() { + template + inline uint& uint::operator--() { base_uint::operator--(); return *this; } - template - inline const uint uint::operator--(int) { + template + inline const uint uint::operator--(int) { // postfix operator const uint ret = *this; --(*this); return ret; } - template uint inline uint::operator~() { + template uint inline uint::operator~() { return ~base_uint(*this); } - template uint inline uint::operator^(const uint &b) { + template uint inline uint::operator^(const uint &b) { return base_uint(*this) ^= b; } - template uint inline uint::operator&(const uint &b) { + template uint inline uint::operator&(const uint &b) { return base_uint(*this) &= b; } - template uint inline uint::operator|(const uint &b) { + template uint inline uint::operator|(const uint &b) { return base_uint(*this) |= b; } - template uint inline uint::operator+(const uint &b) { + template uint inline uint::operator+(const uint &b) { return base_uint(*this) += b; } - template uint inline uint::operator-(const uint &b) { + template uint inline uint::operator-(const uint &b) { return base_uint(*this) -= b; } - template uint inline uint::operator*(const uint &b) { + template uint inline uint::operator*(const uint &b) { return base_uint(*this) *= b; } - template math::division> inline uint::divide(const uint &u) const { + template math::division> inline uint::divide(const uint &u) const { return math::number::natural::divide(*this, u); } - template uint inline uint::operator/(const uint &u) const { + template uint inline uint::operator/(const uint &u) const { return divide(u).Quotient; } - template uint inline uint::operator%(const uint &u) const { + template uint inline uint::operator%(const uint &u) const { return divide(u).Remainder; } - template uint inline &uint::operator/=(const uint &u) { + template uint inline &uint::operator/=(const uint &u) { return *this = *this / u; } - template uint inline &uint::operator%=(const uint &u) { + template uint inline &uint::operator%=(const uint &u) { return *this = *this % u; } - template - inline byte* uint::begin() { + template + inline byte* uint::begin() { return (byte*)base_uint::pn; } - template - inline byte* uint::end() { - return begin() + size; + template + inline byte* uint::end() { + return begin() + X; } - template - inline const byte* uint::begin() const { + template + inline const byte* uint::begin() const { return (byte*)this->pn; } - template - inline const byte* uint::end() const { - return begin() + size; + template + inline const byte* uint::end() const { + return begin() + X; } - template - inline byte* uint::data() { + template + inline byte* uint::data() { return begin(); } - template - inline const byte* uint::data() const { + template + inline const byte* uint::data() const { return begin(); } - template size_t uint::serialized_size() const { + template size_t uint::serialized_size() const { size_t last_0 = 0; - for (size_t i = 0; i < size; i++) if ((*this)[i] != 0x00) last_0 = i + 1; + for (size_t i = 0; i < X; i++) if ((*this)[i] != 0x00) last_0 = i + 1; return last_0 == 0 ? 1 : (*this)[last_0 - 1] & 0x80 ? last_0 + 2 : last_0 + 1; } - template writer inline &operator<<(writer &w, const uint &u) { + template writer inline &operator<<(writer &w, const uint &u) { return w << byte(0x02) << Bitcoin::var_string{natural(u)}; } - template reader &operator>>(reader &re, uint &u) { + template reader &operator>>(reader &re, uint &u) { byte b; re >> b; if (b != 0x02) throw std::logic_error{"invalid uint format"}; natural n; re >> n; - u = uint(n); + u = uint(n); return re; } @@ -733,6 +725,14 @@ namespace Gigamonkey { return integer::greater(a, b); } + template natural inline natural::operator&(const natural &z) const { + throw 0; + } + + template natural inline natural::operator|(const natural &z) const { + throw 0; + } + template integer inline integer::operator-() const { return integer(negate(*this)); } @@ -745,6 +745,14 @@ namespace Gigamonkey { return integer(plus(*this, -z)); } + template integer inline integer::operator&(const integer &z) const { + throw 0; + } + + template integer inline integer::operator|(const integer &z) const { + throw 0; + } + template integer inline integer::operator*(const integer &z) const { return integer(times(*this, z)); } @@ -938,7 +946,7 @@ namespace Gigamonkey { struct numbers { - template using digits = data::arithmetic::digits; + template using digits = data::encoding::words; private: template friend struct integer; @@ -1102,7 +1110,7 @@ namespace Gigamonkey { data::encoding::integer::read(x.substr(1)) : data::encoding::integer::read(x); - bool has_sign_bit = sign_bit(positive_number->digits()); + bool has_sign_bit = sign_bit(positive_number->words()); bytes b; b.resize(positive_number->size() + (has_sign_bit ? 1 : 0)); @@ -1167,14 +1175,14 @@ namespace Gigamonkey { while (ai != ae) { uint16 shift = (shift << 8) + (uint16(*ai) << mod); - *bi = data::greater_half(shift); + *bi = data::encoding::greater_half(shift); ai++; bi++; } while (bi != be) { uint16 shift = shift << 8; - *bi = data::greater_half(shift); + *bi = data::encoding::greater_half(shift); bi++; } diff --git a/include/gigamonkey/satoshi.hpp b/include/gigamonkey/satoshi.hpp index 6f871d78..b42a74fc 100644 --- a/include/gigamonkey/satoshi.hpp +++ b/include/gigamonkey/satoshi.hpp @@ -44,8 +44,8 @@ namespace Gigamonkey::Bitcoin { namespace data::math { - template <> struct identity, Gigamonkey::Bitcoin::satoshi> { - static const Gigamonkey::Bitcoin::satoshi value() { + template <> struct identity, Gigamonkey::Bitcoin::satoshi> { + Gigamonkey::Bitcoin::satoshi operator()() { return {0}; } }; diff --git a/include/gigamonkey/script/instruction.hpp b/include/gigamonkey/script/instruction.hpp index 622c8287..e65433bf 100644 --- a/include/gigamonkey/script/instruction.hpp +++ b/include/gigamonkey/script/instruction.hpp @@ -129,8 +129,8 @@ namespace Gigamonkey::Bitcoin { instruction push_data(int z); instruction push_data(bytes_view); - template - instruction push_data(const data::endian::arithmetic& x); + template + instruction push_data(const data::endian::arithmetic& x); bool is_minimal(const instruction&); @@ -244,8 +244,8 @@ namespace Gigamonkey::Bitcoin { return push_data(Z{z}); } - template - instruction inline push_data(const data::endian::arithmetic& x) { + template + instruction inline push_data(const data::endian::arithmetic& x) { return push_data(bytes_view(x)); } } diff --git a/include/gigamonkey/script/machine.hpp b/include/gigamonkey/script/machine.hpp index f99da82c..89c8b205 100644 --- a/include/gigamonkey/script/machine.hpp +++ b/include/gigamonkey/script/machine.hpp @@ -66,8 +66,8 @@ namespace Gigamonkey::Bitcoin::interpreter { } program inline full(const program unlock, const program lock) { - if (!isP2SH(lock) || data::empty(unlock)) return unlock << OP_CODESEPARATOR << lock; - return unlock << OP_CODESEPARATOR << lock << OP_CODESEPARATOR << decompile(data::reverse(unlock).first().data()); + if (!isP2SH(lock) || data::empty(unlock)) return (unlock << OP_CODESEPARATOR) + lock; + return (unlock << OP_CODESEPARATOR) + (lock << OP_CODESEPARATOR) + decompile(data::reverse(unlock).first().data()); } ScriptError check_scripts(const program unlock, const program lock, uint32 flags) { diff --git a/include/gigamonkey/work/target.hpp b/include/gigamonkey/work/target.hpp index 412745f8..40964f4e 100644 --- a/include/gigamonkey/work/target.hpp +++ b/include/gigamonkey/work/target.hpp @@ -277,7 +277,7 @@ namespace Gigamonkey::work { inline work::difficulty compact::difficulty() const { return work::difficulty{ double(work::difficulty::unit()) / - double(math::number::N(expand()))}; + double(N(expand()))}; }; inline compact::operator work::difficulty() const { diff --git a/src/gigamonkey/schema/hd.cpp b/src/gigamonkey/schema/hd.cpp index 4c8ab40e..b26eb64d 100644 --- a/src/gigamonkey/schema/hd.cpp +++ b/src/gigamonkey/schema/hd.cpp @@ -18,7 +18,6 @@ //#include #include - namespace Gigamonkey::Bitcoin::hd::bip32 { uint256 CURVE_ORDER = secp256k1::secret::order(); @@ -78,9 +77,9 @@ namespace Gigamonkey::Bitcoin::hd::bip32 { uint256 k = uint256{}; std::copy(sec.Secret.Value.begin(), sec.Secret.Value.end(), k.begin()); std::reverse(k.begin(), k.end()); - auto keyCode = (data::math::number::gmp::N) ll; - keyCode += (data::math::number::gmp::N) k; - keyCode %= (data::math::number::gmp::N) CURVE_ORDER; + auto keyCode = (data::N) ll; + keyCode += (data::N) k; + keyCode %= (data::N) CURVE_ORDER; if (keyCode == 0) return derive(sec, child + 1); @@ -168,13 +167,13 @@ namespace Gigamonkey::Bitcoin::hd::bip32 { return secret(); bytes_reader reader(view.begin() + 3, view.end()); - data::endian::arithmetic depth; + data::endian::arithmetic depth; reader >> depth; secret1.Depth = depth; - data::endian::arithmetic parent; + data::endian::arithmetic parent; reader >> parent; secret1.Parent = parent; - data::endian::arithmetic sequence; + data::endian::arithmetic sequence; reader >> sequence; secret1.Sequence = sequence; bytes_view chain_code = view.substr(12, 32); @@ -328,13 +327,13 @@ namespace Gigamonkey::Bitcoin::hd::bip32 { return pubkey(); bytes_reader reader(view.begin() + 3, view.end()); - data::endian::arithmetic depth; + data::endian::arithmetic depth; reader >> depth; pubkey1.Depth = depth; - data::endian::arithmetic parent; + data::endian::arithmetic parent; reader >> parent; pubkey1.Parent = parent; - data::endian::arithmetic sequence; + data::endian::arithmetic sequence; reader >> sequence; pubkey1.Sequence = sequence; //sequence |= view[12] & 0xff; diff --git a/test/testNumber.cpp b/test/testNumber.cpp index ed3f6401..5d01c7c2 100644 --- a/test/testNumber.cpp +++ b/test/testNumber.cpp @@ -69,12 +69,12 @@ namespace Gigamonkey::Bitcoin { TEST(NumberTest, TestNumberConstructorsDecimalPositive) { - EXPECT_EQ(bytes_view(Z("0")), bytes::from_hex("00")); + EXPECT_EQ(bytes_view(Z("0")), bytes::from_hex("")); EXPECT_EQ(bytes_view(Z("127")), bytes::from_hex("7f")); EXPECT_EQ(bytes_view(Z("128")), bytes::from_hex("8000")); EXPECT_EQ(bytes_view(Z("256")), bytes::from_hex("0001")); - EXPECT_EQ(bytes_view(N("0")), bytes::from_hex("00")); + EXPECT_EQ(bytes_view(N("0")), bytes::from_hex("")); EXPECT_EQ(bytes_view(N("1")), bytes::from_hex("01")); EXPECT_EQ(bytes_view(N("127")), bytes::from_hex("7f")); EXPECT_EQ(bytes_view(N("128")), bytes::from_hex("8000")); diff --git a/test/testScript.cpp b/test/testScript.cpp index d28747e5..717d5136 100644 --- a/test/testScript.cpp +++ b/test/testScript.cpp @@ -37,7 +37,7 @@ namespace Gigamonkey::Bitcoin { ms <<= null_push; for (const secp256k1::secret &sk : s) ms <<= push_data(signature::sign(sk, sighash::all, sd)); ms <<= OP_CODESEPARATOR; - return compile(ms << mp); + return compile(ms + mp); } bytes multisig_script(const redemption_document &doc, list s, list p) {