diff options
author | lloyd <[email protected]> | 2010-09-24 21:58:47 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-09-24 21:58:47 +0000 |
commit | 3d0ac39eab74c6f74fe41eda9e5f057d1b396f10 (patch) | |
tree | 7fc75ef1b8fde5d0faa9fa5cd626d24e7626bb31 /src/math/mp/mp_misc.cpp | |
parent | 84aabfe1f6d9cea49c212853bce738b2bb1885c4 (diff) |
Move the core MPI functions to src/math/mp, leaving src/math/bigint just
for the implementation of the BigInt class
Diffstat (limited to 'src/math/mp/mp_misc.cpp')
-rw-r--r-- | src/math/mp/mp_misc.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/math/mp/mp_misc.cpp b/src/math/mp/mp_misc.cpp new file mode 100644 index 000000000..77b8e6f51 --- /dev/null +++ b/src/math/mp/mp_misc.cpp @@ -0,0 +1,102 @@ +/* +* MP Misc Functions +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/internal/mp_core.h> +#include <botan/internal/mp_asm.h> + +namespace Botan { + +extern "C" { + +/* +* Core Division Operation +*/ +u32bit bigint_divcore(word q, word y2, word y1, + word x3, word x2, word x1) + { + // Compute (y2,y1) * q + + word y3 = 0; + y1 = word_madd2(q, y1, &y3); + y2 = word_madd2(q, y2, &y3); + + // Return (y3,y2,y1) >? (x3,x2,x1) + + if(y3 > x3) return 1; + if(y3 < x3) return 0; + if(y2 > x2) return 1; + if(y2 < x2) return 0; + if(y1 > x1) return 1; + if(y1 < x1) return 0; + return 0; + } + +/* +* Compare two MP integers +*/ +s32bit bigint_cmp(const word x[], u32bit x_size, + const word y[], u32bit y_size) + { + if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); } + + while(x_size > y_size) + { + if(x[x_size-1]) + return 1; + x_size--; + } + + for(u32bit j = x_size; j > 0; --j) + { + if(x[j-1] > y[j-1]) + return 1; + if(x[j-1] < y[j-1]) + return -1; + } + + return 0; + } + +/* +* Do a 2-word/1-word Division +*/ +word bigint_divop(word n1, word n0, word d) + { + word high = n1 % d, quotient = 0; + + for(u32bit j = 0; j != MP_WORD_BITS; ++j) + { + word high_top_bit = (high & MP_WORD_TOP_BIT); + + high <<= 1; + high |= (n0 >> (MP_WORD_BITS-1-j)) & 1; + quotient <<= 1; + + if(high_top_bit || high >= d) + { + high -= d; + quotient |= 1; + } + } + + return quotient; + } + +/* +* Do a 2-word/1-word Modulo +*/ +word bigint_modop(word n1, word n0, word d) + { + word z = bigint_divop(n1, n0, d); + word dummy = 0; + z = word_madd2(z, d, &dummy); + return (n0-z); + } + +} + +} |