Coverage Report

Created: 2023-06-08 06:43

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