31 #ifndef LBCRYPTO_MATH_BIGINTFXD_UBINTFXD_H 32 #define LBCRYPTO_MATH_BIGINTFXD_UBINTFXD_H 42 #include <type_traits> 46 #include "utils/inttypes.h" 47 #include "utils/memory.h" 48 #include "utils/palisadebase64.h" 49 #include "utils/serializable.h" 50 #include "math/backend.h" 58 using U64BITS = uint64_t;
59 #if defined(HAVE_INT128) 60 using U128BITS =
unsigned __int128;
76 static const usint value = 1 +
Log2<N / 2>::value;
86 static const usint value = 1;
106 template <
typename Dtype>
108 static const bool value =
false;
117 static const bool value =
true;
126 static const bool value =
true;
135 static const bool value =
true;
144 static const bool value =
true;
153 template <
typename u
int_type>
155 static const int value = 8 *
sizeof(uint_type);
164 template <
typename utype>
202 #if defined(HAVE_INT128) 203 typedef __uint128_t T;
218 template <
typename u
int_type, us
int BITLENGTH>
248 explicit BigInteger(
const std::string &strval);
256 #if defined(HAVE_INT128) 275 template <
typename T>
285 __attribute__((deprecated(
"Cannot construct from a double")));
348 void SetValue(
const std::string &strval);
368 void SetIntAtIndex(usint idx, uint_type value);
748 "ModMulFastConst is not implemented for backend 2");
755 "ModMulFastConstEq is not implemented for backend 2");
845 template <
typename T = big
intnat::BasicInteger>
851 usint ceilInt = m_nSize - ceilIntByUInt(m_MSB);
853 for (usint i = 0; i < num && (m_nSize - i - 1) >= ceilInt; i++) {
854 result += ((T)this->m_value[m_nSize - i - 1] << (m_uintBitLength * i));
859 std::string(
"MSB cannot be bigger than ") +
870 double ConvertToDouble()
const;
886 static BigInteger FromBinaryString(
const std::string &bitString);
895 usint GetMSB()
const;
920 usint GetDigitAtIndexForBase(usint index, usint base)
const;
928 bool CheckIfPowerOfTwo(
const BigInteger &m_numToCheck);
936 uschar GetBitAtIndex(usint index)
const;
952 const std::string ToString()
const;
954 static const std::string IntegerTypeName() {
return "UBFIXINT"; }
963 size_t ceilInt = ceilIntByUInt(this->m_MSB);
965 for (
size_t i = m_nSize - 1; i >= (size_t)(m_nSize - ceilInt); i--) {
966 ret += std::to_string(m_value[i]);
967 if (i != (
size_t)(m_nSize - ceilInt)) ret +=
" ";
979 template <
typename u
int_type_c, us
int BITLENGTH_c>
986 uschar *print_VALUE =
new uschar[ptr_obj.m_numDigitInPrintval];
987 for (
size_t i = 0; i < ptr_obj.m_numDigitInPrintval; i++) {
989 *(print_VALUE + i) = 0;
992 for (
size_t i = print_obj->m_MSB; i > 0; i--) {
1000 for (counter = 0; counter < ptr_obj.m_numDigitInPrintval - 1; counter++) {
1001 if (static_cast<int>(print_VALUE[counter]) != 0) {
1006 for (; counter < ptr_obj.m_numDigitInPrintval; counter++) {
1007 os << static_cast<int>(print_VALUE[counter]);
1010 delete[] print_VALUE;
1017 template <
class Archive>
1018 typename std::enable_if<!cereal::traits::is_text_archive<Archive>::value,
1020 save(Archive &ar, std::uint32_t
const version)
const {
1021 ar(::cereal::binary_data(m_value,
sizeof(m_value)));
1022 ar(::cereal::binary_data(&m_MSB,
sizeof(m_MSB)));
1025 template <
class Archive>
1026 typename std::enable_if<cereal::traits::is_text_archive<Archive>::value,
1028 save(Archive &ar, std::uint32_t
const version)
const {
1029 ar(::cereal::make_nvp(
"v", m_value));
1030 ar(::cereal::make_nvp(
"m", m_MSB));
1033 template <
class Archive>
1034 typename std::enable_if<!cereal::traits::is_text_archive<Archive>::value,
1036 load(Archive &ar, std::uint32_t
const version) {
1037 if (version > SerializedVersion()) {
1039 "serialized object version " + std::to_string(version) +
1040 " is from a later version of the library");
1042 ar(::cereal::binary_data(m_value,
sizeof(m_value)));
1043 ar(::cereal::binary_data(&m_MSB,
sizeof(m_MSB)));
1046 template <
class Archive>
1047 typename std::enable_if<cereal::traits::is_text_archive<Archive>::value,
1049 load(Archive &ar, std::uint32_t
const version) {
1050 if (version > SerializedVersion()) {
1052 "serialized object version " + std::to_string(version) +
1053 " is from a later version of the library");
1055 ar(::cereal::make_nvp(
"v", m_value));
1056 ar(::cereal::make_nvp(
"m", m_MSB));
1059 std::string SerializedObjectName()
const {
return "FXDInteger"; }
1061 static uint32_t SerializedVersion() {
return 1; }
1070 void AssignVal(
const std::string &v);
1081 void SetMSB(usint guessIdxChar);
1086 uint_type m_value[(BITLENGTH + 8 *
sizeof(uint_type) - 1) /
1087 (8 *
sizeof(uint_type))];
1093 static const uschar m_uintBitLength;
1096 static const uint_type m_uintMax;
1100 static const uschar m_logUintBitLength;
1103 static const usint m_nSize;
1107 static const usint m_numDigitInPrintval;
1115 static uint_type ceilIntByUInt(
const uint_type Number);
1126 static usint GetMSBUint_type(uint_type x);
1130 typedef typename DoubleDataType<uint_type>::T Duint_type;
1137 static usint GetMSBDUint_type(Duint_type x);
1144 BigInteger MulByUint(
const uint_type b)
const;
1151 void MulByUintToInt(
const uint_type b,
BigInteger *ans)
const;
1158 static uint_type UintInBinaryToDecimal(uschar *a);
1164 static void double_bitVal(uschar *a);
1171 static void add_bitVal(uschar *a, uschar b);
1178 #endif // LBCRYPTO_MATH_BIGINTFXD_UBINTFXD_H const BigInteger & operator=(uint64_t val)
Definition: ubintfxd.h:324
std::string GetInternalRepresentation(void) const
Definition: ubintfxd.h:961
static BigInteger Allocator()
Definition: ubintfxd.h:942
BigInteger(int val)
Definition: ubintfxd.h:265
Definition: exception.h:119
friend std::ostream & operator<<(std::ostream &os, const BigInteger< uint_type_c, BITLENGTH_c > &ptr_obj)
Definition: ubintfxd.h:980
Definition: exception.h:147
const BigInteger & operator=(const std::string strval)
Definition: ubintfxd.h:313
Definition: exception.h:113
const double LOG2_10
A pre-computed constant of Log base 2 of 10.
Definition: ubintfxd.h:209
T ConvertToInt() const
Definition: ubintfxd.h:846
uschar GetBitAtIndex(usint index) const
Definition: ubintfxd.cpp:1671
Struct for validating if Dtype is amongst {uint8_t, uint16_t, uint32_t}.
Definition: ubintfxd.h:107
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: ubintfxd.h:219
Struct for calculating bit width from data type. Sets value to the bitwidth of uint_type.
Definition: ubintfxd.h:154
void SetIdentity()
Definition: ubintfxd.h:361
Struct to find log value of U where U is a primitive datatype. Needed in the preprocessing step of Bi...
Definition: ubintfxd.h:96
const BigInteger & operator=(const bigintnat::NativeInteger &val)
Definition: ubintfxd.h:335
Struct to determine a datatype that is twice as big(bitwise) as utype. sets T as of type void for def...
Definition: ubintfxd.h:165
usint GetLengthForBase(usint base) const
Definition: ubintfxd.h:904
BigInteger(const bigintnat::NativeIntegerT< T > &val)
Definition: ubintfxd.h:276
Struct to find log value of N. Needed in the preprocessing step of BigInteger to determine bitwidth...
Definition: ubintfxd.h:75
Definition: interface.h:33
BigInteger operator-() const
Definition: ubintfxd.h:408
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60
BigInteger Sub(const BigInteger &b) const
Definition: ubintfxd.cpp:320