Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_gf2m.c
Line
Count
Source
1
/*
2
 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <assert.h>
12
#include <limits.h>
13
#include <stdio.h>
14
#include "internal/cryptlib.h"
15
#include "bn_local.h"
16
17
#ifndef OPENSSL_NO_EC2M
18
#include <openssl/ec.h>
19
20
/*
21
 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
22
 * fail.
23
 */
24
169k
#define MAX_ITERATIONS 50
25
26
15.5G
#define SQR_nibble(w) ((((w) & 8) << 3) \
27
15.5G
    | (((w) & 4) << 2)                  \
28
15.5G
    | (((w) & 2) << 1)                  \
29
15.5G
    | ((w) & 1))
30
31
/* Platform-specific macros to accelerate squaring. */
32
#if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
33
#define SQR1(w) \
34
970M
    SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | SQR_nibble((w) >> 36) << 8 | SQR_nibble((w) >> 32)
35
#define SQR0(w) \
36
970M
    SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | SQR_nibble((w) >> 4) << 8 | SQR_nibble((w))
37
#endif
38
#ifdef THIRTY_TWO_BIT
39
#define SQR1(w) \
40
    SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | SQR_nibble((w) >> 20) << 8 | SQR_nibble((w) >> 16)
41
#define SQR0(w) \
42
    SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | SQR_nibble((w) >> 4) << 8 | SQR_nibble((w))
43
#endif
44
45
#if !defined(OPENSSL_BN_ASM_GF2m)
46
/*
47
 * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
48
 * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
49
 * the variables have the right amount of space allocated.
50
 */
51
#ifdef THIRTY_TWO_BIT
52
static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
53
    const BN_ULONG b)
54
{
55
    register BN_ULONG h, l, s;
56
    BN_ULONG tab[8], top2b = a >> 30;
57
    register BN_ULONG a1, a2, a4;
58
59
    a1 = a & (0x3FFFFFFF);
60
    a2 = a1 << 1;
61
    a4 = a2 << 1;
62
63
    tab[0] = 0;
64
    tab[1] = a1;
65
    tab[2] = a2;
66
    tab[3] = a1 ^ a2;
67
    tab[4] = a4;
68
    tab[5] = a1 ^ a4;
69
    tab[6] = a2 ^ a4;
70
    tab[7] = a1 ^ a2 ^ a4;
71
72
    s = tab[b & 0x7];
73
    l = s;
74
    s = tab[b >> 3 & 0x7];
75
    l ^= s << 3;
76
    h = s >> 29;
77
    s = tab[b >> 6 & 0x7];
78
    l ^= s << 6;
79
    h ^= s >> 26;
80
    s = tab[b >> 9 & 0x7];
81
    l ^= s << 9;
82
    h ^= s >> 23;
83
    s = tab[b >> 12 & 0x7];
84
    l ^= s << 12;
85
    h ^= s >> 20;
86
    s = tab[b >> 15 & 0x7];
87
    l ^= s << 15;
88
    h ^= s >> 17;
89
    s = tab[b >> 18 & 0x7];
90
    l ^= s << 18;
91
    h ^= s >> 14;
92
    s = tab[b >> 21 & 0x7];
93
    l ^= s << 21;
94
    h ^= s >> 11;
95
    s = tab[b >> 24 & 0x7];
96
    l ^= s << 24;
97
    h ^= s >> 8;
98
    s = tab[b >> 27 & 0x7];
99
    l ^= s << 27;
100
    h ^= s >> 5;
101
    s = tab[b >> 30];
102
    l ^= s << 30;
103
    h ^= s >> 2;
104
105
    /* compensate for the top two bits of a */
106
107
    if (top2b & 01) {
108
        l ^= b << 30;
109
        h ^= b >> 2;
110
    }
111
    if (top2b & 02) {
112
        l ^= b << 31;
113
        h ^= b >> 1;
114
    }
115
116
    *r1 = h;
117
    *r0 = l;
118
}
119
#endif
120
#if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
121
static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
122
    const BN_ULONG b)
