annotate libtommath/tommath_private.h @ 1460:58a74cb829b8

Pointer parameter could be declared as pointing to const (callback)
author Francois Perrad <francois.perrad@gadz.org>
date Sat, 19 Aug 2017 22:39:53 +0200
parents 60fc6476e044
children 8bba51a55704
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 /* LibTomMath, multiple-precision integer library -- Tom St Denis
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomMath is a library that provides multiple-precision
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * integer arithmetic as well as number theoretic functionality.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * 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
7 * Michael Fromberger but has been written from scratch with
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * additional optimizations in place.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * 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
11 * guarantee it works.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 *
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * Tom St Denis, [email protected], http://math.libtomcrypt.com
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 #ifndef TOMMATH_PRIV_H_
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #define TOMMATH_PRIV_H_
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 #include <tommath.h>
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 #include <ctype.h>
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 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #define MAX(x,y) (((x) > (y)) ? (x) : (y))
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #ifdef __cplusplus
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 extern "C" {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 /* C++ compilers don't like assigning void * to mp_digit * */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #define OPT_CAST(x) (x *)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #else
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 /* C on the other hand doesn't care */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #define OPT_CAST(x)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 /* define heap macros */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #ifndef XMALLOC
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 /* default to libc stuff */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define XMALLOC malloc
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #define XFREE free
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #define XREALLOC realloc
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #define XCALLOC calloc
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #else
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /* prototypes for our heap functions */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 extern void *XMALLOC(size_t n);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 extern void *XREALLOC(void *p, size_t n);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 extern void *XCALLOC(size_t n, size_t s);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 extern void XFREE(void *p);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 #endif
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 /* lowlevel functions, do not call! */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 int fast_s_mp_sqr(mp_int *a, mp_int *b);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 int s_mp_sqr(mp_int *a, mp_int *b);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 int mp_karatsuba_sqr(mp_int *a, mp_int *b);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 int mp_toom_sqr(mp_int *a, mp_int *b);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 int fast_mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 void bn_reverse(unsigned char *s, int len);
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 extern const char *mp_s_rmap;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 /* Fancy macro to set an MPI from another type.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 * There are several things assumed:
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 * x is the counter and unsigned
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 * a is the pointer to the MPI
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 * b is the original value that should be set in the MPI.
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 #define MP_SET_XLONG(func_name, type) \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 int func_name (mp_int * a, type b) \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 { \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 unsigned int x; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 int res; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 mp_zero (a); \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 /* set four bits at a time */ \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 for (x = 0; x < (sizeof(type) * 2u); x++) { \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 /* shift the number up four bits */ \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 return res; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 } \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 /* OR in the top four bits of the source */ \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 /* shift the source up to the next four bits */ \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 b <<= 4; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 /* ensure that digits are not clamped off */ \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 a->used += 1; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 } \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 mp_clamp (a); \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 return MP_OKAY; \
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 #ifdef __cplusplus
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 /* $Source$ */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 /* $Revision$ */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 /* $Date$ */