25 #ifndef LBCRYPTO_MATH_BIGINTNTL_UBINTNTL_H 26 #define LBCRYPTO_MATH_BIGINTNTL_UBINTNTL_H 28 #include "config_core.h" 32 #include <NTL/ZZ_limbs.h> 41 #include <type_traits> 46 #include "utils/inttypes.h" 47 #include "utils/memory.h" 49 #include "utils/debug.h" 65 static const usint value = 1 +
Log2<N / 2>::value;
75 static const usint value = 1;
92 myZZ(
const NTL::ZZ &val);
109 explicit myZZ(
const std::string &strval);
117 #if defined(HAVE_INT128) 118 myZZ(
unsigned __int128 val);
127 myZZ(uint32_t val) :
myZZ(uint64_t(val)) {}
128 myZZ(
long val) :
myZZ(uint64_t(val)) {}
129 myZZ(
long long val) :
myZZ(uint64_t(val)) {}
136 template <
typename T>
145 __attribute__((deprecated(
"Cannot construct from a double")));
155 const myZZ &operator=(
const myZZ &val);
166 *
this =
myZZ(strval);
188 void SetValue(
const std::string &strval);
195 void SetValue(
const myZZ &val);
197 void SetIdentity() { *
this = 1; }
208 return *
static_cast<const ZZ *
>(
this) + static_cast<const ZZ &>(b);
218 *
static_cast<ZZ *
>(
this) += static_cast<const ZZ &>(b);
232 : (*
static_cast<const ZZ *
>(
this) - static_cast<const ZZ &>(b));
246 *
static_cast<ZZ *
>(
this) -= static_cast<const ZZ &>(b);
258 return *
static_cast<const ZZ *
>(
this) * static_cast<const ZZ &>(b);
268 *
static_cast<ZZ *
>(
this) *= static_cast<const ZZ &>(b);
279 return *
static_cast<const ZZ *
>(
this) / static_cast<const ZZ &>(b);
289 *
static_cast<ZZ *
>(
this) /= static_cast<const ZZ &>(b);
299 myZZ Exp(
const usint p)
const {
return power(*
this, p); }
308 *
this = power(*
this, p);
320 myZZ MultiplyAndRound(
const myZZ &p,
const myZZ &q)
const;
330 const myZZ &MultiplyAndRoundEq(
const myZZ &p,
const myZZ &q);
339 myZZ DivideAndRound(
const myZZ &q)
const;
348 const myZZ &DivideAndRoundEq(
const myZZ &q);
359 return *
static_cast<const ZZ *
>(
this) % static_cast<const ZZ &>(modulus);
369 *
static_cast<ZZ *
>(
this) %= static_cast<const ZZ &>(modulus);
380 temp <<= (2 * this->GetMSB() + 3);
395 return *
static_cast<const ZZ *
>(
this) % static_cast<const ZZ &>(modulus);
408 *
static_cast<ZZ *
>(
this) %= static_cast<const ZZ &>(modulus);
420 return AddMod(this->Mod(modulus), b.
Mod(modulus), modulus);
431 AddMod(*
this, this->Mod(modulus), b.
Mod(modulus), modulus);
443 return AddMod(*
this, b, modulus);
454 *
this = AddMod(*
this, b, modulus);
467 return AddMod(*
this, b, modulus);
479 *
this = AddMod(*
this, b, modulus);
493 myZZ newthis(*
this % modulus);
494 myZZ newb(b % modulus);
495 if (newthis >= newb) {
496 myZZ tmp(SubMod(newthis, newb, modulus));
499 myZZ tmp(newthis + modulus - newb);
514 this->ModEq(modulus);
515 myZZ newb(b % modulus);
517 SubMod(*
this, *
this, newb, modulus);
520 this->AddEq(modulus);
535 return SubMod(*
this, b, modulus);
537 return (*
this + modulus - b);
550 return *
this = SubMod(*
this, b, modulus);
552 return *
this = (*
this + modulus - b);
565 myZZ newthis(*
this % modulus);
566 myZZ newb(b % modulus);
567 if (newthis >= newb) {
568 myZZ tmp(SubMod(newthis, newb, modulus));
571 myZZ tmp(newthis + modulus - newb);
585 this->ModEq(modulus);
586 myZZ newb(b % modulus);
588 SubMod(*
this, *
this, newb, modulus);
591 this->AddEq(modulus);
605 return MulMod(this->Mod(modulus), b.
Mod(modulus), modulus);
616 MulMod(*
this, this->Mod(modulus), b.
Mod(modulus), modulus);
629 return MulMod(this->Mod(modulus), b.
Mod(modulus), modulus);
641 MulMod(*
this, this->Mod(modulus), b.
Mod(modulus), modulus);
653 return MulMod(*
this, b, modulus);
665 *
this = MulMod(*
this, b, modulus);
678 const myZZ &mu)
const {
679 return MulMod(*
this, b, modulus);
692 *
this = MulMod(*
this, b, modulus);
696 myZZ ModMulFastConst(
const myZZ &b,
const myZZ &modulus,
697 const myZZ &bInv)
const {
699 "ModMulFastConst is not implemented for backend 6");
702 const myZZ &ModMulFastConstEq(
const myZZ &b,
const myZZ &modulus,
705 "ModMulFastConstEq is not implemented for backend 6");
717 PowerMod(res, *
this, b, modulus);
729 PowerMod(*
this, *
this, b, modulus);
740 if (modulus ==
myZZ(0)) {
745 tmp = InvMod(*
this % modulus, modulus);
746 }
catch (InvModErrorObject
749 std::stringstream errmsg;
750 errmsg <<
"ModInverse exception " 751 <<
" this: " << *
this <<
" modulus: " << modulus <<
"GCD(" 752 << e.get_a() <<
"," << e.get_n() <<
"!=1" << std::endl;
765 if (modulus ==
myZZ(0)) {
769 *
this = InvMod(*
this % modulus, modulus);
770 }
catch (InvModErrorObject
773 std::stringstream errmsg;
774 errmsg <<
"ModInverse exception " 775 <<
" this: " << *
this <<
" modulus: " << modulus <<
"GCD(" 776 << e.get_a() <<
"," << e.get_n() <<
"!=1" << std::endl;
789 return *
static_cast<const ZZ *
>(
this) << shift;
799 *
static_cast<ZZ *
>(
this) <<= shift;
810 return *
static_cast<const ZZ *
>(
this) >> shift;
820 *
static_cast<ZZ *
>(
this) >>= shift;
827 int Compare(
const myZZ &a)
const {
return compare(*
this, a); }
832 uint64_t ConvertToInt()
const;
834 uint64_t ConvertToUint64()
const;
836 double ConvertToDouble()
const;
845 static myZZ FromBinaryString(
const std::string &bitString);
851 static const myZZ &zero();
853 usint GetMSB()
const;
879 usint GetDigitAtIndexForBase(usint index, usint base)
const;
883 static const usint m_log2LimbBitLength;
892 usint GetBitRangeAtIndex(usint index, usint length)
const;
900 uschar GetBitAtIndex(usint index)
const;
911 const std::string ToString()
const;
913 static const std::string IntegerTypeName() {
return "UBNTLINT"; }
916 friend std::ostream &operator<<(std::ostream &os,
const myZZ &ptr_obj);
924 const ZZ_limb_t *zlp = ZZ_limbs_get(*
this);
926 for (
size_t i = 0; i < (size_t)this->size(); i++) {
927 ret += std::to_string(zlp[i]);
928 if (i < ((
size_t)this->size() - 1)) {
937 template <
class Archive>
938 typename std::enable_if<!cereal::traits::is_text_archive<Archive>::value,
940 save(Archive &ar, std::uint32_t
const version)
const {
941 void *data = this->rep.rep;
942 ::cereal::size_type len = 0;
943 if (data ==
nullptr) {
944 ar(::cereal::binary_data(&len,
sizeof(len)));
946 len = _ntl_ALLOC(this->rep.rep);
948 ar(::cereal::binary_data(&len,
sizeof(len)));
949 ar(::cereal::binary_data(data, len *
sizeof(_ntl_gbigint)));
950 ar(::cereal::make_nvp(
"mb", m_MSB));
954 template <
class Archive>
955 typename std::enable_if<cereal::traits::is_text_archive<Archive>::value,
957 save(Archive &ar, std::uint32_t
const version)
const {
958 ar(::cereal::make_nvp(
"v", ToString()));
961 template <
class Archive>
962 typename std::enable_if<!cereal::traits::is_text_archive<Archive>::value,
964 load(Archive &ar, std::uint32_t
const version) {
965 if (version > SerializedVersion()) {
967 "serialized object version " + std::to_string(version) +
968 " is from a later version of the library");
970 ::cereal::size_type len;
971 ar(::cereal::binary_data(&len,
sizeof(len)));
977 void *mem = malloc(len *
sizeof(_ntl_gbigint));
978 ar(::cereal::binary_data(mem, len *
sizeof(_ntl_gbigint)));
979 WrappedPtr<_ntl_gbigint_body, Deleter> newrep;
980 newrep.rep =
reinterpret_cast<_ntl_gbigint_body *
>(mem);
981 _ntl_gswap(&this->rep, &newrep);
983 ar(::cereal::make_nvp(
"mb", m_MSB));
986 template <
class Archive>
987 typename std::enable_if<cereal::traits::is_text_archive<Archive>::value,
989 load(Archive &ar, std::uint32_t
const version) {
990 if (version > SerializedVersion()) {
992 "serialized object version " + std::to_string(version) +
993 " is from a later version of the library");
996 ar(::cereal::make_nvp(
"v", s));
1000 std::string SerializedObjectName()
const {
return "NTLInteger"; }
1002 static uint32_t SerializedVersion() {
return 1; }
1016 static usint ceilIntByUInt(
const ZZ_limb_t Number);
1018 mutable uint32_t m_MSB;
1019 usint GetMSBLimb_t(ZZ_limb_t x)
const;
1023 NTL_DECLARE_RELOCATABLE((
myZZ *))
1028 #endif // LBCRYPTO_MATH_BIGINTNTL_UBINTNTL_H myZZ(int val)
Definition: ubintntl.h:126
const myZZ & LShiftEq(usshort shift)
Definition: ubintntl.h:798
myZZ DividedBy(const myZZ &b) const
Definition: ubintntl.h:278
const myZZ & ModMulFastEq(const myZZ &b, const myZZ &modulus, const myZZ &mu)
Definition: ubintntl.h:691
myZZ ModAdd(const myZZ &b, const myZZ &modulus, const myZZ &mu) const
Definition: ubintntl.h:466
myZZ Add(const myZZ &b) const
Definition: ubintntl.h:207
myZZ ModMulFast(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:652
myZZ ComputeMu() const
Definition: ubintntl.h:378
myZZ Mod(const myZZ &modulus) const
Definition: ubintntl.h:358
const myZZ & ModExpEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:728
const myZZ & ExpEq(const usint p)
Definition: ubintntl.h:307
myZZ Sub(const myZZ &b) const
Definition: ubintntl.h:229
static myZZ Allocator()
Definition: ubintntl.h:906
const myZZ & ModMulEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:615
myZZ ModInverse(const myZZ &modulus) const
Definition: ubintntl.h:739
myZZ ModSub(const myZZ &b, const myZZ &modulus, const myZZ &mu) const
Definition: ubintntl.h:564
myZZ Exp(const usint p) const
Definition: ubintntl.h:299
const myZZ & SubEq(const myZZ &b)
Definition: ubintntl.h:242
const myZZ & ModEq(const myZZ &modulus)
Definition: ubintntl.h:368
myZZ RShift(usshort shift) const
Definition: ubintntl.h:809
myZZ(const bigintnat::NativeIntegerT< T > &val)
Definition: ubintntl.h:137
Definition: exception.h:119
std::enable_if<!cereal::traits::is_text_archive< Archive >::value, void >::type save(Archive &ar, std::uint32_t const version) const
SERIALIZATION.
Definition: ubintntl.h:940
Definition: exception.h:147
Definition: exception.h:113
const myZZ & AddEq(const myZZ &b)
Definition: ubintntl.h:217
std::string GetInternalRepresentation(void) const
Definition: ubintntl.h:922
const myZZ & ModMulFastEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:664
myZZ LShift(usshort shift) const
Definition: ubintntl.h:788
Struct to find log value of N. Needed in the preprocessing step of ubint to determine bitwidth...
Definition: ubintntl.h:64
const myZZ & ModInverseEq(const myZZ &modulus)
Definition: ubintntl.h:764
myZZ ModMul(const myZZ &b, const myZZ &modulus, const myZZ &mu) const
Definition: ubintntl.h:628
Definition: ubintntl.h:78
myZZ ModMul(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:604
myZZ ModSub(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:492
const myZZ & ModEq(const myZZ &modulus, const myZZ &mu)
Definition: ubintntl.h:407
myZZ ModExp(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:715
myZZ ModAdd(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:419
myZZ ModMulFast(const myZZ &b, const myZZ &modulus, const myZZ &mu) const
Definition: ubintntl.h:677
const myZZ & ModMulEq(const myZZ &b, const myZZ &modulus, const myZZ &mu)
Definition: ubintntl.h:640
const myZZ & DividedByEq(const myZZ &b)
Definition: ubintntl.h:288
const myZZ & ModAddEq(const myZZ &b, const myZZ &modulus, const myZZ &mu)
Definition: ubintntl.h:478
const myZZ & ModAddFastEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:453
const myZZ & ModSubEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:513
const myZZ & operator=(std::string strval)
Definition: ubintntl.h:165
usint GetLengthForBase(usint base) const
Definition: ubintntl.h:862
myZZ ModAddFast(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:442
Definition: interface.h:33
myZZ ModSubFast(const myZZ &b, const myZZ &modulus) const
Definition: ubintntl.h:533
const myZZ & ModSubFastEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:548
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60
const myZZ & RShiftEq(usshort shift)
Definition: ubintntl.h:819
const myZZ & MulEq(const myZZ &b)
Definition: ubintntl.h:267
const myZZ & ModSubEq(const myZZ &b, const myZZ &modulus, const myZZ &mu)
Definition: ubintntl.h:584
myZZ Mod(const myZZ &modulus, const myZZ &mu) const
Definition: ubintntl.h:394
const myZZ & ModAddEq(const myZZ &b, const myZZ &modulus)
Definition: ubintntl.h:430
myZZ Mul(const myZZ &b) const
Definition: ubintntl.h:257
const myZZ & operator=(uint64_t val)
Definition: ubintntl.h:176