123
{
124
    register BN_ULONG h, l, s;
125
    BN_ULONG tab[16], top3b = a >> 61;
126
    register BN_ULONG a1, a2, a4, a8;
127
128
    a1 = a & (0x1FFFFFFFFFFFFFFFULL);
129
    a2 = a1 << 1;
130
    a4 = a2 << 1;
131
    a8 = a4 << 1;
132
133
    tab[0] = 0;
134
    tab[1] = a1;
135
    tab[2] = a2;
136
    tab[3] = a1 ^ a2;
137
    tab[4] = a4;
138
    tab[5] = a1 ^ a4;
139
    tab[6] = a2 ^ a4;
140
    tab[7] = a1 ^ a2 ^ a4;
141
    tab[8] = a8;
142
    tab[9] = a1 ^ a8;
143
    tab[10] = a2 ^ a8;
144
    tab[11] = a1 ^ a2 ^ a8;
145
    tab[12] = a4 ^ a8;
146
    tab[13] = a1 ^ a4 ^ a8;
147
    tab[14] = a2 ^ a4 ^ a8;
148
    tab[15] = a1 ^ a2 ^ a4 ^ a8;
149
150
    s = tab[b & 0xF];
151
    l = s;
152
    s = tab[b >> 4 & 0xF];
153
    l ^= s << 4;
154
    h = s >> 60;
155
    s = tab[b >> 8 & 0xF];
156
    l ^= s << 8;
157
    h ^= s >> 56;
158
    s = tab[b >> 12 & 0xF];
159
    l ^= s << 12;
160
    h ^= s >> 52;
161
    s = tab[b >> 16 & 0xF];
162
    l ^= s << 16;
163
    h ^= s >> 48;
164
    s = tab[b >> 20 & 0xF];
165
    l ^= s << 20;
166
    h ^= s >> 44;
167
    s = tab[b >> 24 & 0xF];
168
    l ^= s << 24;
169
    h ^= s >> 40;
170
    s = tab[b >> 28 & 0xF];
171
    l ^= s << 28;
172
    h ^= s >> 36;
173
    s = tab[b >> 32 & 0xF];
174
    l ^= s << 32;
175
    h ^= s >> 32;
176
    s = tab[b >> 36 & 0xF];
177
    l ^= s << 36;
178
    h ^= s >> 28;
179
    s = tab[b >> 40 & 0xF];
180
    l ^= s << 40;
181
    h ^= s >> 24;
182
    s = tab[b >> 44 & 0xF];
183
    l ^= s << 44;
184
    h ^= s >> 20;
185
    s = tab[b >> 48 & 0xF];
186
    l ^= s << 48;
187
    h ^= s >> 16;
188
    s = tab[b >> 52 & 0xF];
189
    l ^= s << 52;
190
    h ^= s >> 12;
191
    s = tab[b >> 56 & 0xF];
192
    l ^= s << 56;
193
    h ^= s >> 8;
194
    s = tab[b >> 60];
195
    l ^= s << 60;
196
    h ^= s >> 4;
197
198
    /* compensate for the top three bits of a */
199
200
    if (top3b & 01) {
201
        l ^= b << 61;
202
        h ^= b >> 3;
203
    }
204
    if (top3b & 02) {
205
        l ^= b << 62;
206
        h ^= b >> 2;
207
    }
208
    if (top3b & 04) {
209
        l ^= b << 63;
210
        h ^= b >> 1;
211
    }
212
213
    *r1 = h;
214
    *r0 = l;
215
}
216
#endif
217
218
/*
219
 * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
220
 * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
221
 * ensure that the variables have the right amount of space allocated.
222
 */
223
static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
224
    const BN_ULONG b1, const BN_ULONG b0)
225
{
226
    BN_ULONG m1, m0;
227
    /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
228
    bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
229
    bn_GF2m_mul_1x1(r + 1, r, a0, b0);
230
    bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
231
    /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
232
    r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
233
    r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
234
}
235
#else
236
void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
237
    BN_ULONG b0);
238
#endif
239
240
/*
241
 * Add polynomials a and b and store result in r; r could be a or b, a and b
242
 * could be equal; r is the bitwise XOR of a and b.
243
 */
244
int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
245
161M
{
246
161M
    int i;
247
161M
    const BIGNUM *at, *bt;
248
249
161M
    bn_check_top(a);
250
161M
    bn_check_top(b);
251
252
161M
    if (a->top < b->top) {
253
1.24M
        at = b;
254
1.24M
        bt = a;
255
159M
    } else {
256
159M
        at = a;
257
159M
        bt = b;
258
159M
    }
259
260
161M
    if (bn_wexpand(r, at->top) == NULL)
261
0
        return 0;
262
263
1.06G
    for (i = 0; i < bt->top; i++) {
264
904M
        r->d[i] = at->d[i] ^ bt->d[i];
265
904M
    }
266
164M
    for (; i < at->top; i++) {
267
3.75M
        r->d[i] = at->d[i];
268
3.75M
    }
269
270
161M
    r->top = at->top;
271
161M
    bn_correct_top(r);
272
273
161M
    return 1;
274
161M
}
275
276
/*-
277
 * Some functions allow for representation of the irreducible polynomials
278
 * as an int[], say p.  The irreducible f(t) is then of the form:
279
 *     t^p[0] + t^p[1] + ... + t^p[k]
280
 * where m = p[0] > p[1] > ... > p[k] = 0.
281
 */
