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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*************************************************
* Lowest Level MPI Algorithms Header File *
* (C) 1999-2006 Jack Lloyd *
* 2006 Luca Piccarreta *
*************************************************/
#ifndef BOTAN_MP_ASM_INTERNAL_H__
#define BOTAN_MP_ASM_INTERNAL_H__
#include "mp_asm.h"
#if (BOTAN_MP_WORD_BITS != 32)
#error BOTAN_MP_WORD_BITS must be 32 for x86 asm
#endif
namespace Botan {
extern "C" {
/*************************************************
* Word Addition *
*************************************************/
inline word word_add(word x, word y, word* carry)
{
word z = x + y;
word c1 = (z < x);
z += *carry;
*carry = c1 | (z < *carry);
return z;
}
/*************************************************
* Four Word Block Addition, Two Argument *
*************************************************/
inline word word4_add2(word x[4], const word y[4], word carry)
{
__asm {
mov esi,[y]
mov edi,[x]
xor ecx,ecx
sub ecx,[carry] //force CF=1 iff *carry==1
mov eax,[edi]
mov ecx,[edi+4]
adc eax,[esi]
adc ecx,[esi+4]
mov [edi],eax
mov [edi+4],ecx
mov eax,[edi+8]
mov ecx,[edi+12]
adc eax,[esi+8]
adc ecx,[esi+12]
mov [edi+8],eax
mov [edi+12],ecx
sbb edx,edx
neg edx
mov eax,edx
}
}
/*************************************************
* Four Word Block Addition, Three Argument *
*************************************************/
inline word word4_add3(word z[4], const word x[4], const word y[4], word carry)
{
__asm {
mov esi,[y]
mov edi,[x]
xor ecx,ecx
sub ecx,[carry] //force CF=1 iff *carry==1
mov ebx,[z]
mov eax,[edi]
mov ecx,[edi+4]
adc eax,[esi]
adc ecx,[esi+4]
mov [ebx],eax
mov [ebx+4],ecx
mov eax,[edi+8]
mov ecx,[edi+12]
adc eax,[esi+8]
adc ecx,[esi+12]
mov [ebx+8],eax
mov [ebx+12],ecx
sbb edx,edx
neg edx
mov eax,edx
}
}
/*************************************************
* Word Subtraction *
*************************************************/
inline word word_sub(word x, word y, word* carry)
{
word t0 = x - y;
word c1 = (t0 > x);
word z = t0 - *carry;
*carry = c1 | (z > t0);
return z;
}
/*************************************************
* Four Word Block Subtraction, Two Argument *
*************************************************/
inline word word4_sub2(word x[4], const word y[4], word carry)
{
_asm {
mov esi,[y]
mov edi,[x]
xor ecx,ecx
sub ecx,[carry] //force CF=1 iff *carry==1
mov eax,[edi]
mov ecx,[edi+4]
sbb eax,[esi]
sbb ecx,[esi+4]
mov [edi],eax
mov [edi+4],ecx
mov eax,[edi+8]
mov ecx,[edi+12]
sbb eax,[esi+8]
sbb ecx,[esi+12]
mov [edi+8],eax
mov [edi+12],ecx
sbb edx,edx
neg edx
mov eax,edx
}
}
/*************************************************
* Four Word Block Subtraction, Three Argument *
*************************************************/
inline word word4_sub3(word z[4], const word x[4],const word y[4], word carry)
{
__asm {
mov esi,[y]
mov edi,[x]
xor ecx,ecx
sub ecx,[carry] //force CF=1 iff *carry==1
mov ebx,[z]
mov eax,[edi]
mov ecx,[edi+4]
sbb eax,[esi]
sbb ecx,[esi+4]
mov [ebx],eax
mov [ebx+4],ecx
mov eax,[edi+8]
mov ecx,[edi+12]
sbb eax,[esi+8]
sbb ecx,[esi+12]
mov [ebx+8],eax
mov [ebx+12],ecx
sbb edx,edx
neg edx
mov eax,edx
}
}
/*************************************************
* Four Word Block Linear Multiplication *
*************************************************/
inline word word4_linmul2(word x[4], word y, word carry)
{
__asm
{
mov esi,[x]
mov eax,[esi] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,[carry] //sum lo carry
adc edx,0 //sum hi carry
mov ecx,edx //store carry
mov [esi],eax //load a
mov eax,[esi+4] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,ecx //sum lo carry
adc edx,0 //sum hi carry
mov ecx,edx //store carry
mov [esi+4],eax //load a
mov eax,[esi+8] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,ecx //sum lo carry
adc edx,0 //sum hi carry
mov ecx,edx //store carry
mov [esi+8],eax //load a
mov eax,[esi+12] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,ecx //sum lo carry
adc edx,0 //sum hi carry
mov [esi+12],eax //load a
mov eax,edx //store carry
}
}
/*************************************************
* Four Word Block Linear Multiplication *
*************************************************/
inline word word4_linmul3(word z[4], const word x[4], word y, word carry)
{
__asm
{
mov edi,[z]
mov esi,[x]
mov eax,[esi] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,[carry] //sum lo carry
adc edx,0 //sum hi carry
mov ecx,edx //store carry
mov [edi],eax //load a
mov eax,[esi+4] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,ecx //sum lo carry
adc edx,0 //sum hi carry
mov ecx,edx //store carry
mov [edi+4],eax //load a
mov eax,[esi+8] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,ecx //sum lo carry
adc edx,0 //sum hi carry
mov ecx,edx //store carry
mov [edi+8],eax //load a
mov eax,[esi+12] //load a
mul [y] //edx(hi):eax(lo)=a*b
add eax,ecx //sum lo carry
adc edx,0 //sum hi carry
mov [edi+12],eax //load a
mov eax,edx //store carry
}
}
/*************************************************
* Eight Word Block Multiply-Add *
*************************************************/
inline void word8_madd3(word z[], word x, const word y[], word* carry)
{
word_madd(x, y[0], z[0], *carry, z + 0, carry);
word_madd(x, y[1], z[1], *carry, z + 1, carry);
word_madd(x, y[2], z[2], *carry, z + 2, carry);
word_madd(x, y[3], z[3], *carry, z + 3, carry);
word_madd(x, y[4], z[4], *carry, z + 4, carry);
word_madd(x, y[5], z[5], *carry, z + 5, carry);
word_madd(x, y[6], z[6], *carry, z + 6, carry);
word_madd(x, y[7], z[7], *carry, z + 7, carry);
}
/*************************************************
* Multiply-Add Accumulator *
*************************************************/
inline void word3_muladd(word* w2, word* w1, word* w0, word a, word b)
{
dword z = (dword)a * b + (*w0);
*w0 = (word)z; //lo
word t1 = (word)(z >> BOTAN_MP_WORD_BITS); //hi
*w1 += t1; //w1+=lo
*w2 += (*w1 < t1) ? 1 : 0; //w2+=carry
}
/*************************************************
* Multiply-Add Accumulator *
*************************************************/
inline void word3_muladd_2(word* w2, word* w1, word* w0, word a, word b)
{
dword z = (dword)a * b;
word t0 = (word)z;
word t1 = (word)(z >> BOTAN_MP_WORD_BITS);
*w0 += t0;
*w1 += t1 + ((*w0 < t0) ? 1 : 0);
*w2 += (*w1 < t1) ? 1 : 0;
*w0 += t0;
*w1 += t1 + ((*w0 < t0) ? 1 : 0);
*w2 += (*w1 < t1) ? 1 : 0;
}
}
}
#endif
|