annotate libtommath/bn_mp_export.c @ 1535:b918ad1c5b25

Merge branch 'master' of git://github.com/stellarpower/dropbear into stellarpower-master
author Matt Johnston <matt@ucc.asn.au>
date Thu, 22 Feb 2018 23:06:45 +0800
parents 8bba51a55704
children f52919ffd3b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #include <tommath_private.h>
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #ifdef BN_MP_EXPORT_C
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * LibTomMath is a library that provides multiple-precision
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * integer arithmetic as well as number theoretic functionality.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * The library was designed directly after the MPI library by
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Michael Fromberger but has been written from scratch with
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * additional optimizations in place.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * The library is free for all purposes without any express
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * guarantee it works.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * Tom St Denis, [email protected], http://libtom.org
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 /* based on gmp's mpz_export.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * see http://gmplib.org/manual/Integer-Import-and-Export.html
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 int mp_export(void* rop, size_t* countp, int order, size_t size,
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 int endian, size_t nails, mp_int* op) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 int result;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 size_t odd_nails, nail_bytes, i, j, bits, count;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 unsigned char odd_nail_mask;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 mp_int t;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 if ((result = mp_init_copy(&t, op)) != MP_OKAY) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 return result;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 if (endian == 0) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 union {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 unsigned int i;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 char c[4];
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 } lint;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 lint.i = 0x01020304;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 endian = (lint.c[0] == 4) ? -1 : 1;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 odd_nails = (nails % 8);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 odd_nail_mask = 0xff;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 for (i = 0; i < odd_nails; ++i) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 odd_nail_mask ^= (1 << (7 - i));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 nail_bytes = nails / 8;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 bits = mp_count_bits(&t);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 for (i = 0; i < count; ++i) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 for (j = 0; j < size; ++j) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 unsigned char* byte = (
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 (unsigned char*)rop +
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 (((order == -1) ? i : ((count - 1) - i)) * size) +
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 ((endian == -1) ? j : ((size - 1) - j))
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 );
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if (j >= (size - nail_bytes)) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 *byte = 0;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 continue;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 *byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 mp_clear(&t);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 return result;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 mp_clear(&t);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 if (countp != NULL) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 *countp = count;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 return MP_OKAY;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
86 /* ref: $Format:%D$ */
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
87 /* git commit: $Format:%H$ */
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
88 /* commit time: $Format:%ai$ */