49#ifndef MBEDTLS_BIGNUM_H
50#define MBEDTLS_BIGNUM_H
52#if !defined(MBEDTLS_CONFIG_FILE)
55#include MBEDTLS_CONFIG_FILE
61#if defined(MBEDTLS_FS_IO)
65#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
66#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
67#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
68#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
69#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
70#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
71#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
72#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
74#define MBEDTLS_MPI_CHK(f) \
77 if( ( ret = (f) ) != 0 ) \
84#define MBEDTLS_MPI_MAX_LIMBS 10000
86#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
96#define MBEDTLS_MPI_WINDOW_SIZE 6
99#if !defined(MBEDTLS_MPI_MAX_SIZE)
107#define MBEDTLS_MPI_MAX_SIZE 1024
110#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
130#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
131#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
132#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
144#if !defined(MBEDTLS_HAVE_INT32)
145 #if defined(_MSC_VER) && defined(_M_AMD64)
147 #if !defined(MBEDTLS_HAVE_INT64)
148 #define MBEDTLS_HAVE_INT64
152 #elif defined(__GNUC__) && ( \
153 defined(__amd64__) || defined(__x86_64__) || \
154 defined(__ppc64__) || defined(__powerpc64__) || \
155 defined(__ia64__) || defined(__alpha__) || \
156 ( defined(__sparc__) && defined(__arch64__) ) || \
157 defined(__s390x__) || defined(__mips64) )
158 #if !defined(MBEDTLS_HAVE_INT64)
159 #define MBEDTLS_HAVE_INT64
163 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
166 #define MBEDTLS_HAVE_UDBL
168 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
173 #if !defined(MBEDTLS_HAVE_INT64)
174 #define MBEDTLS_HAVE_INT64
178 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
181 #define MBEDTLS_HAVE_UDBL
183 #elif defined(MBEDTLS_HAVE_INT64)
190#if !defined(MBEDTLS_HAVE_INT64)
192 #if !defined(MBEDTLS_HAVE_INT32)
193 #define MBEDTLS_HAVE_INT32
197 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
199 #define MBEDTLS_HAVE_UDBL
462 char *
buf,
size_t buflen,
size_t *olen );
464#if defined(MBEDTLS_FS_IO)
503int mbedtls_mpi_write_file(
const char *
p,
const mbedtls_mpi *
X,
504 int radix,
FILE *fout );
863 int (*f_rng)(
void *,
unsigned char *,
size_t),
899#if !defined(MBEDTLS_DEPRECATED_REMOVED)
900#if defined(MBEDTLS_DEPRECATED_WARNING)
901#define MBEDTLS_DEPRECATED __attribute__((deprecated))
903#define MBEDTLS_DEPRECATED
925 int (*f_rng)(
void *,
unsigned char *,
size_t),
927#undef MBEDTLS_DEPRECATED
958 int (*f_rng)(
void *,
unsigned char *,
size_t),
991 int (*f_rng)(
void *,
unsigned char *,
size_t),
994#if defined(MBEDTLS_SELF_TEST)
1001int mbedtls_mpi_self_test(
int verbose );
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import an MPI from an ASCII string.
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a signed subtraction of MPIs: X = A - B.
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed subtraction of an MPI and an integer: X = A - b.
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed addition of an MPI and an integer: X = A + b.
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge an MPI to the specified number of limbs.
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Make a copy of an MPI.
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Modify a specific bit in an MPI.
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a modular reduction with respect to an integer. r = A mod b.
mbedtls_mpi_gen_prime_flag_t
Flags for mbedtls_mpi_gen_prime()
@ MBEDTLS_MPI_GEN_PRIME_FLAG_DH
@ MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Perform a sliding-window exponentiation: X = A^E mod N.
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform an unsigned addition of MPIs: X = |A| + |B|.
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a division with remainder of two MPIs: A = Q * B + R.
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a signed addition of MPIs: X = A + B.
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of two MPIs.
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
Perform a safe conditional copy of MPI which doesn't reveal whether the condition was true or not.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Store integer value in MPI.
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant bit of value 1.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import an MPI from unsigned big endian binary data.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare two MPIs.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a modular reduction. R = A mod B.
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a division with remainder of an MPI by an integer: A = Q * b + R.
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 with a number of random bytes.
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare the absolute values of two MPIs.
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a prime number.
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Perform a left-shift on an MPI: X <<= count.
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
Perform a safe conditional swap which doesn't reveal whether the condition was true or not.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Return the number of bits of value 0 before the least significant bit of value 1.
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a multiplication of two MPIs: X = A * B.
#define MBEDTLS_DEPRECATED
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
Export an MPI to an ASCII string.
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
This function resizes an MPI downwards, keeping at least the specified number of limbs.
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Compute the modular inverse: X = A^-1 mod N.
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from an MPI.
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime(const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Perform a Miller-Rabin primality test with error probability of 2-80.
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Perform a multiplication of an MPI with an unsigned integer: X = A * b.
int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned *ret)
Check if an MPI is less than the other in constant time.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare an MPI with an integer.
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform an unsigned subtraction of MPIs: X = |A| - |B|.
uint32_t mbedtls_mpi_uint
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Perform a right-shift on an MPI: X >>= count.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Compute the greatest common divisor: G = gcd(A, B)
#define G(r, i, a, b, c, d)
GLuint GLuint GLsizei count
GLdouble GLdouble GLdouble r
GLboolean GLboolean GLboolean b
GLenum GLuint GLenum GLsizei const GLchar * buf