RetroArch
Macros | Functions | Variables
bignum.c File Reference
#include "mbedtls/config.h"
#include "mbedtls/bignum.h"
#include "mbedtls/bn_mul.h"
#include <string.h>
#include "mbedtls/platform.h"
Include dependency graph for bignum.c:

Macros

#define ciL   (sizeof(mbedtls_mpi_uint)) /* chars in limb */
 
#define biL   (ciL << 3) /* bits in limb */
 
#define biH   (ciL << 2) /* half limb size */
 
#define MPI_SIZE_T_MAX   ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
 
#define BITS_TO_LIMBS(i)   ( (i) / biL + ( (i) % biL != 0 ) )
 
#define CHARS_TO_LIMBS(i)   ( (i) / ciL + ( (i) % ciL != 0 ) )
 
#define GCD_PAIR_COUNT   3
 

Functions

static void mbedtls_mpi_zeroize (mbedtls_mpi_uint *v, size_t n)
 
void mbedtls_mpi_init (mbedtls_mpi *X)
 Initialize one MPI (make internal references valid) This just makes it ready to be set or freed, but does not define a value for the MPI. More...
 
void mbedtls_mpi_free (mbedtls_mpi *X)
 Unallocate one MPI. More...
 
int mbedtls_mpi_grow (mbedtls_mpi *X, size_t nblimbs)
 Enlarge to the specified number of limbs. More...
 
int mbedtls_mpi_shrink (mbedtls_mpi *X, size_t nblimbs)
 Resize down, keeping at least the specified number of limbs. More...
 
int mbedtls_mpi_copy (mbedtls_mpi *X, const mbedtls_mpi *Y)
 Copy the contents of Y into X. More...
 
void mbedtls_mpi_swap (mbedtls_mpi *X, mbedtls_mpi *Y)
 Swap the contents of X and Y. More...
 
