Template Class BigIntegerInterface

Template Parameter Order

  1. typename T

Class Documentation

template<typename T>
class lbcrypto::BigIntegerInterface

Public Functions

void SetValue(const std::string &str)

Set from a string

Parameters

str – is the string representation of the value

T Add(const T &b) const

Addition operation.

Parameters

&b – is the value to add.

Returns

result of the addition operation.

T &AddEq(const T &b)
T Sub(const T &b) const

Subtraction operation.

Parameters

&b – is the value to subtract.

Returns

is the result of the subtraction operation.

T &SubEq(const T &b)
T Mul(const T &b) const

Multiplication operation.

Parameters

&b – is the value to multiply with.

Returns

is the result of the multiplication operation.

T &MulEq(const T &b)

Multiplication operation. In-place variant.

Parameters

&b – is the value to multiply with.

Returns

is the result of the multiplication operation.

T DividedBy(const T &b) const

Division operation.

Parameters

&b – is the value to divide by.

Returns

is the result of the division operation.

T &DividedByEq(const T &b)

Division operation. In-place variant.

Parameters

&b – is the value to divide by.

Returns

is the result of the division operation.

T MultiplyAndRound(const T &p, const T &q) const

Multiply and Rounding operation. Returns [x*p/q] where [] is the rounding operation.

Parameters
  • &p – is the numerator to be multiplied.

  • &q – is the denominator to be divided.

Returns

is the result of multiply and round operation.

T &MultiplyAndRoundEq(const T &p, const T &q)
T DivideAndRound(const T &q) const

Divide and Rounding operation. Returns [x/q] where [] is the rounding operation.

Parameters

&q – is the denominator to be divided.

Returns

is the result of divide and round operation.

T &DivideAndRoundEq(const T &q)
T Mod(const T &modulus) const

Naive modulus operation.

Parameters

&modulus – is the modulus to perform.

Returns

is the result of the modulus operation.

T &ModEq(const T &modulus)
T ComputeMu() const

Precomputes a parameter mu for Barrett modular reduction.

Returns

the precomputed parameter mu.

T Mod(const T &modulus, const T &mu) const

Barrett modulus operation. Implements generalized Barrett modular reduction algorithm. Uses one precomputed value of mu.

Parameters
  • &modulus – is the modulus to perform.

  • &mu – is the Barrett value.

Returns

is the result of the modulus operation.

T &ModEq(const T &modulus, const T &mu)
T ModAdd(const T &b, const T &modulus) const

Modulus addition operation.

Parameters
  • &b – is the scalar to add.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus addition operation.

T &ModAddEq(const T &b, const T &modulus)
T ModAddFast(const T &b, const T &modulus) const

Modulus addition where operands are < modulus.

Parameters
  • &b – is the scalar to add.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus addition operation.

T &ModAddFastEq(const T &b, const T &modulus)
T ModAdd(const T &b, const T &modulus, const T &mu) const

Barrett modulus addition operation.

Parameters
  • &b – is the scalar to add.

  • &modulus – is the modulus to perform operations with.

  • &mu – is the Barrett value.

Returns

is the result of the modulus addition operation.

T &ModAddEq(const T &b, const T &modulus, const T &mu)
T ModSub(const T &b, const T &modulus) const

Modulus subtraction operation.

Parameters
  • &b – is the scalar to subtract.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus subtraction operation.

T &ModSubEq(const T &b, const T &modulus)
T ModSubFast(const T &b, const T &modulus) const

Modulus subtraction where operands are < modulus.

Parameters
  • &b – is the scalar to subtract.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus subtraction operation.

T &ModSubFastEq(const T &b, const T &modulus)
T ModSub(const T &b, const T &modulus, const T &mu) const

Barrett modulus subtraction operation.

Parameters
  • &b – is the scalar to subtract.

  • &modulus – is the modulus to perform operations with.

  • &mu – is the Barrett value.

Returns

is the result of the modulus subtraction operation.

T &ModSubEq(const T &b, const T &modulus, const T &mu)
T ModMul(const T &b, const T &modulus) const

Modulus multiplication operation.

Parameters
  • &b – is the scalar to multiply.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus multiplication operation.

T &ModMulEq(const T &b, const T &modulus)
T ModMul(const T &b, const T &modulus, const T &mu) const

Barrett modulus multiplication.

Parameters
  • &b – is the scalar to multiply.

  • &modulus – is the modulus to perform operations with.

  • &mu – is the Barrett value.

Returns

is the result of the modulus multiplication operation.

