API Documentation (Doxygen generated)

These pages are generated with doxygen directly from the source code!

bignum.h File Reference

Multi-precision integer library. More...

#include <stdio.h>
#include <string.h>
#include "config.h"
#include <inttypes.h>
Include dependency graph for bignum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mpi
 MPI structure. More...

Macros

#define POLARSSL_ERR_MPI_FILE_IO_ERROR   -0x0002
 An error occurred while reading from or writing to a file.
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA   -0x0004
 Bad input parameters to function.
#define POLARSSL_ERR_MPI_INVALID_CHARACTER   -0x0006
 There is an invalid character in the digit string.
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL   -0x0008
 The buffer is too small to write to.
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE   -0x000A
 The input arguments are negative or result in illegal output.
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO   -0x000C
 The input argument for division is zero, which is not allowed.
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE   -0x000E
 The input arguments are not acceptable.
#define POLARSSL_ERR_MPI_MALLOC_FAILED   -0x0010
 Memory allocation failed.
#define MPI_CHK(f)   do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
#define POLARSSL_MPI_MAX_LIMBS   10000
#define POLARSSL_MPI_WINDOW_SIZE   6
 Maximum windows size used.
#define POLARSSL_MPI_MAX_SIZE   512
 Maximum number of bytes for usable MPIs.
#define POLARSSL_MPI_MAX_BITS   ( 8 * POLARSSL_MPI_MAX_SIZE )
 Maximum number of bits for usable MPIs.
#define POLARSSL_MPI_MAX_BITS_SCALE100   ( 100 * POLARSSL_MPI_MAX_BITS )
#define LN_2_DIV_LN_10_SCALE100   332
#define POLARSSL_MPI_RW_BUFFER_SIZE   ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
#define POLARSSL_HAVE_INT32
#define POLARSSL_HAVE_UDBL

Typedefs

typedef int32_t t_sint
typedef uint32_t t_uint
typedef unsigned long long t_udbl

Functions

void mpi_init (mpi *X)
 Initialize one MPI.
void mpi_free (mpi *X)
 Unallocate one MPI.
int mpi_grow (mpi *X, size_t nblimbs)
 Enlarge to the specified number of limbs.
int mpi_shrink (mpi *X, size_t nblimbs)
 Resize down, keeping at least the specified number of limbs.
int mpi_copy (mpi *X, const mpi *Y)
 Copy the contents of Y into X.
void mpi_swap (mpi *X, mpi *Y)
 Swap the contents of X and Y.
int mpi_safe_cond_assign (mpi *X, const mpi *Y, unsigned char assign)
 Safe conditional assignement X = Y if assign is 1.
int mpi_safe_cond_swap (mpi *X, mpi *Y, unsigned char assign)
 Safe conditional swap X <-> Y if swap is 1.
int mpi_lset (mpi *X, t_sint z)
 Set value from integer.
int mpi_get_bit (const mpi *X, size_t pos)
 Get a specific bit from X.
int mpi_set_bit (mpi *X, size_t pos, unsigned char val)
 Set a bit of X to a specific value of 0 or 1.
size_t mpi_lsb (const mpi *X)
 Return the number of zero-bits before the least significant '1' bit.
size_t mpi_msb (const mpi *X)
 Return the number of bits up to and including the most significant '1' bit'.
size_t mpi_size (const mpi *X)
 Return the total size in bytes.
int mpi_read_string (mpi *X, int radix, const char *s)
 Import from an ASCII string.
int mpi_write_string (const mpi *X, int radix, char *s, size_t *slen)
 Export into an ASCII string.
int mpi_read_file (mpi *X, int radix, FILE *fin)
 Read X from an opened file.
int mpi_write_file (const char *p, const mpi *X, int radix, FILE *fout)
 Write X into an opened file, or stdout if fout is NULL.
int mpi_read_binary (mpi *X, const unsigned char *buf, size_t buflen)
 Import X from unsigned binary data, big endian.
int mpi_write_binary (const mpi *X, unsigned char *buf, size_t buflen)
 Export X into unsigned binary data, big endian.
int mpi_shift_l (mpi *X, size_t count)
 Left-shift: X <<= count.
int mpi_shift_r (mpi *X, size_t count)
 Right-shift: X >>= count.
int mpi_cmp_abs (const mpi *X, const mpi *Y)
 Compare unsigned values.
int mpi_cmp_mpi (const mpi *X, const mpi *Y)
 Compare signed values.
int mpi_cmp_int (const mpi *X, t_sint z)
 Compare signed values.
int mpi_add_abs (mpi *X, const mpi *A, const mpi *B)
 Unsigned addition: X = |A| + |B|.
int mpi_sub_abs (mpi *X, const mpi *A, const mpi *B)
 Unsigned subtraction: X = |A| - |B|.
int mpi_add_mpi (mpi *X, const mpi *A, const mpi *B)
 Signed addition: X = A + B.
int mpi_sub_mpi (mpi *X, const mpi *A, const mpi *B)
 Signed subtraction: X = A - B.
int mpi_add_int (mpi *X, const mpi *A, t_sint b)
 Signed addition: X = A + b.
