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
225
226
227
228
229
230
231
232
233
234
235
236
237
|
/*************************************************
* Lowest Level MPI Algorithms Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/mp_asm.h>
#include <botan/mp_asmi.h>
#include <botan/mp_core.h>
#include <botan/mem_ops.h>
namespace Botan {
extern "C" {
/*************************************************
* Two Operand Addition, No Carry *
*************************************************/
word bigint_add2_nc(word x[], u32bit x_size, const word y[], u32bit y_size)
{
word carry = 0;
const u32bit blocks = y_size - (y_size % 8);
for(u32bit j = 0; j != blocks; j += 8)
carry = word8_add2(x + j, y + j, carry);
for(u32bit j = blocks; j != y_size; ++j)
x[j] = word_add(x[j], y[j], &carry);
if(!carry)
return 0;
for(u32bit j = y_size; j != x_size; ++j)
if(++x[j])
return 0;
return 1;
}
/*************************************************
* Three Operand Addition, No Carry *
*************************************************/
word bigint_add3_nc(word z[], const word x[], u32bit x_size,
const word y[], u32bit y_size)
{
if(x_size < y_size)
{ return bigint_add3_nc(z, y, y_size, x, x_size); }
word carry = 0;
const u32bit blocks = y_size - (y_size % 8);
for(u32bit j = 0; j != blocks; j += 8)
carry = word8_add3(z + j, x + j, y + j, carry);
for(u32bit j = blocks; j != y_size; ++j)
z[j] = word_add(x[j], y[j], &carry);
for(u32bit j = y_size; j != x_size; ++j)
{
word x_j = x[j] + carry;
if(carry && x_j)
carry = 0;
z[j] = x_j;
}
return carry;
}
/*************************************************
* Two Operand Addition *
*************************************************/
void bigint_add2(word x[], u32bit x_size, const word y[], u32bit y_size)
{
if(bigint_add2_nc(x, x_size, y, y_size))
++x[x_size];
}
/*************************************************
* Three Operand Addition *
*************************************************/
void bigint_add3(word z[], const word x[], u32bit x_size,
const word y[], u32bit y_size)
{
if(bigint_add3_nc(z, x, x_size, y, y_size))
++z[(x_size > y_size ? x_size : y_size)];
}
/*************************************************
* Two Operand Subtraction *
*************************************************/
void bigint_sub2(word x[], u32bit x_size, const word y[], u32bit y_size)
{
word carry = 0;
const u32bit blocks = y_size - (y_size % 8);
for(u32bit j = 0; j != blocks; j += 8)
carry = word8_sub2(x + j, y + j, carry);
for(u32bit j = blocks; j != y_size; ++j)
x[j] = word_sub(x[j], y[j], &carry);
if(!carry) return;
for(u32bit j = y_size; j != x_size; ++j)
{
--x[j];
if(x[j] != MP_WORD_MAX) return;
}
}
/*************************************************
* Three Operand Subtraction *
*************************************************/
void bigint_sub3(word z[], const word x[], u32bit x_size,
const word y[], u32bit y_size)
{
word carry = 0;
const u32bit blocks = y_size - (y_size % 8);
for(u32bit j = 0; j != blocks; j += 8)
carry = word8_sub3(z + j, x + j, y + j, carry);
for(u32bit j = blocks; j != y_size; ++j)
z[j] = word_sub(x[j], y[j], &carry);
for(u32bit j = y_size; j != x_size; ++j)
{
word x_j = x[j] - carry;
if(carry && x_j != MP_WORD_MAX)
carry = 0;
z[j] = x_j;
}
}
/*************************************************
* Two Operand Linear Multiply *
*************************************************/
void bigint_linmul2(word x[], u32bit x_size, word y)
{
const u32bit blocks = x_size - (x_size % 8);
word carry = 0;
for(u32bit j = 0; j != blocks; j += 8)
carry = word8_linmul2(x + j, y, carry);
for(u32bit j = blocks; j != x_size; ++j)
x[j] = word_madd2(x[j], y, &carry);
x[x_size] = carry;
}
/*************************************************
* Three Operand Linear Multiply *
*************************************************/
void bigint_linmul3(word z[], const word x[], u32bit x_size, word y)
{
const u32bit blocks = x_size - (x_size % 8);
word carry = 0;
for(u32bit j = 0; j != blocks; j += 8)
carry = word8_linmul3(z + j, x + j, y, carry);
for(u32bit j = blocks; j != x_size; ++j)
z[j] = word_madd2(x[j], y, &carry);
z[x_size] = carry;
}
/*************************************************
* Simple O(N^2) Multiplication *
*************************************************/
void bigint_simple_mul(word z[], const word x[], u32bit x_size,
const word y[], u32bit y_size)
{
const u32bit blocks = y_size - (y_size % 8);
clear_mem(z, x_size + y_size);
for(u32bit j = 0; j != x_size; ++j)
{
const word x_j = x[j];
word carry = 0;
for(u32bit k = 0; k != blocks; k += 8)
carry = word8_madd3(z + j + k, y + k, x_j, carry);
for(u32bit k = blocks; k != y_size; ++k)
z[j+k] = word_madd3(x_j, y[k], z[j+k], &carry);
z[j+y_size] = carry;
}
}
/*************************************************
* Montgomery Reduction Algorithm *
*************************************************/
void montgomery_reduce(word z[], u32bit z_size,
const word x[], u32bit x_size, word u)
{
for(u32bit j = 0; j != x_size; ++j)
{
word* z_j = z + j;
const word y = z_j[0] * u;
word carry = 0;
const u32bit blocks = x_size - (x_size % 8);
for(u32bit k = 0; k != blocks; k += 8)
carry = word8_madd3(z_j + k, x + k, y, carry);
for(u32bit k = blocks; k != x_size; ++k)
z_j[k] = word_madd3(x[k], y, z_j[k], &carry);
word carry2 = 0;
z_j[x_size] = word_add(z_j[x_size], carry, &carry2);
carry = carry2;
for(u32bit k = x_size + 1; carry && k != z_size - j; ++k)
{
++z_j[k];
carry = !z_j[k];
}
}
if(bigint_cmp(z + x_size, x_size + 1, x, x_size) >= 0)
bigint_sub2(z + x_size, x_size + 1, x, x_size);
}
}
}
|