2 * Copyright (C) 2017 Denys Vlasenko
4 * Licensed under GPLv2, see file LICENSE in this source tree.
6 /* The file is taken almost verbatim from matrixssl-3-7-2b-open/crypto/math/.
7 * Changes are flagged with //bbox
12 * @version 33ef80f (HEAD, tag: MATRIXSSL-3-7-2-OPEN, tag: MATRIXSSL-3-7-2-COMM, origin/master, origin/HEAD, master)
14 * multiple-precision integer library.
17 * Copyright (c) 2013-2015 INSIDE Secure Corporation
18 * Copyright (c) PeerSec Networks, 2002-2011
21 * The latest version of this code is available at http://www.matrixssl.org
23 * This software is open source; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This General Public License does NOT permit incorporating this software
29 * into proprietary programs. If you are unable to comply with the GPL, a
30 * commercial license for this software may be purchased from INSIDE at
31 * http://www.insidesecure.com/eng/Company/Locations
33 * This program is distributed in WITHOUT ANY WARRANTY; without even the
34 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 * See the GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 * http://www.gnu.org/copyleft/gpl.html
42 /******************************************************************************/
48 /* Define this here to avoid including circular limits.h on some platforms */
53 /******************************************************************************/
55 If native 64 bit integers are not supported, we do not support 32x32->64
56 in hardware, so we must set the 16 bit flag to produce 16x16->32 products.
58 #ifndef HAVE_NATIVE_INT64
60 #endif /* ! HAVE_NATIVE_INT64 */
62 /******************************************************************************/
64 Some default configurations.
66 pstm_word should be the largest value the processor can hold as the product
67 of a multiplication. Most platforms support a 32x32->64 MAC instruction,
68 so 64bits is the default pstm_word size.
69 pstm_digit should be half the size of pstm_word
72 /* 8-bit digits, 16-bit word products */
73 typedef unsigned char pstm_digit;
74 typedef unsigned short pstm_word;
77 #elif defined(PSTM_16BIT)
78 /* 16-bit digits, 32-bit word products */
79 typedef unsigned short pstm_digit;
80 typedef unsigned long pstm_word;
83 #elif defined(PSTM_64BIT)
84 /* 64-bit digits, 128-bit word products */
86 #error "64bit digits requires GCC"
88 typedef unsigned long pstm_digit;
89 typedef unsigned long pstm_word __attribute__ ((mode(TI)));
93 /* This is the default case, 32-bit digits, 64-bit word products */
94 typedef uint32 pstm_digit;
95 typedef uint64 pstm_word;
98 #endif /* digit and word size */
100 #define PSTM_MASK (pstm_digit)(-1)
101 #define PSTM_DIGIT_MAX PSTM_MASK
103 /******************************************************************************/
107 #define PSTM_LT -1 /* less than */
108 #define PSTM_EQ 0 /* equal to */
109 #define PSTM_GT 1 /* greater than */
111 #define PSTM_ZPOS 0 /* positive integer */
112 #define PSTM_NEG 1 /* negative */
114 #define PSTM_OKAY PS_SUCCESS
115 #define PSTM_MEM PS_MEM_FAIL
117 /******************************************************************************/
119 Various build options
121 #define PSTM_DEFAULT_INIT 64 /* default (64) digits of allocation */
122 #define PSTM_MAX_SIZE 4096
125 int16 used, alloc, sign;
127 //bbox psPool_t *pool;
130 /******************************************************************************/
132 Operations on large integers
134 #define pstm_iszero(a) (((a)->used == 0) ? PS_TRUE : PS_FALSE)
135 #define pstm_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? PS_TRUE : PS_FALSE)
136 #define pstm_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? PS_TRUE : PS_FALSE)
137 #define pstm_abs(a, b) { pstm_copy(a, b); (b)->sign = 0; }
139 extern void pstm_set(pstm_int *a, pstm_digit b);
141 extern void pstm_zero(pstm_int * a);
144 #define pstm_init(pool, a) \
146 extern int32 pstm_init(psPool_t *pool, pstm_int * a);
149 #define pstm_init_size(pool, a, size) \
150 pstm_init_size( a, size)
151 extern int32 pstm_init_size(psPool_t *pool, pstm_int * a, uint32 size);
154 #define pstm_init_copy(pool, a, b, toSqr) \
155 pstm_init_copy( a, b, toSqr)
156 extern int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b,
159 extern int16 pstm_count_bits (pstm_int * a);
162 #define pstm_init_for_read_unsigned_bin(pool, a, len) \
163 pstm_init_for_read_unsigned_bin( a, len)
164 extern int32 pstm_init_for_read_unsigned_bin(psPool_t *pool, pstm_int *a,
167 extern int32 pstm_read_unsigned_bin(pstm_int *a, unsigned char *b, int32 c);
169 extern int32 pstm_unsigned_bin_size(pstm_int *a);
171 extern int32 pstm_copy(pstm_int * a, pstm_int * b);
173 extern void pstm_exch(pstm_int * a, pstm_int * b);
175 extern void pstm_clear(pstm_int * a);
177 extern void pstm_clear_multi(pstm_int *mp0, pstm_int *mp1, pstm_int *mp2,
178 pstm_int *mp3, pstm_int *mp4, pstm_int *mp5, pstm_int *mp6,
181 extern int32 pstm_grow(pstm_int * a, int16 size);
183 extern void pstm_clamp(pstm_int * a);
185 extern int32 pstm_cmp(pstm_int * a, pstm_int * b);
187 extern int32 pstm_cmp_mag(pstm_int * a, pstm_int * b);
189 extern void pstm_rshd(pstm_int *a, int16 x);
191 extern int32 pstm_lshd(pstm_int * a, int16 b);
194 #define pstm_div(pool, a, b, c, d) \
195 pstm_div( a, b, c, d)
196 extern int32 pstm_div(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
200 #define pstm_div_2d(pool, a, b, c, d) \
201 pstm_div_2d( a, b, c, d)
202 extern int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int16 b, pstm_int *c,
205 extern int32 pstm_div_2(pstm_int * a, pstm_int * b);
207 extern int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c);
209 extern int32 pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c);
212 #define pstm_sub_d(pool, a, b, c) \
214 extern int32 pstm_sub_d(psPool_t *pool, pstm_int *a, pstm_digit b, pstm_int *c);
216 extern int32 pstm_mul_2(pstm_int * a, pstm_int * b);
219 #define pstm_mod(pool, a, b, c) \
221 extern int32 pstm_mod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c);
224 #define pstm_mulmod(pool, a, b, c, d) \
225 pstm_mulmod( a, b, c, d)
226 extern int32 pstm_mulmod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
230 #define pstm_exptmod(pool, G, X, P, Y) \
231 pstm_exptmod( G, X, P, Y)
232 extern int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P,
235 extern int32 pstm_2expt(pstm_int *a, int16 b);
237 extern int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c);
240 #define pstm_to_unsigned_bin(pool, a, b) \
241 pstm_to_unsigned_bin( a, b)
242 extern int32 pstm_to_unsigned_bin(psPool_t *pool, pstm_int *a,
246 #define pstm_to_unsigned_bin_nr(pool, a, b) \
247 pstm_to_unsigned_bin_nr( a, b)
248 extern int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a,
251 extern int32 pstm_montgomery_setup(pstm_int *a, pstm_digit *rho);
254 #define pstm_montgomery_reduce(pool, a, m, mp, paD, paDlen) \
255 pstm_montgomery_reduce( a, m, mp, paD, paDlen)
256 extern int32 pstm_montgomery_reduce(psPool_t *pool, pstm_int *a, pstm_int *m,
257 pstm_digit mp, pstm_digit *paD, uint32 paDlen);
259 #define pstm_mul_comba(pool, A, B, C, paD, paDlen) \
260 pstm_mul_comba( A, B, C, paD, paDlen)
261 extern int32 pstm_mul_comba(psPool_t *pool, pstm_int *A, pstm_int *B,
262 pstm_int *C, pstm_digit *paD, uint32 paDlen);
265 #define pstm_sqr_comba(pool, A, B, paD, paDlen) \
266 pstm_sqr_comba( A, B, paD, paDlen)
267 extern int32 pstm_sqr_comba(psPool_t *pool, pstm_int *A, pstm_int *B,
268 pstm_digit *paD, uint32 paDlen);
270 extern int32 pstm_cmp_d(pstm_int *a, pstm_digit b);
272 extern int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b);
274 extern int32 pstm_mul_d(pstm_int *a, pstm_digit b, pstm_int *c);
277 #define pstm_invmod(pool, a, b, c) \
278 pstm_invmod( a, b, c)
279 extern int32 pstm_invmod(psPool_t *pool, pstm_int * a, pstm_int * b,
282 #else /* DISABLE_PSTM */
283 typedef int32 pstm_int;
284 #endif /* !DISABLE_PSTM */
285 #endif /* _h_PSTMATH */