FBB::BigInt(3bobcat)

🌸 NAME 🌸

FBB::BigInt β€” Arithmetic on Integers of Unlimited Size

hey bestie, welcome to the 3bobcat manual, section 3. this is the page for the class that lets you do math on integers so big they'd make your calculator cry. πŸ’…


✨ SYNOPSIS ✨

#include <bobcat/bigint>
Linking option: -lbobcat -lcrypto

πŸ’– DESCRIPTION πŸ’–

So basically this class is a wrapper class around the openSSL BN series of functions. it hands you members to perform arithmetic on integral values of unlimited sizes, no cap. 🧚 Members are offered to generate primes and to perform all kinds of common arithmetic operations on BigInt objects. Also, conversions to characters and standard numerical value types are offered. it's giving "everything you could ever want", fr fr.

Below, the phrase the object may also refer to the object's value. The context in which this occurs will make it clear that the object's value rather than the object as-is is what's being referred to. iykyk.

Various constructors accept BIGNUM arguments. Type BIGNUM is the type containing an integer of unlimited precision as defined by OpenSSL. BIGNUM's definition is

typedef struct bignum_st BIGNUM;

struct bignum_st
{
    BN_ULONG *d;    // Pointer to an array of ’BN_BITS2’ bit chunks.
    int top;        // Index of last used d +1.
    // The next are internal book keeping for bn_expand.
    int dmax;       // Size of the d array.
    int neg;        // one if the number is negative
    int flags;
};

Signs of BigInt are handled in a special way, okay, pay attention. Whether a BigInt is negative or positive is determined by its sign-flag, and not by a sign bit like it is with int typed values. Since BigInt values have unlimited precision, shifting values to the left won't change their signs. lowkey iconic behavior. πŸ‘‘

Operators return either a reference to the current (modified) object or return a BigInt object containing the computed value. The rule followed here was to implement the operators analogously to the way the operators work on int type values and variables. E.g., operator+() returns a BigInt value whereas operator+=() returns a BigInt & reference.

  • All members modifying their objects return a reference to the current (modified) object.
  • All members not modifying the current object return a BigInt object.
  • If both members exist performing the same functionality, the name of the member returning a BigInt object ends in a c (const) (e.g., addMod and addModc). the c stands for const, she's consistent bestie.
⚠️ Almost all operators, members and constructors (except for the default constructor) throw Exception exceptions on failure. so like, be ready. 😭

🌈 INHERITS FROM 🌈

  • -

