Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/crypto/bn/asm/x86_64-gcc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "../bn_local.h"
11
#if !(defined(__GNUC__) && __GNUC__>=2)
12
# include "../bn_asm.c"         /* kind of dirty hack for Sun Studio */
13
#else
14
/*-
15
 * x86_64 BIGNUM accelerator version 0.1, December 2002.
16
 *
17
 * Implemented by Andy Polyakov <appro@openssl.org> for the OpenSSL
18
 * project.
19
 *
20
 * Rights for redistribution and usage in source and binary forms are
21
 * granted according to the OpenSSL license. Warranty of any kind is
22
 * disclaimed.
23
 *
24
 * Q. Version 0.1? It doesn't sound like Andy, he used to assign real
25
 *    versions, like 1.0...
26
 * A. Well, that's because this code is basically a quick-n-dirty
27
 *    proof-of-concept hack. As you can see it's implemented with
28
 *    inline assembler, which means that you're bound to GCC and that
29
 *    there might be enough room for further improvement.
30
 *
31
 * Q. Why inline assembler?
32
 * A. x86_64 features own ABI which I'm not familiar with. This is
33
 *    why I decided to let the compiler take care of subroutine
34
 *    prologue/epilogue as well as register allocation. For reference.
35
 *    Win64 implements different ABI for AMD64, different from Linux.
36
 *
37
 * Q. How much faster does it get?
38
 * A. 'apps/openssl speed rsa dsa' output with no-asm:
39
 *
40
 *                        sign    verify    sign/s verify/s
41
 *      rsa  512 bits   0.0006s   0.0001s   1683.8  18456.2
42
 *      rsa 1024 bits   0.0028s   0.0002s    356.0   6407.0
43
 *      rsa 2048 bits   0.0172s   0.0005s     58.0   1957.8
44
 *      rsa 4096 bits   0.1155s   0.0018s      8.7    555.6
45
 *                        sign    verify    sign/s verify/s
46
 *      dsa  512 bits   0.0005s   0.0006s   2100.8   1768.3
47
 *      dsa 1024 bits   0.0014s   0.0018s    692.3    559.2
48
 *      dsa 2048 bits   0.0049s   0.0061s    204.7    165.0
49
 *
50
 *    'apps/openssl speed rsa dsa' output with this module:
51
 *
52
 *                        sign    verify    sign/s verify/s
53
 *      rsa  512 bits   0.0004s   0.0000s   2767.1  33297.9
54
 *      rsa 1024 bits   0.0012s   0.0001s    867.4  14674.7
55
 *      rsa 2048 bits   0.0061s   0.0002s    164.0   5270.0
56
 *      rsa 4096 bits   0.0384s   0.0006s     26.1   1650.8
57
 *                        sign    verify    sign/s verify/s
58
 *      dsa  512 bits   0.0002s   0.0003s   4442.2   3786.3
59
 *      dsa 1024 bits   0.0005s   0.0007s   1835.1   1497.4
60
 *      dsa 2048 bits   0.0016s   0.0020s    620.4    504.6
61
 *
62
 *    For the reference. IA-32 assembler implementation performs
63
 *    very much like 64-bit code compiled with no-asm on the same
64
 *    machine.
65
 */
66
67
# undef mul
68
# undef mul_add
69
70
/*-
71
 * "m"(a), "+m"(r)      is the way to favor DirectPath ยต-code;
72
 * "g"(0)               let the compiler to decide where does it
73
 *                      want to keep the value of zero;
74
 */
75
60.0G
# define mul_add(r,a,word,carry) do {   \
76
60.0G
        register BN_ULONG high,low;     \
77
60.0G
        asm ("mulq %3"                  \
78
60.0G
                : "=a"(low),"=d"(high)  \
79
60.0G
                : "a"(word),"m"(a)      \
80
60.0G
                : "cc");                \
81
60.0G
        asm ("addq %2,%0; adcq %3,%1"   \
82
60.0G
                : "+r"(carry),"+d"(high)\
83
60.0G
                : "a"(low),"g"(0)       \
84
60.0G
                : "cc");                \
