1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* BigInt Assignment Operators
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/bigint.h>
#include <botan/internal/mp_core.h>
#include <botan/bit_ops.h>
#include <algorithm>
namespace Botan {
/*
* Addition Operator
*/
BigInt& BigInt::operator+=(const BigInt& y)
{
const u32bit x_sw = sig_words(), y_sw = y.sig_words();
const u32bit reg_size = std::max(x_sw, y_sw) + 1;
grow_to(reg_size);
if(sign() == y.sign())
bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
else
{
s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
if(relative_size < 0)
{
SecureVector<word> z(reg_size - 1);
bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw);
copy_mem(get_reg().begin(), z.begin(), z.size());
set_sign(y.sign());
}
else if(relative_size == 0)
{
get_reg().clear();
set_sign(Positive);
}
else if(relative_size > 0)
bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
}
return (*this);
}
/*
* Subtraction Operator
*/
BigInt& BigInt::operator-=(const BigInt& y)
{
const u32bit x_sw = sig_words(), y_sw = y.sig_words();
s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
const u32bit reg_size = std::max(x_sw, y_sw) + 1;
grow_to(reg_size);
if(relative_size < 0)
{
if(sign() == y.sign())
{
SecureVector<word> z(reg_size - 1);
bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw);
copy_mem(get_reg().begin(), z.begin(), z.size());
}
else
bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
set_sign(y.reverse_sign());
}
else if(relative_size == 0)
{
if(sign() == y.sign())
{
get_reg().clear();
set_sign(Positive);
}
else
bigint_shl1(get_reg(), x_sw, 0, 1);
}
else if(relative_size > 0)
{
if(sign() == y.sign())
bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
else
bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
}
return (*this);
}
/*
* Multiplication Operator
*/
BigInt& BigInt::operator*=(const BigInt& y)
{
const u32bit x_sw = sig_words(), y_sw = y.sig_words();
set_sign((sign() == y.sign()) ? Positive : Negative);
if(x_sw == 0 || y_sw == 0)
{
get_reg().clear();
set_sign(Positive);
}
else if(x_sw == 1 && y_sw)
{
grow_to(y_sw + 2);
bigint_linmul3(get_reg(), y.data(), y_sw, word_at(0));
}
else if(y_sw == 1 && x_sw)
{
grow_to(x_sw + 2);
bigint_linmul2(get_reg(), x_sw, y.word_at(0));
}
else
{
grow_to(size() + y.size());
SecureVector<word> z(data(), x_sw);
SecureVector<word> workspace(size());
bigint_mul(get_reg(), size(), workspace,
z, z.size(), x_sw,
y.data(), y.size(), y_sw);
}
return (*this);
}
/*
* Division Operator
*/
BigInt& BigInt::operator/=(const BigInt& y)
{
if(y.sig_words() == 1 && power_of_2(y.word_at(0)))
(*this) >>= (y.bits() - 1);
else
(*this) = (*this) / y;
return (*this);
}
/*
* Modulo Operator
*/
BigInt& BigInt::operator%=(const BigInt& mod)
{
return (*this = (*this) % mod);
}
/*
* Modulo Operator
*/
word BigInt::operator%=(word mod)
{
if(mod == 0)
throw BigInt::DivideByZero();
if(power_of_2(mod))
{
word result = (word_at(0) & (mod - 1));
clear();
grow_to(2);
get_reg()[0] = result;
return result;
}
word remainder = 0;
for(u32bit j = sig_words(); j > 0; --j)
remainder = bigint_modop(remainder, word_at(j-1), mod);
clear();
grow_to(2);
if(remainder && sign() == BigInt::Negative)
get_reg()[0] = mod - remainder;
else
get_reg()[0] = remainder;
set_sign(BigInt::Positive);
return word_at(0);
}
/*
* Left Shift Operator
*/
BigInt& BigInt::operator<<=(u32bit shift)
{
if(shift)
{
const u32bit shift_words = shift / MP_WORD_BITS,
shift_bits = shift % MP_WORD_BITS,
words = sig_words();
grow_to(words + shift_words + (shift_bits ? 1 : 0));
bigint_shl1(get_reg(), words, shift_words, shift_bits);
}
return (*this);
}
/*
* Right Shift Operator
*/
BigInt& BigInt::operator>>=(u32bit shift)
{
if(shift)
{
const u32bit shift_words = shift / MP_WORD_BITS,
shift_bits = shift % MP_WORD_BITS;
bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits);
if(is_zero())
set_sign(Positive);
}
return (*this);
}
}
|