| 1 | /* |
| 2 | Copyright (C) 1999-2007 The Botan Project. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, for any use, with or without |
| 5 | modification, is permitted provided that the following conditions are met: |
| 6 | |
| 7 | 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | list of conditions, and the following disclaimer. |
| 9 | |
| 10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | this list of conditions, and the following disclaimer in the documentation |
| 12 | and/or other materials provided with the distribution. |
| 13 | |
| 14 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED |
| 15 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. |
| 17 | |
| 18 | IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, |
| 19 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 20 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 23 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 24 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | // LICENSEHEADER_END |
| 27 | namespace QCA { // WRAPNS_LINE |
| 28 | /************************************************* |
| 29 | * BigInt Binary Operators Source File * |
| 30 | * (C) 1999-2007 The Botan Project * |
| 31 | *************************************************/ |
| 32 | |
| 33 | } // WRAPNS_LINE |
| 34 | #include <botan/bigint.h> |
| 35 | namespace QCA { // WRAPNS_LINE |
| 36 | } // WRAPNS_LINE |
| 37 | #include <botan/numthry.h> |
| 38 | namespace QCA { // WRAPNS_LINE |
| 39 | } // WRAPNS_LINE |
| 40 | #include <botan/mp_core.h> |
| 41 | namespace QCA { // WRAPNS_LINE |
| 42 | } // WRAPNS_LINE |
| 43 | #include <botan/bit_ops.h> |
| 44 | namespace QCA { // WRAPNS_LINE |
| 45 | } // WRAPNS_LINE |
| 46 | #include <algorithm> |
| 47 | namespace QCA { // WRAPNS_LINE |
| 48 | |
| 49 | namespace Botan { |
| 50 | |
| 51 | /************************************************* |
| 52 | * Addition Operator * |
| 53 | *************************************************/ |
| 54 | BigInt operator+(const BigInt &x, const BigInt &y) |
| 55 | { |
| 56 | const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); |
| 57 | |
| 58 | #ifdef BOTAN_TYPES_QT |
| 59 | BigInt z(x.sign(), qMax(a: x_sw, b: y_sw) + 1); |
| 60 | #else |
| 61 | BigInt z(x.sign(), std::max(x_sw, y_sw) + 1); |
| 62 | #endif |
| 63 | |
| 64 | if ((x.sign() == y.sign())) |
| 65 | bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); |
| 66 | else { |
| 67 | s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); |
| 68 | |
| 69 | if (relative_size < 0) { |
| 70 | bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw); |
| 71 | z.set_sign(y.sign()); |
| 72 | } else if (relative_size == 0) |
| 73 | z.set_sign(BigInt::Positive); |
| 74 | else if (relative_size > 0) |
| 75 | bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); |
| 76 | } |
| 77 | |
| 78 | return z; |
| 79 | } |
| 80 | |
| 81 | /************************************************* |
| 82 | * Subtraction Operator * |
| 83 | *************************************************/ |
| 84 | BigInt operator-(const BigInt &x, const BigInt &y) |
| 85 | { |
| 86 | const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); |
| 87 | |
| 88 | s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); |
| 89 | |
| 90 | #ifdef BOTAN_TYPES_QT |
| 91 | BigInt z(BigInt::Positive, qMax(a: x_sw, b: y_sw) + 1); |
| 92 | #else |
| 93 | BigInt z(BigInt::Positive, std::max(x_sw, y_sw) + 1); |
| 94 | #endif |
| 95 | |
| 96 | if (relative_size < 0) { |
| 97 | if (x.sign() == y.sign()) |
| 98 | bigint_sub3(z.get_reg(), y.data(), y_sw, x.data(), x_sw); |
| 99 | else |
| 100 | bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); |
| 101 | z.set_sign(y.reverse_sign()); |
| 102 | } else if (relative_size == 0) { |
| 103 | if (x.sign() != y.sign()) |
| 104 | bigint_shl2(z.get_reg(), x.data(), x_sw, 0, 1); |
| 105 | } else if (relative_size > 0) { |
| 106 | if (x.sign() == y.sign()) |
| 107 | bigint_sub3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); |
| 108 | else |
| 109 | bigint_add3(z.get_reg(), x.data(), x_sw, y.data(), y_sw); |
| 110 | z.set_sign(x.sign()); |
| 111 | } |
| 112 | return z; |
| 113 | } |
| 114 | |
| 115 | /************************************************* |
| 116 | * Multiplication Operator * |
| 117 | *************************************************/ |
| 118 | BigInt operator*(const BigInt &x, const BigInt &y) |
| 119 | { |
| 120 | const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); |
| 121 | |
| 122 | BigInt z(BigInt::Positive, x.size() + y.size()); |
| 123 | |
| 124 | if (x_sw == 1 && y_sw) |
| 125 | bigint_linmul3(z.get_reg(), y.data(), y_sw, x.word_at(n: 0)); |
| 126 | else if (y_sw == 1 && x_sw) |
| 127 | bigint_linmul3(z.get_reg(), x.data(), x_sw, y.word_at(n: 0)); |
| 128 | else if (x_sw && y_sw) { |
| 129 | SecureVector<word> workspace(z.size()); |
| 130 | bigint_mul(z.get_reg(), z.size(), workspace, x.data(), x.size(), x_sw, y.data(), y.size(), y_sw); |
| 131 | } |
| 132 | |
| 133 | if (x_sw && y_sw && x.sign() != y.sign()) |
| 134 | z.flip_sign(); |
| 135 | return z; |
| 136 | } |
| 137 | |
| 138 | /************************************************* |
| 139 | * Division Operator * |
| 140 | *************************************************/ |
| 141 | BigInt operator/(const BigInt &x, const BigInt &y) |
| 142 | { |
| 143 | BigInt q, r; |
| 144 | divide(x, y, q, r); |
| 145 | return q; |
| 146 | } |
| 147 | |
| 148 | /************************************************* |
| 149 | * Modulo Operator * |
| 150 | *************************************************/ |
| 151 | BigInt operator%(const BigInt &n, const BigInt &mod) |
| 152 | { |
| 153 | if (mod.is_zero()) |
| 154 | throw BigInt::DivideByZero(); |
| 155 | if (mod.is_negative()) |
| 156 | throw Invalid_Argument("BigInt::operator%: modulus must be > 0" ); |
| 157 | if (n.is_positive() && mod.is_positive() && n < mod) |
| 158 | return n; |
| 159 | |
| 160 | BigInt q, r; |
| 161 | divide(n, mod, q, r); |
| 162 | return r; |
| 163 | } |
| 164 | |
| 165 | /************************************************* |
| 166 | * Modulo Operator * |
| 167 | *************************************************/ |
| 168 | word operator%(const BigInt &n, word mod) |
| 169 | { |
| 170 | if (mod == 0) |
| 171 | throw BigInt::DivideByZero(); |
| 172 | if (power_of_2(mod)) |
| 173 | return (n.word_at(n: 0) & (mod - 1)); |
| 174 | |
| 175 | word remainder = 0; |
| 176 | |
| 177 | for (u32bit j = n.sig_words(); j > 0; --j) |
| 178 | remainder = bigint_modop(remainder, n.word_at(n: j - 1), mod); |
| 179 | |
| 180 | if (remainder && n.sign() == BigInt::Negative) |
| 181 | return mod - remainder; |
| 182 | return remainder; |
| 183 | } |
| 184 | |
| 185 | /************************************************* |
| 186 | * Left Shift Operator * |
| 187 | *************************************************/ |
| 188 | BigInt operator<<(const BigInt &x, u32bit shift) |
| 189 | { |
| 190 | if (shift == 0) |
| 191 | return x; |
| 192 | |
| 193 | const u32bit shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS; |
| 194 | |
| 195 | const u32bit x_sw = x.sig_words(); |
| 196 | |
| 197 | BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0)); |
| 198 | bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); |
| 199 | return y; |
| 200 | } |
| 201 | |
| 202 | /************************************************* |
| 203 | * Right Shift Operator * |
| 204 | *************************************************/ |
| 205 | BigInt operator>>(const BigInt &x, u32bit shift) |
| 206 | { |
| 207 | if (shift == 0) |
| 208 | return x; |
| 209 | if (x.bits() <= shift) |
| 210 | return 0; |
| 211 | |
| 212 | const u32bit shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, x_sw = x.sig_words(); |
| 213 | |
| 214 | BigInt y(x.sign(), x_sw - shift_words); |
| 215 | bigint_shr2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); |
| 216 | return y; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | } // WRAPNS_LINE |
| 221 | |