Botan

BigInt

License
Botan is released under the Simplified BSD License (see LICENSE.md)

struct  BigInt;

Arbitrary precision integer


class  DivideByZero: object.Exception;

 DivideByZero Exception


this(T)(T n) if (isNumeric!T);

Create BigInt from any integer

Parameters
T n initial value of this BigInt

ref BigInt  opAssign(size_t other);

Move constructor


this(in string str);

Create BigInt from a string. If the string starts with 0x the rest of the string will be interpreted as hexadecimal digits. Otherwise, it will be interpreted as a decimal number.

Parameters
string str the string to parse for an integer value

this(const(ubyte)* input, size_t length, Base base = Binary);

Create a BigInt from an integer in a ubyte array

Parameters
const(ubyte)* input the ubyte array holding the value
size_t length size of buf
Base base is the number base of the integer in buf

this(RandomNumberGenerator rng, size_t bits);

Create a random BigInt of the specified size

Parameters
RandomNumberGenerator rng random number generator
size_t bits size in bits

this(Sign s, size_t size);

Create BigInt of specified size, all zeros

Parameters
Sign s the sign
size_t size of the internal register in words

this()(auto ref BigInt other);

Move constructor


void  opAssign()(auto ref BigInt other);

Move assignment


void  swap()(auto ref BigInt other);

Copy assignment

Swap this value with another

Parameters
BigInt other BigInt to  swap values with

void  opOpAssign(string op)(auto ref const BigInt y) if (op == "+");

+= operator

Parameters
BigInt y the BigInt to add to this

void  opOpAssign(string op)(auto ref const BigInt y) if (op == "-");

-= operator

Parameters
BigInt y the BigInt to subtract from this

void  opOpAssign(string op)(ref const BigInt y) if (op == "*");

*= operator

Parameters
BigInt y the BigInt to multiply with this

void  opOpAssign(string op)(auto ref const BigInt y) if (op == "/");

/= operator

Parameters
BigInt y the BigInt to divide this by

void  opOpAssign(string op)(auto ref const BigInt mod) if (op == "%");

Modulo operator

Parameters
BigInt mod the modulus to reduce this by

void  opOpAssign(string op)(word mod) if (op == "%");

Modulo operator

Parameters
word mod the modulus (word) to reduce this by

void  opOpAssign(string op)(size_t shift) if (op == "<<");

Left shift operator

Parameters
size_t shift the number of bits to shift this left by

void  opOpAssign(string op)(size_t shift) if (op == ">>");

Right shift operator

Parameters
size_t shift the number of bits to shift this right by

ref BigInt  opUnary(string op)() if (op == "++");

Increment operator


ref BigInt  opUnary(string op)() if (op == "--");

Decrement operator


const BigInt  opUnary(string op)() if (op == "-");

Unary negation operator

Returns
negative this

const T  opCast(T : bool)();

bool cast

Returns
true iff this is not zero, otherwise false

void  clear();

Zeroize the BigInt. The size of the underlying register is not modified.


const int  cmp(ref const BigInt other, bool check_signs = true);

Compare this to another BigInt

