00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 namespace QCA {
00028
00029
00030
00031
00032
00033 }
00034 #include <botan/bigint.h>
00035 namespace QCA {
00036 }
00037 #include <botan/numthry.h>
00038 namespace QCA {
00039 }
00040 #include <botan/mp_core.h>
00041 namespace QCA {
00042 }
00043 #include <botan/bit_ops.h>
00044 namespace QCA {
00045 }
00046 #include <algorithm>
00047 namespace QCA {
00048
00049 namespace Botan {
00050
00051
00052
00053
00054 BigInt operator+(const BigInt& x, const BigInt& y)
00055 {
00056 const u32bit x_sw = x.sig_words(), y_sw = y.sig_words();
00057
00058 #ifdef BOTAN_TYPES_QT
00059 BigInt z(x.sign(), qMax(x_sw, y_sw) + 1);
00060 #else
00061 BigInt z(x.sign(), std::max(x_sw, y_sw) + 1);
00062 #endif
00063
00064 if((x.sign() == y.sign()))
00065 bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00066 else
00067 {
00068 s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw);
00069
00070 if(relative_size < 0)
00071 {
00072 bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw);
00073 z.set_sign(y.sign());
00074 }
00075 else if(relative_size == 0)
00076 z.set_sign(BigInt::Positive);
00077 else if(relative_size > 0)
00078 bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00079 }
00080
00081 return z;
00082 }
00083
00084
00085
00086
00087 BigInt operator-(const BigInt& x, const BigInt& y)
00088 {
00089 const u32bit x_sw = x.sig_words(), y_sw = y.sig_words();
00090
00091 s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw);
00092
00093 #ifdef BOTAN_TYPES_QT
00094 BigInt z(BigInt::Positive, qMax(x_sw, y_sw) + 1);
00095 #else
00096 BigInt z(BigInt::Positive, std::max(x_sw, y_sw) + 1);
00097 #endif
00098
00099 if(relative_size < 0)
00100 {
00101 if(x.sign() == y.sign())
00102 bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw);
00103 else
00104 bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00105 z.set_sign(y.reverse_sign());
00106 }
00107 else if(relative_size == 0)
00108 {
00109 if(x.sign() != y.sign())
00110 bigint_shl2(z.get_reg(), x.data(), x_sw, 0, 1);
00111 }
00112 else if(relative_size > 0)
00113 {
00114 if(x.sign() == y.sign())
00115 bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00116 else
00117 bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw);
00118 z.set_sign(x.sign());
00119 }
00120 return z;
00121 }
00122
00123
00124
00125
00126 BigInt operator*(const BigInt& x, const BigInt& y)
00127 {
00128 const u32bit x_sw = x.sig_words(), y_sw = y.sig_words();
00129
00130 BigInt z(BigInt::Positive, x.size() + y.size());
00131
00132 if(x_sw == 1 && y_sw)
00133 bigint_linmul3(z.get_reg(), y.data(), y_sw, x.word_at(0));
00134 else if(y_sw == 1 && x_sw)
00135 bigint_linmul3(z.get_reg(), x.data(), x_sw, y.word_at(0));
00136 else if(x_sw && y_sw)
00137 {
00138 SecureVector<word> workspace(z.size());
00139 bigint_mul(z.get_reg(), z.size(), workspace,
00140 x.data(), x.size(), x_sw,
00141 y.data(), y.size(), y_sw);
00142 }
00143
00144 if(x_sw && y_sw && x.sign() != y.sign())
00145 z.flip_sign();
00146 return z;
00147 }
00148
00149
00150
00151
00152 BigInt operator/(const BigInt& x, const BigInt& y)
00153 {
00154 BigInt q, r;
00155 divide(x, y, q, r);
00156 return q;
00157 }
00158
00159
00160
00161
00162 BigInt operator%(const BigInt& n, const BigInt& mod)
00163 {
00164 if(mod.is_zero())
00165 throw BigInt::DivideByZero();
00166 if(mod.is_negative())
00167 throw Invalid_Argument("BigInt::operator%: modulus must be > 0");
00168 if(n.is_positive() && mod.is_positive() && n < mod)
00169 return n;
00170
00171 BigInt q, r;
00172 divide(n, mod, q, r);
00173 return r;
00174 }
00175
00176
00177
00178
00179 word operator%(const BigInt& n, word mod)
00180 {
00181 if(mod == 0)
00182 throw BigInt::DivideByZero();
00183 if(power_of_2(mod))
00184 return (n.word_at(0) & (mod - 1));
00185
00186 word remainder = 0;
00187
00188 for(u32bit j = n.sig_words(); j > 0; --j)
00189 remainder = bigint_modop(remainder, n.word_at(j-1), mod);
00190
00191 if(remainder && n.sign() == BigInt::Negative)
00192 return mod - remainder;
00193 return remainder;
00194 }
00195
00196
00197
00198
00199 BigInt operator<<(const BigInt& x, u32bit shift)
00200 {
00201 if(shift == 0)
00202 return x;
00203
00204 const u32bit shift_words = shift / MP_WORD_BITS,
00205 shift_bits = shift % MP_WORD_BITS;
00206
00207 const u32bit x_sw = x.sig_words();
00208
00209 BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0));
00210 bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits);
00211 return y;
00212 }
00213
00214
00215
00216
00217 BigInt operator>>(const BigInt& x, u32bit shift)
00218 {
00219 if(shift == 0)
00220 return x;
00221 if(x.bits() <= shift)
00222 return 0;
00223
00224 const u32bit shift_words = shift / MP_WORD_BITS,
00225 shift_bits = shift % MP_WORD_BITS,
00226 x_sw = x.sig_words();
00227
00228 BigInt y(x.sign(), x_sw - shift_words);
00229 bigint_shr2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits);
00230 return y;
00231 }
00232
00233 }
00234 }