1436
|
1 #include <tommath_private.h> |
|
2 #ifdef BN_MP_IMPORT_C |
|
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4 * |
|
5 * LibTomMath is a library that provides multiple-precision |
|
6 * integer arithmetic as well as number theoretic functionality. |
|
7 * |
|
8 * The library was designed directly after the MPI library by |
|
9 * Michael Fromberger but has been written from scratch with |
|
10 * additional optimizations in place. |
|
11 * |
|
12 * The library is free for all purposes without any express |
|
13 * guarantee it works. |
|
14 * |
|
15 * Tom St Denis, [email protected], http://libtom.org |
|
16 */ |
|
17 |
|
18 /* based on gmp's mpz_import. |
|
19 * see http://gmplib.org/manual/Integer-Import-and-Export.html |
|
20 */ |
|
21 int mp_import(mp_int* rop, size_t count, int order, size_t size, |
|
22 int endian, size_t nails, const void* op) { |
|
23 int result; |
|
24 size_t odd_nails, nail_bytes, i, j; |
|
25 unsigned char odd_nail_mask; |
|
26 |
|
27 mp_zero(rop); |
|
28 |
|
29 if (endian == 0) { |
|
30 union { |
|
31 unsigned int i; |
|
32 char c[4]; |
|
33 } lint; |
|
34 lint.i = 0x01020304; |
|
35 |
|
36 endian = (lint.c[0] == 4) ? -1 : 1; |
|
37 } |
|
38 |
|
39 odd_nails = (nails % 8); |
|
40 odd_nail_mask = 0xff; |
|
41 for (i = 0; i < odd_nails; ++i) { |
|
42 odd_nail_mask ^= (1 << (7 - i)); |
|
43 } |
|
44 nail_bytes = nails / 8; |
|
45 |
|
46 for (i = 0; i < count; ++i) { |
|
47 for (j = 0; j < (size - nail_bytes); ++j) { |
|
48 unsigned char byte = *( |
|
49 (unsigned char*)op + |
|
50 (((order == 1) ? i : ((count - 1) - i)) * size) + |
|
51 ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)) |
|
52 ); |
|
53 |
|
54 if ( |
|
55 (result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) { |
|
56 return result; |
|
57 } |
|
58 |
|
59 rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte; |
|
60 rop->used += 1; |
|
61 } |
|
62 } |
|
63 |
|
64 mp_clamp(rop); |
|
65 |
|
66 return MP_OKAY; |
|
67 } |
|
68 |
|
69 #endif |
|
70 |
|
71 /* $Source$ */ |
|
72 /* $Revision$ */ |
|
73 /* $Date$ */ |