28 #ifndef LBCRYPTO_MATH_BIGINTDYN_UBINTDYN_H 29 #define LBCRYPTO_MATH_BIGINTDYN_UBINTDYN_H 31 #define NO_BARRETT // currently barrett is slower than mod 39 #include <type_traits> 42 #include "math/nbtheory.h" 43 #include "utils/inttypes.h" 44 #include "utils/memory.h" 45 #include "utils/serializable.h" 50 #define int128_t our_int128_t 52 #define uint128_t our_uint128_t 54 typedef __int128 int128_t;
55 typedef unsigned __int128 uint128_t;
57 #define UINT128_MAX ((uint128_t)-1) 82 static const usint value = 1 +
Log2<N / 2>::value;
92 static const usint value = 1;
101 template <
typename Dtype>
103 static const bool value =
false;
113 static const bool value =
true;
123 static const bool value =
true;
133 static const bool value =
true;
143 static const bool value =
true;
154 static const bool value =
true;
164 template <
typename utype>
213 template <
typename utype>
260 template <
typename utype>
309 template <
typename limb_t>
338 explicit ubint(
const std::string &strval);
345 ubint(
const uint64_t val);
346 #if defined(HAVE_INT128) 347 ubint(
unsigned __int128 val);
358 ubint(
long long val) :
ubint(uint64_t(val)) {}
365 template <
typename T>
374 __attribute__((deprecated(
"Cannot construct from a double")));
400 *
this =
ubint(strval);
433 void SetValue(
const std::string &strval);
440 void SetValue(
const ubint &val);
522 ubint Exp(usint p)
const;
530 const ubint &ExpEq(usint p);
568 const ubint &DivideAndRoundEq(
const ubint &q);
593 ubint ComputeMu()
const;
809 const ubint &bInv)
const {
811 "ModMulFastConst is not implemented for backend 4");
817 "ModMulFastConstEq is not implemented for backend 4");
853 const ubint &ModInverseEq(
const ubint &modulus);
863 ubint LShift(usshort shift)
const;
871 const ubint &LShiftEq(usshort shift);
879 ubint RShift(usshort shift)
const;
887 const ubint &RShiftEq(usshort shift);
898 int Compare(
const ubint &a)
const;
906 template <
typename T = big
intnat::BasicInteger>
909 if (m_value.size() == 0) {
911 "ConvertToInt() on uninitialized bint");
913 if (
sizeof(limb_t) >=
sizeof(T)) {
915 result = (T)m_value[0];
918 uint32_t msbTest =
sizeof(T) * 8;
919 if (msbTest > m_MSB) {
922 usint ceilInt = ceilIntByUInt(msbTest);
924 for (usint i = 0; i < ceilInt; i++) {
925 T tmp = this->m_value[i];
926 tmp <<= (m_limbBitLength * i);
941 float ConvertToFloat()
const;
952 double ConvertToDouble()
const;
963 long double ConvertToLongDouble()
const;
971 static ubint UsintToUbint(usint m);
980 static ubint FromBinaryString(
const std::string &bitString);
989 usint GetMSB()
const;
996 usint GetNumberOfLimbs()
const;
1004 bool isPowerOfTwo(
const ubint &m_numToCheck);
1029 usint GetDigitAtIndexForBase(usint index, usint base)
const;
1034 const std::string GetState()
const;
1041 inline ubint MulIntegerByLimb(limb_t b)
const;
1049 uschar GetBitAtIndex(usint index)
const;
1065 const std::string ToString()
const;
1069 static const std::string IntegerTypeName() {
return "UBDYNINT_32"; }
1072 static const std::string IntegerTypeName() {
return "UBDYNINT_64"; }
1081 std::string ret(
"");
1082 for (
size_t i = 0; i < m_value.size(); i++) {
1083 ret += std::to_string(m_value[i]);
1084 if (i < (m_value.size() - 1)) {
1105 uschar *print_VALUE =
new uschar[ptr_obj.m_numDigitInPrintval]();
1107 for (usint i = ptr_obj.m_MSB; i > 0; i--) {
1108 ubint::double_bitVal(print_VALUE);
1115 for (usint counter = 0; counter < ptr_obj.m_numDigitInPrintval; counter++) {
1116 if (print_VALUE[counter] != 0) {
1120 os << static_cast<int>(print_VALUE[counter]);
1127 delete[] print_VALUE;
1134 void PrintIntegerConstants();
1138 template <
class Archive>
1139 void save(Archive &ar, std::uint32_t
const version)
const {
1140 ar(::cereal::make_nvp(
"v", m_value));
1141 ar(::cereal::make_nvp(
"m", m_MSB));
1142 ar(::cereal::make_nvp(
"s", m_state));
1145 template <
class Archive>
1146 void load(Archive &ar, std::uint32_t
const version) {
1147 if (version > SerializedVersion()) {
1149 "serialized object version " + std::to_string(version) +
1150 " is from a later version of the library");
1152 ar(::cereal::make_nvp(
"v", m_value));
1153 ar(::cereal::make_nvp(
"m", m_MSB));
1154 ar(::cereal::make_nvp(
"s", m_state));
1157 std::string SerializedObjectName()
const {
return "DYNInteger"; }
1159 static uint32_t SerializedVersion() {
return 1; }
1168 void AssignVal(
const std::string &v);
1179 void SetMSB(usint guessIdxChar);
1189 void NormalizeLimbs(
void);
1197 void SetIntAtIndex(usint idx, limb_t value);
1212 vector<limb_t> m_value;
1219 static const usint m_limbBitLength;
1222 static const limb_t m_MaxLimb;
1226 static const usint m_log2LimbBitLength;
1229 static const usint m_nSize;
1233 static const usint m_numDigitInPrintval =
1243 static usint ceilIntByUInt(
1244 const limb_t Number);
1247 static const ubint *m_modChain;
1259 typedef typename DoubleDataType<limb_t>::T Dlimb_t;
1263 typedef typename SignedDataType<limb_t>::T Slimb_t;
1267 typedef typename SignedDoubleDataType<limb_t>::T Sdlimb_t;
1270 enum State { INITIALIZED, GARBAGE };
1287 static limb_t UintInBinaryToDecimal(uschar *a);
1293 static void double_bitVal(uschar *a);
1300 static void add_bitVal(uschar *a, uschar b);
1305 template <
typename limb_t>
1306 inline std::ostream &operator<<(std::ostream &os,
1307 const std::vector<limb_t> &v) {
1309 for (
const auto &itr : v) {
1318 #endif // LBCRYPTO_MATH_BIGINTDYN_UBINTDYN_H Struct to determine a datatype that is the signed version of utype. sets T as of type void for defaul...
Definition: ubintdyn.h:214
Struct to find log value of N. Needed in the preprocessing step of ubint to determine bitwidth...
Definition: ubintdyn.h:81
Struct to determine a datatype that is twice as big(bitwise) as utype. sets T as of type void for def...
Definition: ubintdyn.h:165
uschar GetBitAtIndex(usint index) const
Definition: ubintdyn.cpp:2350
Definition: ubintdyn.h:310
static ubint Allocator()
Definition: ubintdyn.h:1055
usint GetLengthForBase(usint base) const
Definition: ubintdyn.h:1013
Definition: exception.h:119
Definition: exception.h:147
ubint Sub(const ubint &b) const
Definition: ubintdyn.cpp:302
const ubint & operator=(const bigintnat::NativeInteger &val)
Definition: ubintdyn.h:421
Struct for validating if Dtype is amongst {uint8_t, uint16_t, uint32_t, uint64_t, uint128_t}...
Definition: ubintdyn.h:102
std::string GetInternalRepresentation() const
Definition: ubintdyn.h:1080
const double LOG2_10
A pre-computed constant of Log base 2 of 10.
Definition: ubintdyn.h:303
Definition: backend.h:187
friend std::ostream & operator<<(std::ostream &os, const ubint &ptr_obj)
Definition: ubintdyn.h:1100
ubint(int val)
Definition: ubintdyn.h:355
Struct to determine a signed datatype that is twice as big(bitwise) as utype. sets T as of type void ...
Definition: ubintdyn.h:261
const ubint & operator=(const std::string strval)
Definition: ubintdyn.h:399
usint GetMSB64(uint64_t x)
Definition: nbtheory.h:219
ubint(const bigintnat::NativeIntegerT< T > &val)
Definition: ubintdyn.h:366
Definition: interface.h:33
T ConvertToInt() const
Definition: ubintdyn.h:907
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60
Definition: exception.h:126
const ubint & operator=(const uint64_t val)
Definition: ubintdyn.h:410
void SetIdentity()
Definition: ubintdyn.h:445