00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00041 #ifndef _MP_H
00042 #define _MP_H
00043
00044 #include "beecrypt/api.h"
00045 #include "beecrypt/mpopt.h"
00046
00047 #define MP_HWBITS (MP_WBITS >> 1)
00048 #define MP_WBYTES (MP_WBITS >> 3)
00049 #define MP_WNIBBLES (MP_WBITS >> 2)
00050
00051 #if (MP_WBITS == 64)
00052 # define MP_WORDS_TO_BITS(x) ((x) << 6)
00053 # define MP_WORDS_TO_NIBBLES(x) ((x) << 4)
00054 # define MP_WORDS_TO_BYTES(x) ((x) << 3)
00055 # define MP_BITS_TO_WORDS(x) ((x) >> 6)
00056 # define MP_NIBBLES_TO_WORDS(x) ((x) >> 4)
00057 # define MP_BYTES_TO_WORDS(x) ((x) >> 3)
00058 #elif (MP_WBITS == 32)
00059 # define MP_WORDS_TO_BITS(x) ((x) << 5)
00060 # define MP_WORDS_TO_NIBBLES(x) ((x) << 3)
00061 # define MP_WORDS_TO_BYTES(x) ((x) << 2)
00062 # define MP_BITS_TO_WORDS(x) ((x) >> 5)
00063 # define MP_NIBBLES_TO_WORDS(x) ((x) >> 3)
00064 # define MP_BYTES_TO_WORDS(x) ((x) >> 2)
00065 #else
00066 # error
00067 #endif
00068
00069 #define MP_MSBMASK (((mpw) 0x1) << (MP_WBITS-1))
00070 #define MP_LSBMASK ((mpw) 0x1)
00071 #define MP_ALLMASK ~((mpw) 0x0)
00072
00073 #ifdef __cplusplus
00074 extern "C" {
00075 #endif
00076
00077 #ifndef ASM_MPCOPY
00078 # define mpcopy(size, dst, src) memcpy(dst, src, MP_WORDS_TO_BYTES(size))
00079 #else
00080 BEECRYPTAPI
00081 void mpcopy(size_t size, mpw* dest, const mpw* src);
00082 #endif
00083
00084 #ifndef ASM_MPMOVE
00085 # define mpmove(size, dst, src) memmove(dst, src, MP_WORDS_TO_BYTES(size))
00086 #else
00087 BEECRYPTAPI
00088 void mpmove(size_t size, mpw* dest, const mpw* src);
00089 #endif
00090
00096 BEECRYPTAPI
00097 void mpzero(size_t size, mpw* data);
00098
00106 BEECRYPTAPI
00107 void mpfill(size_t size, mpw* data, mpw fill);
00108
00116 BEECRYPTAPI
00117 int mpodd (size_t size, const mpw* data);
00118
00126 BEECRYPTAPI
00127 int mpeven(size_t size, const mpw* data);
00128
00136 BEECRYPTAPI
00137 int mpz (size_t size, const mpw* data);
00138
00146 BEECRYPTAPI
00147 int mpnz (size_t size, const mpw* data);
00148
00158 BEECRYPTAPI
00159 int mpeq (size_t size, const mpw* xdata, const mpw* ydata);
00160
00170 BEECRYPTAPI
00171 int mpne (size_t size, const mpw* xdata, const mpw* ydata);
00172
00183 BEECRYPTAPI
00184 int mpgt (size_t size, const mpw* xdata, const mpw* ydata);
00185
00196 BEECRYPTAPI
00197 int mplt (size_t size, const mpw* xdata, const mpw* ydata);
00198
00209 BEECRYPTAPI
00210 int mpge (size_t size, const mpw* xdata, const mpw* ydata);
00211
00222 BEECRYPTAPI
00223 int mple (size_t size, const mpw* xdata, const mpw* ydata);
00224
00233 BEECRYPTAPI
00234 int mpcmp(size_t size, const mpw* xdata, const mpw* ydata);
00235
00246 BEECRYPTAPI
00247 int mpeqx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00248
00259 BEECRYPTAPI
00260 int mpnex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00261
00273 BEECRYPTAPI
00274 int mpgtx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00275
00287 BEECRYPTAPI
00288 int mpltx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00289
00301 BEECRYPTAPI
00302 int mpgex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00303
00315 BEECRYPTAPI
00316 int mplex(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00317
00326 BEECRYPTAPI
00327 int mpcmpx(size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00328
00337 BEECRYPTAPI
00338 int mpisone(size_t size, const mpw* data);
00339
00348 BEECRYPTAPI
00349 int mpistwo(size_t size, const mpw* data);
00350
00359 BEECRYPTAPI
00360 int mpleone(size_t size, const mpw* data);
00361
00371 BEECRYPTAPI
00372 int mpeqmone(size_t size, const mpw* xdata, const mpw* ydata);
00373
00382 BEECRYPTAPI
00383 int mpmsbset(size_t size, const mpw* data);
00384
00393 BEECRYPTAPI
00394 int mplsbset(size_t size, const mpw* data);
00395
00402 BEECRYPTAPI
00403 void mpsetmsb(size_t size, mpw* data);
00404
00411 BEECRYPTAPI
00412 void mpsetlsb(size_t size, mpw* data);
00413
00420 BEECRYPTAPI
00421 void mpclrmsb(size_t size, mpw* data);
00422
00429 BEECRYPTAPI
00430 void mpclrlsb(size_t size, mpw* data);
00431
00439 BEECRYPTAPI
00440 void mpand(size_t size, mpw* xdata, const mpw* ydata);
00441
00449 BEECRYPTAPI
00450 void mpor(size_t size, mpw* xdata, const mpw* ydata);
00451
00459 BEECRYPTAPI
00460 void mpxor(size_t size, mpw* xdata, const mpw* ydata);
00461
00467 BEECRYPTAPI
00468 void mpnot(size_t size, mpw* data);
00469
00478 BEECRYPTAPI
00479 void mpsetw(size_t size, mpw* xdata, mpw y);
00480
00489 BEECRYPTAPI
00490 void mpsetws(size_t size, mpw* xdata, size_t y);
00491
00501 BEECRYPTAPI
00502 void mpsetx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
00503
00512 BEECRYPTAPI
00513 int mpaddw(size_t size, mpw* xdata, mpw y);
00514
00523 BEECRYPTAPI
00524 int mpadd (size_t size, mpw* xdata, const mpw* ydata);
00525
00535 BEECRYPTAPI
00536 int mpaddx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
00537
00546 BEECRYPTAPI
00547 int mpsubw(size_t size, mpw* xdata, mpw y);
00548
00557 BEECRYPTAPI
00558 int mpsub (size_t size, mpw* xdata, const mpw* ydata);
00559
00569 BEECRYPTAPI
00570 int mpsubx(size_t xsize, mpw* xdata, size_t ysize, const mpw* ydata);
00571
00572 BEECRYPTAPI
00573 int mpmultwo(size_t size, mpw* data);
00574
00580 BEECRYPTAPI
00581 void mpneg(size_t size, mpw* data);
00582
00589 BEECRYPTAPI
00590 size_t mpsize(size_t size, const mpw* data);
00591
00598 BEECRYPTAPI
00599 size_t mpbits(size_t size, const mpw* data);
00600
00601 BEECRYPTAPI
00602 size_t mpmszcnt(size_t size, const mpw* data);
00603
00604 BEECRYPTAPI
00605 size_t mplszcnt(size_t size, const mpw* data);
00606
00607 BEECRYPTAPI
00608 void mplshift(size_t size, mpw* data, size_t count);
00609
00610 BEECRYPTAPI
00611 void mprshift(size_t size, mpw* data, size_t count);
00612
00613 BEECRYPTAPI
00614 size_t mprshiftlsz(size_t size, mpw* data);
00615
00616 BEECRYPTAPI
00617 size_t mpnorm(size_t size, mpw* data);
00618
00619 BEECRYPTAPI
00620 void mpdivtwo (size_t size, mpw* data);
00621
00622 BEECRYPTAPI
00623 void mpsdivtwo(size_t size, mpw* data);
00624
00639 BEECRYPTAPI
00640 mpw mpsetmul (size_t size, mpw* result, const mpw* data, mpw y);
00641
00655 BEECRYPTAPI
00656 mpw mpaddmul (size_t size, mpw* result, const mpw* data, mpw y);
00657
00662 BEECRYPTAPI
00663 void mpaddsqrtrc(size_t size, mpw* result, const mpw* data);
00664
00668 BEECRYPTAPI
00669 void mpmul(mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata);
00670
00674 BEECRYPTAPI
00675 void mpsqr(mpw* result, size_t size, const mpw* data);
00676
00677 BEECRYPTAPI
00678 void mpgcd_w(size_t size, const mpw* xdata, const mpw* ydata, mpw* result, mpw* wksp);
00679
00680 BEECRYPTAPI
00681 int mpextgcd_w(size_t size, const mpw* xdata, const mpw* ydata, mpw* result, mpw* wksp);
00682
00683 BEECRYPTAPI
00684 mpw mppndiv(mpw xhi, mpw xlo, mpw y);
00685
00686 BEECRYPTAPI
00687 void mpmod (mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw*ydata, mpw* wksp);
00688
00689 BEECRYPTAPI
00690 void mpndivmod(mpw* result, size_t xsize, const mpw* xdata, size_t ysize, const mpw* ydata, mpw* wksp);
00691
00692
00693
00694
00695
00696 BEECRYPTAPI
00697 void mpprint(size_t size, const mpw* data);
00698
00699 BEECRYPTAPI
00700 void mpprintln(size_t size, const mpw* data);
00701
00702 BEECRYPTAPI
00703 void mpfprint(FILE* f, size_t size, const mpw* data);
00704
00705 BEECRYPTAPI
00706 void mpfprintln(FILE* f, size_t size, const mpw* data);
00707
00708
00709
00710
00711
00712 BEECRYPTAPI
00713 int i2osp(byte* osdata, size_t ossize, const mpw* idata, size_t isize);
00714
00715 BEECRYPTAPI
00716 int os2ip(mpw* idata, size_t isize, const byte* osdata, size_t ossize);
00717
00718 BEECRYPTAPI
00719 int hs2ip(mpw* idata, size_t isize, const char* hsdata, size_t hssize);
00720
00721 #ifdef __cplusplus
00722 }
00723 #endif
00724
00725 #endif