BigDigits: bigdigits.h File Reference (original) (raw)

Interface to core BigDigits "mp" functions using fixed-length arrays. More...

Typedefs
typedef uint32_t DIGIT_T
The basic BigDigit element, an unsigned 32-bit integer. More...
Functions
int mpAbs (DIGIT_T x[], const DIGIT_T y[], size_t ndigits)
Sets x = |y
DIGIT_T mpAdd (DIGIT_T w[], const DIGIT_T u[], const DIGIT_T v[], size_t ndigits)
Computes w = u + v, returns carry. More...
void mpAndBits (DIGIT_T a[], const DIGIT_T b[], const DIGIT_T c[], size_t ndigits)
Computes bitwise a = b AND c. More...
size_t mpBitLength (const DIGIT_T a[], size_t ndigits)
Returns number of significant bits in a. More...
int mpChs (DIGIT_T x[], const DIGIT_T y[], size_t ndigits)
Sets x = -y. More...
int mpCompare (const DIGIT_T a[], const DIGIT_T b[], size_t ndigits)
Returns sign of (a-b) as {-1,0,+1} More...
int mpCompare_ct (const DIGIT_T a[], const DIGIT_T b[], size_t ndigits)
Returns sign of (a-b) as {-1,0,+1} using constant-time algorithm. More...
const char * mpCompileTime (void)
Returns a pointer to a static string containing the time of compilation. More...
size_t mpConvFromDecimal (DIGIT_T a[], size_t ndigits, const char *s)
Converts a string in decimal format to a big digit. More...
size_t mpConvFromHex (DIGIT_T a[], size_t ndigits, const char *s)
Converts a string in hexadecimal format to a big digit. More...
size_t mpConvFromOctets (DIGIT_T a[], size_t ndigits, const unsigned char *c, size_t nbytes)
Converts nbytes octets into big digit a of max size ndigits. More...
size_t mpConvFromStr (DIGIT_T a[], size_t ndigits, const char *s, char **endptr, int base)
Converts a string of digits in the specified radix base to a big digit array. More...
size_t mpConvToDecimal (const DIGIT_T a[], size_t ndigits, char *s, size_t smax)
Converts big digit a into a string in decimal format, where s has size smax including the terminating zero. More...
size_t mpConvToHex (const DIGIT_T a[], size_t ndigits, char *s, size_t smax)
Converts big digit a into a string in hexadecimal format, where s has size smax including the terminating zero. More...
size_t mpConvToOctets (const DIGIT_T a[], size_t ndigits, unsigned char *c, size_t nbytes)
Converts big digit a into string of octets, in big-endian order, padding to nbytes or truncating if necessary. More...
int mpCubeRoot (DIGIT_T s[], const DIGIT_T x[], size_t ndigits)
Computes integer cube root s = floor(cuberoot(x)) More...
int mpDivide (DIGIT_T q[], DIGIT_T r[], const DIGIT_T u[], size_t udigits, DIGIT_T v[], size_t vdigits)
Computes integer division of u by v such that u=qv+r. More...
int mpEqual (const DIGIT_T a[], const DIGIT_T b[], size_t ndigits)
Returns true if a == b, else false. More...
int mpEqual_ct (const DIGIT_T a[], const DIGIT_T b[], size_t ndigits)
Returns true if a == b, else false, using constant-time algorithm. More...
int mpGcd (DIGIT_T g[], const DIGIT_T x[], const DIGIT_T y[], size_t ndigits)
Computes g = gcd(x, y), the greatest common divisor of x and y. More...
int mpGetBit (const DIGIT_T a[], size_t ndigits, size_t n)
Returns value 1 or 0 of bit n (0..nbits-1) More...
int mpIsNegative (const DIGIT_T x[], size_t ndigits)
Returns true (1) if x < 0, else false (0) More...
int mpIsPrime (DIGIT_T w[], size_t ndigits, size_t t)
Returns true (1) if w is probably prime. More...
int mpIsZero (const DIGIT_T a[], size_t ndigits)
Returns true if a is zero, else false. More...
int mpIsZero_ct (const DIGIT_T a[], size_t ndigits)
Returns true if a is zero, else false, using constant-time algorithm. More...
int mpJacobi (const DIGIT_T a[], const DIGIT_T n[], size_t ndigits)
Returns the Jacobi symbol (a/n) in {-1, 0, +1}. More...
void mpModAdd (DIGIT_T w[], const DIGIT_T u[], const DIGIT_T v[], const DIGIT_T m[], size_t ndigits)
Computes w = u + v (mod m) More...
int mpModExp (DIGIT_T y[], const DIGIT_T x[], const DIGIT_T e[], DIGIT_T m[], size_t ndigits)
Computes y = x^e mod m. More...
int mpModExp_ct (DIGIT_T yout[], const DIGIT_T x[], const DIGIT_T e[], DIGIT_T m[], size_t ndigits)
Computes y = x^e mod m in constant time. More...
void mpModHalve (DIGIT_T w[], const DIGIT_T u[], const DIGIT_T p[], size_t ndigits)
Computes w = u/2 (mod p) for an odd prime p. More...
int mpModInv (DIGIT_T inv[], const DIGIT_T u[], const DIGIT_T m[], size_t ndigits)
Computes the inverse of u modulo m, inv = u^{-1} mod m. More...
int mpModMult (DIGIT_T a[], const DIGIT_T x[], const DIGIT_T y[], DIGIT_T m[], size_t ndigits)
Computes a = (x * y) mod m. More...
void mpModPowerOf2 (DIGIT_T a[], size_t ndigits, size_t L)
Computes a = a mod 2^L, ie clears all bits greater than L. More...
void mpModSpecial (DIGIT_T u[], const DIGIT_T v[], const DIGIT_T m[], size_t ndigits)
Computes u = v (mod m) in the special case where 0<=v<km for small k. More...
int mpModSqrt (DIGIT_T x[], const DIGIT_T a[], DIGIT_T p[], size_t ndigits)
Computes x = one square root of an integer a modulo an odd prime p More...
int mpModSquare (DIGIT_T a[], const DIGIT_T x[], DIGIT_T m[], size_t ndigits)
Computes a = x^2 mod m. More...
void mpModSub (DIGIT_T w[], const DIGIT_T u[], const DIGIT_T v[], const DIGIT_T m[], size_t ndigits)
Computes w = u - v (mod m) More...
int mpModulo (DIGIT_T r[], const DIGIT_T u[], size_t udigits, DIGIT_T v[], size_t vdigits)
Computes remainder r = u mod v. More...
int mpMultiply (DIGIT_T w[], const DIGIT_T u[], const DIGIT_T v[], size_t ndigits)
Computes product w = u * v. More...
void mpNotBits (DIGIT_T a[], const DIGIT_T b[], size_t ndigits)
Computes bitwise a = NOT b. More...
void mpOrBits (DIGIT_T a[], const DIGIT_T b[], const DIGIT_T c[], size_t ndigits)
Computes bitwise a = b OR c. More...
void mpPrint (const DIGIT_T *a, size_t ndigits)
Print all digits in hex incl leading zero digits. More...
void mpPrintBits (const char *prefix, DIGIT_T *a, size_t ndigits, const char *suffix)
Print in bit (0/1) format with optional prefix and suffix strings. More...
void mpPrintDecimal (const char *prefix, const DIGIT_T *a, size_t ndigits, const char *suffix)
Print in decimal format with optional prefix and suffix strings. More...
void mpPrintDecimalSigned (const char *prefix, DIGIT_T *a, size_t ndigits, const char *suffix)
Print a signed integer in decimal format with optional prefix and suffix strings. More...
void mpPrintHex (const char *prefix, const DIGIT_T *a, size_t ndigits, const char *suffix)
Print in hex format with optional prefix and suffix strings. More...
void mpPrintNL (const DIGIT_T *a, size_t ndigits)
Print all digits in hex with newlines. More...
void mpPrintTrim (const DIGIT_T *a, size_t ndigits)
Print in hex but trim leading zero digits. More...
void mpPrintTrimNL (const DIGIT_T *a, size_t ndigits)
Print in hex, trim leading zeroes, add newlines. More...
size_t mpQuickRandBits (DIGIT_T a[], size_t ndigits, size_t nbits)
Generate a quick-and-dirty random mp number a of bit length at most nbits using plain-old-rand. More...
int mpRabinMiller (DIGIT_T w[], size_t ndigits, size_t t)
Returns true (1) if w is probably prime using just the Rabin-Miller test. More...
int mpSetBit (DIGIT_T a[], size_t ndigits, size_t n, int value)
Sets bit n of a (0..nbits-1) with value 1 or 0. More...
void mpSetDigit (DIGIT_T a[], DIGIT_T d, size_t ndigits)
Sets a = d where d is a single digit. More...
void mpSetEqual (DIGIT_T a[], const DIGIT_T b[], size_t ndigits)
Sets a = b. More...
volatile DIGIT_T mpSetZero (volatile DIGIT_T a[], size_t ndigits)
Sets a = 0. More...
DIGIT_T mpShiftLeft (DIGIT_T a[], const DIGIT_T b[], size_t x, size_t ndigits)
Computes a = b << x. More...
DIGIT_T mpShiftRight (DIGIT_T a[], const DIGIT_T b[], size_t x, size_t ndigits)
Computes a = b >> x. More...
DIGIT_T mpShortAdd (DIGIT_T w[], const DIGIT_T u[], DIGIT_T d, size_t ndigits)
Computes w = u + d, returns carry. More...
int mpShortCmp (const DIGIT_T a[], DIGIT_T d, size_t ndigits)
Returns sign of (a - d) where d is a single digit. More...
DIGIT_T mpShortDiv (DIGIT_T q[], const DIGIT_T u[], DIGIT_T d, size_t ndigits)
Computes quotient q = u div d, returns remainder. More...
int mpShortIsEqual (const DIGIT_T a[], DIGIT_T d, size_t ndigits)
Returns true if a == d, else false, where d is a single digit. More...
DIGIT_T mpShortMod (const DIGIT_T a[], DIGIT_T d, size_t ndigits)
Computes remainder r = a mod d. More...
DIGIT_T mpShortMult (DIGIT_T p[], const DIGIT_T x[], DIGIT_T d, size_t ndigits)
Computes product p = x * d. More...
DIGIT_T mpShortSub (DIGIT_T w[], const DIGIT_T u[], DIGIT_T d, size_t ndigits)
Computes w = u - d, returns borrow. More...
size_t mpSizeof (const DIGIT_T a[], size_t ndigits)
Returns number of significant non-zero digits in a. More...
int mpSqrt (DIGIT_T s[], const DIGIT_T x[], size_t ndigits)
Computes integer square root s = floor(sqrt(x)) More...
int mpSquare (DIGIT_T w[], const DIGIT_T x[], size_t ndigits)
Computes square w = x^2. More...
DIGIT_T mpSubtract (DIGIT_T w[], const DIGIT_T u[], const DIGIT_T v[], size_t ndigits)
Computes w = u - v, returns borrow. More...
DIGIT_T mpToShort (const DIGIT_T a[], size_t ndigits)
Returns the least significant digit in a. More...
int mpVersion (void)
Returns version number = major*1000+minor*100+release*10+PP_OPTIONS. More...
void mpXorBits (DIGIT_T a[], const DIGIT_T b[], const DIGIT_T c[], size_t ndigits)
Computes bitwise a = b XOR c. More...
DIGIT_T spDivide (DIGIT_T *q, DIGIT_T *r, const DIGIT_T u[2], DIGIT_T v)
Computes quotient q = u div v, remainder r = u mod v, where q, r and v are single digits. More...
int spMultiply (DIGIT_T p[2], DIGIT_T x, DIGIT_T y)
Computes p = x * y, where x and y are single digits. More...
DIGIT_T spSimpleRand (DIGIT_T lower, DIGIT_T upper)
Returns a simple pseudo-random digit between lower and upper. More...