282
283
/* Performs modular reduction of a and store result in r.  r could be a. */
284
int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
285
173M
{
286
173M
    int j, k;
287
173M
    int n, dN, d0, d1;
288
173M
    BN_ULONG zz, *z;
289
290
173M
    bn_check_top(a);
291
292
173M
    if (p[0] == 0) {
293
        /* reduction mod 1 => return 0 */
294
0
        BN_zero(r);
295
0
        return 1;
296
0
    }
297
298
    /*
299
     * Since the algorithm does reduction in the r value, if a != r, copy the
300
     * contents of a into r so we can do reduction in r.
301
     */
302
173M
    if (a != r) {
303
173M
        if (!bn_wexpand(r, a->top))
304
0
            return 0;
305
2.04G
        for (j = 0; j < a->top; j++) {
306
1.86G
            r->d[j] = a->d[j];
307
1.86G
        }
308
173M
        r->top = a->top;
309
173M
    }
310
173M
    z = r->d;
311
312
    /* start reduction */
313
173M
    dN = p[0] / BN_BITS2;
314
1.99G
    for (j = r->top - 1; j > dN;) {
315
1.81G
        zz = z[j];
316
1.81G
        if (z[j] == 0) {
317
908M
            j--;
318
908M
            continue;
319
908M
        }
320
908M
        z[j] = 0;
321
322
3.52G
        for (k = 1; p[k] != 0; k++) {
323
            /* reducing component t^p[k] */
324
2.61G
            n = p[0] - p[k];
325
2.61G
            d0 = n % BN_BITS2;
326
2.61G
            d1 = BN_BITS2 - d0;
327
2.61G
            n /= BN_BITS2;
328
2.61G
            z[j - n] ^= (zz >> d0);
329
2.61G
            if (d0)
330
2.60G
                z[j - n - 1] ^= (zz << d1);
331
2.61G
        }
332
333
        /* reducing component t^0 */
334
908M
        n = dN;
335
908M
        d0 = p[0] % BN_BITS2;
336
908M
        d1 = BN_BITS2 - d0;
337
908M
        z[j - n] ^= (zz >> d0);
338
908M
        if (d0)
339
908M
            z[j - n - 1] ^= (zz << d1);
340
908M
    }
341
342
    /* final round of reduction */
343
343M
    while (j == dN) {
344
345
343M
        d0 = p[0] % BN_BITS2;
346
343M
        zz = z[dN] >> d0;
347
343M
        if (zz == 0)
348
172M
            break;
349
170M
        d1 = BN_BITS2 - d0;
350
351
        /* clear up the top d1 bits */
352
170M
        if (d0)
353
170M
            z[dN] = (z[dN] << d1) >> d1;
354
0
        else
355
0
            z[dN] = 0;
356
170M
        z[0] ^= zz; /* reduction t^0 component */
357
358
656M
        for (k = 1; p[k] != 0; k++) {
359
486M
            BN_ULONG tmp_ulong;
360
361
            /* reducing component t^p[k] */
362
486M
            n = p[k] / BN_BITS2;
363
486M
            d0 = p[k] % BN_BITS2;
364
486M
            d1 = BN_BITS2 - d0;
365
486M
            z[n] ^= (zz << d0);
366
486M
            if (d0 && (tmp_ulong = zz >> d1))
367
36.7M
                z[n + 1] ^= tmp_ulong;
368
486M
        }
369
170M
    }
370
371
173M
    bn_correct_top(r);
372
173M
    return 1;
373
173M
}
374
375
/*
376
 * Performs modular reduction of a by p and store result in r.  r could be a.
377
 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
378
 * function is only provided for convenience; for best performance, use the
379
 * BN_GF2m_mod_arr function.
380
 */
381
int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
382
181k
{
383
181k
    int ret = 0;
384
181k
    int arr[6];
385
181k
    bn_check_top(a);
386
181k
    bn_check_top(p);
387
181k
    ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
388
181k
    if (!ret || ret > (int)OSSL_NELEM(arr)) {
389
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
390
0
        return 0;
391
0
    }
392
181k
    ret = BN_GF2m_mod_arr(r, a, arr);
393
181k
    bn_check_top(r);
394
181k
    return ret;
395
181k
}
396
397
/*
398
 * Compute the product of two polynomials a and b, reduce modulo p, and store
399
 * the result in r.  r could be a or b; a could be b.
400
 */
401
int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
402
    const int p[], BN_CTX *ctx)
403
89.1M
{
404
89.1M
    int zlen, i, j, k, ret = 0;
405
89.1M
    BIGNUM *s;
406
89.1M
    BN_ULONG x1, x0, y1, y0, zz[4];
407
408
89.1M
    bn_check_top(a);
409
89.1M
    bn_check_top(b);
410
411
89.1M
    if (a == b) {
412
0
        return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
413
0
    }
414
415
89.1M
    BN_CTX_start(ctx);
416
89.1M
    if ((s = BN_CTX_get(ctx)) == NULL)
417
0
        goto err;
418
419
89.1M
    zlen = a->top + b->top + 4;
420
89.1M
    if (!bn_wexpand(s, zlen))
421
0
        goto err;
422
89.1M
    s->top = zlen;
423
424
1.45G
    for (i = 0; i < zlen; i++)
425
1.36G
        s->d[i] = 0;
426
427
352M
    for (j = 0; j < b->top; j += 2) {
428
263M
        y0 = b->d[j];
429
263M
        y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
430
1.07G
        for (i = 0; i < a->top; i += 2) {
431
816M
            x0 = a->d[i];
432
816M
            x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
433
816M
            bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
434
4.08G
            for (k = 0; k < 4; k++)
435
3.26G
                s->d[i + j + k] ^= zz[k];
436
816M
        }
437
263M
    }
438
439
89.1M
    bn_correct_top(s);
440
89.1M
    if (BN_GF2m_mod_arr(r, s, p))
441
89.1M
        ret = 1;
442
89.1M
    bn_check_top(r);
443
444
89.1M
err:
445
89.1M
    BN_CTX_end(ctx);
446
89.1M
    return ret;
447
89.1M
}
448
449
/*
450
 * Compute the product of two polynomials a and b, reduce modulo p, and store
451
 * the result in r.  r could be a or b; a could equal b. This function calls
452
 * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
453
 * only provided for convenience; for best performance, use the
454
 * BN_GF2m_mod_mul_arr function.
455
 */
