Coverage Report

Created: 2022-09-28 06:21

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