BigInt

BigInt, in bigint.h, is an implementation of a signed magnitude multiple-precision integer, which is used to implement certain older public key algorithms such as RSA. It also appears in other contexts, for example X.509 certificate serial numbers are technically integer values and can be quite large, and so are represented using a BigInt.

A BigInt is a sequence of smaller integers of type word; this type is defined to be either uint32_t or uint64_t, depending on the word size of the processor.

Warning

While it is possible to use the APIs provided by BigInt as a general calculation facility, it is extremely inadvisable that you attempt to implement a cryptographic scheme of any kind directly using BigInt. Botan internally has many facilities for fast and side channel safe arithmetic which are not exposed to callers.

In general, as a library user, avoid doing anything with BigInt besides serializing or deserializing them as required to call other interfaces. Some of the general calculation facilities of BigInt may be made internal to the library in a future major release.

class BigInt
static BigInt BigInt::from_string(std::string_view str)

Create a BigInt from a string. By default decimal is expected. With an 0x prefix, instead it is treated as hexadecimal. A - prefix to indicate negative numbers is also accepted.

static BigInt::from_bytes(std::span<const uint8_t> buf)

Create a BigInt from a binary array (big-endian encoding). The result of this function will always be positive; there is no support for a sign bit, 2s complement encoding, or similar methods for indicating a negative value.

void serialize_to(std::span<uint8_t> buf)

Encode this BigInt as a big-endian integer. The sign is ignored.

There must be sufficient space to encode the entire integer in buf. If buf is larger than required, sufficient zero bytes will be prefixed.

size_t bytes() const

Return number of bytes needed to represent value of *this

size_t bits() const

Return number of bits needed to represent value of *this

std::string to_dec_string() const

Encode the integer as a decimal string.

std::string to_hex_string() const

Encode the integer as a hexadecimal string, with “0x” prefix

BigInt::zero()

Create a BigInt with value zero

BigInt::from_u64(uint64_t n)

Create a BigInt with value n

BigInt operator+(const BigInt &x, const BigInt &y)

Add x and y and return result.

BigInt operator+(const BigInt &x, word y)

Add x and y and return result.

BigInt operator+(word x, const BigInt &y)

Add x and y and return result.

BigInt operator-(const BigInt &x, const BigInt &y)

Subtract y from x and return result.

BigInt operator-(const BigInt &x, word y)

Subtract y from x and return result.

BigInt operator*(const BigInt &x, const BigInt &y)

Multiply x and y and return result.

BigInt operator/(const BigInt &x, const BigInt &y)

Divide x by y and return result.

BigInt operator%(const BigInt &x, const BigInt &y)

Divide x by y and return remainder.

word operator%(const BigInt &x, word y)

Divide x by y and return remainder.

word operator<<(const BigInt &x, size_t n)

Left shift x by n and return result.

word operator>>(const BigInt &x, size_t n)

Right shift x by n and return result.

BigInt &operator+=(const BigInt &y)

Add y to *this

BigInt &operator+=(word y)

Add y to *this

BigInt &operator-=(const BigInt &y)

Subtract y from *this

BigInt &operator-=(word y)

Subtract y from *this

BigInt &operator*=(const BigInt &y)

Multiply *this with y

BigInt &operator*=(word y)

Multiply *this with y

BigInt &operator/=(const BigInt &y)

Divide *this by y

BigInt &operator%=(const BigInt &y)

Divide *this by y and set *this to the remainder.

word operator%=(word y)

Divide *this by y and set *this to the remainder.

word operator<<=(size_t shift)

Left shift *this by shift bits

word operator>>=(size_t shift)

Right shift *this by shift bits

BigInt &operator++()

Increment *this by 1

BigInt &operator--()

Decrement *this by 1

BigInt operator++(int)

Postfix increment *this by 1

BigInt operator--(int)

Postfix decrement *this by 1

BigInt operator-() const

Negation operator

bool operator!() const

Return true unless *this is zero

void clear()

Set *this to zero

uint32_t to_u32bit() const

Return value of *this as a 32-bit integer, if possible. If the integer is negative or not in range, an exception is thrown.

bool is_even() const

Return true if *this is even

bool is_odd() const

Return true if *this is odd

bool is_nonzero() const

Return true if *this is not zero

bool is_zero() const

Return true if *this is zero

bool is_negative() const

Return true if *this is less than zero

bool is_positive() const

Return true if *this is greater than or equal to zero

BigInt abs() const

Return absolute value of *this