456
int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
457
    const BIGNUM *p, BN_CTX *ctx)
458
538k
{
459
538k
    int ret = 0;
460
538k
    const int max = BN_num_bits(p) + 1;
461
538k
    int *arr;
462
463
538k
    bn_check_top(a);
464
538k
    bn_check_top(b);
465
538k
    bn_check_top(p);
466
467
538k
    arr = OPENSSL_malloc(sizeof(*arr) * max);
468
538k
    if (arr == NULL) {
469
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
470
0
        return 0;
471
0
    }
472
538k
    ret = BN_GF2m_poly2arr(p, arr, max);
473
538k
    if (!ret || ret > max) {
474
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
475
0
        goto err;
476
0
    }
477
538k
    ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
478
538k
    bn_check_top(r);
479
538k
err:
480
538k
    OPENSSL_free(arr);
481
538k
    return ret;
482
538k
}
483
484
/* Square a, reduce the result mod p, and store it in a.  r could be a. */
485
int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
486
    BN_CTX *ctx)
487
177M
{
488
177M
    int i, ret = 0;
489
177M
    BIGNUM *s;
490
491
177M
    bn_check_top(a);
492
177M
    BN_CTX_start(ctx);
493
177M
    if ((s = BN_CTX_get(ctx)) == NULL)
494
0
        goto err;
495
177M
    if (!bn_wexpand(s, 2 * a->top))
496
0
        goto err;
497
498
1.14G
    for (i = a->top - 1; i >= 0; i--) {
499
970M
        s->d[2 * i + 1] = SQR1(a->d[i]);
500
970M
        s->d[2 * i] = SQR0(a->d[i]);
501
970M
    }
502
503
177M
    s->top = 2 * a->top;
504
177M
    bn_correct_top(s);
505
177M
    if (!BN_GF2m_mod_arr(r, s, p))
506
0
        goto err;
507
177M
    bn_check_top(r);
508
177M
    ret = 1;
509
177M
err:
510
177M
    BN_CTX_end(ctx);
511
177M
    return ret;
512
177M
}
513
514
/*
515
 * Square a, reduce the result mod p, and store it in a.  r could be a. This
516
 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
517
 * wrapper function is only provided for convenience; for best performance,
518
 * use the BN_GF2m_mod_sqr_arr function.
519
 */
520
int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
521
0
{
522
0
    int ret = 0;
523
0
    const int max = BN_num_bits(p) + 1;
524
0
    int *arr;
525
526
0
    bn_check_top(a);
527
0
    bn_check_top(p);
528
529
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
530
0
    if (arr == NULL) {
531
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
532
0
        return 0;
533
0
    }
534
0
    ret = BN_GF2m_poly2arr(p, arr, max);
535
0
    if (!ret || ret > max) {
536
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
537
0
        goto err;
538
0
    }
539
0
    ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
540
0
    bn_check_top(r);
541
0
err:
542
0
    OPENSSL_free(arr);
543
0
    return ret;
544
0
}
545
546
/*
547
 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
548
 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
549
 * Hernandez, J.L., and Menezes, A.  "Software Implementation of Elliptic
550
 * Curve Cryptography Over Binary Fields".
551
 */
552
static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
553
    const BIGNUM *p, BN_CTX *ctx)
