Merge branch 'ccbrown/master' into develop

This commit is contained in:
Steffen Jaeckel 2013-11-24 15:20:36 +01:00
commit bb622822da
5 changed files with 579 additions and 728 deletions

87
bn_mp_export.c Normal file
View File

@ -0,0 +1,87 @@
#include <tommath.h>
#ifdef BN_MP_EXPORT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* based on gmp's mpz_export.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
int mp_export(void* rop, size_t* countp, int order, size_t size,
int endian, size_t nails, mp_int* op) {
int result;
size_t odd_nails, nail_bytes, i, j, bits, count;
unsigned char odd_nail_mask;
mp_int t;
if ((result = mp_init_copy(&t, op)) != MP_OKAY) {
return result;
}
if (endian == 0) {
union {
unsigned int i;
char c[4];
} lint = {0x01020304};
endian = (lint.c[0] == 4 ? -1 : 1);
}
odd_nails = (nails % 8);
odd_nail_mask = 0xff;
for (i = 0; i < odd_nails; ++i) {
odd_nail_mask ^= (1 << (7 - i));
}
nail_bytes = nails / 8;
bits = mp_count_bits(&t);
count = bits / (size * 8 - nails) + (bits % (size * 8 - nails) ? 1 : 0);
for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) {
unsigned char* byte = (
(unsigned char*)rop +
(order == -1 ? i : count - 1 - i) * size +
(endian == -1 ? j : size - 1 - j)
);
if (j >= (size - nail_bytes)) {
*byte = 0;
continue;
}
*byte = (unsigned char)(j == size - nail_bytes - 1 ? (t.dp[0] & odd_nail_mask) : t.dp[0] & 0xFF);
if ((result = mp_div_2d(&t, (j == size - nail_bytes - 1 ? 8 - odd_nails : 8), &t, NULL)) != MP_OKAY) {
mp_clear(&t);
return result;
}
}
}
mp_clear(&t);
if (countp) {
*countp = count;
}
return MP_OKAY;
}
#endif
/* $Source$ */
/* $Revision$ */
/* $Date$ */

72
bn_mp_import.c Normal file
View File

@ -0,0 +1,72 @@
#include <tommath.h>
#ifdef BN_MP_IMPORT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* based on gmp's mpz_import.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
int mp_import(mp_int* rop, size_t count, int order, size_t size,
int endian, size_t nails, const void* op) {
int result;
size_t odd_nails, nail_bytes, i, j;
unsigned char odd_nail_mask;
mp_zero(rop);
if (endian == 0) {
union {
unsigned int i;
char c[4];
} lint = {0x01020304};
endian = (lint.c[0] == 4 ? -1 : 1);
}
odd_nails = (nails % 8);
odd_nail_mask = 0xff;
for (i = 0; i < odd_nails; ++i) {
odd_nail_mask ^= (1 << (7 - i));
}
nail_bytes = nails / 8;
for (i = 0; i < count; ++i) {
for (j = 0; j < size - nail_bytes; ++j) {
unsigned char byte = *(
(unsigned char*)op +
(order == 1 ? i : count - 1 - i) * size +
(endian == 1 ? j + nail_bytes : size - 1 - j - nail_bytes)
);
if (
(result = mp_mul_2d(rop, (j == 0 ? 8 - odd_nails : 8), rop)) != MP_OKAY) {
return result;
}
rop->dp[0] |= (j == 0 ? (byte & odd_nail_mask) : byte);
rop->used += 1;
}
}
mp_clamp(rop);
return MP_OKAY;
}
#endif
/* $Source$ */
/* $Revision$ */
/* $Date$ */

File diff suppressed because it is too large Load Diff

View File

@ -249,6 +249,12 @@ int mp_init_copy(mp_int *a, mp_int *b);
/* trim unused digits */ /* trim unused digits */
void mp_clamp(mp_int *a); void mp_clamp(mp_int *a);
/* import binary data */
int mp_import(mp_int* rop, size_t count, int order, size_t size, int endian, size_t nails, const void* op);
/* export binary data */
int mp_export(void* rop, size_t* countp, int order, size_t size, int endian, size_t nails, mp_int* op);
/* ---> digit manipulation <--- */ /* ---> digit manipulation <--- */
/* right shift by "b" digits */ /* right shift by "b" digits */

View File

