comparison libtommath/bn_mp_unpack.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h"
2 #ifdef BN_MP_UNPACK_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5
6 /* based on gmp's mpz_import.
7 * see http://gmplib.org/manual/Integer-Import-and-Export.html
8 */
9 mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
10 mp_endian endian, size_t nails, const void *op)
11 {
12 mp_err err;
13 size_t odd_nails, nail_bytes, i, j;
14 unsigned char odd_nail_mask;
15
16 mp_zero(rop);
17
18 if (endian == MP_NATIVE_ENDIAN) {
19 MP_GET_ENDIANNESS(endian);
20 }
21
22 odd_nails = (nails % 8u);
23 odd_nail_mask = 0xff;
24 for (i = 0; i < odd_nails; ++i) {
25 odd_nail_mask ^= (unsigned char)(1u << (7u - i));
26 }
27 nail_bytes = nails / 8u;
28
29 for (i = 0; i < count; ++i) {
30 for (j = 0; j < (size - nail_bytes); ++j) {
31 unsigned char byte = *((const unsigned char *)op +
32 (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
33 ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
34
35 if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
36 return err;
37 }
38
39 rop->dp[0] |= (j == 0u) ? (mp_digit)(byte & odd_nail_mask) : (mp_digit)byte;
40 rop->used += 1;
41 }
42 }
43
44 mp_clamp(rop);
45
46 return MP_OKAY;
47 }
48
49 #endif