BeeCrypt  4.2.1
mp.h
Go to the documentation of this file.
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 
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
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
88 void mpmove(size_t size, mpw* dest, const mpw* src);
89 #endif
90 
97 void mpzero(size_t size, mpw* data);
98 
107 void mpfill(size_t size, mpw* data, mpw fill);
108 
117 int mpodd (size_t size, const mpw* data);
118 
127 int mpeven(size_t size, const mpw* data);
128 
137 int mpz (size_t size, const mpw* data);
138 
147 int mpnz (size_t size, const mpw* data);
148 
159 int mpeq (size_t size, const mpw* xdata, const mpw* ydata);
160 
171 int mpne (size_t size, const mpw* xdata, const mpw* ydata);
172 
184 int mpgt (size_t size, const mpw* xdata, const mpw* ydata);
185 
197 int mplt (size_t size, const mpw* xdata, const mpw* ydata);
198 
210 int mpge (size_t size, const mpw* xdata, const mpw* ydata);
211 
223 int mple (size_t size, const mpw* xdata, const mpw* ydata);
224 
234 int mpcmp(size_t size, const mpw* xdata, const mpw* ydata);
235 
247 int mpeqx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
248 
260 int mpnex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
261 
274 int mpgtx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
275 
288 int mpltx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
289 
302 int mpgex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
303 
316 int mplex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
317 
327 int mpcmpx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
328 
338 int mpisone(size_t size, const mpw* data);
339 
349 int mpistwo(size_t size, const mpw* data);
350 
360 int mpleone(size_t size, const mpw* data);
361 
372 int mpeqmone(size_t size, const mpw* xdata, const mpw* ydata);
373 
383 int mpmsbset(size_t size, const mpw* data);
384 
394 int mplsbset(size_t size, const mpw* data);
395 
403 void mpsetmsb(size_t size, mpw* data);
404 
412 void mpsetlsb(size_t size, mpw* data);
413 
421 void mpclrmsb(size_t size, mpw* data);
422 
430 void mpclrlsb(size_t size, mpw* data);
431 
440 void mpand(size_t size, mpw* xdata, const mpw* ydata);
441 
450 void mpor(size_t size, mpw* xdata, const mpw* ydata);
451 
460 void mpxor(size_t size, mpw* xdata, const mpw* ydata);
461 
468 void mpnot(size_t size, mpw* data);
469 
479 void mpsetw(size_t size, mpw* xdata, mpw y);
480 
490 void mpsetws(size_t size, mpw* xdata, size_t y);
491 
502 void mpsetx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
503 
513 int mpaddw(size_t size, mpw* xdata, mpw y);
514 
524 int mpadd (size_t size, mpw* xdata, const mpw* ydata);
525 
536 int mpaddx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
537 
547 int mpsubw(size_t size, mpw* xdata, mpw y);
548 
558 int mpsub (size_t size, mpw* xdata, const mpw* ydata);
559 
570 int mpsubx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
571 
573 int mpmultwo(size_t size, mpw* data);
574 
581 void mpneg(size_t size, mpw* data);
582 
590 size_t mpsize(size_t size, const mpw* data);
591 
599 size_t mpbits(size_t size, const mpw* data);
600 
602 size_t mpmszcnt(size_t size, const mpw* data);
603 
605 size_t mplszcnt(size_t size, const mpw* data);
606 
608 void mplshift(size_t size, mpw* data, size_t count);
609 
611 void mprshift(size_t size, mpw* data, size_t count);
612 
614 size_t mprshiftlsz(size_t size, mpw* data);
615 
617 size_t mpnorm(size_t size, mpw* data);
618 
620 void mpdivtwo (size_t size, mpw* data);
621 
623 void mpsdivtwo(size_t size, mpw* data);
624 
640 mpw mpsetmul (size_t size, mpw* result, const mpw* data, mpw y);
641 
656 mpw mpaddmul (size_t size, mpw* result, const mpw* data, mpw y);
657 
663 void mpaddsqrtrc(size_t size, mpw* result, const mpw* data);
664 
669 void mpmul(mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
670 
675 void mpsqr(mpw* result, size_t size, const mpw* data);
676 
678 void mpgcd_w(size_t size, const mpw* xdata, const mpw* ydata, mpw* result, mpw* wksp);
679 
681 int mpextgcd_w(size_t size, const mpw* xdata, const mpw* ydata, mpw* result, mpw* wksp);
682 
684 mpw mppndiv(mpw xhi, mpw xlo, mpw y);
685 
687 void mpmod (mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw*ydata, mpw* wksp);
688 
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 
697 void mpprint(size_t size, const mpw* data);
698 
700 void mpprintln(size_t size, const mpw* data);
701 
703 void mpfprint(FILE* f, size_t size, const mpw* data);
704 
706 void mpfprintln(FILE* f, size_t size, const mpw* data);
707 
708 /*
709  * Conversion Routines
710  */
711 
713 int i2osp(byte* osdata, size_t ossize, const mpw* idata, size_t isize);
714 
716 int os2ip(mpw* idata, size_t isize, const byte* osdata, size_t ossize);
717 
719 int hs2ip(mpw* idata, size_t isize, const char* hsdata, size_t hssize);
720 
721 #ifdef __cplusplus
722 }
723 #endif
724 
725 #endif
int mpsubx(size_t xsize, mpw *xdata, size_t ysize, const mpw *ydata)
This function subtracts two multi-precision integers of different size. The performed operation in ps...
int mpgex(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function tests if the first of two multi-precision integers of different size is greater than or...
mpw mpsetmul(size_t size, mpw *result, const mpw *data, mpw y)
This function performs a multi-precision multiply-setup.
void mpxor(size_t size, mpw *xdata, const mpw *ydata)
This function computes the bit-wise XOR of two multi-precision integers. Modifies xdata...
int mpistwo(size_t size, const mpw *data)
This function tests if the value of a multi-precision integer is equal to two.
void mplshift(size_t size, mpw *data, size_t count)
void mpmul(mpw *result, size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function computes a full multi-precision product.
BeeCrypt API, portability headers.
int mpeven(size_t size, const mpw *data)
This function tests if a multi-precision integer is even.
void mpnot(size_t size, mpw *data)
This function flips all bits of a multi-precision integer.
Multi-precision integer optimization definitions.
int mpsub(size_t size, mpw *xdata, const mpw *ydata)
This function subtracts two multi-precision integers of equal size. The performed operation in pseudo...
void mpgcd_w(size_t size, const mpw *xdata, const mpw *ydata, mpw *result, mpw *wksp)
void mprshift(size_t size, mpw *data, size_t count)
void mpsetmsb(size_t size, mpw *data)
This function sets the most significant bit of a multi-precision integer.
int mpcmp(size_t size, const mpw *xdata, const mpw *ydata)
This function performs a comparison of two multi-precision integers of the same size.
int mpsubw(size_t size, mpw *xdata, mpw y)
This function subtracts one word to a multi-precision integer. The performed operation in pseudocode:...
int mplsbset(size_t size, const mpw *data)
This function tests if the leiast significant bit of a multi-precision integer is set...
void mpneg(size_t size, mpw *data)
This function negates a multi-precision integer.
size_t mprshiftlsz(size_t size, mpw *data)
#define mpmove(size, dst, src)
Definition: mp.h:85
int mpmultwo(size_t size, mpw *data)
mpw mpaddmul(size_t size, mpw *result, const mpw *data, mpw y)
This function performs a mult-precision multiply-accumulate.
int mpisone(size_t size, const mpw *data)
This functions tests if the value of a multi-precision integer is equal to one.
void mpsetws(size_t size, mpw *xdata, size_t y)
This function sets the value of a multi-precision integer to the given word. The given value is copie...
int mpgtx(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function tests if the first of two multi-precision integers of different size is greater than th...
void mpndivmod(mpw *result, size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata, mpw *wksp)
void mpfprint(FILE *f, size_t size, const mpw *data)
void mpprintln(size_t size, const mpw *data)
void mpor(size_t size, mpw *xdata, const mpw *ydata)
This function computes the bit-wise OR of two multi-precision integers. Modifies xdata.
void mpsqr(mpw *result, size_t size, const mpw *data)
This function computes a full multi-precision square.
uint8_t byte
Definition: api.h:72
int mpadd(size_t size, mpw *xdata, const mpw *ydata)
This function adds two multi-precision integers of equal size. The performed operation is in pseudoco...
size_t mpbits(size_t size, const mpw *data)
This function returns the number of significant bits in a multi-precision integer.
void mpmod(mpw *result, size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata, mpw *wksp)
int mpeq(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if two multi-precision integers of the same size are equal.
uint64_t mpw
Definition: api.h:87
void mpprint(size_t size, const mpw *data)
int mpz(size_t size, const mpw *data)
This function tests if a multi-precision integer is zero.
int mpaddx(size_t xsize, mpw *xdata, size_t ysize, const mpw *ydata)
This function adds two multi-precision integers of different size. The performed operation in pseudoc...
int mpltx(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function tests if the first of two multi-precision integers of different size is less than the s...
int mpcmpx(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function performs a comparison of two multi-precision integers of the different size...
int mple(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if the first of two multi-precision integers of the same size is less than or equ...
void mpaddsqrtrc(size_t size, mpw *result, const mpw *data)
This function is used in the calculation of a multi-precision squaring.
int mplex(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function tests if the first of two multi-precision integers of different size is less than or eq...
mpw mppndiv(mpw xhi, mpw xlo, mpw y)
size_t mplszcnt(size_t size, const mpw *data)
int hs2ip(mpw *idata, size_t isize, const char *hsdata, size_t hssize)
void mpdivtwo(size_t size, mpw *data)
int mpnex(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function tests if two multi-precision integers of different size are equal.
#define BEECRYPTAPI
Definition: api.h:52
void mpsetlsb(size_t size, mpw *data)
This function sets the least significant bit of a multi-precision integer.
int mpodd(size_t size, const mpw *data)
This functions tests if a multi-precision integer is odd.
void mpfill(size_t size, mpw *data, mpw fill)
This function fills each word of a multi-precision integer with a given value.
int mpleone(size_t size, const mpw *data)
This function tests if the value of a multi-precision integer is less than or equal to one...
int mpextgcd_w(size_t size, const mpw *xdata, const mpw *ydata, mpw *result, mpw *wksp)
size_t mpnorm(size_t size, mpw *data)
#define mpcopy(size, dst, src)
Definition: mp.h:78
int mpgt(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if the first of two multi-precision integers of the same size is greater than the...
int mpeqmone(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if multi-precision integer x is equal to y minus one.
void mpclrmsb(size_t size, mpw *data)
This function clears the most significant bit of a multi-precision integer.
void mpzero(size_t size, mpw *data)
This function zeroes a multi-precision integer of a given size.
int i2osp(byte *osdata, size_t ossize, const mpw *idata, size_t isize)
int mpge(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if the first of two multi-precision integers of the same size is greater than or ...
void mpclrlsb(size_t size, mpw *data)
This function clears the least significant bit of a multi-precision integer.
void mpfprintln(FILE *f, size_t size, const mpw *data)
int mpeqx(size_t xsize, const mpw *xdata, size_t ysize, const mpw *ydata)
This function tests if two multi-precision integers of different size are equal.
void mpsetw(size_t size, mpw *xdata, mpw y)
This function sets the value of a multi-precision integer to the given word. The given value is copie...
int mpmsbset(size_t size, const mpw *data)
This function tests if the most significant bit of a multi-precision integer is set.
int os2ip(mpw *idata, size_t isize, const byte *osdata, size_t ossize)
int mpne(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if two multi-precision integers of the same size differ.
size_t mpsize(size_t size, const mpw *data)
This function returns the true size of a multi-precision integer, after stripping leading zero words...
void mpand(size_t size, mpw *xdata, const mpw *ydata)
This function computes the bit-wise AND of two multi-precision integers. Modifies xdata...
int mpnz(size_t size, const mpw *data)
This function tests if a multi-precision integer is not zero.
int mpaddw(size_t size, mpw *xdata, mpw y)
This function adds one word to a multi-precision integer. The performed operation is in pseudocode: x...
size_t mpmszcnt(size_t size, const mpw *data)
void mpsdivtwo(size_t size, mpw *data)
void mpsetx(size_t xsize, mpw *xdata, size_t ysize, const mpw *ydata)
This function set the value of the first multi-precision integer to the second, truncating the most s...
int mplt(size_t size, const mpw *xdata, const mpw *ydata)
This function tests if the first of two multi-precision integers of the same size is less than the se...