@ -38,6 +38,7 @@
#define BN_MP_DR_REDUCE_C #define BN_MP_DR_REDUCE_C
#define BN_MP_DR_SETUP_C #define BN_MP_DR_SETUP_C
#define BN_MP_EXCH_C #define BN_MP_EXCH_C
#define BN_MP_EXPORT_C
#define BN_MP_EXPT_D_C #define BN_MP_EXPT_D_C
#define BN_MP_EXPTMOD_C #define BN_MP_EXPTMOD_C
#define BN_MP_EXPTMOD_FAST_C #define BN_MP_EXPTMOD_FAST_C
@ -47,6 +48,7 @@
#define BN_MP_GCD_C #define BN_MP_GCD_C
#define BN_MP_GET_INT_C #define BN_MP_GET_INT_C
#define BN_MP_GROW_C #define BN_MP_GROW_C
#define BN_MP_IMPORT_C
#define BN_MP_INIT_C #define BN_MP_INIT_C
#define BN_MP_INIT_COPY_C #define BN_MP_INIT_COPY_C
#define BN_MP_INIT_MULTI_C #define BN_MP_INIT_MULTI_C
@ -315,12 +317,19 @@
#if defined(BN_MP_EXCH_C) #if defined(BN_MP_EXCH_C)
#endif #endif
#if defined(BN_MP_EXPORT_C)
#define BN_MP_INIT_COPY_C
#define BN_MP_COUNT_BITS_C
#define BN_MP_DIV_2D_C
#define BN_MP_CLEAR_C
#endif
#if defined(BN_MP_EXPT_D_C) #if defined(BN_MP_EXPT_D_C)
#define BN_MP_INIT_COPY_C #define BN_MP_INIT_COPY_C
#define BN_MP_SET_C #define BN_MP_SET_C
#define BN_MP_SQR_C
#define BN_MP_CLEAR_C
#define BN_MP_MUL_C #define BN_MP_MUL_C
#define BN_MP_CLEAR_C
#define BN_MP_SQR_C
#endif #endif
#if defined(BN_MP_EXPTMOD_C) #if defined(BN_MP_EXPTMOD_C)
@ -387,7 +396,6 @@
#if defined(BN_MP_GCD_C) #if defined(BN_MP_GCD_C)
#define BN_MP_ISZERO_C #define BN_MP_ISZERO_C
#define BN_MP_ABS_C #define BN_MP_ABS_C
#define BN_MP_ZERO_C
#define BN_MP_INIT_COPY_C #define BN_MP_INIT_COPY_C
#define BN_MP_CNT_LSB_C #define BN_MP_CNT_LSB_C
#define BN_MP_DIV_2D_C #define BN_MP_DIV_2D_C
@ -404,6 +412,12 @@
#if defined(BN_MP_GROW_C) #if defined(BN_MP_GROW_C)
#endif #endif
#if defined(BN_MP_IMPORT_C)
#define BN_MP_ZERO_C
#define BN_MP_MUL_2D_C
#define BN_MP_CLAMP_C
#endif
#if defined(BN_MP_INIT_C) #if defined(BN_MP_INIT_C)
#endif #endif
@ -481,8 +495,9 @@
#define BN_MP_MUL_C #define BN_MP_MUL_C
#define BN_MP_INIT_SIZE_C #define BN_MP_INIT_SIZE_C
#define BN_MP_CLAMP_C #define BN_MP_CLAMP_C
#define BN_MP_SUB_C #define BN_S_MP_ADD_C
#define BN_MP_ADD_C #define BN_MP_ADD_C
#define BN_S_MP_SUB_C
#define BN_MP_LSHD_C #define BN_MP_LSHD_C
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#endif #endif
@ -491,8 +506,8 @@
#define BN_MP_INIT_SIZE_C #define BN_MP_INIT_SIZE_C
#define BN_MP_CLAMP_C #define BN_MP_CLAMP_C
#define BN_MP_SQR_C #define BN_MP_SQR_C
#define BN_MP_SUB_C
#define BN_S_MP_ADD_C #define BN_S_MP_ADD_C
#define BN_S_MP_SUB_C
#define BN_MP_LSHD_C #define BN_MP_LSHD_C
#define BN_MP_ADD_C #define BN_MP_ADD_C
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
@ -516,8 +531,9 @@
#define BN_MP_INIT_C #define BN_MP_INIT_C
#define BN_MP_DIV_C #define BN_MP_DIV_C
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#define BN_MP_ADD_C #define BN_MP_ISZERO_C
#define BN_MP_EXCH_C #define BN_MP_EXCH_C
#define BN_MP_ADD_C
#endif #endif
#if defined(BN_MP_MOD_2D_C) #if defined(BN_MP_MOD_2D_C)
@ -667,9 +683,9 @@
#endif #endif
#if defined(BN_MP_RADIX_SIZE_C) #if defined(BN_MP_RADIX_SIZE_C)
#define BN_MP_ISZERO_C
#define BN_MP_COUNT_BITS_C #define BN_MP_COUNT_BITS_C
#define BN_MP_INIT_COPY_C #define BN_MP_INIT_COPY_C
#define BN_MP_ISZERO_C
#define BN_MP_DIV_D_C #define BN_MP_DIV_D_C
#define BN_MP_CLEAR_C #define BN_MP_CLEAR_C
#endif #endif
@ -687,7 +703,6 @@
#if defined(BN_MP_READ_RADIX_C) #if defined(BN_MP_READ_RADIX_C)
#define BN_MP_ZERO_C #define BN_MP_ZERO_C
#define BN_MP_S_RMAP_C #define BN_MP_S_RMAP_C
#define BN_MP_RADIX_SMAP_C
#define BN_MP_MUL_D_C #define BN_MP_MUL_D_C
#define BN_MP_ADD_D_C #define BN_MP_ADD_D_C
#define BN_MP_ISZERO_C #define BN_MP_ISZERO_C