Coverage Report

Created: 2025-12-31 06:58

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
228k
#define MAX_ITERATIONS 50
25
26
19.3G
#define SQR_nibble(w) ((((w) & 8) << 3) \
27
19.3G
    | (((w) & 4) << 2)                  \
28
19.3G
    | (((w) & 2) << 1)                  \
29
19.3G
    | ((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
1.21G
    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
1.21G
    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
200M
{
246
200M
    int i;
247
200M
    const BIGNUM *at, *bt;
248
249
200M
    bn_check_top(a);
250
200M
    bn_check_top(b);
251
252
200M
    if (a->top < b->top) {
253
1.18M
        at = b;
254
1.18M
        bt = a;
255
199M
    } else {
256
199M
        at = a;
257
199M
        bt = b;
258
199M
    }
259
260
200M
    if (bn_wexpand(r, at->top) == NULL)
261
0
        return 0;
262
263
1.35G
    for (i = 0; i < bt->top; i++) {
264
1.15G
        r->d[i] = at->d[i] ^ bt->d[i];
265
1.15G
    }
266
204M
    for (; i < at->top; i++) {
267
3.90M
        r->d[i] = at->d[i];
268
3.90M
    }
269
270
200M
    r->top = at->top;
271
200M
    bn_correct_top(r);
272
273
200M
    return 1;
274
200M
}
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
236M
{
286
236M
    int j, k;
287
236M
    int n, dN, d0, d1;
288
236M
    BN_ULONG zz, *z;
289
290
236M
    bn_check_top(a);
291
292
236M
    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
236M
    if (a != r) {
303
236M
        if (!bn_wexpand(r, a->top))
304
0
            return 0;
305
2.88G
        for (j = 0; j < a->top; j++) {
306
2.64G
            r->d[j] = a->d[j];
307
2.64G
        }
308
236M
        r->top = a->top;
309
236M
    }
310
236M
    z = r->d;
311
312
    /* start reduction */
313
236M
    dN = p[0] / BN_BITS2;
314
2.83G
    for (j = r->top - 1; j > dN;) {
315
2.60G
        zz = z[j];
316
2.60G
        if (z[j] == 0) {
317
1.30G
            j--;
318
1.30G
            continue;
319
1.30G
        }
320
1.30G
        z[j] = 0;
321
322
5.08G
        for (k = 1; p[k] != 0; k++) {
323
            /* reducing component t^p[k] */
324
3.78G
            n = p[0] - p[k];
325
3.78G
            d0 = n % BN_BITS2;
326
3.78G
            d1 = BN_BITS2 - d0;
327
3.78G
            n /= BN_BITS2;
328
3.78G
            z[j - n] ^= (zz >> d0);
329
3.78G
            if (d0)
330
3.77G
                z[j - n - 1] ^= (zz << d1);
331
3.78G
        }
332
333
        /* reducing component t^0 */
334
1.30G
        n = dN;
335
1.30G
        d0 = p[0] % BN_BITS2;
336
1.30G
        d1 = BN_BITS2 - d0;
337
1.30G
        z[j - n] ^= (zz >> d0);
338
1.30G
        if (d0)
339
1.30G
            z[j - n - 1] ^= (zz << d1);
340
1.30G
    }
341
342
    /* final round of reduction */
343
469M
    while (j == dN) {
344
345
468M
        d0 = p[0] % BN_BITS2;
346
468M
        zz = z[dN] >> d0;
347
468M
        if (zz == 0)
348
236M
            break;
349
232M
        d1 = BN_BITS2 - d0;
350
351
        /* clear up the top d1 bits */
352
232M
        if (d0)
353
232M
            z[dN] = (z[dN] << d1) >> d1;
354
0
        else
355
0
            z[dN] = 0;
356
232M
        z[0] ^= zz; /* reduction t^0 component */
357
358
906M
        for (k = 1; p[k] != 0; k++) {
359
674M
            BN_ULONG tmp_ulong;
360
361
            /* reducing component t^p[k] */
362
674M
            n = p[k] / BN_BITS2;
363
674M
            d0 = p[k] % BN_BITS2;
364
674M
            d1 = BN_BITS2 - d0;
365
674M
            z[n] ^= (zz << d0);
366
674M
            if (d0 && (tmp_ulong = zz >> d1))
367
30.9M
                z[n + 1] ^= tmp_ulong;
368
674M
        }
369
232M
    }
370
371
236M
    bn_correct_top(r);
372
236M
    return 1;
373
236M
}
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
157k
{
383
157k
    int ret = 0;
384
157k
    int arr[6];
385
157k
    bn_check_top(a);
386
157k
    bn_check_top(p);
387
157k
    ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
388
157k
    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
157k
    ret = BN_GF2m_mod_arr(r, a, arr);
393
157k
    bn_check_top(r);
394
157k
    return ret;
395
157k
}
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
109M
{
404
109M
    int zlen, i, j, k, ret = 0;
405
109M
    BIGNUM *s;
406
109M
    BN_ULONG x1, x0, y1, y0, zz[4];
407
408
109M
    bn_check_top(a);
409
109M
    bn_check_top(b);
410
411
109M
    if (a == b) {
412
0
        return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
413
0
    }
414
415
109M
    BN_CTX_start(ctx);
416
109M
    if ((s = BN_CTX_get(ctx)) == NULL)
417
0
        goto err;
418
419
109M
    zlen = a->top + b->top + 4;
420
109M
    if (!bn_wexpand(s, zlen))
421
0
        goto err;
422
109M
    s->top = zlen;
423
424
1.80G
    for (i = 0; i < zlen; i++)
425
1.69G
        s->d[i] = 0;
426
427
432M
    for (j = 0; j < b->top; j += 2) {
428
323M
        y0 = b->d[j];
429
323M
        y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
430
1.32G
        for (i = 0; i < a->top; i += 2) {
431
997M
            x0 = a->d[i];
432
997M
            x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
433
997M
            bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
434
4.98G
            for (k = 0; k < 4; k++)
435
3.98G
                s->d[i + j + k] ^= zz[k];
436
997M
        }
437
323M
    }
438
439
109M
    bn_correct_top(s);
440
109M
    if (BN_GF2m_mod_arr(r, s, p))
441
109M
        ret = 1;
442
109M
    bn_check_top(r);
443
444
109M
err:
445
109M
    BN_CTX_end(ctx);
446
109M
    return ret;
447
109M
}
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
466k
{
459
466k
    int ret = 0;
460
466k
    const int max = BN_num_bits(p) + 1;
461
466k
    int *arr;
462
463
466k
    bn_check_top(a);
464
466k
    bn_check_top(b);
465
466k
    bn_check_top(p);
466
467
466k
    arr = OPENSSL_malloc(sizeof(*arr) * max);
468
466k
    if (arr == NULL) {
469
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
470
0
        return 0;
471
0
    }
472
466k
    ret = BN_GF2m_poly2arr(p, arr, max);
473
466k
    if (!ret || ret > max) {
474
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
475
0
        goto err;
476
0
    }
477
466k
    ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
478
466k
    bn_check_top(r);
479
466k
err:
480
466k
    OPENSSL_free(arr);
481
466k
    return ret;
482
466k
}
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
215M
{
488
215M
    int i, ret = 0;
489
215M
    BIGNUM *s;
490
491
215M
    bn_check_top(a);
492
215M
    BN_CTX_start(ctx);
493
215M
    if ((s = BN_CTX_get(ctx)) == NULL)
494
0
        goto err;
495
215M
    if (!bn_wexpand(s, 2 * a->top))
496
0
        goto err;
497
498
1.42G
    for (i = a->top - 1; i >= 0; i--) {
499
1.21G
        s->d[2 * i + 1] = SQR1(a->d[i]);
500
1.21G
        s->d[2 * i] = SQR0(a->d[i]);
501
1.21G
    }
502
503
215M
    s->top = 2 * a->top;
504
215M
    bn_correct_top(s);
505
215M
    if (!BN_GF2m_mod_arr(r, s, p))
506
0
        goto err;
507
215M
    bn_check_top(r);
508
215M
    ret = 1;
509
215M
err:
510
215M
    BN_CTX_end(ctx);
511
215M
    return ret;
512
215M
}
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
157k
{
555
157k
    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
556
157k
    int ret = 0;
557
558
157k
    bn_check_top(a);
559
157k
    bn_check_top(p);
560
561
157k
    BN_CTX_start(ctx);
562
563
157k
    b = BN_CTX_get(ctx);
564
157k
    c = BN_CTX_get(ctx);
565
157k
    u = BN_CTX_get(ctx);
566
157k
    v = BN_CTX_get(ctx);
567
157k
    if (v == NULL)
568
0
        goto err;
569
570
157k
    if (!BN_GF2m_mod(u, a, p))
571
0
        goto err;
572
157k
    if (BN_is_zero(u))
573
0
        goto err;
574
575
157k
    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
157k
    {
614
157k
        int i;
615
157k
        int ubits = BN_num_bits(u);
616
157k
        int vbits = BN_num_bits(v); /* v is copy of p */
617
157k
        int top = p->top;
618
157k
        BN_ULONG *udp, *bdp, *vdp, *cdp;
619
620
157k
        if (!bn_wexpand(u, top))
621
0
            goto err;
622
157k
        udp = u->d;
623
163k
        for (i = u->top; i < top; i++)
624
6.15k
            udp[i] = 0;
625
157k
        u->top = top;
626
157k
        if (!bn_wexpand(b, top))
627
0
            goto err;
628
157k
        bdp = b->d;
629
157k
        bdp[0] = 1;
630
571k
        for (i = 1; i < top; i++)
631
414k
            bdp[i] = 0;
632
157k
        b->top = top;
633
157k
        if (!bn_wexpand(c, top))
634
0
            goto err;
635
157k
        cdp = c->d;
636
729k
        for (i = 0; i < top; i++)
637
571k
            cdp[i] = 0;
638
157k
        c->top = top;
639
157k
        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
25.5M
        while (1) {
644
76.3M
            while (ubits && !(udp[0] & 1)) {
645
50.7M
                BN_ULONG u0, u1, b0, b1, mask;
646
647
50.7M
                u0 = udp[0];
648
50.7M
                b0 = bdp[0];
649
50.7M
                mask = (BN_ULONG)0 - (b0 & 1);
650
50.7M
                b0 ^= p->d[0] & mask;
651
213M
                for (i = 0; i < top - 1; i++) {
652
162M
                    u1 = udp[i + 1];
653
162M
                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
654
162M
                    u0 = u1;
655
162M
                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
656
162M
                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
657
162M
                    b0 = b1;
658
162M
                }
659
50.7M
                udp[i] = u0 >> 1;
660
50.7M
                bdp[i] = b0 >> 1;
661
50.7M
                ubits--;
662
50.7M
            }
663
664
25.5M
            if (ubits <= BN_BITS2) {
665
8.29M
                if (udp[0] == 0) /* poly was reducible */
666
0
                    goto err;
667
8.29M
                if (udp[0] == 1)
668
157k
                    break;
669
8.29M
            }
670
671
25.4M
            if (ubits < vbits) {
672
10.3M
                i = ubits;
673
10.3M
                ubits = vbits;
674
10.3M
                vbits = i;
675
10.3M
                tmp = u;
676
10.3M
                u = v;
677
10.3M
                v = tmp;
678
10.3M
                tmp = b;
679
10.3M
                b = c;
680
10.3M
                c = tmp;
681
10.3M
                udp = vdp;
682
10.3M
                vdp = v->d;
683
10.3M
                bdp = cdp;
684
10.3M
                cdp = c->d;
685
10.3M
            }
686
132M
            for (i = 0; i < top; i++) {
687
106M
                udp[i] ^= vdp[i];
688
106M
                bdp[i] ^= cdp[i];
689
106M
            }
690
25.4M
            if (ubits == vbits) {
691
5.12M
                BN_ULONG ul;
692
5.12M
                int utop = (ubits - 1) / BN_BITS2;
693
694
5.25M
                while ((ul = udp[utop]) == 0 && utop)
695
130k
                    utop--;
696
5.12M
                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
697
5.12M
            }
698
25.4M
        }
699
157k
        bn_correct_top(b);
700
157k
    }
701
0
#endif
702
703
157k
    if (!BN_copy(r, b))
704
0
        goto err;
705
157k
    bn_check_top(r);
706
157k
    ret = 1;
707
708
157k
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
157k
    BN_CTX_end(ctx);
716
157k
    return ret;
717
157k
}
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
157k
{
726
157k
    BIGNUM *b = NULL;
727
157k
    int ret = 0;
728
157k
    int numbits;
729
730
157k
    BN_CTX_start(ctx);
731
157k
    if ((b = BN_CTX_get(ctx)) == NULL)
732
0
        goto err;
733
734
    /* Fail on a non-sensical input p value */
735
157k
    numbits = BN_num_bits(p);
736
157k
    if (numbits <= 1)
737
0
        goto err;
738
739
    /* generate blinding value */
740
157k
    do {
741
157k
        if (!BN_priv_rand_ex(b, numbits - 1,
742
157k
                BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
743
0
            goto err;
744
157k
    } while (BN_is_zero(b));
745
746
    /* r := a * b */
747
157k
    if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
748
0
        goto err;
749
750
    /* r := 1/(a * b) */
751
157k
    if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
752
0
        goto err;
753
754
    /* r := b/(a * b) = 1/a */
755
157k
    if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
756
0
        goto err;
757
758
157k
    ret = 1;
759
760
157k
err:
761
157k
    BN_CTX_end(ctx);
762
157k
    return ret;
763
157k
}
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
151k
{
799
151k
    BIGNUM *xinv = NULL;
800
151k
    int ret = 0;
801
802
151k
    bn_check_top(y);
803
151k
    bn_check_top(x);
804
151k
    bn_check_top(p);
805
806
151k
    BN_CTX_start(ctx);
807
151k
    xinv = BN_CTX_get(ctx);
808
151k
    if (xinv == NULL)
809
0
        goto err;
810
811
151k
    if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
812
0
        goto err;
813
151k
    if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
814
0
        goto err;
815
151k
    bn_check_top(r);
816
151k
    ret = 1;
817
818
151k
err:
819
151k
    BN_CTX_end(ctx);
820
151k
    return ret;
821
151k
}
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
25.7k
{
860
25.7k
    int ret = 0, i, n;
861
25.7k
    BIGNUM *u;
862
863
25.7k
    bn_check_top(a);
864
25.7k
    bn_check_top(b);
865
866
25.7k
    if (BN_is_zero(b))
867
0
        return BN_one(r);
868
869
25.7k
    if (BN_abs_is_word(b, 1))
870
0
        return (BN_copy(r, a) != NULL);
871
872
25.7k
    BN_CTX_start(ctx);
873
25.7k
    if ((u = BN_CTX_get(ctx)) == NULL)
874
0
        goto err;
875
876
25.7k
    if (!BN_GF2m_mod_arr(u, a, p))
877
0
        goto err;
878
879
25.7k
    n = BN_num_bits(b) - 1;
880
3.57M
    for (i = n - 1; i >= 0; i--) {
881
3.54M
        if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
882
0
            goto err;
883
3.54M
        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
3.54M
    }
888
25.7k
    if (!BN_copy(r, u))
889
0
        goto err;
890
25.7k
    bn_check_top(r);
891
25.7k
    ret = 1;
892
25.7k
err:
893
25.7k
    BN_CTX_end(ctx);
894
25.7k
    return ret;
895
25.7k
}
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
25.7k
{
938
25.7k
    int ret = 0;
939
25.7k
    BIGNUM *u;
940
941
25.7k
    bn_check_top(a);
942
943
25.7k
    if (p[0] == 0) {
944
        /* reduction mod 1 => return 0 */
945
0
        BN_zero(r);
946
0
        return 1;
947
0
    }
948
949
25.7k
    BN_CTX_start(ctx);
950
25.7k
    if ((u = BN_CTX_get(ctx)) == NULL)
951
0
        goto err;
952
953
25.7k
    if (!BN_set_bit(u, p[0] - 1))
954
0
        goto err;
955
25.7k
    ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
956
25.7k
    bn_check_top(r);
957
958
25.7k
err:
959
25.7k
    BN_CTX_end(ctx);
960
25.7k
    return ret;
961
25.7k
}
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
125k
{
1002
125k
    int ret = 0, count = 0, j;
1003
125k
    BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1004
1005
125k
    bn_check_top(a_);
1006
1007
125k
    if (p[0] == 0) {
1008
        /* reduction mod 1 => return 0 */
1009
0
        BN_zero(r);
1010
0
        return 1;
1011
0
    }
1012
1013
125k
    BN_CTX_start(ctx);
1014
125k
    a = BN_CTX_get(ctx);
1015
125k
    z = BN_CTX_get(ctx);
1016
125k
    w = BN_CTX_get(ctx);
1017
125k
    if (w == NULL)
1018
0
        goto err;
1019
1020
125k
    if (!BN_GF2m_mod_arr(a, a_, p))
1021
0
        goto err;
1022
1023
125k
    if (BN_is_zero(a)) {
1024
517
        BN_zero(r);
1025
517
        ret = 1;
1026
517
        goto err;
1027
517
    }
1028
1029
125k
    if (p[0] & 0x1) { /* m is odd */
1030
        /* compute half-trace of a */
1031
87.4k
        if (!BN_copy(z, a))
1032
0
            goto err;
1033
6.96M
        for (j = 1; j <= (p[0] - 1) / 2; j++) {
1034
6.87M
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1035
0
                goto err;
1036
6.87M
            if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1037
0
                goto err;
1038
6.87M
            if (!BN_GF2m_add(z, z, a))
1039
0
                goto err;
1040
6.87M
        }
1041
1042
87.4k
    } else { /* m is even */
1043
1044
37.5k
        rho = BN_CTX_get(ctx);
1045
37.5k
        w2 = BN_CTX_get(ctx);
1046
37.5k
        tmp = BN_CTX_get(ctx);
1047
37.5k
        if (tmp == NULL)
1048
0
            goto err;
1049
261k
        do {
1050
261k
            if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
1051
261k
                    0, ctx))
1052
0
                goto err;
1053
261k
            if (!BN_GF2m_mod_arr(rho, rho, p))
1054
0
                goto err;
1055
261k
            BN_zero(z);
1056
261k
            if (!BN_copy(w, rho))
1057
0
                goto err;
1058
92.1M
            for (j = 1; j <= p[0] - 1; j++) {
1059
91.8M
                if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1060
0
                    goto err;
1061
91.8M
                if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1062
0
                    goto err;
1063
91.8M
                if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1064
0
                    goto err;
1065
91.8M
                if (!BN_GF2m_add(z, z, tmp))
1066
0
                    goto err;
1067
91.8M
                if (!BN_GF2m_add(w, w2, rho))
1068
0
                    goto err;
1069
91.8M
            }
1070
261k
            count++;
1071
261k
        } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1072
37.5k
        if (BN_is_zero(w)) {
1073
4.15k
            ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
1074
4.15k
            goto err;
1075
4.15k
        }
1076
37.5k
    }
1077
1078
120k
    if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1079
0
        goto err;
1080
120k
    if (!BN_GF2m_add(w, z, w))
1081
0
        goto err;
1082
120k
    if (BN_GF2m_cmp(w, a)) {
1083
48.8k
        ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
1084
48.8k
        goto err;
1085
48.8k
    }
1086
1087
72.0k
    if (!BN_copy(r, z))
1088
0
        goto err;
1089
72.0k
    bn_check_top(r);
1090
1091
72.0k
    ret = 1;
1092
1093
125k
err:
1094
125k
    BN_CTX_end(ctx);
1095
125k
    return ret;
1096
72.0k
}
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
893k
{
1150
893k
    int i, j, k = 0;
1151
893k
    BN_ULONG mask;
1152
1153
893k
    if (!BN_is_odd(a))
1154
0
        return 0;
1155
1156
4.18M
    for (i = a->top - 1; i >= 0; i--) {
1157
3.29M
        if (!a->d[i])
1158
            /* skip word if a->d[i] == 0 */
1159
1.33M
            continue;
1160
1.96M
        mask = BN_TBIT;
1161
127M
        for (j = BN_BITS2 - 1; j >= 0; j--) {
1162
125M
            if (a->d[i] & mask) {
1163
4.03M
                if (k < max)
1164
4.03M
                    p[k] = BN_BITS2 * i + j;
1165
4.03M
                k++;
1166
4.03M
            }
1167
125M
            mask >>= 1;
1168
125M
        }
1169
1.96M
    }
1170
1171
893k
    if (k > 0 && p[0] > OPENSSL_ECC_MAX_FIELD_BITS)
1172
0
        return 0;
1173
1174
893k
    if (k < max)
1175
893k
        p[k] = -1;
1176
1177
893k
    return k + 1;
1178
893k
}
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