Interface to core BigDigits "mp" functions using fixed-length arrays.

DIGIT_T

The basic BigDigit element, an unsigned 32-bit integer.

mpAbs()

Sets x = |y|, the absolute value of y.

mpAdd()

Computes w = u + v, returns carry.

Precondition

w and v must not overlap.

mpAndBits()

Computes bitwise a = b AND c.

mpBitLength()

size_t mpBitLength ( const DIGIT_T _a_[],
size_t ndigits
)

Returns number of significant bits in a.

mpChs()

mpCompare()

int mpCompare ( const DIGIT_T _a_[],
const DIGIT_T _b_[],
size_t ndigits
)

Returns sign of (a-b) as {-1,0,+1}

mpCompare_ct()

int mpCompare_ct ( const DIGIT_T _a_[],
const DIGIT_T _b_[],
size_t ndigits
)

Returns sign of (a-b) as {-1,0,+1} using constant-time algorithm.

mpCompileTime()

const char* mpCompileTime ( void )

Returns a pointer to a static string containing the time of compilation.

mpConvFromDecimal()

size_t mpConvFromDecimal ( DIGIT_T _a_[],
size_t ndigits,
const char * s
)

Converts a string in decimal format to a big digit.

Returns

actual number of (possibly zero) digits set.

mpConvFromHex()

