- Main Page
- Related Pages
- Data Structures
- Files

# bignum.h

Go to the documentation of this file.

    00001
    00023 #ifndef POLARSSL_BIGNUM_H
    00024 #define POLARSSL_BIGNUM_H
    00025
    00026 #include <stdio.h>
    00027
    00028 #define POLARSSL_ERR_MPI_FILE_IO_ERROR                     0x0002
    00029 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA                    0x0004
    00030 #define POLARSSL_ERR_MPI_INVALID_CHARACTER                 0x0006
    00031 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL                  0x0008
    00032 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE                    0x000A
    00033 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO                  0x000C
    00034 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE                    0x000E
    00035
    00036 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
    00037
    00038 /*
    00039  * Define the base integer type, architecture-wise
    00040  */
    00041 #if defined(POLARSSL_HAVE_INT8)
    00042 typedef unsigned char  t_int;
    00043 typedef unsigned short t_dbl;
    00044 #else
    00045 #if defined(POLARSSL_HAVE_INT16)
    00046 typedef unsigned short t_int;
    00047 typedef unsigned long  t_dbl;
    00048 #else
    00049   typedef unsigned long t_int;
    00050   #if defined(_MSC_VER) && defined(_M_IX86)
    00051   typedef unsigned __int64 t_dbl;
    00052   #else
    00053     #if defined(__amd64__) || defined(__x86_64__)    || \
    00054         defined(__ppc64__) || defined(__powerpc64__) || \
    00055         defined(__ia64__)  || defined(__alpha__)
    00056     typedef unsigned int t_dbl __attribute__((mode(TI)));
    00057     #else
    00058       #if defined(POLARSSL_HAVE_LONGLONG)
    00059       typedef unsigned long long t_dbl;
    00060       #endif
    00061     #endif
    00062   #endif
    00063 #endif
    00064 #endif
    00065
    00069 typedef struct
    00070 {
    00071     int s;
    00072     int n;
    00073     t_int *p;
    00074 }
    00075 mpi;
    00076
    00077 #ifdef __cplusplus
    00078 extern "C" {
    00079 #endif
    00080
    00084 void mpi_init( mpi *X, ... );
    00085
    00089 void mpi_free( mpi *X, ... );
    00090
    00100 int mpi_grow( mpi *X, int nblimbs );
    00101
    00111 int mpi_copy( mpi *X, mpi *Y );
    00112
    00119 void mpi_swap( mpi *X, mpi *Y );
    00120
    00130 int mpi_lset( mpi *X, int z );
    00131
    00137 int mpi_lsb( mpi *X );
    00138
    00144 int mpi_msb( mpi *X );
    00145
    00151 int mpi_size( mpi *X );
    00152
    00162 int mpi_read_string( mpi *X, int radix, char *s );
    00163
    00177 int mpi_write_string( mpi *X, int radix, char *s, int *slen );
    00178
    00188 int mpi_read_file( mpi *X, int radix, FILE *fin );
    00189
    00202 int mpi_write_file( char *p, mpi *X, int radix, FILE *fout );
    00203
    00214 int mpi_read_binary( mpi *X, unsigned char *buf, int buflen );
    00215
    00226 int mpi_write_binary( mpi *X, unsigned char *buf, int buflen );
    00227
    00237 int mpi_shift_l( mpi *X, int count );
    00238
    00248 int mpi_shift_r( mpi *X, int count );
    00249
    00260 int mpi_cmp_abs( mpi *X, mpi *Y );
    00261
    00272 int mpi_cmp_mpi( mpi *X, mpi *Y );
    00273
    00284 int mpi_cmp_int( mpi *X, int z );
    00285
    00296 int mpi_add_abs( mpi *X, mpi *A, mpi *B );
    00297
    00308 int mpi_sub_abs( mpi *X, mpi *A, mpi *B );
    00309
    00320 int mpi_add_mpi( mpi *X, mpi *A, mpi *B );
    00321
    00332 int mpi_sub_mpi( mpi *X, mpi *A, mpi *B );
    00333
    00344 int mpi_add_int( mpi *X, mpi *A, int b );
    00345
    00356 int mpi_sub_int( mpi *X, mpi *A, int b );
    00357
    00368 int mpi_mul_mpi( mpi *X, mpi *A, mpi *B );
    00369
    00382 int mpi_mul_int( mpi *X, mpi *A, t_int b );
    00383
    00398 int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
    00399
    00414 int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
    00415
    00428 int mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
    00429
    00442 int mpi_mod_int( t_int *r, mpi *A, int b );
    00443
    00461 int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
    00462
    00473 int mpi_gcd( mpi *G, mpi *A, mpi *B );
    00474
    00487 int mpi_inv_mod( mpi *X, mpi *A, mpi *N );
    00488
    00500 int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
    00501
    00515 int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
    00516                    int (*f_rng)(void *), void *p_rng );
    00517
    00523 int mpi_self_test( int verbose );
    00524
    00525 #ifdef __cplusplus
    00526 }
    00527 #endif
    00528
    00529 #endif /* bignum.h */