(nothing. she's self-made. main character energy. πŸ’…)


πŸ¦‹ TYPE πŸ¦‹

The class BigInt defines the type Word, which is equal to the type BN_ULONG used by OpenSSL to store integral values of unlimited precision. A Word is an unsigned long, which is, depending on the architecture, usually 64 or 32 bits long.


πŸŽ€ ENUMERATIONS πŸŽ€

Msb

This (most significant bit) enumeration is used when generating a cryptographically strong random number. Its values are:

  • MSB_UNKNOWN β€” The most significant bit may be 0 or 1.
  • MSB_IS_ONE β€” The most significant bit is guaranteed to be 1.
  • TOP_TWO_BITS_ONE β€” The two most significant bits are guaranteed to be 1, resulting in a product of two values each containing nBits having 2 * nBits bits.

Lsb

This (least significant bit) enumeration is used when generating random numbers, ensuring that the resulting value is either odd or even.

  • EVEN β€” The random value will be an even value;
  • ODD β€” The random value will be an odd value.

πŸ’… CONSTRUCTORS πŸ’…

  • BigInt() β€” The default constructor initializes a BigInt value to 0;
  • explicit BigInt(BIGNUM const &value) β€” This constructor initializes a BigInt from a const BIGNUM;
  • explicit BigInt(BIGNUM const *value) β€” This constructor initializes a BigInt from a pointer to a const BIGNUM;
  • explicit BigInt(BIGNUM *value) β€” This constructor initializes a BigInt from a pointer to a BIGNUM (the BIGNUM value pointed to by value is not mondified by the constructor. This constructor is a mere wrapper around the previous constructor). Note that none of the constructors expecting a BIGNUM argument modify their argument. If the memory used by the BIGNUM argument must be returned to the common pool an explicit BN_free(3ssl) call is required;
  • BigInt(Type value) β€” This constructor is defined as a member template. Any type that can be converted using a static cast to an unsigned long can be used as argument to this constructor. Promotion is allowed, so in many situations where BigInts are expected a plain numerical value can be used as well;
  • BigInt(char const *bigEndian, size_t length, bool negative = false) β€” This constructor initializes a BigInt from length big-endian encoded bytes stored in bigEndian (having its most significant value at index 0). This constructor interprets the char values pointed at by bigEndian as unsigned values. Use this constructor to reconstruct a BigInt object from the data made available by the bigEndian member (most significant byte at index 0). If the number represents a negative value, then provide a third argument true;
  • explicit BigInt(std::string const &bigEndian, bool negative = false) β€” This constructor initializes a BigInt from the bytes stored in bigEndian, which must be big-endian encoded (having its most significant value at index 0). This constructor interprets the char values stored in bigEndian as unsigned values. If the number that is stored in bigEndian represents a negative value, then provide a second argument true;
  • BigInt(size_t length, char const *littleEndian, bool negative = false) β€” This constructor initializes a BigInt from length little-endian encoded bytes stored in littleEndian (having its least significant value at index 0). This constructor interprets the char values pointed at by littleEndian as unsigned values. Use this constructor to reconstruct a BigInt object from the data made available by the littleEndian member (most significant byte at index 0). If the number represents a negative value, then provide a third argument true;
  • explicit BigInt(BigInt::Little endian, std::string littleEndian, bool negative = false) β€” This constructor initializes a BigInt from the bytes stored in littleEndian, which must be little-endian encoded (having its least significant value at index 0). This constructor interprets the char values stored in littleEndian as unsigned values. If the number that is stored in littleEndian represents a negative value, then provide a third argument true. The consructor's first parameter is used to distinguish this constructor from the constructor expecting a t(string) whose bytes represent a big-endian encoded value, and is not used by this constructor itself. It can be specified as BigInt::Little{}.

Copy and move constructors (and assignment operators) are available. 🫢


πŸ”₯ MEMBER FUNCTIONS πŸ”₯

  • BigInt &addMod(BigInt const &rhs, BigInt const &mod) β€” rhs is added (modulo mod) to the current object;
  • BigInt addModc(BigInt const &rhs, BigInt const &mod) β€” The sum (modulo mod) of the current object and rhs is returned;
  • BigInt::Word at(size_t index) const β€” Returns the Word at index. E.g., on a 32 bit architecture, if the BigInt value equals 2, then at(0) returns 0, and at(1) returns 2. If index equals or exceeds the value returned by nWords an FBB::Exception is thrown;
  • BIGNUM const &bignum() const β€” A reference to the BIGNUM value maintained by the current BigInt object is returned;
  • char *bigEndian() const β€” The value represented by the current object is stored in a series of char typed values in big-endian order. If a value consists of 5 chars the eight most significant bits will be stored in the char having index value 0, the eight least significant bits will be stored in the char having index value 4. When needed simply swap char[i] with char[j] (i = 0 .. nBytes/2, j = nBytes-1 .. nBytes/2) to convert to little-endian order or use the member littleEndian to receive the representation in little-endian order. The return value consists of a series of sizeInBytes() (see below) dynamically allocated char values. The caller of bigEndian owns the allocated memory and should eventually delete it again using delete[]. Note that the current object's sign cannot be inferred from the return value;
  • BigInt &clearBit(size_t index) β€” The current object's bit at index position index is cleared;
  • BigInt clearBit(size_t index) const β€” A copy of the current object having its bit at index position index cleared;
  • BigInt &div(BigInt *remainder, BigInt const &rhs) β€” The current object is divided by rhs. The division's remainder is returned in *remainder;
  • BigInt divc(BigInt *remainder, BigInt const &rhs) const β€” The quotient of the current object and rhs is returned. The division's remainder is returned in *remainder;
  • int compare(BigInt const &rsh) const β€” Using signed values, if the current object is smaller than rhs -1 is returned; if they are equal 0 is returned; if the current object is larger than ths 1 is returned (see also uCompare);
  • BigInt &exp(BigInt const &exponent) β€” The current object is raised to the power exponent;
  • BigInt expc(BigInt const &exponent) const β€” The current object raised to the power exponent is returned;
  • BigInt &expMod(BigInt const &exponent, BigInt const &mod) β€” The current object is raised to the power exponent modulo mod;
  • BigInt expModc(BigInt const &exponent, BigInt const &mod) const β€” The current object raised to the power exponent modulo mod is returned;
  • BigInt &gcd(BigInt const &rhs) β€” The greatest common divisor (gcd) of the current object and rhs is assigned to the current object. To compute the least common multiple (lcm) the following relationship can be used:
lcm(a, b) = a * b / a.gcd(b)
  • BigInt gcdc(BigInt const &rhs) const β€” The greatest common divisor (gcd) of the current object and rhs is returned. To compute the least common multiple (lcm) the following relationship can be used:
lcm(a, b) = a * b / a.gcd(b)

okay bestie stay with me, we're only part of the way through this page β€” more parts are coming so don't go anywhere πŸ’–

  • bool hasBit(size_t index) β€” returns true if the bit sitting at index position index has been set, and false if it hasn't. That's the whole vibe check. βœ…
  • BigInt &inverseMod(BigInt const &mod) β€” the inverse of the current object, modulo mod, gets assigned straight into the current object. Meaning it's the value ret where this iconic little equation holds:
(*this * ret) % mod = 1
  • BigInt inverseModc(BigInt const &mod) const β€” same inverse-of-the-current-object-modulo-mod situation, except it just returns it, no touching the original. Queen behaviour. πŸ’…
  • bool isNegative() const β€” true if the current object is holding a negative value, bestie.
  • bool isOdd() const β€” true if the current object is an odd value. very demure, very mindful. πŸŽ€
  • bool isOne() const β€” true if the current object equals one (1). The loneliest number got representation.
  • BigInt &isqrt() β€” the current object's integer square root gets assigned to the current object. FYI the integer square root of a value x is the biggest integral value whose square doesn't exceed x. Like, isqrt(17) == 4, no cap. And it throws an Exception if the current object's value is smaller than one. 🚨
  • BigInt isqrtc() const β€” returns the integer square root of the current object. Same deal: an Exception gets thrown if the current object's value is smaller than one.
  • bool isZero() const β€” true if the current object equals zero (0).
  • char *littleEndian() const β€” the value in the current object gets stored as a series of char typed values in little-endian order. So if a value is 5 chars long, the eight least significant bits land in the char with index value 0. Want big-endian instead? Use the member bigEndian. What you get back is a series of sizeInBytes() (scroll down bestie) dynamically allocated char values. The caller of littleEndian owns that allocated memory and should eventually delete[] it. Also heads up: the current object's sign cannot be inferred from the return value. πŸ™ˆ
  • BigInt &lshift() β€” the current object's bits get shifted one bit to the left. The object's sign stays exactly as it was, unaltered. ✨
  • BigInt lshiftc() β€” returns the current object's bits shifted one bit to the left. The object's sign will equal the current object's sign.
  • BigInt &lshift(size_t nBits) β€” the current object's bits get shifted nBits to the left. Sign remains unaltered.
  • BigInt lshiftc(size_t nBits) const β€” returns the current object's bits shifted nBits to the left. The object's sign will equal the current object's sign.
  • BigInt &maskBits(size_t lowerNBits) β€” keeps only the lowerNBits lower bits of the current object and clears all the higher order bits. The object's sign is not affected. 🫢
  • BigInt maskBitsc(size_t lowerNBits) const β€” returns a copy of the current object with everything but its lowerNBits lower bits cleared. The returned object's sign equals the current object's sign.
  • size_t maxWordIndex() const β€” returns the maximum Word-index that at and setWord can use on the current BigInt value.
  • BigInt &mulMod(BigInt const &rhs, BigInt const &mod) β€” the current object gets multiplied by rhs, modulo mod.
  • BigInt mulModc(BigInt const &rhs, BigInt const &mod) const β€” returns the current object multiplied by rhs, modulo mod.
  • BigInt &negate() β€” the current object's value gets negated, i.e. the value flips its sign. main character flip. πŸ”„
  • BigInt negatec() const β€” returns the negated value of the current object.
  • size_t nWords() const β€” returns how many words are needed to store the BigInt value. Important: that number depends on how many bytes per word your architecture has. 32-bit architectures get four bytes per word, 64-bit architectures get eight bytes per word.
  • BigInt &rshift() β€” the current object's bits get shifted one bit to the right. The object's sign remains unaltered.
  • BigInt rshiftc() β€” returns the current object's bits shifted one bit to the right. The object's sign will equal the current object's sign.
  • BigInt &rshift(size_t nBits) β€” the current object's bits get shifted nBits to the right. Sign remains unaltered.
  • BigInt rshiftc(size_t nBits) const β€” returns the current object's bits shifted nBits to the right. The object's sign will equal the current object's sign.
  • BigInt &setBit(size_t index) β€” sets the bit at index position index.
  • BigInt setBitc(size_t index) const β€” returns a copy of the current object with its bit at index position index set.
  • BigInt &setBit(size_t index, bool value) β€” sets the bit at index position index to value.
  • BigInt setBitc(size_t index, bool value) const β€” returns a copy of the current object with its bit at index position index set to value.
  • BigInt &setNegative(bool negative) β€” the current object's sign becomes negative if the function's argument is true, and positive if the argument is false.
  • BigInt setNegativec(bool negative) const β€” returns a copy of the current object with a negative sign if the argument is true and a positive sign if the argument is false.
  • void setWord(size_t index, BigInt::Word value) β€” assigns value to the Word at index. Example time: on a 32 bit architecture, if the BigInt value equals 2, then after setWord(1, 1) the value has become 2. And if index exceeds the value returned by nWords, an FBB::Exception gets thrown. 🚨
  • size_t size() const β€” returns the number of significant bits required to store the current BIGNUM value.
  • size_t sizeInBytes() const β€” returns the number of bytes required to store the current BIGNUM value.
  • size_t constexpr sizeOfWord() const β€” BigInt values are stored in units of words, which are unsigned long values, and those can consist of, like, 32 or 64 bits. This returns how many bytes one word takes up: 4 for a 32 bit value, 8 for a 64 bit value, and possibly other values depending on specific architecture peculiarities. So yeah, this return value is architecture dependent, periodt. πŸ—οΈ
  • BigInt &sqr() β€” the current object's value gets squared.
  • BigInt sqrc() const β€” returns the square of the current object.
  • BigInt &sqrMod(BigInt const &mod) const β€” the current object's value gets squared modulo mod.
  • BigInt sqrModc(BigInt const &mod) const β€” returns the square of the current object, modulo mod.
  • BigInt &subMod(BigInt const &rhs, BigInt const &mod) β€” rhs gets subtracted from the current object, modulo mod.
  • BigInt subModc(BigInt const &rhs, BigInt const &mod) const β€” returns the difference between the current object and rhs, modulo mod.
  • void swap(BigInt &other) β€” the current object swaps its value with other. trading places moment. πŸ”
  • BigInt &tildeBits() β€” every bit in the bytes of the current object and the sign of the current object get toggled. So after
Bigint b(5);
b.tildeBits();

b contains the value -250. Also go read the discussion with operator~() below. πŸ‘€

  • BigInt tildeBitsc() const β€” returns a copy of the current object with its bits toggled.
  • BigInt &tildeInt() β€” the tilde operation is performed on the current object using the standard int semantics. Like, ~5 results in -6. Also check the discussion with operator~() below.
  • BigInt tildeIntc() const β€” returns a copy of the current object with the tilde operation performed on it using the standard int semantics.
  • unsigned long ulong() const β€” returns the absolute value stored in the current object as an unsigned long. If it can't be represented by an unsigned long, it returns 0xffffffffL. that's the oh-no signal bestie 😭
  • int uCompare(BigInt const &rsh) const β€” using absolute values: if the current object is smaller than rsh, -1 is returned; if they're equal, 0 is returned; if the current object is larger than rsh β€” the page literally says ths there, we do not gatekeep typos πŸ’€ β€” 1 is returned (see also uCompare).

✨ OVERLOADED OPERATORS ✨

Okay bestie, so like, most of these operators just do the obvious thing, no drama πŸ’…. But when that's not completely true, we've added a lil explanatory note so you're not left confused. For example, operator*() multiplies two BigInts and might promote one of the operands; operator*=() multiplies the lhs by the rhs BigInt and might promote the rhs operand. Slay.

Here's the full list, no cap:

Unary operators πŸ¦‹

  • bool operator bool() const β€” returns true if the BigInt value is unequal zero, otherwise false is returned;
  • BigInt &operator++() β€” unary prefix increment operator;
  • BigInt operator++(int) β€” unary postfix increment operator;
  • BigInt &operator--() β€” unary prefix decrement operator;
  • BigInt operator--(int) β€” unary postfix decrement operator;
  • BigInt operator-() β€” unary negation operator;
  • int operator[](size_t idx) const β€” with BigInt objects it returns the bit-value of the object's idxth bit as the value 0 or 1;
  • BigInt::Bit operator[](size_t idx) β€” with non-const BigInt objects it returns a reference to the bit-value of the object's idxth bit. When used as lvalue, assigning a 0 or non-zero value to the operator's return value will either clear or set the bit. Likewise, the following arithmetic assignment operators may be used: binary or (|=), binary and (&=) or binary xor (^=). When used as rvalue the value of the object's idxth bit is returned as a bool value. When inserted into a std::ostream the bit's value is displayed as 0 or 1;
  • BigInt operator~() β€” bestie, this one is not implemented 😭 and the reason is a whole saga:
    • When used on int values this operator toggles all the int's bits. E.g., ~5 represents -6, and ~-6 again equals five. The -6 is the result of the sign bit of int values. The obvious implementation of BigInt::operator~() is to toggle all the value's bits and to toggle its sign bit. For 5 this would result in -250: 5, being 101 (binary), fits in one byte, so ~5 becomes 11111010 (binary), which is 250. Its sign must be reversed as well, so it becomes -250. This clearly differs from the value represented by the int constant ~5: when constructing BigInt(~5), the value -6 is obtained.
    • It is possible to change the implementation. E.g., after
Bigint b(5);
b = ~b;

~b could be implemented so that it results in the value -6. But this too leads to unexpected results. While 5 & ~5 == 0, this would no longer hold true for BigInt objects: Assuming b contains 5 then b & ~b would expand to (binary) 101 & (negative)110 which equals (binary) 100;

  • Since either implementation produces unexpected results BigInt::operator~() was not implemented. Instead two members are offered: tildeBits(), toggling all the bits of all the BigInt bytes and toggling its sign (so
Bigint b(5);
b.tildeBits();

changes b's value into -250), and tildeInt() changing the object's value into the value that would have been obtained if a BigInt was a mere int (so

Bigint b(5);
b.tildeInt();

changes b's value into -6).

Binary operators πŸ’–

  • BigInt operator*(BigInt const &lhs, BigInt const &rhs) β€” multiplies, obviously πŸ’…
  • BigInt operator/(BigInt const &lhs, BigInt const &rhs) β€” returns the quotient of the lhs object divided by the rhs object. The remainder is lost 😭 (the member div performs the division and makes the remainder available as well);
  • BigInt operator%(BigInt const &lhs, BigInt const &rhs) β€” modulo, you know the vibe;
  • BigInt operator+(BigInt const &lhs, BigInt const &rhs) β€” addition, periodt;
  • BigInt operator-(BigInt const &lhs, BigInt const &rhs) β€” subtraction, slay;
  • BigInt operator<<(BigInt const &lhs, size_t nBits) β€” see also the lshift member;
  • BigInt operator>>=(BigInt const &lhs, size_t nBits) β€” see also the rshift member;
  • BigInt operator&(BigInt const &lhs, BigInt const &rhs) β€” returns a BigInt value consisting of the bit_and-ed bits and sign flags of lhs and rhs operands. Consequently, if one operand is positive, the resulting value will be positive;
  • BigInt operator|(BigInt const &lhs, BigInt const &rhs) β€” returns a BigInt value consisting of the bit_or-ed bits and sign flags of lhs and rhs operands. Consequently, if either operand is negative, the result will be negative;
  • BigInt operator^(BigInt const &lhs, BigInt const &rhs) β€” returns a BigInt value consisting of the bit_xor-ed bits and sign flags of lhs and rhs operands. Consequently, if exactly one operand is negative, the result will be negative.

(Arithmetic) assignment operator(s) πŸ‘‘

  • BigInt &operator*=(BigInt const &rhs) β€” multiply-assign, go off;
  • BigInt &operator/=(BigInt const &rhs) β€” assigns the result of the (integer) division of the current BigInt object by this to the current object. The remainder is lost. The member div divides and makes the remainder available as well;
  • BigInt &operator%=(BigInt const &rhs) β€” modulo-assign, iconic;
  • BigInt &operator+=(BigInt const &rhs) β€” add-assign, we love;
  • BigInt &operator-=(BigInt const &rhs) β€” subtract-assign, bestie;
  • BigInt &operator<<=(size_t nBits) β€” see also the lshift members;
  • BigInt &operator>>=(size_t nBits) β€” see also the rshift members;
  • BigInt &operator&=(BigInt const &rhs) β€” bit_ands the bits and sign flags of the current object and the rhs operand;
  • BigInt &operator|=(BigInt const &rhs) β€” bit_ors the bits and sign flags of the current object and the rhs operand;
  • BigInt &operator^=(BigInt const &rhs) β€” bit_xors the bits and sign flags of the current object and the rhs operand.

✨ STATIC MEMBERS ✨

Okay so bestie, every member that hands you back a BigInt and doesn't need an existing BigInt object to do its thing is a static member. And the very first diophantus member β€” the one that returns a long long β€” is static too. πŸ’…

  • long long diophantus(long long *factor1, long long *factor2, long long value1, long long value2) β€” it computes the integral solution of factor1 * value1 + factor2 * value2 = gcd. The function returns the greatest common divisor (gcd) of value1 and value2, and drops their multiplication factors into *factor1 and *factor2 respectively. Warning bestie: the solution is not unique β€” you get another one by adding k * value2 to factor1 and subtracting k * value1 from factor2. For values exceeding std::numeric_limits<long, long>::max() you gotta use the next member instead.
  • BigInt diophantus(BigInt *factor1, BigInt *factor2, BigInt const &value1, BigInt const &value2) β€” same energy, BigInt edition πŸ’–. It computes the integral solution of factor1 * value1 + factor2 * value2 = gcd, returns the greatest common divisor (gcd) of value1 and value2, and returns their multiplication factors in *factor1 and *factor2. Again, not unique: add k * value2 to factor1 and subtract k * value1 from factor2 for another solution.
  • BigInt fromText(std::string text, int mode = 0) β€” this one turns a textual representation of a number into an actual BigInt value πŸ¦‹. Conversion keeps going until the end of text, or until it hits a character outside of an expected range.

You can preset that expected range by specifying mode as ios::dec, ios::oct, or ios::hex β€” or, by default, the expected range is figured out by fromText itself by snooping on the characters in text πŸ‘€.

Default behaviour, no cap: if text contains hexadecimal characters then fromText assumes the number is hexadecimal (e.g., "abc" becomes the decimal value 2748); if text starts with 0 and only contains characters in the range 0 until (including) 7 then it assumes octal (e.g., "01234" becomes the decimal value 668). Otherwise it assumes a decimal value.

And if text doesn't represent a valid numerical value (for the given extraction mode) then a FBB::Exception exception gets thrown β€” message: fromText: text does not represent a BigInt value. 🫠

  • BigInt rand(size_t size, Msb msb = MSB_IS_ONE, Lsb lsb = ODD) β€” returns a cryptographically strong pseudo-random number of size bits 🌈. The most significant bit(s) can be controlled by msb (default MSB_IS_ONE), and the least significant bit can be controlled by lsb (default ODD). Important: before you call this one for real, the random number generator must have been seeded. Like, must.

Straight from the RAND_add(3ssl) man-page, bestie:

OpenSSL makes sure that the PRNG state is unique for each thread. On systems that provide /dev/urandom, the randomness device is used to seed the PRNG transparently. However, on all other systems, the application is responsible for seeding the PRNG by calling RAND_add(3ssl), RAND_egd(3ssl), RAND_load_file(3ssl), or RAND_seed(3ssl).
  • BigInt randRange(BigInt const &max) β€” returns a cryptographically strong pseudo-random number in the range 0 <= number < max. Same deal as above: seed the random number generator before calling this for real (peep rand, described just up there) ✨.
  • BigInt setBigEndian(std::string const &bytes) β€” the bytes.length() bytes of bytes are used to compute a BigInt object, which this function returns. The characters in bytes are read as a series of bytes in big-endian order. See also the member function bigEndian() above (iykyk). The returned BigInt has a positive value πŸ’–.
  • BigInt prime(size_t nBits, BigInt const *mod = 0, BigInt const *rem = 0, PrimeType primeType = ANY) β€” returns a prime number of nBits bits πŸ‘‘. If both mod and rem are non-zero, then the condition prime % mod == rem holds. (E.g., use prime % mod == 1 in order to suit a given generator.) The primeType parameter can be ANY, in which case (prime - 1) / 2 may or may not be a prime. If it's SAFE then (prime - 1) / 2 will be a (so-called safe) prime.
  • BigInt pseudoRand(size_t size, Msb msb = MSB_IS_ONE, Lsb lsb = ODD) β€” this member merely calls BigInt::rand. That's it. That's the whole vibe πŸŽ€.
  • BigInt pseudoRandRange(BigInt const &max) β€” this member merely calls BigInt::randRange. Periodt.

🌸 FREE FUNCTIONS IN THE FBB NAMESPACE 🌸

  • std::ostream &operator<<(ostream &out, BigInt const &value) β€” inserts value into the provided ostream. If the hex manipulator has been inserted into the stream before inserting the BigInt value, the value shows up as hexadecimal (with no leading 0x); if the oct manipulator has been inserted, the value is represented as octal (starting with a 0). And it's shown as a decimal value if the dec manipulator is active. If the BigInt value is negative, its value gets a - character in front of it.

Lowkey, this conversion isn't very fast 🐒. For faster conversion consider using the LDC class (cf. ldc(3bobcat)) in statements like:

BigInt value;           // contains a positive value
                        // insert value using decimal digits:
std::cout << LDC{ value };
  • std::istream &operator>>(istream &in, BigInt &value) β€” extracts value from the provided istream. Depending on the currently set extraction mode (dec, oct, or hex), the matching set of characters is extracted from in and converted to a number, which is stored in value. Extraction stops at EOF or at the first character outside the range of characters matching the extraction mode. If no numerical characters were extracted, the stream's failbit is set 😭. The extracted value may be preceded by a - character, which results in an extracted negative value.

πŸ’œ EXAMPLE πŸ’œ

#include <iostream>
#include <bobcat/bigint>

using namespace std;
using namespace FBB;

int main()
{
    BigInt value(BigInt::prime(100));
    BigInt mod(BigInt::rand(50));
    BigInt inverse(value.inverseModc(mod));

    cout << ’(’ << value << " * " << inverse << ") % " << mod << " = " <<
             (    value       *      inverse     ) %      mod << ’\n’;
}

// shows:
// (1258586273445895786081124957771 * 828997573545038) %
//                                                  1007205247048889 = 1

🧚 FILES 🧚

  • bobcat/bigint β€” defines the class interface

⭐ SEE ALSO ⭐

bobcat(7), diffiehellman(3bobcat), ldc(3bobcat), RAND_add(3ssl), RAND_egd(3ssl), RAND_load_file(3ssl), RAND_seed(3).

For BIGNUM:

https://www.openssl.org/docs/man1.0.2/man3/bn_sub_words.html


πŸ™ˆ BUGS πŸ™ˆ

None Reported. Flawless behaviour, honestly πŸ’….


πŸ’€ BOBCAT PROJECT FILES πŸ’€

Debian Bobcat project files:

  • libbobcat6: debian package containing the shared library, changelog and copyright note;
  • libbobcat-dev: debian package containing the static library, headers, manual pages, and developer info;

🌈 BOBCAT 🌈

Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.


This is free software, distributed under the terms of the GNU General Public License (GPL).


πŸ‘‘ AUTHOR πŸ‘‘

Frank B. Brokken (f.b.brokken@rug.nl).


Okay bestie that's the whole page, start to finish, and we ate fr fr πŸ“βœ¨. This one ships in the libbobcat-dev package, version 6.15.01, and the party's been going since 2005 all the way through 2026 β€” so go forth, make big numbers, and stay seeded πŸ’œπŸ¦‹πŸŽ€.

πŸ“¦ where this page comes from