size_t mpConvFromHex ( DIGIT_T _a_[],
size_t ndigits,
const char * s
)

Converts a string in hexadecimal format to a big digit.

Returns

actual number of (possibly zero) digits set.

mpConvFromOctets()

size_t mpConvFromOctets ( DIGIT_T _a_[],
size_t ndigits,
const unsigned char * c,
size_t nbytes
)

Converts nbytes octets into big digit a of max size ndigits.

Returns

actual number of digits set

mpConvFromStr()

size_t mpConvFromStr ( DIGIT_T _a_[],
size_t ndigits,
const char * s,
char ** endptr,
int base
)

Converts a string of digits in the specified radix base to a big digit array.

Supported bases are 10 (decimal), 16 (hexadecimal), and 2 (binary). The conversion stops at the first invalid character or end of string. Sets endptr to point to the first character that could not be converted, similar to ANSI strtoul. If base is 0 then base 10 is assumed unless the prefix 0x or 0b is present, denoting base 16 or base 2 respectively.

mpConvToDecimal()

size_t mpConvToDecimal ( const DIGIT_T _a_[],
size_t ndigits,
char * s,
size_t smax
)

Converts big digit a into a string in decimal format, where s has size smax including the terminating zero.

Returns

number of chars required excluding leading zeroes.