85
60.0G
        asm ("addq %2,%0; adcq %3,%1"   \
86
60.0G
                : "+m"(r),"+d"(high)    \
87
60.0G
                : "r"(carry),"g"(0)     \
88
60.0G
                : "cc");                \
89
60.0G
        carry=high;                     \
90
60.0G
        } while (0)
91
92
11.5G
# define mul(r,a,word,carry) do {       \
93
11.5G
        register BN_ULONG high,low;     \
94
11.5G
        asm ("mulq %3"                  \
95
11.5G
                : "=a"(low),"=d"(high)  \
96
11.5G
                : "a"(word),"g"(a)      \
97
11.5G
                : "cc");                \
98
11.5G
        asm ("addq %2,%0; adcq %3,%1"   \
99
11.5G
                : "+r"(carry),"+d"(high)\
100
11.5G
                : "a"(low),"g"(0)       \
101
11.5G
                : "cc");                \
102
11.5G
        (r)=carry, carry=high;          \
103
11.5G
        } while (0)
104
# undef sqr
105
# define sqr(r0,r1,a)                   \
106
36.3M
        asm ("mulq %2"                  \
107
36.3M
                : "=a"(r0),"=d"(r1)     \
108
36.3M
                : "a"(a)                \
109
36.3M
                : "cc");
110
111
BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
112
                          BN_ULONG w)
113
78.1M
{
114
78.1M
    BN_ULONG c1 = 0;
115
116
78.1M
    if (num <= 0)
117
0
        return c1;
118
119
15.0G
    while (num & ~3) {
120
14.9G
        mul_add(rp[0], ap[0], w, c1);
121
14.9G
        mul_add(rp[1], ap[1], w, c1);
122
14.9G
        mul_add(rp[2], ap[2], w, c1);
123
14.9G
        mul_add(rp[3], ap[3], w, c1);
124
14.9G
        ap += 4;
125
14.9G
        rp += 4;
126
14.9G
        num -= 4;
127
14.9G
    }
128
78.1M
    if (num) {
129
48.3M
        mul_add(rp[0], ap[0], w, c1);
130
48.3M
        if (--num == 0)
131
22.8M
            return c1;
132
25.5M
        mul_add(rp[1], ap[1], w, c1);
133
25.5M
        if (--num == 0)
134
12.9M
            return c1;
135
12.5M
        mul_add(rp[2], ap[2], w, c1);
136
12.5M
        return c1;
137
25.5M
    }
138
139
29.7M
    return c1;
140
78.1M
}
141
142
BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
143
251M
{
144
251M
    BN_ULONG c1 = 0;
145
146
251M
    if (num <= 0)
147
0
        return c1;
148
149
3.12G
    while (num & ~3) {
150
2.87G
        mul(rp[0], ap[0], w, c1);
151
2.87G
        mul(rp[1], ap[1], w, c1);
152
2.87G
        mul(rp[2], ap[2], w, c1);
153
2.87G
        mul(rp[3], ap[3], w, c1);
154
2.87G
        ap += 4;
155
2.87G
        rp += 4;
156
2.87G
        num -= 4;
157
2.87G
    }
158
251M
    if (num) {
159
33.7M
        mul(rp[0], ap[0], w, c1);
160
33.7M
        if (--num == 0)
161
20.0M
            return c1;
162
13.7M
        mul(rp[1], ap[1], w, c1);
163
13.7M
        if (--num == 0)
164
8.25M
            return c1;
165
5.50M
        mul(rp[2], ap[2], w, c1);
166
5.50M
    }
167
223M
    return c1;
168
251M
}
169
170
void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
171
5.82M
{
172
5.82M
    if (n <= 0)
173
0
        return;
174
175
13.2M
    while (n & ~3) {
176
7.40M
        sqr(r[0], r[1], a[0]);
177
7.40M
        sqr(r[2], r[3], a[1]);
178
7.40M
        sqr(r[4], r[5], a[2]);
179
7.40M
        sqr(r[6], r[7], a[3]);
180
7.40M
        a += 4;
181
7.40M
        r += 8;
182
7.40M
        n -= 4;
183
7.40M
    }
184
5.82M
    if (n) {
185
5.79M
        sqr(r[0], r[1], a[0]);
186
5.79M
        if (--n == 0)
187
4.96M
            return;
188
828k
        sqr(r[2], r[3], a[1]);
189
828k
        if (--n == 0)
190
734k
            return;
191
93.4k
        sqr(r[4], r[5], a[2]);
192
93.4k
    }
193
5.82M
}
194
195
BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
196
230M
{
197
230M
    BN_ULONG ret, waste;
198
199
230M
 asm("divq      %4":"=a"(ret), "=d"(waste)
200
230M
 :     "a"(l), "d"(h), "r"(d)
201
230M
 :     "cc");
202
203
230M
    return ret;
204
230M
}
205
206
BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
207
                      int n)
