Skip to content

Commit

Permalink
Keep commonly used functions inline
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Oct 25, 2024
1 parent 57d263d commit e246aa4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 154 deletions.
125 changes: 0 additions & 125 deletions nano/lib/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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 ());
Expand Down Expand Up @@ -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 ());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ());
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
133 changes: 104 additions & 29 deletions nano/lib/numbers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit e246aa4

Please sign in to comment.