mpConvToHex()

size_t mpConvToHex ( const DIGIT_T _a_[],
size_t ndigits,
char * s,
size_t smax
)

Converts big digit a into a string in hexadecimal format, where s has size smax including the terminating zero.

Returns

number of chars required excluding leading zeroes.

mpConvToOctets()

size_t mpConvToOctets ( const DIGIT_T _a_[],
size_t ndigits,
unsigned char * c,
size_t nbytes
)

Converts big digit a into string of octets, in big-endian order, padding to nbytes or truncating if necessary.

Returns

number of non-zero octets required.

mpCubeRoot()

Computes integer cube root s = floor(cuberoot(x))

mpDivide()

Computes integer division of u by v such that u=qv+r.

Parameters

[out] q to receive quotient = u div v, an array of size udigits
[out] r to receive divisor = u mod v, an array of size udigits
[in] u dividend of size udigits
[in] udigits size of arrays q r and u
[in] v divisor of size vdigits
[in] vdigits size of array v

Precondition

q and r must be independent of u and v.

Warning

Trashes q and r first

mpEqual()

Returns true if a == b, else false.

mpEqual_ct()

int mpEqual_ct ( const DIGIT_T _a_[],
const DIGIT_T _b_[],
size_t ndigits
)

Returns true if a == b, else false, using constant-time algorithm.