208
405M
{
209
405M
    BN_ULONG ret;
210
405M
    size_t i = 0;
211
212
405M
    if (n <= 0)
213
3.56M
        return 0;
214
215
402M
    asm volatile ("       subq    %0,%0           \n" /* clear carry */
216
402M
                  "       jmp     1f              \n"
217
402M
                  ".p2align 4                     \n"
218
402M
                  "1:     movq    (%4,%2,8),%0    \n"
219
402M
                  "       adcq    (%5,%2,8),%0    \n"
220
402M
                  "       movq    %0,(%3,%2,8)    \n"
221
402M
                  "       lea     1(%2),%2        \n"
222
402M
                  "       dec     %1              \n"
223
402M
                  "       jnz     1b              \n"
224
402M
                  "       sbbq    %0,%0           \n"
225
402M
                  :"=&r" (ret), "+c"(n), "+r"(i)
226
402M
                  :"r"(rp), "r"(ap), "r"(bp)
227
402M
                  :"cc", "memory");
228
229
402M
    return ret & 1;
230
405M
}
231
232
# ifndef SIMICS
233
BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
234
                      int n)
235
396M
{
236
396M
    BN_ULONG ret;
237
396M
    size_t i = 0;
238
239
396M
    if (n <= 0)
240
4.21M
        return 0;
241
242
392M
    asm volatile ("       subq    %0,%0           \n" /* clear borrow */
243
392M
                  "       jmp     1f              \n"
244
392M
                  ".p2align 4                     \n"
245
392M
                  "1:     movq    (%4,%2,8),%0    \n"
246
392M
                  "       sbbq    (%5,%2,8),%0    \n"
247
392M
                  "       movq    %0,(%3,%2,8)    \n"
248
392M
                  "       lea     1(%2),%2        \n"
249
392M
                  "       dec     %1              \n"
250
392M
                  "       jnz     1b              \n"
251
392M
                  "       sbbq    %0,%0           \n"
252
392M
                  :"=&r" (ret), "+c"(n), "+r"(i)
253
392M
                  :"r"(rp), "r"(ap), "r"(bp)
254
392M
                  :"cc", "memory");
255
256
392M
    return ret & 1;
257
396M
}
258
# else
259
/* Simics 1.4<7 has buggy sbbq:-( */
260
#  define BN_MASK2 0xffffffffffffffffL
261
BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
262
{
263
    BN_ULONG t1, t2;
264
    int c = 0;
265
266
    if (n <= 0)
267
        return (BN_ULONG)0;
268
269
    for (;;) {
270
        t1 = a[0];
271
        t2 = b[0];
272
        r[0] = (t1 - t2 - c) & BN_MASK2;
273
        if (t1 != t2)
274
            c = (t1 < t2);
275
        if (--n <= 0)
276
            break;
277
278
        t1 = a[1];
279
        t2 = b[1];
280
        r[1] = (t1 - t2 - c) & BN_MASK2;
281
        if (t1 != t2)
282
            c = (t1 < t2);
283
        if (--n <= 0)
284
            break;
285
286
        t1 = a[2];
287
        t2 = b[2];
288
        r[2] = (t1 - t2 - c) & BN_MASK2;
289
        if (t1 != t2)
290
            c = (t1 < t2);
291
        if (--n <= 0)
292
            break;
293
294
        t1 = a[3];
295
        t2 = b[3];
296
        r[3] = (t1 - t2 - c) & BN_MASK2;
297
        if (t1 != t2)
298
            c = (t1 < t2);
299
        if (--n <= 0)
300
            break;
301
302
        a += 4;
303
        b += 4;
304
        r += 4;
305
    }
306
    return c;
307
}
308
# endif
309
310
/* mul_add_c(a,b,c0,c1,c2)  -- c+=a*b for three word number c=(c2,c1,c0) */
311
/* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
312
/* sqr_add_c(a,i,c0,c1,c2)  -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
313
/*
314
 * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
315
 * c=(c2,c1,c0)
316
 */