int mbedtls_mpi_safe_cond_assign (mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
 Safe conditional assignement X = Y if assign is 1. More...
 
int mbedtls_mpi_safe_cond_swap (mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap)
 Safe conditional swap X <-> Y if swap is 1. More...
 
int mbedtls_mpi_lset (mbedtls_mpi *X, mbedtls_mpi_sint z)
 Set value from integer. More...
 
int mbedtls_mpi_get_bit (const mbedtls_mpi *X, size_t pos)
 Get a specific bit from X. More...
 
int mbedtls_mpi_set_bit (mbedtls_mpi *X, size_t pos, unsigned char val)
 Set a bit of X to a specific value of 0 or 1. More...
 
size_t mbedtls_mpi_lsb (const mbedtls_mpi *X)
 Return the number of zero-bits before the least significant '1' bit. More...
 
static size_t mbedtls_clz (const mbedtls_mpi_uint x)
 
size_t mbedtls_mpi_bitlen (const mbedtls_mpi *X)
 Return the number of bits up to and including the most significant '1' bit'. More...
 
size_t mbedtls_mpi_size (const mbedtls_mpi *X)
 Return the total size in bytes. More...
 
static int mpi_get_digit (mbedtls_mpi_uint *d, int radix, char c)
 
int mbedtls_mpi_read_string (mbedtls_mpi *X, int radix, const char *s)
 Import from an ASCII string. More...
 
static int mpi_write_hlp (mbedtls_mpi *X, int radix, char **p)
 
int mbedtls_mpi_write_string (const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
 Export into an ASCII string. More...
 
int mbedtls_mpi_read_file (mbedtls_mpi *X, int radix, FILE *fin)
 Read MPI from a line in an opened file. More...
 
int mbedtls_mpi_write_file (const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
 Write X into an opened file, or stdout if fout is NULL. More...
 
int mbedtls_mpi_read_binary (mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
 Import X from unsigned binary data, big endian. More...
 
int mbedtls_mpi_write_binary (const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
 Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with zeros if the number is smaller. More...
 
int mbedtls_mpi_shift_l (mbedtls_mpi *X, size_t count)
 Left-shift: X <<= count. More...
 
int mbedtls_mpi_shift_r (mbedtls_mpi *X, size_t count)
 Right-shift: X >>= count. More...
 
int mbedtls_mpi_cmp_abs (const mbedtls_mpi *X, const mbedtls_mpi *Y)
 Compare unsigned values. More...
 
int mbedtls_mpi_cmp_mpi (const mbedtls_mpi *X, const mbedtls_mpi *Y)
 Compare signed values. More...
 
int mbedtls_mpi_cmp_int (const mbedtls_mpi *X, mbedtls_mpi_sint z)
 Compare signed values. More...
 
int mbedtls_mpi_add_abs (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Unsigned addition: X = |A| + |B|. More...
 
static void mpi_sub_hlp (size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d)
 
int mbedtls_mpi_sub_abs (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Unsigned subtraction: X = |A| - |B|. More...
 
int mbedtls_mpi_add_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Signed addition: X = A + B. More...
 
int mbedtls_mpi_sub_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Signed subtraction: X = A - B. More...
 
int mbedtls_mpi_add_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
 Signed addition: X = A + b. More...
 
int mbedtls_mpi_sub_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
 Signed subtraction: X = A - b. More...
 
static void mpi_mul_hlp (size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b)
 
int mbedtls_mpi_mul_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Baseline multiplication: X = A * B. More...
 
int mbedtls_mpi_mul_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
 Baseline multiplication: X = A * b. More...
 
static mbedtls_mpi_uint mbedtls_int_div_int (mbedtls_mpi_uint u1, mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r)
 
int mbedtls_mpi_div_mpi (mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Division by mbedtls_mpi: A = Q * B + R. More...
 
int mbedtls_mpi_div_int (mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
 Division by int: A = Q * b + R. More...
 
int mbedtls_mpi_mod_mpi (mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Modulo: R = A mod B. More...
 
int mbedtls_mpi_mod_int (mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
 Modulo: r = A mod b. More...
 
static void mpi_montg_init (mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
 
static int mpi_montmul (mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T)
 
static int mpi_montred (mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T)
 
int mbedtls_mpi_exp_mod (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
 Sliding-window exponentiation: X = A^E mod N. More...
 
int mbedtls_mpi_gcd (mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
 Greatest common divisor: G = gcd(A, B) More...
 
int mbedtls_mpi_fill_random (mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Fill an MPI X with size bytes of random. More...
 
int mbedtls_mpi_inv_mod (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
 Modular inverse: X = A^-1 mod N. More...
 
static int mpi_check_small_factors (const mbedtls_mpi *X)
 
static int mpi_miller_rabin (const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 
int mbedtls_mpi_is_prime (const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Miller-Rabin primality test. More...
 
int mbedtls_mpi_gen_prime (mbedtls_mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Prime number generation. More...
 
int mbedtls_mpi_self_test (int verbose)
 Checkup routine. More...
 

Variables

static const int small_prime []
 
static const int gcd_pairs [GCD_PAIR_COUNT][3]
 

Macro Definition Documentation

◆ biH

#define biH   (ciL << 2) /* half limb size */

◆ biL

#define biL   (ciL << 3) /* bits in limb */

◆ BITS_TO_LIMBS

#define BITS_TO_LIMBS (   i)    ( (i) / biL + ( (i) % biL != 0 ) )

◆ CHARS_TO_LIMBS

#define CHARS_TO_LIMBS (   i)    ( (i) / ciL + ( (i) % ciL != 0 ) )

◆ ciL

#define ciL   (sizeof(mbedtls_mpi_uint)) /* chars in limb */

◆ GCD_PAIR_COUNT

#define GCD_PAIR_COUNT   3

◆ MPI_SIZE_T_MAX

#define MPI_SIZE_T_MAX   ( (size_t) -1 ) /* SIZE_T_MAX is not standard */

Function Documentation

◆ mbedtls_clz()

static size_t mbedtls_clz ( const mbedtls_mpi_uint  x)
static
Here is the caller graph for this function:

◆ mbedtls_int_div_int()

static mbedtls_mpi_uint mbedtls_int_div_int ( mbedtls_mpi_uint  u1,
mbedtls_mpi_uint  u0,
mbedtls_mpi_uint  d,
mbedtls_mpi_uint r 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_add_abs()

int mbedtls_mpi_add_abs ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Unsigned addition: X = |A| + |B|.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_add_int()

int mbedtls_mpi_add_int ( mbedtls_mpi X,
const mbedtls_mpi A,
mbedtls_mpi_sint  b 
)

Signed addition: X = A + b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to add
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_add_mpi()

int mbedtls_mpi_add_mpi ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Signed addition: X = A + B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_bitlen()

size_t mbedtls_mpi_bitlen ( const mbedtls_mpi X)

Return the number of bits up to and including the most significant '1' bit'.

Note: Thus also the one-based index of the most significant '1' bit

Parameters
XMPI to use
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_cmp_abs()

int mbedtls_mpi_cmp_abs ( const mbedtls_mpi X,
const mbedtls_mpi Y 
)

Compare unsigned values.

Parameters
XLeft-hand MPI
YRight-hand MPI
Returns
1 if |X| is greater than |Y|, -1 if |X| is lesser than |Y| or 0 if |X| is equal to |Y|
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_cmp_int()

int mbedtls_mpi_cmp_int ( const mbedtls_mpi X,
mbedtls_mpi_sint  z 
)

Compare signed values.

Parameters
XLeft-hand MPI
zThe integer value to compare to
Returns
1 if X is greater than z, -1 if X is lesser than z or 0 if X is equal to z
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_cmp_mpi()

int mbedtls_mpi_cmp_mpi ( const mbedtls_mpi X,
const mbedtls_mpi Y 
)

Compare signed values.

Parameters
XLeft-hand MPI
YRight-hand MPI
Returns
1 if X is greater than Y, -1 if X is lesser than Y or 0 if X is equal to Y
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_copy()

int mbedtls_mpi_copy ( mbedtls_mpi X,
const mbedtls_mpi Y 
)

Copy the contents of Y into X.

Parameters
XDestination MPI
YSource MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_div_int()

int mbedtls_mpi_div_int ( mbedtls_mpi Q,
mbedtls_mpi R,
const mbedtls_mpi A,
mbedtls_mpi_sint  b 
)

Division by int: A = Q * b + R.

Parameters
QDestination MPI for the quotient
RDestination MPI for the rest value
ALeft-hand MPI
bInteger to divide by
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Note
Either Q or R can be NULL.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_div_mpi()

int mbedtls_mpi_div_mpi ( mbedtls_mpi Q,
mbedtls_mpi R,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Division by mbedtls_mpi: A = Q * B + R.

Parameters
QDestination MPI for the quotient
RDestination MPI for the rest value
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Note
Either Q or R can be NULL.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_exp_mod()

int mbedtls_mpi_exp_mod ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi E,
const mbedtls_mpi N,
mbedtls_mpi _RR 
)

Sliding-window exponentiation: X = A^E mod N.

Parameters
XDestination MPI
ALeft-hand MPI
EExponent MPI
NModular MPI
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_fill_random()

int mbedtls_mpi_fill_random ( mbedtls_mpi X,
size_t  size,
int(*)(void *, unsigned char *, size_t)  f_rng,
void p_rng 
)

Fill an MPI X with size bytes of random.

Parameters
XDestination MPI
sizeSize in bytes
f_rngRNG function
p_rngRNG parameter
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_free()

void mbedtls_mpi_free ( mbedtls_mpi X)

Unallocate one MPI.

Parameters
XOne MPI to unallocate.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_gcd()

int mbedtls_mpi_gcd ( mbedtls_mpi G,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Greatest common divisor: G = gcd(A, B)

Parameters
GDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_gen_prime()

int mbedtls_mpi_gen_prime ( mbedtls_mpi X,
size_t  nbits,
int  dh_flag,
int(*)(void *, unsigned char *, size_t)  f_rng,
void p_rng 
)

Prime number generation.

Parameters
XDestination MPI
nbitsRequired size of X in bits ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
dh_flagIf 1, then (X-1)/2 will be prime too
f_rngRNG function
p_rngRNG parameter
Returns
0 if successful (probably prime), MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_get_bit()

int mbedtls_mpi_get_bit ( const mbedtls_mpi X,
size_t  pos 
)

Get a specific bit from X.

Parameters
XMPI to use
posZero-based index of the bit in X
Returns
Either a 0 or a 1
Here is the caller graph for this function:

◆ mbedtls_mpi_grow()

int mbedtls_mpi_grow ( mbedtls_mpi X,
size_t  nblimbs 
)

Enlarge to the specified number of limbs.

Parameters
XMPI to grow
nblimbsThe target number of limbs
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_init()

void mbedtls_mpi_init ( mbedtls_mpi X)

Initialize one MPI (make internal references valid) This just makes it ready to be set or freed, but does not define a value for the MPI.

Parameters
XOne MPI to initialize.
Here is the caller graph for this function:

◆ mbedtls_mpi_inv_mod()

int mbedtls_mpi_inv_mod ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi N 
)

Modular inverse: X = A^-1 mod N.

Parameters
XDestination MPI
ALeft-hand MPI
NRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1, MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_is_prime()

int mbedtls_mpi_is_prime ( const mbedtls_mpi X,
int(*)(void *, unsigned char *, size_t)  f_rng,
void p_rng 
)

Miller-Rabin primality test.

Parameters
XMPI to check
f_rngRNG function
p_rngRNG parameter
Returns
0 if successful (probably prime), MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_lsb()

size_t mbedtls_mpi_lsb ( const mbedtls_mpi X)

Return the number of zero-bits before the least significant '1' bit.

Note: Thus also the zero-based index of the least significant '1' bit

Parameters
XMPI to use
Here is the caller graph for this function:

◆ mbedtls_mpi_lset()

int mbedtls_mpi_lset ( mbedtls_mpi X,
mbedtls_mpi_sint  z 
)

Set value from integer.

Parameters
XMPI to set
zValue to use
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_mod_int()

int mbedtls_mpi_mod_int ( mbedtls_mpi_uint r,
const mbedtls_mpi A,
mbedtls_mpi_sint  b 
)

Modulo: r = A mod b.

Parameters
rDestination mbedtls_mpi_uint
ALeft-hand MPI
bInteger to divide by
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0, MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Here is the caller graph for this function:

◆ mbedtls_mpi_mod_mpi()

int mbedtls_mpi_mod_mpi ( mbedtls_mpi R,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Modulo: R = A mod B.

Parameters
RDestination MPI for the rest value
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0, MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_mul_int()

int mbedtls_mpi_mul_int ( mbedtls_mpi X,
const mbedtls_mpi A,
mbedtls_mpi_uint  b 
)

Baseline multiplication: X = A * b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe unsigned integer value to multiply with
Note
b is unsigned
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_mul_mpi()

int mbedtls_mpi_mul_mpi ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Baseline multiplication: X = A * B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_read_binary()

int mbedtls_mpi_read_binary ( mbedtls_mpi X,
const unsigned char *  buf,
size_t  buflen 
)

Import X from unsigned binary data, big endian.

Parameters
XDestination MPI
bufInput buffer
buflenInput buffer size
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_read_file()

int mbedtls_mpi_read_file ( mbedtls_mpi X,
int  radix,
FILE fin 
)

Read MPI from a line in an opened file.

Parameters
XDestination MPI
radixInput numeric base
finInput file handle
Returns
0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer is too small or a MBEDTLS_ERR_MPI_XXX error code
Note
On success, this function advances the file stream to the end of the current line or to EOF.

The function returns 0 on an empty line.

Leading whitespaces are ignored, as is a '0x' prefix for radix 16.

Here is the call graph for this function:

◆ mbedtls_mpi_read_string()

int mbedtls_mpi_read_string ( mbedtls_mpi X,
int  radix,
const char *  s 
)

Import from an ASCII string.

Parameters
XDestination MPI
radixInput numeric base
sNull-terminated string buffer
Returns
0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_safe_cond_assign()

int mbedtls_mpi_safe_cond_assign ( mbedtls_mpi X,
const mbedtls_mpi Y,
unsigned char  assign 
)

Safe conditional assignement X = Y if assign is 1.

Parameters
XMPI to conditionally assign to
YValue to be assigned
assign1: perform the assignment, 0: keep X's original value
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Note
This function is equivalent to if( assign ) mbedtls_mpi_copy( X, Y ); except that it avoids leaking any information about whether the assignment was done or not (the above code may leak information through branch prediction and/or memory access patterns analysis).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_safe_cond_swap()

int mbedtls_mpi_safe_cond_swap ( mbedtls_mpi X,
mbedtls_mpi Y,
unsigned char  assign 
)

Safe conditional swap X <-> Y if swap is 1.

Parameters
XFirst mbedtls_mpi value
YSecond mbedtls_mpi value
assign1: perform the swap, 0: keep X and Y's original values
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Note
This function is equivalent to if( assign ) mbedtls_mpi_swap( X, Y ); except that it avoids leaking any information about whether the assignment was done or not (the above code may leak information through branch prediction and/or memory access patterns analysis).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_self_test()

int mbedtls_mpi_self_test ( int  verbose)

Checkup routine.

Returns
0 if successful, or 1 if the test failed
Here is the call graph for this function:

◆ mbedtls_mpi_set_bit()

int mbedtls_mpi_set_bit ( mbedtls_mpi X,
size_t  pos,
unsigned char  val 
)

Set a bit of X to a specific value of 0 or 1.

Note
Will grow X if necessary to set a bit to 1 in a not yet existing limb. Will not grow if bit should be set to 0
Parameters
XMPI to use
posZero-based index of the bit in X
valThe value to set the bit to (0 or 1)
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_shift_l()

int mbedtls_mpi_shift_l ( mbedtls_mpi X,
size_t  count 
)

Left-shift: X <<= count.

Parameters
XMPI to shift
countAmount to shift
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_shift_r()

int mbedtls_mpi_shift_r ( mbedtls_mpi X,
size_t  count 
)

Right-shift: X >>= count.

Parameters
XMPI to shift
countAmount to shift
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_shrink()

int mbedtls_mpi_shrink ( mbedtls_mpi X,
size_t  nblimbs 
)

Resize down, keeping at least the specified number of limbs.

Parameters
XMPI to shrink
nblimbsThe minimum number of limbs to keep
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_size()

size_t mbedtls_mpi_size ( const mbedtls_mpi X)

Return the total size in bytes.

Parameters
XMPI to use
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_sub_abs()

int mbedtls_mpi_sub_abs ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Unsigned subtraction: X = |A| - |B|.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_sub_int()

int mbedtls_mpi_sub_int ( mbedtls_mpi X,
const mbedtls_mpi A,
mbedtls_mpi_sint  b 
)

Signed subtraction: X = A - b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to subtract
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_sub_mpi()

int mbedtls_mpi_sub_mpi ( mbedtls_mpi X,
const mbedtls_mpi A,
const mbedtls_mpi B 
)

Signed subtraction: X = A - B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_swap()

void mbedtls_mpi_swap ( mbedtls_mpi X,
mbedtls_mpi Y 
)

Swap the contents of X and Y.

Parameters
XFirst MPI value
YSecond MPI value
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_write_binary()

int mbedtls_mpi_write_binary ( const mbedtls_mpi X,
unsigned char *  buf,
size_t  buflen 
)

Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with zeros if the number is smaller.

Parameters
XSource MPI
bufOutput buffer
buflenOutput buffer size
Returns
0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_write_file()

int mbedtls_mpi_write_file ( const char *  p,
const mbedtls_mpi X,
int  radix,
FILE fout 
)

Write X into an opened file, or stdout if fout is NULL.

Parameters
pPrefix, can be NULL
XSource MPI
radixOutput numeric base
foutOutput file handle (can be NULL)
Returns
0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Note
Set fout == NULL to print X on the console.
Here is the call graph for this function:

◆ mbedtls_mpi_write_string()

int mbedtls_mpi_write_string ( const mbedtls_mpi X,
int  radix,
char *  buf,
size_t  buflen,
size_t *  olen 
)

Export into an ASCII string.

Parameters
XSource MPI
radixOutput numeric base
bufBuffer to write the string to
buflenLength of buf
olenLength of the string written, including final NUL byte
Returns
0 if successful, or a MBEDTLS_ERR_MPI_XXX error code. *olen is always updated to reflect the amount of data that has (or would have) been written.
Note
Call this function with buflen = 0 to obtain the minimum required buffer size in *olen.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_mpi_zeroize()

static void mbedtls_mpi_zeroize ( mbedtls_mpi_uint v,
size_t  n 
)
static
Here is the caller graph for this function:

◆ mpi_check_small_factors()

static int mpi_check_small_factors ( const mbedtls_mpi X)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mpi_get_digit()

static int mpi_get_digit ( mbedtls_mpi_uint d,
int  radix,
char  c 
)
static
Here is the caller graph for this function:

◆ mpi_miller_rabin()

static int mpi_miller_rabin ( const mbedtls_mpi X,
int(*)(void *, unsigned char *, size_t)  f_rng,
void p_rng 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mpi_montg_init()

static void mpi_montg_init ( mbedtls_mpi_uint mm,
const mbedtls_mpi N 
)
static
Here is the caller graph for this function:

◆ mpi_montmul()

static int mpi_montmul ( mbedtls_mpi A,
const mbedtls_mpi B,
const mbedtls_mpi N,
mbedtls_mpi_uint  mm,
const mbedtls_mpi T 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mpi_montred()

static int mpi_montred ( mbedtls_mpi A,
const mbedtls_mpi N,
mbedtls_mpi_uint  mm,
const mbedtls_mpi T 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mpi_mul_hlp()

static void mpi_mul_hlp ( size_t  i,
mbedtls_mpi_uint s,
mbedtls_mpi_uint d,
mbedtls_mpi_uint  b 
)
static
Here is the caller graph for this function:

◆ mpi_sub_hlp()

static void mpi_sub_hlp ( size_t  n,
mbedtls_mpi_uint s,
mbedtls_mpi_uint d 
)
static
Here is the caller graph for this function:

◆ mpi_write_hlp()

static int mpi_write_hlp ( mbedtls_mpi X,
int  radix,
char **  p 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ gcd_pairs

const int gcd_pairs[GCD_PAIR_COUNT][3]
static
Initial value:
=
{
{ 693, 609, 21 },
{ 1764, 868, 28 },
{ 768454923, 542167814, 1 }
}

◆ small_prime

const int small_prime[]
static
Initial value:
=
{
3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227,
229, 233, 239, 241, 251, 257, 263, 269,
271, 277, 281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353, 359, 367,
373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461,
463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571,
577, 587, 593, 599, 601, 607, 613, 617,
619, 631, 641, 643, 647, 653, 659, 661,
673, 677, 683, 691, 701, 709, 719, 727,
733, 739, 743, 751, 757, 761, 769, 773,
787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883,
887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997, -103
}