mpGcd()

Computes g = gcd(x, y), the greatest common divisor of x and y.

mpGetBit()

int mpGetBit ( const DIGIT_T _a_[],
size_t ndigits,
size_t n
)

Returns value 1 or 0 of bit n (0..nbits-1)

mpIsNegative()

int mpIsNegative ( const DIGIT_T _x_[],
size_t ndigits
)

Returns true (1) if x < 0, else false (0)

mpIsPrime()

int mpIsPrime ( DIGIT_T _w_[],
size_t ndigits,
size_t t
)

Returns true (1) if w is probably prime.

Parameters

[in] w Number to test
[in] ndigits size of array w
[in] t The count of Rabin-Miller primality tests to carry out (recommended at least 80)

Returns

true (1) if w is probably prime otherwise false (0)

See also

mpRabinMiller().

mpIsZero()

int mpIsZero ( const DIGIT_T _a_[],
size_t ndigits
)

Returns true if a is zero, else false.

mpIsZero_ct()

int mpIsZero_ct ( const DIGIT_T _a_[],
size_t ndigits
)

Returns true if a is zero, else false, using constant-time algorithm.

mpJacobi()

Returns the Jacobi symbol (a/n) in {-1, 0, +1}.

mpModAdd()

Computes w = u + v (mod m)

Precondition

Require u and v to be in the range [0, m-1]. The variables w and v must not overlap.

mpModExp()

mpModExp_ct()

Computes y = x^e mod m in constant time.

mpModHalve()

Computes w = u/2 (mod p) for an odd prime p.

Precondition

Require u to be in the range [0,p-1]

mpModInv()

Computes the inverse of u modulo m, inv = u^{-1} mod m.

mpModMult()

Computes a = (x * y) mod m.

mpModPowerOf2()

void mpModPowerOf2 ( DIGIT_T _a_[],
size_t ndigits,
size_t L
)

Computes a = a mod 2^L, ie clears all bits greater than L.

mpModSpecial()

Computes u = v (mod m) in the special case where 0<=v<km for small k.

Precondition

v is in the range [0,km] where k is a small integer

Warning

Use only if you know k is small, say < 3.

mpModSqrt()

Computes x = one square root of an integer a modulo an odd prime p

Parameters

x To receive the result
a An integer expected to be a quadratic residue modulo p
p An odd prime
ndigits The number of digits in each of the parameters

Returns

0 if successful, or -1 if square root does not exist

Warning

Check the return value before using the result x.

mpModSquare()

mpModSub()

Computes w = u - v (mod m)

Precondition

Require u and v to be in the range [0, m-1]. The variables w and v must not overlap.

mpModulo()

Computes remainder r = u mod v.

Parameters

[out] r to receive divisor = u mod v, an array of size vdigits
[in] u dividend of size udigits
[in] udigits size of arrays r and u
[in] v divisor of size vdigits
[in] vdigits size of array v

Precondition

r and u must not overlap.

mpMultiply()

Computes product w = u * v.

Parameters

[out] w To receive the product, an array of size 2 x ndigits
[in] u An array of size ndigits
[in] v An array of size ndigits
[in] ndigits size of arrays u and v

Precondition

w and u must not overlap.

Warning

The product must be of size 2 x ndigits

mpNotBits()

Computes bitwise a = NOT b.

mpOrBits()