317
318
/*
319
 * Keep in mind that carrying into high part of multiplication result
320
 * can not overflow, because it cannot be all-ones.
321
 */
322
# if 0
323
/* original macros are kept for reference purposes */
324
#  define mul_add_c(a,b,c0,c1,c2)       do {    \
325
        BN_ULONG ta = (a), tb = (b);            \
326
        BN_ULONG lo, hi;                        \
327
        BN_UMULT_LOHI(lo,hi,ta,tb);             \
328
        c0 += lo; hi += (c0<lo)?1:0;            \
329
        c1 += hi; c2 += (c1<hi)?1:0;            \
330
        } while(0)
331
332
#  define mul_add_c2(a,b,c0,c1,c2)      do {    \
333
        BN_ULONG ta = (a), tb = (b);            \
334
        BN_ULONG lo, hi, tt;                    \
335
        BN_UMULT_LOHI(lo,hi,ta,tb);             \
336
        c0 += lo; tt = hi+((c0<lo)?1:0);        \
337
        c1 += tt; c2 += (c1<tt)?1:0;            \
338
        c0 += lo; hi += (c0<lo)?1:0;            \
339
        c1 += hi; c2 += (c1<hi)?1:0;            \
340
        } while(0)
341
342
#  define sqr_add_c(a,i,c0,c1,c2)       do {    \
343
        BN_ULONG ta = (a)[i];                   \
344
        BN_ULONG lo, hi;                        \
345
        BN_UMULT_LOHI(lo,hi,ta,ta);             \
346
        c0 += lo; hi += (c0<lo)?1:0;            \
347
        c1 += hi; c2 += (c1<hi)?1:0;            \
348
        } while(0)
349
# else
350
4.78G
#  define mul_add_c(a,b,c0,c1,c2) do {  \
351
4.78G
        BN_ULONG t1,t2;                 \
352
4.78G
        asm ("mulq %3"                  \
353
4.78G
                : "=a"(t1),"=d"(t2)     \
354
4.78G
                : "a"(a),"m"(b)         \
355
4.78G
                : "cc");                \
356
4.78G
        asm ("addq %3,%0; adcq %4,%1; adcq %5,%2"       \
357
4.78G
                : "+r"(c0),"+r"(c1),"+r"(c2)            \
358
4.78G
                : "r"(t1),"r"(t2),"g"(0)                \
359
4.78G
                : "cc");                                \
360
4.78G
        } while (0)
361
362
261M
#  define sqr_add_c(a,i,c0,c1,c2) do {  \
363
261M
        BN_ULONG t1,t2;                 \
364
261M
        asm ("mulq %2"                  \
365
261M
                : "=a"(t1),"=d"(t2)     \
366
261M
                : "a"(a[i])             \
367
261M
                : "cc");                \
368
261M
        asm ("addq %3,%0; adcq %4,%1; adcq %5,%2"       \
369
261M
                : "+r"(c0),"+r"(c1),"+r"(c2)            \
370
261M
                : "r"(t1),"r"(t2),"g"(0)                \
371
261M
                : "cc");                                \
372
261M
        } while (0)