554
181k
{
555
181k
    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
556
181k
    int ret = 0;
557
558
181k
    bn_check_top(a);
559
181k
    bn_check_top(p);
560
561
181k
    BN_CTX_start(ctx);
562
563
181k
    b = BN_CTX_get(ctx);
564
181k
    c = BN_CTX_get(ctx);
565
181k
    u = BN_CTX_get(ctx);
566
181k
    v = BN_CTX_get(ctx);
567
181k
    if (v == NULL)
568
0
        goto err;
569
570
181k
    if (!BN_GF2m_mod(u, a, p))
571
0
        goto err;
572
181k
    if (BN_is_zero(u))
573
0
        goto err;
574
575
181k
    if (!BN_copy(v, p))
576
0
        goto err;
577
#if 0
578
    if (!BN_one(b))
579
        goto err;
580
581
    while (1) {
582
        while (!BN_is_odd(u)) {
583
            if (BN_is_zero(u))
584
                goto err;
585
            if (!BN_rshift1(u, u))
586
                goto err;
587
            if (BN_is_odd(b)) {
588
                if (!BN_GF2m_add(b, b, p))
589
                    goto err;
590
            }
591
            if (!BN_rshift1(b, b))
592
                goto err;
593
        }
594
595
        if (BN_abs_is_word(u, 1))
596
            break;
597
598
        if (BN_num_bits(u) < BN_num_bits(v)) {
599
            tmp = u;
600
            u = v;
601
            v = tmp;
602
            tmp = b;
603
            b = c;
604
            c = tmp;
605
        }
606
607
        if (!BN_GF2m_add(u, u, v))
608
            goto err;
609
        if (!BN_GF2m_add(b, b, c))
610
            goto err;
611
    }
612
#else
613
181k
    {
614
181k
        int i;
615
181k
        int ubits = BN_num_bits(u);
616
181k
        int vbits = BN_num_bits(v); /* v is copy of p */
617
181k
        int top = p->top;
618
181k
        BN_ULONG *udp, *bdp, *vdp, *cdp;
619
620
181k
        if (!bn_wexpand(u, top))
621
0
            goto err;
622
181k
        udp = u->d;
623
188k
        for (i = u->top; i < top; i++)
624
6.47k
            udp[i] = 0;
625
181k
        u->top = top;
626
181k
        if (!bn_wexpand(b, top))
627
0
            goto err;
628
181k
        bdp = b->d;
629
181k
        bdp[0] = 1;
630
655k
        for (i = 1; i < top; i++)
631
473k
            bdp[i] = 0;
632
181k
        b->top = top;
633
181k
        if (!bn_wexpand(c, top))
634
0
            goto err;
635
181k
        cdp = c->d;
636
836k
        for (i = 0; i < top; i++)
637
655k
            cdp[i] = 0;
638
181k
        c->top = top;
639
181k
        vdp = v->d; /* It pays off to "cache" *->d pointers,
640
                     * because it allows optimizer to be more
641
                     * aggressive. But we don't have to "cache"
642
                     * p->d, because *p is declared 'const'... */
643
29.1M
        while (1) {
644
86.9M
            while (ubits && !(udp[0] & 1)) {
645
57.7M
                BN_ULONG u0, u1, b0, b1, mask;
646
647
57.7M
                u0 = udp[0];
648
57.7M
                b0 = bdp[0];
649
57.7M
                mask = (BN_ULONG)0 - (b0 & 1);
650
57.7M
                b0 ^= p->d[0] & mask;
651
238M
                for (i = 0; i < top - 1; i++) {
652
180M
                    u1 = udp[i + 1];
653
180M
                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
654
180M
                    u0 = u1;
655
180M
                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
656
180M
                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
657
180M
                    b0 = b1;
658
180M
                }
659
57.7M
                udp[i] = u0 >> 1;
660
57.7M
                bdp[i] = b0 >> 1;
661
57.7M
                ubits--;
662
57.7M
            }
663
664
29.1M
            if (ubits <= BN_BITS2) {
665
9.56M
                if (udp[0] == 0) /* poly was reducible */
666
0
                    goto err;
667
9.56M
                if (udp[0] == 1)
668
181k
                    break;
669
9.56M
            }
670
671
28.9M
            if (ubits < vbits) {
672
11.7M
                i = ubits;
673
11.7M
                ubits = vbits;
674
11.7M
                vbits = i;
675
11.7M
                tmp = u;
676
11.7M
                u = v;
677
11.7M
                v = tmp;
678
11.7M
                tmp = b;
679
11.7M
                b = c;
680
11.7M
                c = tmp;
681
11.7M
                udp = vdp;
682
11.7M
                vdp = v->d;
683
11.7M
                bdp = cdp;
684
11.7M
                cdp = c->d;
685
11.7M
            }
686
148M
            for (i = 0; i < top; i++) {
687
119M
                udp[i] ^= vdp[i];
688
119M
                bdp[i] ^= cdp[i];
689
119M
            }
690
28.9M
            if (ubits == vbits) {
691
5.83M
                BN_ULONG ul;
692
5.83M
                int utop = (ubits - 1) / BN_BITS2;
693
694
5.97M
                while ((ul = udp[utop]) == 0 && utop)
695
143k
                    utop--;
696
5.83M
                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
697
5.83M
            }
698
28.9M
        }
699
181k
        bn_correct_top(b);
700
181k
    }
701
0
#endif
702
703
181k
    if (!BN_copy(r, b))
704
0
        goto err;
705
181k
    bn_check_top(r);
706
181k
    ret = 1;
707
708
181k
err:
709
#ifdef BN_DEBUG
710
    /* BN_CTX_end would complain about the expanded form */
711
    bn_correct_top(c);
712
    bn_correct_top(u);
713
    bn_correct_top(v);
714
#endif
715
181k
    BN_CTX_end(ctx);
716
181k
    return ret;
717
181k
}
718
719
/*-
720
 * Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
721
 * This is not constant time.
722
 * But it does eliminate first order deduction on the input.
723
 */