Computes bitwise a = b OR c.

mpPrint()

void mpPrint ( const DIGIT_T * a,
size_t ndigits
)

Print all digits in hex incl leading zero digits.

mpPrintBits()

void mpPrintBits ( const char * prefix,
DIGIT_T * a,
size_t ndigits,
const char * suffix
)

Print in bit (0/1) format with optional prefix and suffix strings.

mpPrintDecimal()

void mpPrintDecimal ( const char * prefix,
const DIGIT_T * a,
size_t ndigits,
const char * suffix
)

Print in decimal format with optional prefix and suffix strings.

mpPrintDecimalSigned()

void mpPrintDecimalSigned ( const char * prefix,
DIGIT_T * a,
size_t ndigits,
const char * suffix
)

Print a signed integer in decimal format with optional prefix and suffix strings.

mpPrintHex()

void mpPrintHex ( const char * prefix,
const DIGIT_T * a,
size_t ndigits,
const char * suffix
)

Print in hex format with optional prefix and suffix strings.

mpPrintNL()

void mpPrintNL ( const DIGIT_T * a,
size_t ndigits
)

Print all digits in hex with newlines.

mpPrintTrim()

void mpPrintTrim ( const DIGIT_T * a,
size_t ndigits
)

mpPrintTrimNL()

void mpPrintTrimNL ( const DIGIT_T * a,
size_t ndigits
)

mpQuickRandBits()

size_t mpQuickRandBits ( DIGIT_T _a_[],
size_t ndigits,
size_t nbits
)

Generate a quick-and-dirty random mp number a of bit length at most nbits using plain-old-rand.

See also

mpRandomBits()

mpRabinMiller()

int mpRabinMiller ( DIGIT_T _w_[],
size_t ndigits,
size_t t
)

Returns true (1) if w is probably prime using just the Rabin-Miller test.

See also

mpIsPrime() is preferred.

mpSetBit()

int mpSetBit ( DIGIT_T _a_[],
size_t ndigits,
size_t n,
int value
)

Sets bit n of a (0..nbits-1) with value 1 or 0.

mpSetDigit()

Sets a = d where d is a single digit.

mpSetEqual()

mpSetZero()

mpShiftLeft()

mpShiftRight()

mpShortAdd()

Computes w = u + d, returns carry.

mpShortCmp()

Returns sign of (a - d) where d is a single digit.

mpShortDiv()

Computes quotient q = u div d, returns remainder.

mpShortIsEqual()

Returns true if a == d, else false, where d is a single digit.

mpShortMod()

Computes remainder r = a mod d.

mpShortMult()

Computes product p = x * d.

mpShortSub()

Computes w = u - d, returns borrow.

mpSizeof()

size_t mpSizeof ( const DIGIT_T _a_[],
size_t ndigits
)

Returns number of significant non-zero digits in a.

mpSqrt()

Computes integer square root s = floor(sqrt(x))

mpSquare()

Computes square w = x^2.

Parameters

[out] w array of size 2 x ndigits to receive square
[in] x array of size ndigits
[in] ndigits size of array x

Precondition

w and x must not overlap.

Warning

The product w must be of size 2 x ndigits

mpSubtract()

Computes w = u - v, returns borrow.

Precondition

w and v must not overlap.

mpToShort()

Returns the least significant digit in a.

mpVersion()

Returns version number = major*1000+minor*100+release*10+PP_OPTIONS.

mpXorBits()

Computes bitwise a = b XOR c.

spDivide()

Computes quotient q = u div v, remainder r = u mod v, where q, r and v are single digits.

spMultiply()

Computes p = x * y, where x and y are single digits.

spSimpleRand()

Returns a simple pseudo-random digit between lower and upper.

See also

[spBetterRand()](bigdigits%5Frand%5F8h.html#ac9fb601230883e955fcef22643ea4844 "Returns a "better" pseudo-random digit using internal RNG.")