From e246aa4e40da715b27d76564dad431ec3255773d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 24 Oct 2024 23:18:46 +0200 Subject: [PATCH] Keep commonly used functions inline --- nano/lib/numbers.cpp | 125 ---------------------------------------- nano/lib/numbers.hpp | 133 +++++++++++++++++++++++++++++++++---------- 2 files changed, 104 insertions(+), 154 deletions(-) diff --git a/nano/lib/numbers.cpp b/nano/lib/numbers.cpp index d5cb7f0c97..3d2e888236 100644 --- a/nano/lib/numbers.cpp +++ b/nano/lib/numbers.cpp @@ -61,11 +61,6 @@ std::string nano::public_key::to_account () const return result; } -nano::public_key::public_key () : - uint256_union{ 0 } -{ -} - nano::public_key const & nano::public_key::null () { return nano::hardened_constants::get ().not_an_account; @@ -143,12 +138,6 @@ bool nano::public_key::decode_account (std::string const & source_a) return error; } -nano::uint256_union::uint256_union (nano::uint256_t const & number_a) -{ - bytes.fill (0); - boost::multiprecision::export_bits (number_a, bytes.rbegin (), 8, false); -} - // Construct a uint256_union = AES_ENC_CTR (cleartext, key, iv) void nano::uint256_union::encrypt (nano::raw_key const & cleartext, nano::raw_key const & key, uint128_union const & iv) { @@ -157,11 +146,6 @@ void nano::uint256_union::encrypt (nano::raw_key const & cleartext, nano::raw_ke enc.ProcessData (bytes.data (), cleartext.bytes.data (), sizeof (cleartext.bytes)); } -bool nano::uint256_union::is_zero () const -{ - return qwords[0] == 0 && qwords[1] == 0 && qwords[2] == 0 && qwords[3] == 0; -} - std::string nano::uint256_union::to_string () const { std::string result; @@ -193,22 +177,9 @@ nano::uint256_union nano::uint256_union::operator^ (nano::uint256_union const & nano::uint256_union::uint256_union (std::string const & hex_a) { auto error (decode_hex (hex_a)); - release_assert (!error); } -void nano::uint256_union::clear () -{ - qwords.fill (0); -} - -nano::uint256_t nano::uint256_union::number () const -{ - nano::uint256_t result; - boost::multiprecision::import_bits (result, bytes.begin (), bytes.end ()); - return result; -} - void nano::uint256_union::encode_hex (std::string & text) const { debug_assert (text.empty ()); @@ -281,41 +252,6 @@ bool nano::uint256_union::decode_dec (std::string const & text) return error; } -nano::uint256_union::uint256_union (uint64_t value0) -{ - *this = nano::uint256_t (value0); -} - -nano::uint512_union::uint512_union (nano::uint256_union const & upper, nano::uint256_union const & lower) -{ - uint256s[0] = upper; - uint256s[1] = lower; -} - -nano::uint512_union::uint512_union (nano::uint512_t const & number_a) -{ - bytes.fill (0); - boost::multiprecision::export_bits (number_a, bytes.rbegin (), 8, false); -} - -bool nano::uint512_union::is_zero () const -{ - return qwords[0] == 0 && qwords[1] == 0 && qwords[2] == 0 && qwords[3] == 0 - && qwords[4] == 0 && qwords[5] == 0 && qwords[6] == 0 && qwords[7] == 0; -} - -void nano::uint512_union::clear () -{ - bytes.fill (0); -} - -nano::uint512_t nano::uint512_union::number () const -{ - nano::uint512_t result; - boost::multiprecision::import_bits (result, bytes.begin (), bytes.end ()); - return result; -} - void nano::uint512_union::encode_hex (std::string & text) const { debug_assert (text.empty ()); @@ -350,13 +286,6 @@ bool nano::uint512_union::decode_hex (std::string const & text) return error; } -nano::uint512_union & nano::uint512_union::operator^= (nano::uint512_union const & other_a) -{ - uint256s[0] ^= other_a.uint256s[0]; - uint256s[1] ^= other_a.uint256s[1]; - return *this; -} - std::string nano::uint512_union::to_string () const { std::string result; @@ -421,28 +350,9 @@ bool nano::validate_message (nano::public_key const & public_key, nano::uint256_ nano::uint128_union::uint128_union (std::string const & string_a) { auto error (decode_hex (string_a)); - release_assert (!error); } -nano::uint128_union::uint128_union (uint64_t value_a) -{ - *this = nano::uint128_t (value_a); -} - -nano::uint128_union::uint128_union (nano::uint128_t const & number_a) -{ - bytes.fill (0); - boost::multiprecision::export_bits (number_a, bytes.rbegin (), 8, false); -} - -nano::uint128_t nano::uint128_union::number () const -{ - nano::uint128_t result; - boost::multiprecision::import_bits (result, bytes.begin (), bytes.end ()); - return result; -} - void nano::uint128_union::encode_hex (std::string & text) const { debug_assert (text.empty ()); @@ -717,16 +627,6 @@ std::string nano::uint128_union::format_balance (nano::uint128_t scale, int prec return ::format_balance (number (), scale, precision, group_digits, thousands_sep, decimal_point, grouping); } -void nano::uint128_union::clear () -{ - qwords.fill (0); -} - -bool nano::uint128_union::is_zero () const -{ - return qwords[0] == 0 && qwords[1] == 0; -} - std::string nano::uint128_union::to_string () const { std::string result; @@ -741,31 +641,6 @@ std::string nano::uint128_union::to_string_dec () const return result; } -nano::hash_or_account::hash_or_account () : - account{} -{ -} - -nano::hash_or_account::hash_or_account (uint64_t value_a) : - raw{ value_a } -{ -} - -nano::hash_or_account::hash_or_account (uint256_union const & value_a) : - raw{ value_a } -{ -} - -bool nano::hash_or_account::is_zero () const -{ - return raw.is_zero (); -} - -void nano::hash_or_account::clear () -{ - raw.clear (); -} - bool nano::hash_or_account::decode_hex (std::string const & text_a) { return raw.decode_hex (text_a); diff --git a/nano/lib/numbers.hpp b/nano/lib/numbers.hpp index 82b4786e85..2db23118b2 100644 --- a/nano/lib/numbers.hpp +++ b/nano/lib/numbers.hpp @@ -24,8 +24,13 @@ class uint128_union { public: uint128_union () = default; - uint128_union (uint64_t); - uint128_union (nano::uint128_t const &); + uint128_union (uint64_t value) : + uint128_union (nano::uint128_t{ value }){}; + uint128_union (nano::uint128_t const & value) + { + bytes.fill (0); + boost::multiprecision::export_bits (value, bytes.rbegin (), 8, false); + } /** * Decode from hex string @@ -42,9 +47,21 @@ class uint128_union std::string format_balance (nano::uint128_t scale, int precision, bool group_digits) const; std::string format_balance (nano::uint128_t scale, int precision, bool group_digits, std::locale const & locale) const; - nano::uint128_t number () const; - void clear (); - bool is_zero () const; + void clear () + { + qwords.fill (0); + } + bool is_zero () const + { + return qwords[0] == 0 && qwords[1] == 0; + } + + nano::uint128_t number () const + { + nano::uint128_t result; + boost::multiprecision::import_bits (result, bytes.begin (), bytes.end ()); + return result; + } std::string to_string () const; std::string to_string_dec () const; @@ -100,8 +117,13 @@ class uint256_union { public: uint256_union () = default; - uint256_union (uint64_t); - uint256_union (uint256_t const &); + uint256_union (uint64_t value) : + uint256_union (nano::uint256_t{ value }){}; + uint256_union (uint256_t const & value) + { + bytes.fill (0); + boost::multiprecision::export_bits (value, bytes.rbegin (), 8, false); + } /** * Decode from hex string @@ -119,9 +141,21 @@ class uint256_union void encode_dec (std::string &) const; bool decode_dec (std::string const &); - nano::uint256_t number () const; - void clear (); - bool is_zero () const; + void clear () + { + qwords.fill (0); + } + bool is_zero () const + { + return owords[0].is_zero () && owords[1].is_zero (); + } + + nano::uint256_t number () const + { + nano::uint256_t result; + boost::multiprecision::import_bits (result, bytes.begin (), bytes.end ()); + return result; + } std::string to_string () const; @@ -197,19 +231,17 @@ class public_key final : public uint256_union public: using uint256_union::uint256_union; - public_key (); + public_key () : + uint256_union{ 0 } {}; static const public_key & null (); - std::string to_node_id () const; - bool decode_node_id (std::string const & source_a); + bool decode_node_id (std::string const &); void encode_account (std::string &) const; - std::string to_account () const; bool decode_account (std::string const &); - operator nano::link const & () const; - operator nano::root const & () const; - operator nano::hash_or_account const & () const; + std::string to_node_id () const; + std::string to_account () const; public: // Keep operators inlined auto operator<=> (nano::public_key const & other) const @@ -228,6 +260,18 @@ class public_key final : public uint256_union { return number (); } + operator nano::link () const + { + return nano::link{ *this }; + } + operator nano::root () const + { + return nano::root{ *this }; + } + operator nano::hash_or_account () const + { + return nano::hash_or_account{ *this }; + } }; class wallet_id : public uint256_union @@ -241,16 +285,26 @@ using account = public_key; class hash_or_account { public: - hash_or_account (); - hash_or_account (uint64_t); - explicit hash_or_account (uint256_union const &); + hash_or_account () : + account{} {}; + hash_or_account (uint64_t value) : + raw{ value } {}; + explicit hash_or_account (uint256_union const & value) : + raw{ value } {}; - bool is_zero () const; - void clear (); + void clear () + { + raw.clear (); + } + bool is_zero () const + { + return raw.is_zero (); + } - std::string to_string () const; bool decode_hex (std::string const &); bool decode_account (std::string const &); + + std::string to_string () const; std::string to_account () const; public: @@ -345,18 +399,39 @@ class uint512_union { public: uint512_union () = default; - uint512_union (nano::uint256_union const &, nano::uint256_union const &); - uint512_union (nano::uint512_t const &); + uint512_union (nano::uint256_union const & upper, nano::uint256_union const & lower) : + uint256s{ upper, lower } {}; + uint512_union (nano::uint512_t const & value) + { + bytes.fill (0); + boost::multiprecision::export_bits (value, bytes.rbegin (), 8, false); + } - nano::uint512_union & operator^= (nano::uint512_union const &); + nano::uint512_union & operator^= (nano::uint512_union const & other) + { + uint256s[0] ^= other.uint256s[0]; + uint256s[1] ^= other.uint256s[1]; + return *this; + } void encode_hex (std::string &) const; bool decode_hex (std::string const &); - void clear (); - bool is_zero () const; + void clear () + { + bytes.fill (0); + } + bool is_zero () const + { + return uint256s[0].is_zero () && uint256s[1].is_zero (); + } - nano::uint512_t number () const; + nano::uint512_t number () const + { + nano::uint512_t result; + boost::multiprecision::import_bits (result, bytes.begin (), bytes.end ()); + return result; + } std::string to_string () const;