724
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
725
181k
{
726
181k
    BIGNUM *b = NULL;
727
181k
    int ret = 0;
728
181k
    int numbits;
729
730
181k
    BN_CTX_start(ctx);
731
181k
    if ((b = BN_CTX_get(ctx)) == NULL)
732
0
        goto err;
733
734
    /* Fail on a non-sensical input p value */
735
181k
    numbits = BN_num_bits(p);
736
181k
    if (numbits <= 1)
737
0
        goto err;
738
739
    /* generate blinding value */
740
181k
    do {
741
181k
        if (!BN_priv_rand_ex(b, numbits - 1,
742
181k
                BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
743
0
            goto err;
744
181k
    } while (BN_is_zero(b));
745
746
    /* r := a * b */
747
181k
    if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
748
0
        goto err;
749
750
    /* r := 1/(a * b) */
751
181k
    if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
752
0
        goto err;
753
754
    /* r := b/(a * b) = 1/a */
755
181k
    if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
756
0
        goto err;
757
758
181k
    ret = 1;
759
760
181k
err:
761
181k
    BN_CTX_end(ctx);
762
181k
    return ret;
763
181k
}
764
765
/*
766
 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
767
 * This function calls down to the BN_GF2m_mod_inv implementation; this
768
 * wrapper function is only provided for convenience; for best performance,
769
 * use the BN_GF2m_mod_inv function.
770
 */
771
int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
772
    BN_CTX *ctx)
773
0
{
774
0
    BIGNUM *field;
775
0
    int ret = 0;
776
777
0
    bn_check_top(xx);
778
0
    BN_CTX_start(ctx);
779
0
    if ((field = BN_CTX_get(ctx)) == NULL)
780
0
        goto err;
781
0
    if (!BN_GF2m_arr2poly(p, field))
782
0
        goto err;
783
784
0
    ret = BN_GF2m_mod_inv(r, xx, field, ctx);
785
0
    bn_check_top(r);
786
787
0
err:
788
0
    BN_CTX_end(ctx);
789
0
    return ret;
790
0
}
791
792
/*
793
 * Divide y by x, reduce modulo p, and store the result in r. r could be x
794
 * or y, x could equal y.
795
 */
796
int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
797
    const BIGNUM *p, BN_CTX *ctx)
798
174k
{
799
174k
    BIGNUM *xinv = NULL;
800
174k
    int ret = 0;
801
802
174k
    bn_check_top(y);
803
174k
    bn_check_top(x);
804
174k
    bn_check_top(p);
805
806
174k
    BN_CTX_start(ctx);
807
174k
    xinv = BN_CTX_get(ctx);
808
174k
    if (xinv == NULL)
809
0
        goto err;
810
811
174k
    if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
812
0
        goto err;
813
174k
    if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
814
0
        goto err;
815
174k
    bn_check_top(r);
816
174k
    ret = 1;
817
818
174k
err:
819
174k
    BN_CTX_end(ctx);
820
174k
    return ret;
821
174k
}
822
823
/*
824
 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
825
 * * or yy, xx could equal yy. This function calls down to the
826
 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
827
 * convenience; for best performance, use the BN_GF2m_mod_div function.
828
 */
829
int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
830
    const int p[], BN_CTX *ctx)
831
0
{
832
0
    BIGNUM *field;
833
0
    int ret = 0;
834
835
0
    bn_check_top(yy);
836
0
    bn_check_top(xx);
837
838
0
    BN_CTX_start(ctx);
839
0
    if ((field = BN_CTX_get(ctx)) == NULL)
840
0
        goto err;
841
0
    if (!BN_GF2m_arr2poly(p, field))
842
0
        goto err;
843
844
0
    ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
845
0
    bn_check_top(r);
846
847
0
err:
848
0
    BN_CTX_end(ctx);
849
0
    return ret;
850
0
}
851
852
/*
853
 * Compute the bth power of a, reduce modulo p, and store the result in r.  r
854
 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
855
 * P1363.
856
 */
857
int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
858
    const int p[], BN_CTX *ctx)
859
29.4k
{
860
29.4k
    int ret = 0, i, n;
861
29.4k
    BIGNUM *u;
862
863
29.4k
    bn_check_top(a);
864
29.4k
    bn_check_top(b);
865
866
29.4k
    if (BN_is_zero(b))
867
0
        return BN_one(r);
868
869
29.4k
    if (BN_abs_is_word(b, 1))
870
0
        return (BN_copy(r, a) != NULL);
871
872
29.4k
    BN_CTX_start(ctx);
873
29.4k
    if ((u = BN_CTX_get(ctx)) == NULL)
874
0
        goto err;
875
876
29.4k
    if (!BN_GF2m_mod_arr(u, a, p))
877
0
        goto err;
878
879
29.4k
    n = BN_num_bits(b) - 1;
880
4.12M
    for (i = n - 1; i >= 0; i--) {
881
4.09M
        if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
882
0
            goto err;
883
4.09M
        if (BN_is_bit_set(b, i)) {
884
0
            if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
885
0
                goto err;
886
0
        }
887
4.09M
    }
888
29.4k
    if (!BN_copy(r, u))
889
0
        goto err;
890
29.4k
    bn_check_top(r);
891
29.4k
    ret = 1;
892
29.4k
err:
893
29.4k
    BN_CTX_end(ctx);
894
29.4k
    return ret;
895
29.4k
}
896
897
/*
898
 * Compute the bth power of a, reduce modulo p, and store the result in r.  r
899
 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
900
 * implementation; this wrapper function is only provided for convenience;
901
 * for best performance, use the BN_GF2m_mod_exp_arr function.
902
 */