373
374
528M
#  define mul_add_c2(a,b,c0,c1,c2) do { \
375
528M
        BN_ULONG t1,t2;                 \
376
528M
        asm ("mulq %3"                  \
377
528M
                : "=a"(t1),"=d"(t2)     \
378
528M
                : "a"(a),"m"(b)         \
379
528M
                : "cc");                \
380
528M
        asm ("addq %3,%0; adcq %4,%1; adcq %5,%2"       \
381
528M
                : "+r"(c0),"+r"(c1),"+r"(c2)            \
382
528M
                : "r"(t1),"r"(t2),"g"(0)                \
383
528M
                : "cc");                                \
384
528M
        asm ("addq %3,%0; adcq %4,%1; adcq %5,%2"       \
385
528M
                : "+r"(c0),"+r"(c1),"+r"(c2)            \
386
528M
                : "r"(t1),"r"(t2),"g"(0)                \
387
528M
                : "cc");                                \
388
528M
        } while (0)
389
# endif
390
391
# define sqr_add_c2(a,i,j,c0,c1,c2)      \
392
528M
        mul_add_c2((a)[i],(a)[j],c0,c1,c2)
393
394
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
395
74.7M
{
396
74.7M
    BN_ULONG c1, c2, c3;
397
398
74.7M
    c1 = 0;
399
74.7M
    c2 = 0;
400
74.7M
    c3 = 0;
401
74.7M
    mul_add_c(a[0], b[0], c1, c2, c3);
402
74.7M
    r[0] = c1;
403
74.7M
    c1 = 0;
404
74.7M
    mul_add_c(a[0], b[1], c2, c3, c1);
405
74.7M
    mul_add_c(a[1], b[0], c2, c3, c1);
406
74.7M
    r[1] = c2;
407
74.7M
    c2 = 0;
408
74.7M
    mul_add_c(a[2], b[0], c3, c1, c2);
409
74.7M
    mul_add_c(a[1], b[1], c3, c1, c2);
410
74.7M
    mul_add_c(a[0], b[2], c3, c1, c2);
411
74.7M
    r[2] = c3;
412
74.7M
    c3 = 0;
413
74.7M
    mul_add_c(a[0], b[3], c1, c2, c3);
414
74.7M
    mul_add_c(a[1], b[2], c1, c2, c3);
415
74.7M
    mul_add_c(a[2], b[1], c1, c2, c3);
416
74.7M
    mul_add_c(a[3], b[0], c1, c2, c3);
417
74.7M
    r[3] = c1;
418
74.7M
    c1 = 0;
419
74.7M
    mul_add_c(a[4], b[0], c2, c3, c1);
420
74.7M
    mul_add_c(a[3], b[1], c2, c3, c1);
421
74.7M
    mul_add_c(a[2], b[2], c2, c3, c1);
422
74.7M
    mul_add_c(a[1], b[3], c2, c3, c1);
423
74.7M
    mul_add_c(a[0], b[4], c2, c3, c1);
424
74.7M
    r[4] = c2;
425
74.7M
    c2 = 0;
426
74.7M
    mul_add_c(a[0], b[5], c3, c1, c2);
427
74.7M
    mul_add_c(a[1], b[4], c3, c1, c2);
428
74.7M
    mul_add_c(a[2], b[3], c3, c1, c2);
429
74.7M
    mul_add_c(a[3], b[2], c3, c1, c2);
430
74.7M
    mul_add_c(a[4], b[1], c3, c1, c2);
431
74.7M
    mul_add_c(a[5], b[0], c3, c1, c2);
432
74.7M
    r[5] = c3;
433
74.7M
    c3 = 0;
434
74.7M
    mul_add_c(a[6], b[0], c1, c2, c3);
435
74.7M
    mul_add_c(a[5], b[1], c1, c2, c3);
436
74.7M
    mul_add_c(a[4], b[2], c1, c2, c3);
437
74.7M
    mul_add_c(a[3], b[3], c1, c2, c3);
438
74.7M
    mul_add_c(a[2], b[4], c1, c2, c3);
439
74.7M
    mul_add_c(a[1], b[5], c1, c2, c3);
440
74.7M
    mul_add_c(a[0], b[6], c1, c2, c3);
441
74.7M
    r[6] = c1;
442
74.7M
    c1 = 0;
443
74.7M
    mul_add_c(a[0], b[7], c2, c3, c1);
444
74.7M
    mul_add_c(a[1], b[6], c2, c3, c1);
445
74.7M
    mul_add_c(a[2], b[5], c2, c3, c1);
446
74.7M
    mul_add_c(a[3], b[4], c2, c3, c1);
447
74.7M
    mul_add_c(a[4], b[3], c2, c3, c1);
448
74.7M
    mul_add_c(a[5], b[2], c2, c3, c1);
449
74.7M
    mul_add_c(a[6], b[1], c2, c3, c1);
450
74.7M
    mul_add_c(a[7], b[0], c2, c3, c1);
451
74.7M
    r[7] = c2;
452
74.7M
    c2 = 0;
453
74.7M
    mul_add_c(a[7], b[1], c3, c1, c2);
454
74.7M
    mul_add_c(a[6], b[2], c3, c1, c2);
455
74.7M
    mul_add_c(a[5], b[3], c3, c1, c2);
456
74.7M
    mul_add_c(a[4], b[4], c3, c1, c2);
457
74.7M
    mul_add_c(a[3], b[5], c3, c1, c2);
458
74.7M
    mul_add_c(a[2], b[6], c3, c1, c2);
459
74.7M
    mul_add_c(a[1], b[7], c3, c1, c2);
460
74.7M
    r[8] = c3;
461
74.7M
    c3 = 0;
462
74.7M
    mul_add_c(a[2], b[7], c1, c2, c3);
463
74.7M
    mul_add_c(a[3], b[6], c1, c2, c3);
464
74.7M
    mul_add_c(a[4], b[5], c1, c2, c3);
465
74.7M
    mul_add_c(a[5], b[4], c1, c2, c3);
466
74.7M
    mul_add_c(a[6], b[3], c1, c2, c3);
467
74.7M
    mul_add_c(a[7], b[2], c1, c2, c3);
468
74.7M
    r[9] = c1;
469
74.7M
    c1 = 0;
470
74.7M
    mul_add_c(a[7], b[3], c2, c3, c1);
471
74.7M
    mul_add_c(a[6], b[4], c2, c3, c1);
472
74.7M
    mul_add_c(a[5], b[5], c2, c3, c1);
473
74.7M
    mul_add_c(a[4], b[6], c2, c3, c1);
474
74.7M
    mul_add_c(a[3], b[7], c2, c3, c1);
475
74.7M
    r[10] = c2;
476
74.7M
    c2 = 0;
477
74.7M
    mul_add_c(a[4], b[7], c3, c1, c2);
478
74.7M
    mul_add_c(a[5], b[6], c3, c1, c2);
479
74.7M
    mul_add_c(a[6], b[5], c3, c1, c2);
480
74.7M
    mul_add_c(a[7], b[4], c3, c1, c2);
481
74.7M
    r[11] = c3;
482
74.7M
    c3 = 0;
483
74.7M
    mul_add_c(a[7], b[5], c1, c2, c3);
484
74.7M
    mul_add_c(a[6], b[6], c1, c2, c3);
485
74.7M
    mul_add_c(a[5], b[7], c1, c2, c3);
486
74.7M
    r[12] = c1;
487
74.7M
    c1 = 0;
488
74.7M
    mul_add_c(a[6], b[7], c2, c3, c1);
489
74.7M
    mul_add_c(a[7], b[6], c2, c3, c1);
490
74.7M
    r[13] = c2;
491
74.7M
    c2 = 0;
492
74.7M
    mul_add_c(a[7], b[7], c3, c1, c2);
493
74.7M
    r[14] = c3;
494
74.7M
    r[15] = c1;
495
74.7M
}
496
497
void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
498
0
{
499
0
    BN_ULONG c1, c2, c3;
500
501
0
    c1 = 0;
502
0
    c2 = 0;
503
0
    c3 = 0;
504
0
    mul_add_c(a[0], b[0], c1, c2, c3);
505
0
    r[0] = c1;
506
0
    c1 = 0;
507
0
    mul_add_c(a[0], b[1], c2, c3, c1);
508
0
    mul_add_c(a[1], b[0], c2, c3, c1);
509
0
    r[1] = c2;
510
0
    c2 = 0;
511
0
    mul_add_c(a[2], b[0], c3, c1, c2);
512
0
    mul_add_c(a[1], b[1], c3, c1, c2);
513
0
    mul_add_c(a[0], b[2], c3, c1, c2);
514
0
    r[2] = c3;
515
0
    c3 = 0;
516
0
    mul_add_c(a[0], b[3], c1, c2, c3);
517
0
    mul_add_c(a[1], b[2], c1, c2, c3);
518
0
    mul_add_c(a[2], b[1], c1, c2, c3);
519
0
    mul_add_c(a[3], b[0], c1, c2, c3);
520
0
    r[3] = c1;
521
0
    c1 = 0;
522
0
    mul_add_c(a[3], b[1], c2, c3, c1);
523
0
    mul_add_c(a[2], b[2], c2, c3, c1);
524
0
    mul_add_c(a[1], b[3], c2, c3, c1);
525
0
    r[4] = c2;
526
0
    c2 = 0;
527
0
    mul_add_c(a[2], b[3], c3, c1, c2);
528
0
    mul_add_c(a[3], b[2], c3, c1, c2);
529
0
    r[5] = c3;
530
0
    c3 = 0;
531
0
    mul_add_c(a[3], b[3], c1, c2, c3);
532
0
    r[6] = c1;
533
0
    r[7] = c2;
534
0
}
535
536
void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
537
8.56M
{
538
8.56M
    BN_ULONG c1, c2, c3;
539
540
8.56M
    c1 = 0;
541
8.56M
    c2 = 0;
542
8.56M
    c3 = 0;
543
8.56M
    sqr_add_c(a, 0, c1, c2, c3);
544
8.56M
    r[0] = c1;
545
8.56M
    c1 = 0;
546
8.56M
    sqr_add_c2(a, 1, 0, c2, c3, c1);
547
8.56M
    r[1] = c2;
548
8.56M
    c2 = 0;
549
8.56M
    sqr_add_c(a, 1, c3, c1, c2);
550
8.56M
    sqr_add_c2(a, 2, 0, c3, c1, c2);
551
8.56M
    r[2] = c3;
552
8.56M
    c3 = 0;
553
8.56M
    sqr_add_c2(a, 3, 0, c1, c2, c3);
554
8.56M
    sqr_add_c2(a, 2, 1, c1, c2, c3);
555
8.56M
    r[3] = c1;
556
8.56M
    c1 = 0;
557
8.56M
    sqr_add_c(a, 2, c2, c3, c1);
558
8.56M
    sqr_add_c2(a, 3, 1, c2, c3, c1);
559
8.56M
    sqr_add_c2(a, 4, 0, c2, c3, c1);
560
8.56M
    r[4] = c2;
561
8.56M
    c2 = 0;
562
8.56M
    sqr_add_c2(a, 5, 0, c3, c1, c2);
563
8.56M
    sqr_add_c2(a, 4, 1, c3, c1, c2);
564
8.56M
    sqr_add_c2(a, 3, 2, c3, c1, c2);
565
8.56M
    r[5] = c3;
566
8.56M
    c3 = 0;
567
8.56M
    sqr_add_c(a, 3, c1, c2, c3);
568
8.56M
    sqr_add_c2(a, 4, 2, c1, c2, c3);
569
8.56M
    sqr_add_c2(a, 5, 1, c1, c2, c3);
570
8.56M
    sqr_add_c2(a, 6, 0, c1, c2, c3);
571
8.56M
    r[6] = c1;
572
8.56M
    c1 = 0;
573
8.56M
    sqr_add_c2(a, 7, 0, c2, c3, c1);
574
8.56M
    sqr_add_c2(a, 6, 1, c2, c3, c1);
575
8.56M
    sqr_add_c2(a, 5, 2, c2, c3, c1);
576
8.56M
    sqr_add_c2(a, 4, 3, c2, c3, c1);
577
8.56M
    r[7] = c2;
578
8.56M
    c2 = 0;
579
8.56M
    sqr_add_c(a, 4, c3, c1, c2);
580
8.56M
    sqr_add_c2(a, 5, 3, c3, c1, c2);
581
8.56M
    sqr_add_c2(a, 6, 2, c3, c1, c2);
582
8.56M
    sqr_add_c2(a, 7, 1, c3, c1, c2);
583
8.56M
    r[8] = c3;
584
8.56M
    c3 = 0;
585
8.56M
    sqr_add_c2(a, 7, 2, c1, c2, c3);
586
8.56M
    sqr_add_c2(a, 6, 3, c1, c2, c3);
587
8.56M
    sqr_add_c2(a, 5, 4, c1, c2, c3);
588
8.56M
    r[9] = c1;
589
8.56M
    c1 = 0;
590
8.56M
    sqr_add_c(a, 5, c2, c3, c1);
591
8.56M
    sqr_add_c2(a, 6, 4, c2, c3, c1);
592
8.56M
    sqr_add_c2(a, 7, 3, c2, c3, c1);
593
8.56M
    r[10] = c2;
594
8.56M
    c2 = 0;
595
8.56M
    sqr_add_c2(a, 7, 4, c3, c1, c2);
596
8.56M
    sqr_add_c2(a, 6, 5, c3, c1, c2);
597
8.56M
    r[11] = c3;
598
8.56M
    c3 = 0;
599
8.56M
    sqr_add_c(a, 6, c1, c2, c3);
600
8.56M
    sqr_add_c2(a, 7, 5, c1, c2, c3);
601
8.56M
    r[12] = c1;
602
8.56M
    c1 = 0;
603
8.56M
    sqr_add_c2(a, 7, 6, c2, c3, c1);
604
8.56M
    r[13] = c2;
605
8.56M
    c2 = 0;
606
8.56M
    sqr_add_c(a, 7, c3, c1, c2);
607
8.56M
    r[14] = c3;
608
8.56M
    r[15] = c1;
609
8.56M
}
610
611
void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
612
48.1M
{
613
48.1M
    BN_ULONG c1, c2, c3;
614
615
48.1M
    c1 = 0;
616
48.1M
    c2 = 0;
617
48.1M
    c3 = 0;
618
48.1M
    sqr_add_c(a, 0, c1, c2, c3);
619
48.1M
    r[0] = c1;
620
48.1M
    c1 = 0;
621
48.1M
    sqr_add_c2(a, 1, 0, c2, c3, c1);
622
48.1M
    r[1] = c2;
623
48.1M
    c2 = 0;
624
48.1M
    sqr_add_c(a, 1, c3, c1, c2);
625
48.1M
    sqr_add_c2(a, 2, 0, c3, c1, c2);
626
48.1M
    r[2] = c3;
627
48.1M
    c3 = 0;
628
48.1M
    sqr_add_c2(a, 3, 0, c1, c2, c3);
629
48.1M
    sqr_add_c2(a, 2, 1, c1, c2, c3);
630
48.1M
    r[3] = c1;
631
48.1M
    c1 = 0;
632
48.1M
    sqr_add_c(a, 2, c2, c3, c1);
633
48.1M
    sqr_add_c2(a, 3, 1, c2, c3, c1);
634
48.1M
    r[4] = c2;
635
48.1M
    c2 = 0;
636
48.1M
    sqr_add_c2(a, 3, 2, c3, c1, c2);
637
48.1M
    r[5] = c3;
638
48.1M
    c3 = 0;
639
48.1M
    sqr_add_c(a, 3, c1, c2, c3);
640
48.1M
    r[6] = c1;
641
48.1M
    r[7] = c2;
642
48.1M
}
643
#endif