Parameters
BigInt other the BigInt value to compare with
bool check_signs include sign in comparison?
Returns
if (thisn) return 1, if both values are identical return 0 [like Perl's <=> operator]

const bool  isEven();

Test if the integer has an even value

Returns
true if the integer is even, false otherwise

const bool  isOdd();

Test if the integer has an odd value

Returns
true if the integer is odd, false otherwise

const bool  isNonzero();

Test if the integer is not zero

Returns
true if the integer is non-zero, false otherwise

const bool  isZero();

Test if the integer is zero

Returns
true if the integer is zero, false otherwise

void  setBit(size_t n);

Set bit at specified position

Parameters
size_t n bit position to set

void  clearBit(size_t n);

Clear bit at specified position

Parameters
size_t n bit position to clear

void  maskBits(size_t n);

Clear all but the lowest n bits

Parameters
size_t n amount of bits to keep

const bool  getBit(size_t n);

Return bit value at specified position

Parameters
size_t n the bit offset to test
Returns
true, if the bit at position n is set, false otherwise

const uint  getSubstring(size_t offset, size_t length);

Return (a maximum of) 32 bits of the complete value

Parameters
size_t offset the offset to start extracting
size_t length amount of bits to extract (starting at offset)
Returns
the integer extracted from the register starting at offset with specified length

const uint  toUint();

Convert this value into a uint, if it is in the range [0 ... 2**32-1], or otherwise throw new an exception.

Returns
the value as a uint if conversion is possible

const ubyte  byteAt(size_t n);

Parameters
size_t n the offset to get a ubyte from
Returns
ubyte at offset n

const word  wordAt(size_t n);

Return the word at a specified position of the internal register

Parameters
size_t n position in the register
Returns
value at position n

const bool  isNegative();

Tests if the sign of the integer is negative

Returns
true, iff the integer has a negative sign

const bool  isPositive();

Tests if the sign of the integer is positive

Returns
true, iff the integer has a positive sign

const Sign  sign();

Return the  sign of the integer

Returns
the  sign of the integer

const Sign  reverseSign();

Returns
the opposite sign of the represented integer value

void  flipSign();

Flip the sign of this BigInt


void  setSign(Sign s);

Set sign of the integer

Parameters
Sign s new Sign to set

const BigInt  abs();

Returns
absolute (positive) value of this

const size_t  size();

Give  size of internal register

Returns
 size of internal register in words

const size_t  sigWords();

Return how many words we need to hold this value

Returns
significant words of the represented integer value

const size_t  bytes();

Give ubyte length of the integer

Returns
ubyte length of the represented integer value

const size_t  bits();

Get the bit length of the integer

Returns
bit length of the represented integer value

word*  mutablePtr();

Return a mutable pointer to the register

Returns
a pointer to the start of the internal register

const @property const(word*)  ptr();

Return a const pointer to the register

Returns
a pointer to the start of the internal register

void  growTo(size_t n);

Increase internal register buffer to at least n words

Parameters
size_t n new size of register

void  randomize(RandomNumberGenerator rng, size_t bitsize = 0);

Fill BigInt with a random number with size of bitsize

Parameters
RandomNumberGenerator rng the random number generator to use
size_t bitsize number of bits the created random value should have

const void  binaryEncode(ubyte* output);

Store BigInt-value in a given ubyte array

Parameters
ubyte* output destination ubyte array for the integer value

void  binaryDecode(const(ubyte)* buf, size_t length);

Read integer value from a ubyte array with given size

Parameters
const(ubyte)* buf ubyte array buffer containing the integer
size_t length size of buf

void  binaryDecode(ALLOC)(auto ref const Vector!(ubyte, ALLOC) buf);
void  binaryDecode(ALLOC)(auto ref const RefCounted!(Vector!(ubyte, ALLOC), ALLOC) buf);

Read integer value from a ubyte array (SecureVector!ubyte)

Parameters
Vector!(ubyte, ALLOC) buf the array to load from

const size_t  encodedSize(Base base = Binary);

Parameters
Base base the base to measure the size for
Returns
size of this integer in base base

BigInt  randomInteger()(RandomNumberGenerator rng, auto ref const BigInt min, auto ref const BigInt max);

Parameters
RandomNumberGenerator rng a random number generator
BigInt min the minimum value
BigInt max the maximum value
Returns
random integer in [min,max)

static BigInt  powerOf2(size_t n);

Create a power of two

Parameters
size_t n the power of two to create
Returns
bigint representing 2^n

Vector!ubyte  encode()(auto ref const BigInt n, Base base = Binary);

Encode the integer value from a BigInt to an Array of bytes

Parameters
BigInt n the BigInt to use as integer source
Base base number-base of resulting ubyte array representation
Returns
SecureVector of bytes containing the integer with given base

SecureVector!ubyte  encodeLocked()(auto ref const BigInt n, Base base = Binary);

Encode the integer value from a BigInt to a Secure Array of bytes

Parameters
BigInt n the BigInt to use as integer source
Base base number-base of resulting ubyte array representation
Returns
SecureVector of bytes containing the integer with given base

void  encode()(ubyte* output, auto ref const BigInt n, Base base = Binary);

Encode the integer value from a BigInt to a ubyte array

Parameters
ubyte* output destination ubyte array for the encoded integer value with given base
BigInt n the BigInt to use as integer source
Base base number-base of resulting ubyte array representation

static BigInt  decode(const(ubyte)* buf, size_t length, Base base = Binary);

Create a BigInt from an integer in a ubyte array

Parameters
const(ubyte)* buf the binary value to load
size_t length size of buf
Base base number-base of the integer in buf
Returns
BigInt representing the integer in the ubyte array

BigInt  decode(ALLOC)(auto ref const RefCounted!(Vector!(ubyte, ALLOC), ALLOC) buf, Base base = Binary);

Create a BigInt from an integer in a ubyte array

Parameters
RefCounted!(Vector!(ubyte, ALLOC), ALLOC) buf the binary value to load
Base base number-base of the integer in buf
Returns
BigInt representing the integer in the ubyte array

BigInt  decode(ALLOC)(auto ref const Vector!(ubyte, ALLOC) buf, Base base = Binary);

Create a BigInt from an integer in a ubyte array

Parameters
Vector!(ubyte, ALLOC) buf the binary value to load
Base base number-base of the integer in buf
Returns
BigInt representing the integer in the ubyte array

SecureVector!ubyte  encode1363()(auto ref const BigInt n, size_t bytes);

Encode a BigInt to a ubyte array according to IEEE 1363

Parameters
BigInt n the BigInt to encode
size_t bytes the length of the resulting SecureVector!ubyte
Returns
a SecureVector!ubyte containing the encoded BigInt