15#ifndef CIPHERS_UINT256_T_HPP_
16#define CIPHERS_UINT256_T_HPP_
21struct std::is_integral<
uint256_t> : std::true_type {};
24struct std::is_arithmetic<
uint256_t> : std::true_type {};
27struct std::is_unsigned<
uint256_t> : std::true_type {};
45 if (str.size() > 1 && str[1] ==
'x') {
46 for (
auto i = 2; i < str.size(); ++i) {
48 if (str[i] >=
'0' && str[i] <=
'9') {
49 *
this += (str[i] -
'0');
50 }
else if (str[i] >=
'A' && str[i] <=
'F') {
51 *
this += (str[i] -
'A' + 10);
52 }
else if (str[i] >=
'a' && str[i] <=
'f') {
53 *
this += (str[i] -
'a' + 10);
73 template <
typename T,
typename =
typename std::enable_if<
74 std::is_integral<T>::value, T>::type>
96 : f(std::move(num.f)), s(std::move(num.s)) {}
104 : f(std::move(high)), s(std::move(low)) {}
111 uint256_t(
const uint64_t high,
const uint64_t low) : f(high), s(low) {}
127 return 128 + s.
_lez();
139 return 128 + f.
_trz();
146 inline explicit operator bool()
const {
return f || s; }
153 template <
typename T,
typename =
typename std::enable_if<
154 std::is_integral<T>::value, T>::type>
155 inline explicit operator T()
const {
156 return static_cast<T
>(s);
163 inline explicit operator uint128_t()
const {
return s; }
190 template <
typename T,
typename =
typename std::enable_if<
191 std::is_integral<T>::value, T>::type>
218 template <
typename T,
typename =
typename std::enable_if<
219 std::is_integral<T>::value, T>::type>
221 bool app = s + p < s;
231 bool app = (s + p.s < s);
232 return {f + app + p.f, s + p.s};
241 template <
typename T,
typename =
typename std::enable_if<
242 std::is_integral<T>::value, T>::type>
244 bool app = (p + s < s);
256 bool app = (s + p.s < s);
286 template <
typename T,
typename =
typename std::enable_if<
287 std::is_integral<T>::value, T>::type>
300 return {f - p.f - app, s - p.s};
333 template <
typename T,
typename =
typename std::enable_if<
334 std::is_integral<T>::value, T>::type>
360 template <
typename T,
typename =
typename std::enable_if<
361 std::is_integral<T>::value, T>::type>
373 s_second(p.s.
lower());
374 uint128_t fi = f_first * s_first, se = f_first * s_second,
375 th = s_first * f_second, fo = s_second * f_second;
376 uint128_t tmp = se << 64, tmp2 = th << 64;
377 int cc = (tmp + tmp2 < tmp);
379 cc += (tmp + fo < tmp);
380 return {f * p.s + s * p.f + fi + se.
upper() + th.upper() + cc,
390 template <
typename T,
typename =
typename std::enable_if<
391 std::is_integral<T>::value, T>::type>
403 s_second(p.s.
lower());
404 uint128_t fi = f_first * s_first, se = f_first * s_second,
405 th = s_first * f_second, fo = s_second * f_second;
406 uint128_t tmp = se << 64, tmp2 = th << 64;
407 int cc = (tmp + tmp2 < tmp);
409 cc += (tmp + fo < tmp);
410 f = f * p.s + s * p.f + fi + se.
upper() + th.upper() + cc;
424 }
else if (*
this == p) {
428 uint16_t left = tmp.
_lez() -
_lez();
433 uint16_t shf = tmp2._lez() - tmp.
_lez();
447 return {quotient << left, tmp2};
456 template <
typename T,
typename =
typename std::enable_if<
457 std::is_integral<T>::value, T>::type>
487 template <
typename T,
typename =
typename std::enable_if<
488 std::is_integral<T>::value, T>::type>
507 template <
typename T,
typename =
typename std::enable_if<
508 std::is_integral<T>::value, T>::type>
531 template <
typename T,
typename =
typename std::enable_if<
532 std::is_integral<T>::value, T>::type>
544 return f < other.f || (f == other.f && s < other.s);
553 return f < other.f || (f == other.f && s <= other.s);
562 return f > other.f || (f == other.f && s > other.s);
571 return (f > other.f) || (f == other.f && s >= other.s);
580 return f == other.f && s == other.s;
589 return !((*this) == other);
604 return (s || f) && (b.s || b.f);
613 return (s || f) || (b.s || b.f);
628 template <
typename T,
typename =
typename std::enable_if<
629 std::is_integral<T>::value, T>::type>
640 template <
typename T,
typename =
typename std::enable_if<
641 std::is_integral<T>::value, T>::type>
652 template <
typename T,
typename =
typename std::enable_if<
653 std::is_integral<T>::value, T>::type>
664 template <
typename T,
typename =
typename std::enable_if<
665 std::is_integral<T>::value, T>::type>
676 template <
typename T,
typename =
typename std::enable_if<
677 std::is_integral<T>::value, T>::type>
688 template <
typename T,
typename =
typename std::enable_if<
689 std::is_integral<T>::value, T>::type>
700 template <
typename T,
typename =
typename std::enable_if<
701 std::is_integral<T>::value, T>::type>
703 return (s || f) && (b);
713 template <
typename T,
typename =
typename std::enable_if<
714 std::is_integral<T>::value, T>::type>
716 return (s || f) || (b);
731 template <
typename T,
typename =
typename std::enable_if<
732 std::is_integral<T>::value, T>::type>
735 return {this->f, this->s};
736 }
else if (p >= 128) {
739 return uint256_t((this->f << p) + (this->s >> (128 - p)),
749 template <
typename T,
typename =
typename std::enable_if<
750 std::is_integral<T>::value, T>::type>
754 this->f = (this->s << (p - 128));
757 f = ((this->s >> (128 - p)) + (this->f << p));
770 template <
typename T,
typename =
typename std::enable_if<
771 std::is_integral<T>::value, T>::type>
774 return {this->f, this->s};
775 }
else if (p >= 128) {
779 (this->s >> p) + (this->f << (128 - p)));
788 template <
typename T,
typename =
typename std::enable_if<
789 std::is_integral<T>::value, T>::type>
794 s = (this->f >> (p - 128));
796 s = (this->s >> p) + (this->f << (128 - p));
809 template <
typename T,
typename =
typename std::enable_if<
810 std::is_integral<T>::value, T>::type>
821 return {f & p.f, s & p.s};
841 template <
typename T,
typename =
typename std::enable_if<
842 std::is_integral<T>::value, T>::type>
854 template <
typename T,
typename =
typename std::enable_if<
855 std::is_integral<T>::value, T>::type>
866 return {this->f | p.f, this->s | p.s};
875 template <
typename T,
typename =
typename std::enable_if<
876 std::is_integral<T>::value, T>::type>
899 template <
typename T,
typename =
typename std::enable_if<
900 std::is_integral<T>::value, T>::type>
911 return {this->f ^ p.f, this->s ^ p.s};
931 template <
typename T,
typename =
typename std::enable_if<
932 std::is_integral<T>::value, T>::type>
951 std::string out =
"0", p_2 =
"1";
953 for (uint64_t i = 0; i < 128; ++i) {
961 for (
int i = 0; i < 128; ++i) {
975template <
typename T,
typename =
typename std::enable_if<
976 std::is_integral<T>::value, T>::type>
981template <
typename T,
typename =
typename std::enable_if<
982 std::is_integral<T>::value, T>::type>
987template <
typename T,
typename =
typename std::enable_if<
988 std::is_integral<T>::value, T>::type>
993template <
typename T,
typename =
typename std::enable_if<
994 std::is_integral<T>::value, T>::type>
999template <
typename T,
typename =
typename std::enable_if<
1000 std::is_integral<T>::value, T>::type>
1006template <
typename T,
typename =
typename std::enable_if<
1007 std::is_integral<T>::value, T>::type>
1012template <
typename T,
typename =
typename std::enable_if<
1013 std::is_integral<T>::value, T>::type>
1018template <
typename T,
typename =
typename std::enable_if<
1019 std::is_integral<T>::value, T>::type>
1025template <
typename T,
typename =
typename std::enable_if<
1026 std::is_integral<T>::value, T>::type>
1027inline bool operator&&(
const T p,
const uint256_t &q) {
1031template <
typename T,
typename =
typename std::enable_if<
1032 std::is_integral<T>::value, T>::type>
1033inline bool operator||(
const T p,
const uint256_t &q) {
1038template <
typename T,
typename =
typename std::enable_if<
1039 std::is_integral<T>::value, T>::type>
1040inline bool operator==(
const T p,
const uint256_t &q) {
1044template <
typename T,
typename =
typename std::enable_if<
1045 std::is_integral<T>::value, T>::type>
1046inline bool operator!=(
const T p,
const uint256_t &q) {
1050template <
typename T,
typename =
typename std::enable_if<
1051 std::is_integral<T>::value, T>::type>
1052inline bool operator<(
const T p,
const uint256_t &q) {
1056template <
typename T,
typename =
typename std::enable_if<
1057 std::is_integral<T>::value, T>::type>
1058inline bool operator<=(
const T p,
const uint256_t &q) {
1062template <
typename T,
typename =
typename std::enable_if<
1063 std::is_integral<T>::value, T>::type>
1064inline bool operator>(
const T p,
const uint256_t &q) {
1068template <
typename T,
typename =
typename std::enable_if<
1069 std::is_integral<T>::value, T>::type>
1070inline bool operator>=(
const T p,
const uint256_t &q) {
class for 128-bit unsigned integer
uint64_t upper() const
returns upper 64-bit integer part
uint32_t _trz()
Trailing zeroes in binary.
uint64_t lower() const
returns lower 64-bit integer part
uint32_t _lez()
Leading zeroes in binary.
class for 256-bit unsigned integer
uint256_t(uint128_t high, uint128_t low)
Parameterized constructor.
bool operator!()
operator ! for uint256_t
uint32_t _lez()
Leading zeroes in binary.
uint256_t(uint256_t &&num) noexcept
Move constructor.
uint256_t & operator<<=(const T &p)
operator <<= for uint256_t
bool operator<=(const uint256_t &other)
operator <= for uint256_t
uint256_t(const std::string &str)
Parameterized constructor.
bool operator<=(const T &other)
operator <= for other types
uint256_t operator+(const T &p)
operator + for uint256_t and other integer types.
uint256_t operator--(int p)
operator – (post-decrement)
uint256_t(const uint64_t high, const uint64_t low)
Parameterized constructor.
uint256_t & operator%=(const uint256_t &p)
operator %= for uint256_t
uint256_t operator|(const uint256_t &p)
operator | for uint256_t (bitwise operator)
bool operator&&(const T &b)
operator && for other types
uint256_t & operator&=(const uint256_t &p)
operator &= for uint256_t (bitwise operator)
uint256_t & operator^=(const T &p)
operator ^= for other types (bitwise operator)
~uint256_t()=default
Destructor for uint256_t.
uint256_t operator-()
operator - using twos complement
bool operator||(const uint256_t &b)
operator || for uint256_t
uint256_t(const uint256_t &num)=default
Copy constructor.
uint256_t & operator*=(const uint256_t &p)
operator *= for uint256_t and other integer types.
uint256_t operator>>(const T &p)
operator >> for uint256_t
uint256_t operator<<(const T &p)
operator << for uint256_t
bool operator||(const T &b)
operator || for other types
uint256_t & operator=(const uint256_t &p)=default
operator = for uint256_t
uint256_t operator/(const uint256_t &p)
operator / for uint256_t and other integer types.
uint256_t & operator+=(const T &p)
operator += for uint256_t and other integer types.
uint256_t & operator-=(const uint256_t &p)
operator -= for uint256_t
uint256_t & operator=(uint256_t &&p)=default
Move assignment operator.
uint256_t operator&(const T &p)
operator & for other types (bitwise operator)
uint256_t operator~()
operator ~ for uint256_t
uint256_t operator^(const uint256_t &p)
operator ^ for uint256_t (bitwise operator)
uint256_t & operator%=(const T &p)
operator %= for uint256_t
bool operator()()
operator () for uint256_t
uint256_t operator++(int)
post-increment operator
uint256_t operator%(const T &p)
operator % for uint256_t and other integer types.
std::pair< uint256_t, uint256_t > divide(const uint256_t &p)
divide function for uint256_t and other integer types.
uint256_t & operator=(const std::string &p)
operator = for type string
uint256_t operator-(const T &p)
operator - for uint256_t and other integer types.
bool operator!=(const T &other)
operator != for other types
bool operator==(const uint256_t &other)
operator == for uint256_t
friend std::ostream & operator<<(std::ostream &op, uint256_t p)
operator << for printing uint256_t integer
bool operator==(const T &other)
operator == for other types
uint256_t operator&(const uint256_t &p)
operator & for uint256_t (bitwise operator)
uint32_t _trz()
Trailing zeroes in binary.
uint256_t & operator--()
operator – (pre-decrement)
bool operator&&(const uint256_t &b)
operator && for uint256_t
uint256_t & operator|=(const uint256_t &p)
operator |= for uint256_t (bitwise operator)
uint128_t lower() const
returns lower 128-bit integer part
uint256_t operator*(const uint256_t &p)
operator * for uint256_t and other integer types.
uint256_t operator*(const T &p)
operator * for uint256_t and other integer types.
bool operator!=(const uint256_t &other)
operator != for uint256_t
uint256_t operator-(const uint256_t &p)
operator - for uint256_t
uint256_t & operator/=(const T &p)
operator /= for uint256_t and other integer types.
uint256_t & operator/=(const uint256_t &p)
operator /= for uint256_t
bool operator<(const T &other)
operator < for other types
uint256_t & operator+=(const uint256_t &p)
operator += for uint256_t
uint256_t & operator^=(const uint256_t &p)
operator ^= for uint256_t (bitwise operator)
uint256_t operator/(const T &p)
operator / for uint256_t and other integer types.
bool operator>(const uint256_t &other)
operator > for uint256_t
uint256_t operator^(const T &p)
operator ^ for other types (bitwise operator)
uint256_t operator-=(const T p)
operator -= for uint256_t and other integer types.
uint256_t operator|(const T &p)
operator | for other types (bitwise operator)
uint256_t operator%(const uint256_t &p)
operator % for uint256_t
bool operator>(const T &other)
operator > for other types
uint256_t & operator>>=(const T &p)
operator >>= for uint256_t
uint256_t & operator=(const T &p)
operator = for other types
bool operator>=(const uint256_t &other)
operator >= for uint256_t
uint128_t upper() const
returns upper 128-bit integer part
uint256_t(T low)
Parameterized constructor.
uint256_t & operator|=(const T &p)
operator |= for other types (bitwise operator)
uint256_t & operator++()
pre-increment operator
uint256_t operator+(const uint256_t &p)
operator + for uint256_t and other integer types.
uint256_t & operator*=(const T &p)
operator *= for uint256_t and other integer types.
uint256_t & operator&=(const T p)
operator &= for other types (bitwise operator)
bool operator<(const uint256_t &other)
operator < for uint256_t
void __get_integer_from_string(const std::string &str)
First and second half of 256 bit number.
bool operator>=(const T &other)
operator >= for other types
std::string add(const std::string &first, const std::string &second)
Adding two string.