Template Class BigIntegerInterface

Template Parameter Order

  1. typename T

Class Documentation

template<typename T>
class lbcrypto::BigIntegerInterface

Interface (CRTP base) implemented by every big-integer backend; T is the derived integer type. Naming conventions: the plain method returns a new value, the “Eq” suffix performs the operation in place on *this and returns *this, and “Fast” variants skip the reduction of their inputs (the operands must already be < modulus). The “Barrett” overloads take mu, the value precomputed by ComputeMu() for the modulus.

Template Parameters

T – the derived big-integer type

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)

Addition operation. In-place variant.

Parameters

b – is the value to add.

Returns

result of the addition operation.

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)

Subtraction operation. In-place variant.

Parameters

b – is the value to subtract.

Returns

is the result of the subtraction operation.

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)

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

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 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)

Divide and Rounding operation. Returns [x/q] where [] is the rounding operation. In-place variant.

Parameters

q – is the denominator to be divided.

Returns

is the result of divide and round operation.

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)

Naive modulus operation. In-place variant.

Parameters

modulus – is the modulus to perform.

Returns

is the result of the modulus operation.

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)

Barrett modulus operation using the precomputed value mu. In-place variant.

Parameters
  • modulus – is the modulus to perform.

  • mu – is the Barrett value.

Returns

is the result of the modulus operation.

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)

Modulus addition operation. In-place variant.

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 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)

Modulus addition where operands are < modulus. In-place variant.

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 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)

Barrett modulus addition operation. In-place variant.

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 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)

Modulus subtraction operation. In-place variant.

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 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)

Modulus subtraction where operands are < modulus. In-place variant.

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 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)

Barrett modulus subtraction operation. In-place variant.

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 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)

Modulus multiplication operation. In-place variant.

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 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)

Barrett modulus multiplication. In-place variant.

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 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)

Modulus multiplication that assumes the operands are < modulus. In-place variant.

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 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)

Barrett modulus multiplication that assumes the operands are < modulus. In-place variant.

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 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)

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

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 ModExp(const T &b, const T &modulus) const

Modulus exponentiation operation.

Parameters
  • b – is the exponent.

  • 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)

Modulus exponentiation operation. In-place variant.

Parameters
  • b – is the exponent.

  • modulus – is the modulus to perform operations with.

Returns

is the result of the modulus exponentiation operation.

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)

Modulus inverse operation. In-place variant.

Parameters

modulus – is the modulus to perform.

Returns

is the result of the modulus inverse operation.

T LShift(uint16_t shift) const

Left shift operation.

Parameters

shift – # of bits.

Returns

result of the shift operation.

T &LShiftEq(uint16_t shift)

Left shift operation. In-place variant.

Parameters

shift – # of bits.

Returns

result of the shift operation.

T RShift(uint16_t shift) const

Right shift operation.

Parameters

shift – # of bits.

Returns

result of the shift operation.

T &RShiftEq(uint16_t shift)

Right shift operation. In-place variant.

Parameters

shift – # of bits.

Returns

result of the shift operation.

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 conditions.

uint64_t ConvertToInt() const

Convert the value to an int.

Returns

the int representation of the value.

uint32_t GetMSB() const

Returns the MSB location of the value.

Returns

the index of the most significant bit.

uint32_t GetLengthForBase(uint32_t 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.

uint32_t GetDigitAtIndexForBase(uint32_t index, uint32_t base) const

Get the digit at a specific index 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 of the digit decomposition.

Returns

the digit at the specific index in the 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 in-place operator for the addition operation.

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 in-place operator for the subtraction operation.

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 in-place operator for the multiplication operation.

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 in-place operator for the division operation.

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

inline operators for the modulus operation.

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

inline in-place operator for the modulus operation.

inline friend T operator<<(const T &a, uint16_t shift)

inline operators for the left shift operations.

inline friend T &operator<<=(T &a, uint16_t shift)

inline in-place operator for the left shift operation.

inline friend T operator>>(const T &a, uint16_t shift)

inline operators for the right shift operations.

inline friend T &operator>>=(T &a, uint16_t shift)

inline in-place operator for the right shift operation.

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

relational operators, using Compare equality, using Compare

inline friend bool operator!=(const T &a, const T &b)

inequality, using Compare

inline friend bool operator>(const T &a, const T &b)

strictly greater than, using Compare

inline friend bool operator>=(const T &a, const T &b)

greater than or equal to, using Compare

inline friend bool operator<(const T &a, const T &b)

strictly less than, using Compare

inline friend bool operator<=(const T &a, const T &b)

less than or equal to, using Compare