/src/botan/src/lib/math/bigint/big_ops2.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 1999-2007,2018 Jack Lloyd |
3 | | * 2016 Matthias Gierlings |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/bigint.h> |
9 | | |
10 | | #include <botan/internal/bit_ops.h> |
11 | | #include <botan/internal/mp_core.h> |
12 | | #include <algorithm> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | 1.49M | BigInt& BigInt::add(const word y[], size_t y_words, Sign y_sign) { |
17 | 1.49M | const size_t x_sw = sig_words(); |
18 | | |
19 | 1.49M | grow_to(std::max(x_sw, y_words) + 1); |
20 | | |
21 | 1.49M | if(sign() == y_sign) { |
22 | 1.44M | word carry = bigint_add2(mutable_data(), size() - 1, y, y_words); |
23 | 1.44M | mutable_data()[size() - 1] += carry; |
24 | 1.44M | } else { |
25 | 41.5k | const int32_t relative_size = bigint_cmp(_data(), x_sw, y, y_words); |
26 | | |
27 | 41.5k | if(relative_size >= 0) { |
28 | | // *this >= y |
29 | 41.4k | bigint_sub2(mutable_data(), x_sw, y, y_words); |
30 | 41.4k | } else { |
31 | | // *this < y: compute *this = y - *this |
32 | 91 | bigint_sub2_rev(mutable_data(), y, y_words); |
33 | 91 | } |
34 | | |
35 | 41.5k | if(relative_size < 0) { |
36 | 91 | set_sign(y_sign); |
37 | 41.4k | } else if(relative_size == 0) { |
38 | 77 | set_sign(Positive); |
39 | 77 | } |
40 | 41.5k | } |
41 | | |
42 | 1.49M | return (*this); |
43 | 1.49M | } |
44 | | |
45 | 1.04k | BigInt& BigInt::mod_add(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) { |
46 | 1.04k | if(this->is_negative() || s.is_negative() || mod.is_negative()) { |
47 | 0 | throw Invalid_Argument("BigInt::mod_add expects all arguments are positive"); |
48 | 0 | } |
49 | | |
50 | 1.04k | BOTAN_DEBUG_ASSERT(*this < mod); |
51 | 1.04k | BOTAN_DEBUG_ASSERT(s < mod); |
52 | | |
53 | | /* |
54 | | t + s or t + s - p == t - (p - s) |
55 | | |
56 | | So first compute ws = p - s |
57 | | |
58 | | Then compute t + s and t - ws |
59 | | |
60 | | If t - ws does not borrow, then that is the correct valued |
61 | | */ |
62 | | |
63 | 1.04k | const size_t mod_sw = mod.sig_words(); |
64 | 1.04k | BOTAN_ARG_CHECK(mod_sw > 0, "BigInt::mod_add modulus must be positive"); |
65 | | |
66 | 1.04k | this->grow_to(mod_sw); |
67 | 1.04k | s.grow_to(mod_sw); |
68 | | |
69 | | // First mod_sw for p - s, 2*mod_sw for bigint_addsub workspace |
70 | 1.04k | if(ws.size() < 3 * mod_sw) { |
71 | 320 | ws.resize(3 * mod_sw); |
72 | 320 | } |
73 | | |
74 | | // NOLINTBEGIN(readability-container-data-pointer) |
75 | | |
76 | 1.04k | word borrow = bigint_sub3(&ws[0], mod._data(), mod_sw, s._data(), mod_sw); |
77 | 1.04k | BOTAN_DEBUG_ASSERT(borrow == 0); |
78 | 1.04k | BOTAN_UNUSED(borrow); |
79 | | |
80 | | // Compute t - ws |
81 | 1.04k | borrow = bigint_sub3(&ws[mod_sw], this->_data(), mod_sw, &ws[0], mod_sw); |
82 | | |
83 | | // Compute t + s |
84 | 1.04k | bigint_add3(&ws[mod_sw * 2], this->_data(), mod_sw, s._data(), mod_sw); |
85 | | |
86 | 1.04k | CT::conditional_copy_mem(borrow, &ws[0], &ws[mod_sw * 2], &ws[mod_sw], mod_sw); |
87 | 1.04k | set_words(&ws[0], mod_sw); |
88 | | |
89 | | // NOLINTEND(readability-container-data-pointer) |
90 | | |
91 | 1.04k | return (*this); |
92 | 1.04k | } |
93 | | |
94 | 14.2k | BigInt& BigInt::mod_sub(const BigInt& s, const BigInt& mod, secure_vector<word>& ws) { |
95 | 14.2k | if(this->is_negative() || s.is_negative() || mod.is_negative()) { |
96 | 0 | throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive"); |
97 | 0 | } |
98 | | |
99 | | // We are assuming in this function that *this and s are no more than mod_sw words long |
100 | 14.2k | BOTAN_DEBUG_ASSERT(*this < mod); |
101 | 14.2k | BOTAN_DEBUG_ASSERT(s < mod); |
102 | | |
103 | 14.2k | const size_t mod_sw = mod.sig_words(); |
104 | | |
105 | 14.2k | this->grow_to(mod_sw); |
106 | 14.2k | s.grow_to(mod_sw); |
107 | | |
108 | 14.2k | if(ws.size() < mod_sw) { |
109 | 0 | ws.resize(mod_sw); |
110 | 0 | } |
111 | | |
112 | 14.2k | const word borrow = bigint_sub3(ws.data(), mutable_data(), mod_sw, s._data(), mod_sw); |
113 | | |
114 | | // Conditionally add back the modulus |
115 | 14.2k | bigint_cnd_add(borrow, ws.data(), mod._data(), mod_sw); |
116 | | |
117 | 14.2k | copy_mem(mutable_data(), ws.data(), mod_sw); |
118 | | |
119 | 14.2k | return (*this); |
120 | 14.2k | } |
121 | | |
122 | 4.17k | BigInt& BigInt::mod_mul(uint8_t y, const BigInt& mod, secure_vector<word>& ws) { |
123 | 4.17k | BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive"); |
124 | 4.17k | BOTAN_ARG_CHECK(y < 16, "y too large"); |
125 | | |
126 | 4.17k | BOTAN_DEBUG_ASSERT(*this < mod); |
127 | | |
128 | 4.17k | *this *= static_cast<word>(y); |
129 | 4.17k | this->reduce_below(mod, ws); |
130 | 4.17k | return (*this); |
131 | 4.17k | } |
132 | | |
133 | 0 | BigInt& BigInt::rev_sub(const word y[], size_t y_sw, secure_vector<word>& ws) { |
134 | 0 | BOTAN_UNUSED(ws); |
135 | 0 | BigInt y_bn; |
136 | 0 | y_bn.m_data.set_words(y, y_sw); |
137 | 0 | *this = y_bn - *this; |
138 | 0 | return (*this); |
139 | 0 | } |
140 | | |
141 | | /* |
142 | | * Multiplication Operator |
143 | | */ |
144 | 0 | BigInt& BigInt::operator*=(const BigInt& y) { |
145 | 0 | secure_vector<word> ws; |
146 | 0 | return this->mul(y, ws); |
147 | 0 | } |
148 | | |
149 | 0 | BigInt& BigInt::mul(const BigInt& y, secure_vector<word>& ws) { |
150 | 0 | const size_t x_sw = sig_words(); |
151 | 0 | const size_t y_sw = y.sig_words(); |
152 | 0 | set_sign((sign() == y.sign()) ? Positive : Negative); |
153 | |
|
154 | 0 | if(x_sw == 0 || y_sw == 0) { |
155 | 0 | clear(); |
156 | 0 | set_sign(Positive); |
157 | 0 | } else if(x_sw == 1 && y_sw > 0) { |
158 | 0 | grow_to(y_sw + 1); |
159 | 0 | bigint_linmul3(mutable_data(), y._data(), y_sw, word_at(0)); |
160 | 0 | } else { |
161 | 0 | const size_t new_size = x_sw + y_sw + 1; |
162 | 0 | if(ws.size() < new_size) { |
163 | 0 | ws.resize(new_size); |
164 | 0 | } |
165 | 0 | secure_vector<word> z_reg(new_size); |
166 | |
|
167 | 0 | bigint_mul(z_reg.data(), z_reg.size(), _data(), size(), x_sw, y._data(), y.size(), y_sw, ws.data(), ws.size()); |
168 | |
|
169 | 0 | this->swap_reg(z_reg); |
170 | 0 | } |
171 | |
|
172 | 0 | return (*this); |
173 | 0 | } |
174 | | |
175 | 509 | BigInt& BigInt::square(secure_vector<word>& ws) { |
176 | 509 | const size_t sw = sig_words(); |
177 | | |
178 | 509 | secure_vector<word> z(2 * sw); |
179 | 509 | ws.resize(z.size()); |
180 | | |
181 | 509 | bigint_sqr(z.data(), z.size(), _data(), size(), sw, ws.data(), ws.size()); |
182 | | |
183 | 509 | swap_reg(z); |
184 | 509 | set_sign(BigInt::Positive); |
185 | | |
186 | 509 | return (*this); |
187 | 509 | } |
188 | | |
189 | 1.45M | BigInt& BigInt::operator*=(word y) { |
190 | 1.45M | if(y == 0) { |
191 | 0 | clear(); |
192 | 0 | set_sign(Positive); |
193 | 0 | } |
194 | | |
195 | 1.45M | const word carry = bigint_linmul2(mutable_data(), size(), y); |
196 | 1.45M | set_word_at(size(), carry); |
197 | | |
198 | 1.45M | return (*this); |
199 | 1.45M | } |
200 | | |
201 | | /* |
202 | | * Division Operator |
203 | | */ |
204 | 0 | BigInt& BigInt::operator/=(const BigInt& y) { |
205 | 0 | if(y.sig_words() == 1 && is_power_of_2(y.word_at(0))) { |
206 | 0 | (*this) >>= (y.bits() - 1); |
207 | 0 | } else { |
208 | 0 | (*this) = (*this) / y; |
209 | 0 | } |
210 | 0 | return (*this); |
211 | 0 | } |
212 | | |
213 | | /* |
214 | | * Modulo Operator |
215 | | */ |
216 | 25.2k | BigInt& BigInt::operator%=(const BigInt& mod) { |
217 | 25.2k | return (*this = (*this) % mod); |
218 | 25.2k | } |
219 | | |
220 | | /* |
221 | | * Modulo Operator |
222 | | */ |
223 | 0 | word BigInt::operator%=(word mod) { |
224 | 0 | if(mod == 0) { |
225 | 0 | throw Invalid_Argument("BigInt::operator%= divide by zero"); |
226 | 0 | } |
227 | | |
228 | 0 | word remainder = 0; |
229 | |
|
230 | 0 | if(is_power_of_2(mod)) { |
231 | 0 | remainder = (word_at(0) & (mod - 1)); |
232 | 0 | } else { |
233 | 0 | const size_t sw = sig_words(); |
234 | 0 | for(size_t i = sw; i > 0; --i) { |
235 | 0 | remainder = bigint_modop_vartime(remainder, word_at(i - 1), mod); |
236 | 0 | } |
237 | 0 | } |
238 | |
|
239 | 0 | if(remainder != 0 && sign() == BigInt::Negative) { |
240 | 0 | remainder = mod - remainder; |
241 | 0 | } |
242 | |
|
243 | 0 | m_data.set_to_zero(); |
244 | 0 | m_data.set_word_at(0, remainder); |
245 | 0 | set_sign(BigInt::Positive); |
246 | 0 | return remainder; |
247 | 0 | } |
248 | | |
249 | | /* |
250 | | * Left Shift Operator |
251 | | */ |
252 | 51.7k | BigInt& BigInt::operator<<=(size_t shift) { |
253 | 51.7k | const size_t sw = sig_words(); |
254 | 51.7k | const size_t new_size = sw + (shift + WordInfo<word>::bits - 1) / WordInfo<word>::bits; |
255 | | |
256 | 51.7k | m_data.grow_to(new_size); |
257 | | |
258 | 51.7k | bigint_shl1(m_data.mutable_data(), new_size, sw, shift); |
259 | | |
260 | 51.7k | return (*this); |
261 | 51.7k | } |
262 | | |
263 | | /* |
264 | | * Right Shift Operator |
265 | | */ |
266 | 82.0k | BigInt& BigInt::operator>>=(size_t shift) { |
267 | 82.0k | bigint_shr1(m_data.mutable_data(), m_data.size(), shift); |
268 | | |
269 | 82.0k | if(is_negative() && is_zero()) { |
270 | 0 | set_sign(Positive); |
271 | 0 | } |
272 | | |
273 | 82.0k | return (*this); |
274 | 82.0k | } |
275 | | |
276 | | } // namespace Botan |