1 /*
2  * Copyright (c) 2002, 2003 Bob Deblier
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19 
20 /*!\file mp.h
21  * \brief Multi-precision integer routines.
22  *
23  * The routines declared here are all low-level operations, most of them
24  * suitable to be implemented in assembler. Prime candidates are in order
25  * of importance (according to gprof):
26  * <ul>
27  *  <li>mpaddmul
28  *  <li>mpsetmul
29  *  <li>mpaddsqrtrc
30  *  <li>mpsub
31  *  <li>mpadd
32  * </ul>
33  *
34  * With some smart use of available assembler instructions, it's possible
35  * to speed these routines up by a factor of 2 to 4.
36  *
37  * \author Bob Deblier <bob.deblier@telenet.be>
38  * \ingroup MP_m
39  */
40 
41 #ifndef _MP_H
42 #define _MP_H
43 
44 #include "beecrypt/api.h"
45 #include "beecrypt/mpopt.h"
46 
47 #define MP_HWBITS	(MP_WBITS >> 1)
48 #define MP_WBYTES	(MP_WBITS >> 3)
49 #define MP_WNIBBLES	(MP_WBITS >> 2)
50 
51 #if (MP_WBITS == 64)
52 # define MP_WORDS_TO_BITS(x)	((x) << 6)
53 # define MP_WORDS_TO_NIBBLES(x)	((x) << 4)
54 # define MP_WORDS_TO_BYTES(x)	((x) << 3)
55 # define MP_BITS_TO_WORDS(x)	((x) >> 6)
56 # define MP_NIBBLES_TO_WORDS(x)	((x) >> 4)
57 # define MP_BYTES_TO_WORDS(x)	((x) >> 3)
58 #elif (MP_WBITS == 32)
59 # define MP_WORDS_TO_BITS(x)	((x) << 5)
60 # define MP_WORDS_TO_NIBBLES(x)	((x) << 3)
61 # define MP_WORDS_TO_BYTES(x)	((x) << 2)
62 # define MP_BITS_TO_WORDS(x)	((x) >> 5)
63 # define MP_NIBBLES_TO_WORDS(x)	((x) >> 3)
64 # define MP_BYTES_TO_WORDS(x)	((x) >> 2)
65 #else
66 # error
67 #endif
68 
69 #define MP_MSBMASK	(((mpw) 0x1) << (MP_WBITS-1))
70 #define MP_LSBMASK	 ((mpw) 0x1)
71 #define MP_ALLMASK	~((mpw) 0x0)
72 
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 
77 #ifndef ASM_MPCOPY
78 # define mpcopy(size, dst, src) memcpy(dst, src, MP_WORDS_TO_BYTES(size))
79 #else
80 BEECRYPTAPI
81 void mpcopy(size_t size, mpw* dest, const mpw* src);
82 #endif
83 
84 #ifndef ASM_MPMOVE
85 # define mpmove(size, dst, src) memmove(dst, src, MP_WORDS_TO_BYTES(size))
86 #else
87 BEECRYPTAPI
88 void mpmove(size_t size, mpw* dest, const mpw* src);
89 #endif
90 
91 /*!\fn void mpzero(size_t size, mpw* data)
92  * \brief This function zeroes a multi-precision integer of a given size.
93  * \param size The size of the multi-precision integer.
94  * \param data The multi-precision integer data.
95  */
96 BEECRYPTAPI
97 void mpzero(size_t size, mpw* data);
98 
99 /*!\fn void mpfill(size_t size, mpw* data, mpw fill)
100  * \brief This function fills each word of a multi-precision integer with a
101  *  given value.
102  * \param size The size of the multi-precision integer.
103  * \param data The multi-precision integer data.
104  * \param fill The value fill the data with.
105  */
106 BEECRYPTAPI
107 void mpfill(size_t size, mpw* data, mpw fill);
108 
109 /*!\fn int mpodd(size_t size, const mpw* data)
110  * \brief This functions tests if a multi-precision integer is odd.
111  * \param size The size of the multi-precision integer.
112  * \param data The multi-precision integer data.
113  * \retval 1 if odd
114  * \retval 0 if even
115  */
116 BEECRYPTAPI
117 int mpodd (size_t size, const mpw* data);
118 
119 /*!\fn int mpeven(size_t size, const mpw* data)
120  * \brief This function tests if a multi-precision integer is even.
121  * \param size The size of the multi-precision integer.
122  * \param data The multi-precision integer data.
123  * \retval 1 if even
124  * \retval 0 if odd
125  */
126 BEECRYPTAPI
127 int mpeven(size_t size, const mpw* data);
128 
129 /*!\fn int mpz(size_t size, const mpw* data)
130  * \brief This function tests if a multi-precision integer is zero.
131  * \param size The size of the multi-precision integer.
132  * \param data The multi-precision integer data.
133  * \retval 1 if zero
134  * \retval 0 if not zero
135  */
136 BEECRYPTAPI
137 int mpz  (size_t size, const mpw* data);
138 
139 /*!\fn int mpnz(size_t size, const mpw* data)
140  * \brief This function tests if a multi-precision integer is not zero.
141  * \param size The size of the multi-precision integer.
142  * \param data The multi-precision integer data.
143  * \retval 1 if not zero
144  * \retval 0 if zero
145  */
146 BEECRYPTAPI
147 int mpnz (size_t size, const mpw* data);
148 
149 /*!\fn int mpeq(size_t size, const mpw* xdata, const mpw* ydata)
150  * \brief This function tests if two multi-precision integers of the same size
151  *  are equal.
152  * \param size The size of the multi-precision integers.
153  * \param xdata The first multi-precision integer.
154  * \param ydata The second multi-precision integer.
155  * \retval 1 if equal
156  * \retval 0 if not equal
157  */
158 BEECRYPTAPI
159 int mpeq (size_t size, const mpw* xdata, const mpw* ydata);
160 
161 /*!\fn int mpne(size_t size, const mpw* xdata, const mpw* ydata)
162  * \brief This function tests if two multi-precision integers of the same size
163  *  differ.
164  * \param size The size of the multi-precision integers.
165  * \param xdata The first multi-precision integer.
166  * \param ydata The second multi-precision integer.
167  * \retval 1 if not equal
168  * \retval 0 if equal
169  */
170 BEECRYPTAPI
171 int mpne (size_t size, const mpw* xdata, const mpw* ydata);
172 
173 /*!\fn int mpgt(size_t size, const mpw* xdata, const mpw* ydata)
174  * \brief This function tests if the first of two multi-precision integers
175  *  of the same size is greater than the second.
176  * \note The comparison treats the arguments as unsigned.
177  * \param size The size of the multi-precision integers.
178  * \param xdata The first multi-precision integer.
179  * \param ydata The second multi-precision integer.
180  * \retval 1 if greater
181  * \retval 0 if less or equal
182  */
183 BEECRYPTAPI
184 int mpgt (size_t size, const mpw* xdata, const mpw* ydata);
185 
186 /*!\fn int mplt(size_t size, const mpw* xdata, const mpw* ydata)
187  * \brief This function tests if the first of two multi-precision integers
188  *  of the same size is less than the second.
189  * \note The comparison treats the arguments as unsigned.
190  * \param size The size of the multi-precision integers.
191  * \param xdata The first multi-precision integer.
192  * \param ydata The second multi-precision integer.
193  * \retval 1 if less
194  * \retval 0 if greater or equal
195  */
196 BEECRYPTAPI
197 int mplt (size_t size, const mpw* xdata, const mpw* ydata);
198 
199 /*!\fn int mpge(size_t size, const mpw* xdata, const mpw* ydata)
200  * \brief This function tests if the first of two multi-precision integers
201  *  of the same size is greater than or equal to the second.
202  * \note The comparison treats the arguments as unsigned.
203  * \param size The size of the multi-precision integers.
204  * \param xdata The first multi-precision integer.
205  * \param ydata The second multi-precision integer.
206  * \retval 1 if greater or equal
207  * \retval 0 if less
208  */
209 BEECRYPTAPI
210 int mpge (size_t size, const mpw* xdata, const mpw* ydata);
211 
212 /*!\fn int mple(size_t size, const mpw* xdata, const mpw* ydata)
213  * \brief This function tests if the first of two multi-precision integers
214  *  of the same size is less than or equal to the second.
215  * \note The comparison treats the arguments as unsigned.
216  * \param size The size of the multi-precision integers.
217  * \param xdata The first multi-precision integer.
218  * \param ydata The second multi-precision integer.
219  * \retval 1 if less or equal
220  * \retval 0 if greater
221  */
222 BEECRYPTAPI
223 int mple (size_t size, const mpw* xdata, const mpw* ydata);
224 
225 /*!\fn int mpcmp(size_t size, const mpw* xdata, const mpw* ydata)
226  * \brief This function performs a comparison of two multi-precision
227  *  integers of the same size.
228  * \note The comparison treats the arguments as unsigned.
229  * \retval -1 if x < y
230  * \retval 0 if x == y
231  * \retval 1 if x > y
232  */
233 BEECRYPTAPI
234 int mpcmp(size_t size, const mpw* xdata, const mpw* ydata);
235 
236 /*!\fn int mpeqx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
237  * \brief This function tests if two multi-precision integers of different
238  *  size are equal.
239  * \param xsize The size of the first multi-precision integer.
240  * \param xdata The first multi-precision integer.
241  * \param ysize The size of the first multi-precision integer.
242  * \param ydata The second multi-precision integer.
243  * \retval 1 if equal
244  * \retval 0 if not equal
245  */
246 BEECRYPTAPI
247 int mpeqx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
248 
249 /*!\fn int mpnex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
250  * \brief This function tests if two multi-precision integers of different
251  *  size are equal.
252  * \param xsize The size of the first multi-precision integer.
253  * \param xdata The first multi-precision integer.
254  * \param ysize The size of the first multi-precision integer.
255  * \param ydata The second multi-precision integer.
256  * \retval 1 if equal
257  * \retval 0 if not equal
258 */
259 BEECRYPTAPI
260 int mpnex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
261 
262 /*!\fn int mpgtx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
263  * \brief This function tests if the first of two multi-precision integers
264  *  of different size is greater than the second.
265  * \note The comparison treats the arguments as unsigned.
266  * \param xsize The size of the first multi-precision integer.
267  * \param xdata The first multi-precision integer.
268  * \param ysize The size of the second multi-precision integer.
269  * \param ydata The second multi-precision integer.
270  * \retval 1 if greater
271  * \retval 0 if less or equal
272  */
273 BEECRYPTAPI
274 int mpgtx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
275 
276 /*!\fn int mpltx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
277  * \brief This function tests if the first of two multi-precision integers
278  *  of different size is less than the second.
279  * \note The comparison treats the arguments as unsigned.
280  * \param xsize The size of the first multi-precision integer.
281  * \param xdata The first multi-precision integer.
282  * \param ysize The size of the second multi-precision integer.
283  * \param ydata The second multi-precision integer.
284  * \retval 1 if less
285  * \retval 0 if greater or equal
286  */
287 BEECRYPTAPI
288 int mpltx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
289 
290 /*!\fn int mpgex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
291  * \brief This function tests if the first of two multi-precision integers
292  *  of different size is greater than or equal to the second.
293  * \note The comparison treats the arguments as unsigned.
294  * \param xsize The size of the first multi-precision integer.
295  * \param xdata The first multi-precision integer.
296  * \param ysize The size of the second multi-precision integer.
297  * \param ydata The second multi-precision integer.
298  * \retval 1 if greater or equal
299  * \retval 0 if less
300  */
301 BEECRYPTAPI
302 int mpgex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
303 
304 /*!\fn int mplex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
305  * \brief This function tests if the first of two multi-precision integers
306  *  of different size is less than or equal to the second.
307  * \note The comparison treats the arguments as unsigned.
308  * \param xsize The size of the first multi-precision integer.
309  * \param xdata The first multi-precision integer.
310  * \param ysize The size of the second multi-precision integer.
311  * \param ydata The second multi-precision integer.
312  * \retval 1 if less or equal
313  * \retval 0 if greater
314  */
315 BEECRYPTAPI
316 int mplex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
317 
318 /*!\fn int mpcmpx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
319  * \brief This function performs a comparison of two multi-precision
320  *  integers of the different size.
321  * \note The comparison treats the arguments as unsigned.
322  * \retval -1 if x < y
323  * \retval 0 if x == y
324  * \retval 1 if x > y
325  */
326 BEECRYPTAPI
327 int mpcmpx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
328 
329 /*!\fn int mpisone(size_t size, const mpw* data)
330  * \brief This functions tests if the value of a multi-precision integer is
331  *  equal to one.
332  * \param size The size of the multi-precision integer.
333  * \param data The multi-precision integer data.
334  * \retval 1 if one
335  * \retval 0 if not one
336  */
337 BEECRYPTAPI
338 int mpisone(size_t size, const mpw* data);
339 
340 /*!\fn int mpistwo(size_t size, const mpw* data)
341  * \brief This function tests if the value of a multi-precision integer is
342  *  equal to two.
343  * \param size The size of the multi-precision integer.
344  * \param data The multi-precision integer data.
345  * \retval 1 if two
346  * \retval 0 if not two
347  */
348 BEECRYPTAPI
349 int mpistwo(size_t size, const mpw* data);
350 
351 /*!\fn int mpleone(size_t size, const mpw* data);
352  * \brief This function tests if the value of a multi-precision integer is
353  *  less than or equal to one.
354  * \param size The size of the multi-precision integer.
355  * \param data The multi-precision integer data.
356  * \retval 1 if less than or equal to one.
357  * \retval 0 if greater than one.
358  */
359 BEECRYPTAPI
360 int mpleone(size_t size, const mpw* data);
361 
362 /*!\fn int mpeqmone(size_t size, const mpw* xdata, const mpw* ydata);
363  * \brief This function tests if multi-precision integer x is equal to y
364  *  minus one.
365  * \param size The size of the multi-precision integers.
366  * \param xdata The first multi-precision integer.
367  * \param ydata The second multi-precision integer.
368  * \retval 1 if less than or equal to one.
369  * \retval 0 if greater than one.
370  */
371 BEECRYPTAPI
372 int mpeqmone(size_t size, const mpw* xdata, const mpw* ydata);
373 
374 /*!\fn int mpmsbset(size_t size, const mpw* data)
375  * \brief This function tests if the most significant bit of a multi-precision
376  *  integer is set.
377  * \param size The size of the multi-precision integer.
378  * \param data The multi-precision integer data.
379  * \retval 1 if set
380  * \retval 0 if not set
381  */
382 BEECRYPTAPI
383 int mpmsbset(size_t size, const mpw* data);
384 
385 /*!\fn int mplsbset(size_t size, const mpw* data)
386  * \brief This function tests if the leiast significant bit of a multi-precision
387  *  integer is set.
388  * \param size The size of the multi-precision integer.
389  * \param data The multi-precision integer data.
390  * \retval 1 if set
391  * \retval 0 if not set
392  */
393 BEECRYPTAPI
394 int mplsbset(size_t size, const mpw* data);
395 
396 /*!\fn void mpsetmsb(size_t size, mpw* data)
397  * \brief This function sets the most significant bit of a multi-precision
398  *  integer.
399  * \param size The size of the multi-precision integer.
400  * \param data The multi-precision integer data.
401  */
402 BEECRYPTAPI
403 void mpsetmsb(size_t size, mpw* data);
404 
405 /*!\fn void mpsetlsb(size_t size, mpw* data)
406  * \brief This function sets the least significant bit of a multi-precision
407  *  integer.
408  * \param size The size of the multi-precision integer.
409  * \param data The multi-precision integer data.
410  */
411 BEECRYPTAPI
412 void mpsetlsb(size_t size, mpw* data);
413 
414 /*!\fn void mpclrmsb(size_t size, mpw* data)
415  * \brief This function clears the most significant bit of a multi-precision
416  *  integer.
417  * \param size The size of the multi-precision integer.
418  * \param data The multi-precision integer data.
419  */
420 BEECRYPTAPI
421 void mpclrmsb(size_t size, mpw* data);
422 
423 /*!\fn void mpclrlsb(size_t size, mpw* data)
424  * \brief This function clears the least significant bit of a multi-precision
425  *  integer.
426  * \param size The size of the multi-precision integer.
427  * \param data The multi-precision integer data.
428  */
429 BEECRYPTAPI
430 void mpclrlsb(size_t size, mpw* data);
431 
432 /*!\fn mpand(size_t size, mpw* xdata, const mpw* ydata)
433  * \brief This function computes the bit-wise AND of two multi-precision
434  *  integers. Modifies xdata.
435  * \param size The size of the multi-precision integers.
436  * \param xdata The multi-precision integer data.
437  * \param ydata The multi-precision integer data.
438  */
439 BEECRYPTAPI
440 void mpand(size_t size, mpw* xdata, const mpw* ydata);
441 
442 /*!\fn void mpor(size_t size, mpw* xdata, const mpw* ydata)
443  * \brief This function computes the bit-wise OR of two multi-precision
444  *  integers. Modifies xdata.
445  * \param size The size of the multi-precision integer.
446  * \param xdata The multi-precision integer data.
447  * \param ydata The multi-precision integer data.
448  */
449 BEECRYPTAPI
450 void mpor(size_t size, mpw* xdata, const mpw* ydata);
451 
452 /*!\fn void mpxor(size_t size, mpw* xdata, const mpw* ydata)
453  * \brief This function computes the bit-wise XOR of two multi-precision
454  *  integers. Modifies xdata.
455  * \param size The size of the multi-precision integer.
456  * \param xdata The multi-precision integer data.
457  * \param ydata The multi-precision integer data.
458  */
459 BEECRYPTAPI
460 void mpxor(size_t size, mpw* xdata, const mpw* ydata);
461 
462 /*!\fn mpnot(size_t size, mpw* data)
463  * \brief This function flips all bits of a multi-precision integer.
464  * \param size The size of the multi-precision integer.
465  * \param data The multi-precision integer data.
466  */
467 BEECRYPTAPI
468 void mpnot(size_t size, mpw* data);
469 
470 /*!\fn void mpsetw(size_t size, mpw* xdata, mpw y)
471  * \brief This function sets the value of a multi-precision integer to the
472  *  given word. The given value is copied into the least significant word,
473  *  while the most significant words are zeroed.
474  * \param size The size of the multi-precision integer.
475  * \param xdata The multi-precision integer data.
476  * \param y The value to be assigned.
477  */
478 BEECRYPTAPI
479 void mpsetw(size_t size, mpw* xdata, mpw y);
480 
481 /*!\fn void mpsetws(size_t size, mpw* xdata, size_t y)
482  * \brief This function sets the value of a multi-precision integer to the
483  *  given word. The given value is copied into the least significant word(s),
484  *  while the most significant words are zeroed.
485  * \param size The size of the multi-precision integer.
486  * \param xdata The multi-precision integer data.
487  * \param y The value.
488  */
489 BEECRYPTAPI
490 void mpsetws(size_t size, mpw* xdata, size_t y);
491 
492 /*!\fn void mpsetx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata)
493  * \brief This function set the value of the first multi-precision integer
494  *  to the second, truncating the most significant words if ysize > xsize, or
495  *  zeroing the most significant words if ysize < xsize.
496  * \param xsize The size of the first multi-precision integer.
497  * \param xdata The first multi-precision integer.
498  * \param ysize The size of the second multi-precision integer.
499  * \param ydata The second multi-precision integer.
500  */
501 BEECRYPTAPI
502 void mpsetx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
503 
504 /*!\fn int mpaddw(size_t size, mpw* xdata, mpw y)
505  * \brief This function adds one word to a multi-precision integer.
506  *  The performed operation is in pseudocode: x += y.
507  * \param size The size of the multi-precision integer.
508  * \param xdata The first multi-precision integer.
509  * \param y The multi-precision word.
510  * \return The carry-over value of the operation; this value is either 0 or 1.
511  */
512 BEECRYPTAPI
513 int mpaddw(size_t size, mpw* xdata, mpw y);
514 
515 /*!\fn int mpadd(size_t size, mpw* xdata, const mpw* ydata)
516  * \brief This function adds two multi-precision integers of equal size.
517  *  The performed operation is in pseudocode: x += y.
518  * \param size The size of the multi-precision integers.
519  * \param xdata The first multi-precision integer.
520  * \param ydata The second multi-precision integer.
521  * \return The carry-over value of the operation; this value is either 0 or 1.
522  */
523 BEECRYPTAPI
524 int mpadd (size_t size, mpw* xdata, const mpw* ydata);
525 
526 /*!\fn int mpaddx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata)
527  * \brief This function adds two multi-precision integers of different size.
528  *  The performed operation in pseudocode: x += y.
529  * \param xsize The size of the first multi-precision integer.
530  * \param xdata The first multi-precision integer.
531  * \param ysize The size of the second multi-precision integer.
532  * \param ydata The second multi-precision integer.
533  * \return The carry-over value of the operation; this value is either 0 or 1.
534  */
535 BEECRYPTAPI
536 int mpaddx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
537 
538 /*!\fn int mpsubw(size_t size, mpw* xdata, mpw y)
539  * \brief This function subtracts one word to a multi-precision integer.
540  *  The performed operation in pseudocode: x -= y
541  * \param size The size of the multi-precision integers.
542  * \param xdata The first multi-precision integer.
543  * \param y The multi-precision word.
544  * \return The carry-over value of the operation; this value is either 0 or 1.
545  */
546 BEECRYPTAPI
547 int mpsubw(size_t size, mpw* xdata, mpw y);
548 
549 /*!\fn int mpsub(size_t size, mpw* xdata, const mpw* ydata)
550  * \brief This function subtracts two multi-precision integers of equal size.
551  *  The performed operation in pseudocode: x -= y
552  * \param size The size of the multi-precision integers.
553  * \param xdata The first multi-precision integer.
554  * \param ydata The second multi-precision integer.
555  * \return The carry-over value of the operation; this value is either 0 or 1.
556  */
557 BEECRYPTAPI
558 int mpsub (size_t size, mpw* xdata, const mpw* ydata);
559 
560 /*!\fn int mpsubx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata)
561  * \brief This function subtracts two multi-precision integers of different
562  *  size. The performed operation in pseudocode: x -= y.
563  * \param xsize The size of the first multi-precision integer.
564  * \param xdata The first multi-precision integer.
565  * \param ysize The size of the second multi-precision integer.
566  * \param ydata The second multi-precision integer.
567  * \return The carry-over value of the operation; this value is either 0 or 1.
568  */
569 BEECRYPTAPI
570 int mpsubx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
571 
572 BEECRYPTAPI
573 int mpmultwo(size_t size, mpw* data);
574 
575 /*!\fn void mpneg(size_t size, mpw* data)
576  * \brief This function negates a multi-precision integer.
577  * \param size The size of the multi-precision integer.
578  * \param data The multi-precision integer data.
579  */
580 BEECRYPTAPI
581 void mpneg(size_t size, mpw* data);
582 
583 /*!\fn size_t mpsize(size_t size, const mpw* data)
584  * \brief This function returns the true size of a multi-precision
585  *  integer, after stripping leading zero words.
586  * \param size The size of the multi-precision integer.
587  * \param data The multi-precision integer data.
588  */
589 BEECRYPTAPI
590 size_t mpsize(size_t size, const mpw* data);
591 
592 /*!\fn size_t mpbits(size_t size, const mpw* data)
593  * \brief This function returns the number of significant bits
594  *  in a multi-precision integer.
595  * \param size The size of the multi-precision integer.
596  * \param data The multi-precision integer data.
597  */
598 BEECRYPTAPI
599 size_t mpbits(size_t size, const mpw* data);
600 
601 BEECRYPTAPI
602 size_t mpmszcnt(size_t size, const mpw* data);
603 
604 BEECRYPTAPI
605 size_t mplszcnt(size_t size, const mpw* data);
606 
607 BEECRYPTAPI
608 void mplshift(size_t size, mpw* data, size_t count);
609 
610 BEECRYPTAPI
611 void mprshift(size_t size, mpw* data, size_t count);
612 
613 BEECRYPTAPI
614 size_t mprshiftlsz(size_t size, mpw* data);
615 
616 BEECRYPTAPI
617 size_t mpnorm(size_t size, mpw* data);
618 
619 BEECRYPTAPI
620 void mpdivtwo (size_t size, mpw* data);
621 
622 BEECRYPTAPI
623 void mpsdivtwo(size_t size, mpw* data);
624 
625 /*!\fn mpw mpsetmul(size_t size, mpw* result, const mpw* data, mpw y)
626  * \brief This function performs a multi-precision multiply-setup.
627  *
628  * This function is used in the computation of a full multi-precision
629  * multiplication. By using it we can shave off a few cycles; otherwise we'd
630  * have to zero the least significant half of the result first and use
631  * another call to the slightly slower mpaddmul function.
632  *
633  * \param size The size of multi-precision integer multiplier.
634  * \param result The place where result will be accumulated.
635  * \param data The multi-precision integer multiplier.
636  * \param y The multiplicand.
637  * \return The carry-over multi-precision word.
638  */
639 BEECRYPTAPI
640 mpw mpsetmul   (size_t size, mpw* result, const mpw* data, mpw y);
641 
642 /*!\fn mpw mpaddmul(size_t size, mpw* result, const mpw* data, mpw y)
643  * \brief This function performs a mult-precision multiply-accumulate.
644  *
645  * This function is used in the computation of a full multi-precision
646  * multiplication. It computes the product-by-one-word and accumulates it with
647  * the previous result.
648  *
649  * \param size The size of multi-precision integer multiplier.
650  * \param result The place where result will be accumulated.
651  * \param data The multi-precision integer multiplier.
652  * \param y The multiplicand.
653  * \retval The carry-over multi-precision word.
654  */
655 BEECRYPTAPI
656 mpw mpaddmul   (size_t size, mpw* result, const mpw* data, mpw y);
657 
658 /*!\fn void mpaddsqrtrc(size_t size, mpw* result, const mpw* data)
659  * \brief This function is used in the calculation of a multi-precision
660  * squaring.
661  */
662 BEECRYPTAPI
663 void mpaddsqrtrc(size_t size, mpw* result, const mpw* data);
664 
665 /*!\fn void mpmul(mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata)
666  * \brief This function computes a full multi-precision product.
667  */
668 BEECRYPTAPI
669 void mpmul(mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
670 
671 /*!\fn void mpsqr(mpw* result, size_t size, const mpw* data)
672  * \brief This function computes a full multi-precision square.
673  */
674 BEECRYPTAPI
675 void mpsqr(mpw* result, size_t size, const mpw* data);
676 
677 BEECRYPTAPI
678 void mpgcd_w(size_t size, const mpw* xdata, const mpw* ydata, mpw* result, mpw* wksp);
679 
680 BEECRYPTAPI
681 int  mpextgcd_w(size_t size, const mpw* xdata, const mpw* ydata, mpw* result, mpw* wksp);
682 
683 BEECRYPTAPI
684 mpw mppndiv(mpw xhi, mpw xlo, mpw y);
685 
686 BEECRYPTAPI
687 void mpmod (mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw*ydata, mpw* wksp);
688 
689 BEECRYPTAPI
690 void mpndivmod(mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata, mpw* wksp);
691 
692 /*
693  * Output Routines
694  */
695 
696 BEECRYPTAPI
697 void mpprint(size_t size, const mpw* data);
698 
699 BEECRYPTAPI
700 void mpprintln(size_t size, const mpw* data);
701 
702 BEECRYPTAPI
703 void mpfprint(FILE* f, size_t size, const mpw* data);
704 
705 BEECRYPTAPI
706 void mpfprintln(FILE* f, size_t size, const mpw* data);
707 
708 /*
709  * Conversion Routines
710  */
711 
712 BEECRYPTAPI
713 int i2osp(byte* osdata, size_t ossize, const mpw* idata, size_t isize);
714 
715 BEECRYPTAPI
716 int os2ip(mpw* idata, size_t isize, const byte* osdata, size_t ossize);
717 
718 BEECRYPTAPI
719 int hs2ip(mpw* idata, size_t isize, const char* hsdata, size_t hssize);
720 
721 #ifdef __cplusplus
722 }
723 #endif
724 
725 #endif
726