T &ModMulEq(const T &b, const T &modulus, const T &mu)
T ModMulFast(const T &b, const T &modulus) const

Modulus multiplication that assumes the operands are < modulus.

Parameters
  • &b – is the scalar to multiply.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus multiplication operation.

T &ModMulFastEq(const T &b, const T &modulus)
T ModMulFast(const T &b, const T &modulus, const T &mu) const

Barrett modulus multiplication that assumes the operands are < modulus.

Parameters
  • &b – is the scalar to multiply.

  • &modulus – is the modulus to perform operations with.

  • &mu – is the Barrett value.

Returns

is the result of the modulus multiplication operation.

T &ModMulFastEq(const T &b, const T &modulus, const T &mu)
T ModMulFastConst(const T &b, const T &modulus, const T &bInv) const

NTL-optimized modular multiplication using a precomputation for the multiplicand. Assumes operands are < modulus.

Parameters
  • &b – is the scalar to multiply.

  • &modulus – is the modulus to perform operations with.

  • &bInv – NTL precomputation for b.

Returns

is the result of the modulus multiplication operation.

T &ModMulFastConstEq(const T &b, const T &modulus, const T &bInv)
T ModExp(const T &b, const T &modulus) const

Modulus exponentiation operation.

Parameters
  • &b – is the scalar to exponentiate at all locations.

  • &modulus – is the modulus to perform operations with.

Returns

is the result of the modulus exponentiation operation.

T &ModExpEq(const T &b, const T &modulus)
T ModInverse(const T &modulus) const

Modulus inverse operation.

Parameters

&modulus – is the modulus to perform.

Returns

is the result of the modulus inverse operation.

T &ModInverseEq(const T &modulus)
T LShift(usshort shift) const

Left shift operation.

Parameters

shift – # of bits.

Returns

result of the shift operation.

T &LShiftEq(usshort shift)
T RShift(usshort shift) const

Right shift operation.

Parameters

shift – # of bits.

Returns

result of the shift operation.

T &RShiftEq(usshort shift)
int Compare(const T &a) const

Compares the current BigInteger to BigInteger a.

Parameters

a – is the BigInteger to be compared with.

Returns

-1 for strictly less than, 0 for equal to and 1 for strictly greater than conditons.

uint64_t ConvertToInt() const

Convert the value to an int.

Returns

the int representation of the value.

usint GetMSB() const

Returns the MSB location of the value.

Returns

the index of the most significant bit.

usint GetLengthForBase(usint base) const

Get the number of digits using a specific base - support for arbitrary base may be needed.

Parameters

base – is the base with which to determine length in.

Returns

the length of the representation in a specific base.

usint GetDigitAtIndexForBase(usint index, usint base) const

Get the number of digits using a specific base - support for arbitrary base may be needed. Example: for number 83, index 2 and base 4 we have:

                    index:0,1,2,3
83 &#8212;base 4 decomposition–> (3,0,1,1) &#8212;at index 2–> 1

The return number is 1.

Parameters
  • index – is the location to return value from in the specific base.

  • base – is the base with which to determine length in.

Returns

the length of the representation in a specific base.

const std::string ToString() const

Convert this integer into a std::string, for serialization

Returns

the value of this T as a string.

Protected Functions

~BigIntegerInterface() = default

Friends

inline friend T operator+(const T &a, const T &b)

inline operators for the addition operation.

inline friend T &operator+=(T &a, const T &b)
inline friend T operator-(const T &a, const T &b)

inline operators for the subtraction operation.

inline friend T &operator-=(T &a, const T &b)
inline friend T operator*(const T &a, const T &b)

inline operators for the multiplication operation.

inline friend T &operator*=(T &a, const T &b)
inline friend T operator/(const T &a, const T &b)

inline operators for the division operation.

inline friend T &operator/=(T &a, const T &b)
inline friend T operator%(const T &a, const T &b)
inline friend T &operator%=(T &a, const T &b)
inline friend T operator<<(const T &a, usshort shift)

inline operators for the left shift operations.

inline friend T &operator<<=(T &a, usshort shift)
inline friend T operator>>(const T &a, usshort shift)

inline operators for the right shift operations.

inline friend T &operator>>=(T &a, usshort shift)
inline friend bool operator==(const T &a, const T &b)
inline friend bool operator!=(const T &a, const T &b)
inline friend bool operator>(const T &a, const T &b)
inline friend bool operator>=(const T &a, const T &b)
inline friend bool operator<(const T &a, const T &b)
inline friend bool operator<=(const T &a, const T &b)