903
int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
904
    const BIGNUM *p, BN_CTX *ctx)
905
0
{
906
0
    int ret = 0;
907
0
    const int max = BN_num_bits(p) + 1;
908
0
    int *arr;
909
910
0
    bn_check_top(a);
911
0
    bn_check_top(b);
912
0
    bn_check_top(p);
913
914
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
915
0
    if (arr == NULL) {
916
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
917
0
        return 0;
918
0
    }
919
0
    ret = BN_GF2m_poly2arr(p, arr, max);
920
0
    if (!ret || ret > max) {
921
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
922
0
        goto err;
923
0
    }
924
0
    ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
925
0
    bn_check_top(r);
926
0
err:
927
0
    OPENSSL_free(arr);
928
0
    return ret;
929
0
}
930
931
/*
932
 * Compute the square root of a, reduce modulo p, and store the result in r.
933
 * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
934
 */
935
int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
936
    BN_CTX *ctx)
937
29.4k
{
938
29.4k
    int ret = 0;
939
29.4k
    BIGNUM *u;
940
941
29.4k
    bn_check_top(a);
942
943
29.4k
    if (p[0] == 0) {
944
        /* reduction mod 1 => return 0 */
945
0
        BN_zero(r);
946
0
        return 1;
947
0
    }
948
949
29.4k
    BN_CTX_start(ctx);
950
29.4k
    if ((u = BN_CTX_get(ctx)) == NULL)
951
0
        goto err;
952
953
29.4k
    if (!BN_set_bit(u, p[0] - 1))
954
0
        goto err;
955
29.4k
    ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
956
29.4k
    bn_check_top(r);
957
958
29.4k
err:
959
29.4k
    BN_CTX_end(ctx);
960
29.4k
    return ret;
961
29.4k
}
962
963
/*
964
 * Compute the square root of a, reduce modulo p, and store the result in r.
965
 * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
966
 * implementation; this wrapper function is only provided for convenience;
967
 * for best performance, use the BN_GF2m_mod_sqrt_arr function.
968
 */
969
int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
970
0
{
971
0
    int ret = 0;
972
0
    const int max = BN_num_bits(p) + 1;
973
0
    int *arr;
974
975
0
    bn_check_top(a);
976
0
    bn_check_top(p);
977
978
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
979
0
    if (arr == NULL) {
980
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
981
0
        return 0;
982
0
    }
983
0
    ret = BN_GF2m_poly2arr(p, arr, max);
984
0
    if (!ret || ret > max) {
985
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
986
0
        goto err;
987
0
    }
988
0
    ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
989
0
    bn_check_top(r);
990
0
err:
991
0
    OPENSSL_free(arr);
992
0
    return ret;
993
0
}
994
995
/*
996
 * Find r such that r^2 + r = a mod p.  r could be a. If no r exists returns
997
 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
998
 */
999
int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1000
    BN_CTX *ctx)
1001
143k
{
1002
143k
    int ret = 0, count = 0, j;
1003
143k
    BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1004
1005
143k
    bn_check_top(a_);
1006
1007
143k
    if (p[0] == 0) {
1008
        /* reduction mod 1 => return 0 */
1009
0
        BN_zero(r);
1010
0
        return 1;
1011
0
    }
1012
1013
143k
    BN_CTX_start(ctx);
1014
143k
    a = BN_CTX_get(ctx);
1015
143k
    z = BN_CTX_get(ctx);
1016
143k
    w = BN_CTX_get(ctx);
1017
143k
    if (w == NULL)
1018
0
        goto err;
1019
1020
143k
    if (!BN_GF2m_mod_arr(a, a_, p))
1021
0
        goto err;
1022
1023
143k
    if (BN_is_zero(a)) {
1024
476
        BN_zero(r);
1025
476
        ret = 1;
1026
476
        goto err;
1027
476
    }
1028
1029
143k
    if (p[0] & 0x1) { /* m is odd */
1030
        /* compute half-trace of a */
1031
101k
        if (!BN_copy(z, a))
1032
0
            goto err;
1033
8.12M
        for (j = 1; j <= (p[0] - 1) / 2; j++) {
1034
8.02M
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1035
0
                goto err;
1036
8.02M
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1037
0
                goto err;
1038
8.02M
            if (!BN_GF2m_add(z, z, a))
1039
0
                goto err;
1040
8.02M
        }
1041
1042
101k
    } else { /* m is even */
1043
1044
42.2k
        rho = BN_CTX_get(ctx);
1045
42.2k
        w2 = BN_CTX_get(ctx);
1046
42.2k
        tmp = BN_CTX_get(ctx);
1047
42.2k
        if (tmp == NULL)
1048
0
            goto err;
1049
208k
        do {
1050
208k
            if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
1051
208k
                    0, ctx))
1052
0
                goto err;
1053
208k
            if (!BN_GF2m_mod_arr(rho, rho, p))
1054
0
                goto err;
1055
208k
            BN_zero(z);
1056
208k
            if (!BN_copy(w, rho))
1057
0
                goto err;
1058
71.5M
            for (j = 1; j <= p[0] - 1; j++) {
1059
71.3M
                if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1060
0
                    goto err;
1061
71.3M
                if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1062
0
                    goto err;
1063
71.3M
                if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1064
0
                    goto err;
1065
71.3M
                if (!BN_GF2m_add(z, z, tmp))
1066
0
                    goto err;
1067
71.3M
                if (!BN_GF2m_add(w, w2, rho))
1068
0
                    goto err;
1069
71.3M
            }
1070
208k
            count++;
1071
208k
        } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1072