int mpi_sub_int (mpi *X, const mpi *A, t_sint b)
 Signed subtraction: X = A - b.
int mpi_mul_mpi (mpi *X, const mpi *A, const mpi *B)
 Baseline multiplication: X = A * B.
int mpi_mul_int (mpi *X, const mpi *A, t_sint b)
 Baseline multiplication: X = A * b Note: despite the functon signature, b is treated as a t_uint.
int mpi_div_mpi (mpi *Q, mpi *R, const mpi *A, const mpi *B)
 Division by mpi: A = Q * B + R.
int mpi_div_int (mpi *Q, mpi *R, const mpi *A, t_sint b)
 Division by int: A = Q * b + R.
int mpi_mod_mpi (mpi *R, const mpi *A, const mpi *B)
 Modulo: R = A mod B.
int mpi_mod_int (t_uint *r, const mpi *A, t_sint b)
 Modulo: r = A mod b.
int mpi_exp_mod (mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
 Sliding-window exponentiation: X = A^E mod N.
int mpi_fill_random (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.
int mpi_gcd (mpi *G, const mpi *A, const mpi *B)
 Greatest common divisor: G = gcd(A, B)
int mpi_inv_mod (mpi *X, const mpi *A, const mpi *N)
 Modular inverse: X = A^-1 mod N.
int mpi_is_prime (mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Miller-Rabin primality test.
int mpi_gen_prime (mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Prime number generation.
int mpi_self_test (int verbose)
 Checkup routine.

Detailed Description

Multi-precision integer library.

Copyright (C) 2006-2013, Brainspark B.V.

This file is part of PolarSSL (http://www.polarssl.org) Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Definition in file bignum.h.

Macro Definition Documentation

#define LN_2_DIV_LN_10_SCALE100   332

Definition at line 112 of file bignum.h.

#define MPI_CHK (   f)    do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )

Definition at line 61 of file bignum.h.

#define POLARSSL_ERR_MPI_BAD_INPUT_DATA   -0x0004

Bad input parameters to function.

Definition at line 53 of file bignum.h.

#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL   -0x0008

The buffer is too small to write to.

Definition at line 55 of file bignum.h.

#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO   -0x000C

The input argument for division is zero, which is not allowed.

Definition at line 57 of file bignum.h.

#define POLARSSL_ERR_MPI_FILE_IO_ERROR   -0x0002

An error occurred while reading from or writing to a file.

Definition at line 52 of file bignum.h.

#define POLARSSL_ERR_MPI_INVALID_CHARACTER   -0x0006

There is an invalid character in the digit string.

Definition at line 54 of file bignum.h.

#define POLARSSL_ERR_MPI_MALLOC_FAILED   -0x0010

Memory allocation failed.

Definition at line 59 of file bignum.h.

#define POLARSSL_ERR_MPI_NEGATIVE_VALUE   -0x000A

The input arguments are negative or result in illegal output.

Definition at line 56 of file bignum.h.

#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE   -0x000E

The input arguments are not acceptable.

Definition at line 58 of file bignum.h.

#define POLARSSL_HAVE_INT32

Definition at line 153 of file bignum.h.

#define POLARSSL_HAVE_UDBL

Definition at line 162 of file bignum.h.

#define POLARSSL_MPI_MAX_BITS   ( 8 * POLARSSL_MPI_MAX_SIZE )

Maximum number of bits for usable MPIs.

Definition at line 91 of file bignum.h.

#define POLARSSL_MPI_MAX_BITS_SCALE100   ( 100 * POLARSSL_MPI_MAX_BITS )

Definition at line 111 of file bignum.h.

#define POLARSSL_MPI_MAX_LIMBS   10000

Definition at line 66 of file bignum.h.

#define POLARSSL_MPI_MAX_SIZE   512

Maximum number of bytes for usable MPIs.

Definition at line 87 of file bignum.h.

#define POLARSSL_MPI_RW_BUFFER_SIZE   ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )

Definition at line 113 of file bignum.h.

#define POLARSSL_MPI_WINDOW_SIZE   6

Maximum windows size used.

Definition at line 78 of file bignum.h.

Typedef Documentation

typedef int32_t t_sint

Definition at line 154 of file bignum.h.

typedef unsigned long long t_udbl

Definition at line 161 of file bignum.h.

typedef uint32_t t_uint

Definition at line 155 of file bignum.h.

Function Documentation

int mpi_add_abs ( mpi X,
const mpi A,
const mpi B 
)

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

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_add_int ( mpi X,
const mpi A,
t_sint  b 
)

Signed addition: X = A + b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to add
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_add_mpi ( mpi X,
const mpi A,
const mpi B 
)

Signed addition: X = A + B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_cmp_abs ( const mpi X,
const 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|
int mpi_cmp_int ( const mpi X,
t_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
int mpi_cmp_mpi ( const mpi X,
const 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
int mpi_copy ( mpi X,
const mpi Y 
)

Copy the contents of Y into X.

Parameters
XDestination MPI
YSource MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_div_int ( mpi Q,
mpi R,
const mpi A,
t_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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Note
Either Q or R can be NULL.
int mpi_div_mpi ( mpi Q,
mpi R,
const mpi A,
const mpi B 
)

Division by 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Note
Either Q or R can be NULL.
int mpi_exp_mod ( mpi X,
const mpi A,
const mpi E,
const mpi N,
mpi _RR 
)

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

Parameters
XDestination MPI
ALeft-hand MPI
EExponent MPI
NModular MPI
_RRSpeed-up MPI used for recalculations
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if E is negative
Note
_RR is used to avoid re-computing R*R mod N across multiple calls, which speeds up things a bit. It can be set to NULL if the extra performance is unneeded.
int mpi_fill_random ( 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
void mpi_free ( mpi X)

Unallocate one MPI.

Parameters
XOne MPI to unallocate.
int mpi_gcd ( mpi G,
const mpi A,
const mpi B 
)

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

Parameters
GDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_gen_prime ( 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 <= POLARSSL_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), POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
int mpi_get_bit ( const 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
int mpi_grow ( 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
void mpi_init ( mpi X)

Initialize one MPI.

Parameters
XOne MPI to initialize.
int mpi_inv_mod ( mpi X,
const mpi A,
const mpi N 
)

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

Parameters
XDestination MPI
ALeft-hand MPI
NRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
int mpi_is_prime ( 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), POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
size_t mpi_lsb ( const 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
int mpi_lset ( mpi X,
t_sint  z 
)

Set value from integer.

Parameters
XMPI to set
zValue to use
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_mod_int ( t_uint r,
const mpi A,
t_sint  b 
)

Modulo: r = A mod b.

Parameters
rDestination t_uint
ALeft-hand MPI
bInteger to divide by
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
int mpi_mod_mpi ( mpi R,
const mpi A,
const mpi B 
)

Modulo: R = A mod B.

Parameters
RDestination MPI for the rest value
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
size_t mpi_msb ( const 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

Referenced by d2i_RSA_PUBKEY().

int mpi_mul_int ( mpi X,
const mpi A,
t_sint  b 
)

Baseline multiplication: X = A * b Note: despite the functon signature, b is treated as a t_uint.

Negative values of b are treated as large positive values.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to multiply with
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_mul_mpi ( mpi X,
const mpi A,
const mpi B 
)

Baseline multiplication: X = A * B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_read_binary ( 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Referenced by d2i_RSA_PUBKEY().

int mpi_read_file ( mpi X,
int  radix,
FILE *  fin 
)

Read X from an opened file.

Parameters
XDestination MPI
radixInput numeric base
finInput file handle
Returns
0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer is too small or a POLARSSL_ERR_MPI_XXX error code
int mpi_read_string ( 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 POLARSSL_ERR_MPI_XXX error code
int mpi_safe_cond_assign ( mpi X,
const 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Note
This function is equivalent to if( assign ) 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).
int mpi_safe_cond_swap ( mpi X,
mpi Y,
unsigned char  assign 
)

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

Parameters
XFirst mpi value
YSecond mpi value
assign1: perform the swap, 0: keep X and Y's original values
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Note
This function is equivalent to if( assign ) 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).
int mpi_self_test ( int  verbose)

Checkup routine.

Returns
0 if successful, or 1 if the test failed

Referenced by main().

int mpi_set_bit ( 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
int mpi_shift_l ( mpi X,
size_t  count 
)

Left-shift: X <<= count.

Parameters
XMPI to shift
countAmount to shift
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_shift_r ( mpi X,
size_t  count 
)

Right-shift: X >>= count.

Parameters
XMPI to shift
countAmount to shift
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_shrink ( 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, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
size_t mpi_size ( const mpi X)

Return the total size in bytes.

Parameters
XMPI to use
int mpi_sub_abs ( mpi X,
const mpi A,
const mpi B 
)

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

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
int mpi_sub_int ( mpi X,
const mpi A,
t_sint  b 
)

Signed subtraction: X = A - b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to subtract
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
int mpi_sub_mpi ( mpi X,
const mpi A,
const mpi B 
)

Signed subtraction: X = A - B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
void mpi_swap ( mpi X,
mpi Y 
)

Swap the contents of X and Y.

Parameters
XFirst MPI value
YSecond MPI value
int mpi_write_binary ( const mpi X,
unsigned char *  buf,
size_t  buflen 
)

Export X into unsigned binary data, big endian.

Parameters
XSource MPI
bufOutput buffer
buflenOutput buffer size
Returns
0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
int mpi_write_file ( const char *  p,
const 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 POLARSSL_ERR_MPI_XXX error code
Note
Set fout == NULL to print X on the console.
int mpi_write_string ( const mpi X,
int  radix,
char *  s,
size_t *  slen 
)

Export into an ASCII string.

Parameters
XSource MPI
radixOutput numeric base
sString buffer
slenString buffer size
Returns
0 if successful, or a POLARSSL_ERR_MPI_XXX error code. *slen is always updated to reflect the amount of data that has (or would have) been written.
Note
Call this function with *slen = 0 to obtain the minimum required buffer size in *slen.