aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/bn/asm/x86nt32.asm
blob: 0198c2c583e8b5a20aefd5b8e2ac177315d80106 (plain)
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
	TITLE	bn_mulw.c
	.386P
.model FLAT
PUBLIC	_bn_mul_add_word
_TEXT	SEGMENT
; File bn_mulw.c
_bn_mul_add_word PROC NEAR
	push	ebp
	push	ebx
	push	esi
	push	edi
	mov	edi,DWORD PTR 20[esp]   ; r
	mov	ebx,DWORD PTR 24[esp]	; a
	mov	ecx,DWORD PTR 32[esp]	; w
	xor	esi,esi			; c=0

	mov	ebp,DWORD PTR 28[esp]	; num
	shr	ebp,2			; num/4
	jz	$L666

$L546:
	; Round one
	mov	eax,DWORD PTR [ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR [edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR [edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow

	; Round two
	mov	eax,DWORD PTR 4[ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR 4[edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR 4[edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow

	; Round three
	mov	eax,DWORD PTR 8[ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR 8[edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR 8[edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow

	; Round four
	mov	eax,DWORD PTR 12[ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR 12[edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR 12[edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow

	add	ebx,16
	add	edi,16

	dec	ebp
	jz	$L666
	jmp	$L546
$L666:
	mov	ebp,DWORD PTR 28[esp]	; num
	and	ebp,3			; num%4
	jz	$L547

	; Round one
	mov	eax,DWORD PTR [ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR [edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR [edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow
	dec	ebp
	jz	$L547
	; Round two
	mov	eax,DWORD PTR 4[ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR 4[edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR 4[edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow
	dec	ebp
	jz	$L547
	; Round three
	mov	eax,DWORD PTR 8[ebx]	; edx:eax = *a * w
	mul	ecx
	add	eax,DWORD PTR 8[edi]	; *r+=ax
	adc	edx,0
	add	eax,esi			; edx:eax += c
	adc	edx,0
	mov	DWORD PTR 8[edi],eax	; *r+=ax
	mov	esi,edx			; c = overflow

$L547:
	mov	eax,esi
	pop	edi
	pop	esi
	pop	ebx
	pop	ebp
	ret
_bn_mul_add_word ENDP
_TEXT	ENDS
PUBLIC	_bn_mul_word
_TEXT	SEGMENT
_bn_mul_word PROC NEAR
	push	ebp
	push	ebx
	push	esi
	push	edi

	mov	edi,DWORD PTR 20[esp]	; r
	mov	ebx,DWORD PTR 24[esp]	; a
	mov	ebp,DWORD PTR 28[esp]	; num
	mov	ecx,DWORD PTR 32[esp]	; w
	xor	esi,esi			; c=0

	shr	ebp,2			; num/4
	jz	$L266

$L593:
	; Round one
	mov	eax,DWORD PTR [ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR [edi],eax	; *r=eax
	mov	esi,edx			; c=edx
	; Round two
	mov	eax,DWORD PTR 4[ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR 4[edi],eax	; *r=eax
	mov	esi,edx			; c=edx
	; Round three
	mov	eax,DWORD PTR 8[ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR 8[edi],eax	; *r=eax
	mov	esi,edx			; c=edx
	; Round four
	mov	eax,DWORD PTR 12[ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR 12[edi],eax	; *r=eax
	mov	esi,edx			; c=edx

	add	ebx,16
	add	edi,16

	dec	ebp
	jz	$L266
	jmp	$L593
$L266:
	mov	ebp,DWORD PTR 28[esp]	; num
	and	ebp,3	
	jz	$L601

	; Round one
	mov	eax,DWORD PTR [ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR [edi],eax	; *r=eax
	mov	esi,edx			; c=edx
	dec	ebp
	jz	$L601
	; Round two
	mov	eax,DWORD PTR 4[ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR 4[edi],eax	; *r=eax
	mov	esi,edx			; c=edx
	dec	ebp
	jz	$L601
	; Round three
	mov	eax,DWORD PTR 8[ebx]	; edx:eax= w * *a 
	mul	ecx
	add	eax,esi			; edx:eax+=c
	adc	edx,0
	mov	DWORD PTR 8[edi],eax	; *r=eax
	mov	esi,edx			; c=edx

$L601:
	mov	eax,esi
	pop	edi
	pop	esi
	pop	ebx
	pop	ebp
	ret
_bn_mul_word ENDP
_TEXT	ENDS
PUBLIC	_bn_sqr_words
_TEXT	SEGMENT
_bn_sqr_words PROC NEAR
	push	ebx
	push	esi
	push	edi
	mov	esi,DWORD PTR 16[esp]	; r
	mov	edi,DWORD PTR 20[esp]	; a
	mov	ebx,DWORD PTR 24[esp]	; num
	
	shr	ebx,2			; num/4
	jz	$L111
$L640:
	; Round 1
	mov	eax, DWORD PTR [edi]
	mul	eax			; *a * *a
	mov	DWORD PTR [esi],eax
	mov	DWORD PTR 4[esi],edx
	; Round 2
	mov	eax, DWORD PTR 4[edi]
	mul	eax			; *a * *a
	mov	DWORD PTR 8[esi],eax
	mov	DWORD PTR 12[esi],edx
	; Round 3
	mov	eax, DWORD PTR 8[edi]
	mul	eax			; *a * *a
	mov	DWORD PTR 16[esi],eax
	mov	DWORD PTR 20[esi],edx
	; Round 4
	mov	eax, DWORD PTR 12[edi]
	mul	eax			; *a * *a
	mov	DWORD PTR 24[esi],eax
	mov	DWORD PTR 28[esi],edx

	add	edi,16
	add	esi,32

	dec	ebx
	jz	$L111
	jmp	$L640
$L111:
	mov	ebx,DWORD PTR 24[esp]	; num
	and	ebx,3			; num%3
	jz	$L645

	; Round 1
	mov	eax, DWORD PTR [edi]
	mul	eax			; *a * *a
	mov	DWORD PTR [esi],eax
	mov	DWORD PTR 4[esi],edx
	dec	ebx
	jz	$L645
	; Round 2
	mov	eax, DWORD PTR 4[edi]
	mul	eax			; *a * *a
	mov	DWORD PTR 8[esi],eax
	mov	DWORD PTR 12[esi],edx
	dec	ebx
	jz	$L645
	; Round 3
	mov	eax, DWORD PTR 8[edi]
	mul	eax			; *a * *a
	mov	DWORD PTR 16[esi],eax
	mov	DWORD PTR 20[esi],edx

$L645:
	pop	edi
	pop	esi
	pop	ebx
	ret
_bn_sqr_words ENDP
_TEXT	ENDS
PUBLIC	_bn_div64
_TEXT	SEGMENT
_bn_div64 PROC NEAR
	mov	edx, DWORD PTR 4[esp]
	mov	eax, DWORD PTR 8[esp]
	div	DWORD PTR 12[esp]
	ret
_bn_div64 ENDP
_TEXT	ENDS
END