42.2k
        if (BN_is_zero(w)) {
1073
2.89k
            ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
1074
2.89k
            goto err;
1075
2.89k
        }
1076
42.2k
    }
1077
1078
140k
    if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1079
0
        goto err;
1080
140k
    if (!BN_GF2m_add(w, z, w))
1081
0
        goto err;
1082
140k
    if (BN_GF2m_cmp(w, a)) {
1083
55.6k
        ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
1084
55.6k
        goto err;
1085
55.6k
    }
1086
1087
84.7k
    if (!BN_copy(r, z))
1088
0
        goto err;
1089
84.7k
    bn_check_top(r);
1090
1091
84.7k
    ret = 1;
1092
1093
143k
err:
1094
143k
    BN_CTX_end(ctx);
1095
143k
    return ret;
1096
84.7k
}
1097
1098
/*
1099
 * Find r such that r^2 + r = a mod p.  r could be a. If no r exists returns
1100
 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1101
 * implementation; this wrapper function is only provided for convenience;
1102
 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1103
 */
1104
int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1105
    BN_CTX *ctx)
1106
0
{
1107
0
    int ret = 0;
1108
0
    const int max = BN_num_bits(p) + 1;
1109
0
    int *arr;
1110
1111
0
    bn_check_top(a);
1112
0
    bn_check_top(p);
1113
1114
0
    arr = OPENSSL_malloc(sizeof(*arr) * max);
1115
0
    if (arr == NULL) {
1116
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
1117
0
        goto err;
1118
0
    }
1119
0
    ret = BN_GF2m_poly2arr(p, arr, max);
1120
0
    if (!ret || ret > max) {
1121
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
1122
0
        goto err;
1123
0
    }
1124
0
    ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1125
0
    bn_check_top(r);
1126
0
err:
1127
0
    OPENSSL_free(arr);
1128
0
    return ret;
1129
0
}
1130
1131
/*
1132
 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1133
 * x^i) into an array of integers corresponding to the bits with non-zero
1134
 * coefficient.  The array is intended to be suitable for use with
1135
 * `BN_GF2m_mod_arr()`, and so the constant term of the polynomial must not be
1136
 * zero.  This translates to a requirement that the input BIGNUM `a` is odd.
1137
 *
1138
 * Given sufficient room, the array is terminated with -1.  Up to max elements
1139
 * of the array will be filled.
1140
 *
1141
 * The return value is total number of array elements that would be filled if
1142
 * array was large enough, including the terminating `-1`.  It is `0` when `a`
1143
 * is not odd or the constant term is zero contrary to requirement.
1144
 *
1145
 * The return value is also `0` when the leading exponent exceeds
1146
 * `OPENSSL_ECC_MAX_FIELD_BITS`, this guards against CPU exhaustion attacks,
1147
 */
1148
int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1149
1.02M
{
1150
1.02M
    int i, j, k = 0;
1151
1.02M
    BN_ULONG mask;
1152
1153
1.02M
    if (!BN_is_odd(a))
1154
0
        return 0;
1155
1156
4.73M
    for (i = a->top - 1; i >= 0; i--) {
1157
3.71M
        if (!a->d[i])
1158
            /* skip word if a->d[i] == 0 */
1159
1.49M
            continue;
1160
2.22M
        mask = BN_TBIT;
1161
144M
        for (j = BN_BITS2 - 1; j >= 0; j--) {
1162
142M
            if (a->d[i] & mask) {
1163
4.59M
                if (k < max)
1164
4.59M
                    p[k] = BN_BITS2 * i + j;
1165
4.59M
                k++;
1166
4.59M
            }
1167
142M
            mask >>= 1;
1168
142M
        }
1169
2.22M
    }
1170
1171
1.02M
    if (k > 0 && p[0] > OPENSSL_ECC_MAX_FIELD_BITS)
1172
0
        return 0;
1173
1174
1.02M
    if (k < max)
1175
1.02M
        p[k] = -1;
1176
1177
1.02M
    return k + 1;
1178
1.02M
}
1179
1180
/*
1181
 * Convert the coefficient array representation of a polynomial to a
1182
 * bit-string.  The array must be terminated by -1.
1183
 */
1184
int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1185
0
{
1186
0
    int i;
1187
1188
0
    bn_check_top(a);
1189
0
    BN_zero(a);
1190
0
    for (i = 0; p[i] != -1; i++) {
1191
0
        if (BN_set_bit(a, p[i]) == 0)
1192
0
            return 0;
1193
0
    }
1194
0
    bn_check_top(a);
1195
1196
0
    return 1;
1197
0
}
1198
1199
#endif