2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
14 #include "internal/cryptlib.h"
17 #ifndef OPENSSL_NO_EC2M
20 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
23 # define MAX_ITERATIONS 50
25 # define SQR_nibble(w) ((((w) & 8) << 3) \
31 /* Platform-specific macros to accelerate squaring. */
32 # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
34 SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | \
35 SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | \
36 SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | \
37 SQR_nibble((w) >> 36) << 8 | SQR_nibble((w) >> 32)
39 SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | \
40 SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | \
41 SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \
42 SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) )
44 # ifdef THIRTY_TWO_BIT
46 SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | \
47 SQR_nibble((w) >> 20) << 8 | SQR_nibble((w) >> 16)
49 SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \
50 SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) )
53 # if !defined(OPENSSL_BN_ASM_GF2m)
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.
59 # ifdef THIRTY_TWO_BIT
60 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
63 register BN_ULONG h, l, s;
64 BN_ULONG tab[8], top2b = a >> 30;
65 register BN_ULONG a1, a2, a4;
67 a1 = a & (0x3FFFFFFF);
78 tab[7] = a1 ^ a2 ^ a4;
82 s = tab[b >> 3 & 0x7];
85 s = tab[b >> 6 & 0x7];
88 s = tab[b >> 9 & 0x7];
91 s = tab[b >> 12 & 0x7];
94 s = tab[b >> 15 & 0x7];
97 s = tab[b >> 18 & 0x7];
100 s = tab[b >> 21 & 0x7];
103 s = tab[b >> 24 & 0x7];
106 s = tab[b >> 27 & 0x7];
113 /* compensate for the top two bits of a */
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,
132 register BN_ULONG h, l, s;
133 BN_ULONG tab[16], top3b = a >> 61;
134 register BN_ULONG a1, a2, a4, a8;
136 a1 = a & (0x1FFFFFFFFFFFFFFFULL);
148 tab[7] = a1 ^ a2 ^ a4;
152 tab[11] = a1 ^ a2 ^ a8;
154 tab[13] = a1 ^ a4 ^ a8;
155 tab[14] = a2 ^ a4 ^ a8;
156 tab[15] = a1 ^ a2 ^ a4 ^ a8;
160 s = tab[b >> 4 & 0xF];
163 s = tab[b >> 8 & 0xF];
166 s = tab[b >> 12 & 0xF];
169 s = tab[b >> 16 & 0xF];
172 s = tab[b >> 20 & 0xF];
175 s = tab[b >> 24 & 0xF];
178 s = tab[b >> 28 & 0xF];
181 s = tab[b >> 32 & 0xF];
184 s = tab[b >> 36 & 0xF];
187 s = tab[b >> 40 & 0xF];
190 s = tab[b >> 44 & 0xF];
193 s = tab[b >> 48 & 0xF];
196 s = tab[b >> 52 & 0xF];
199 s = tab[b >> 56 & 0xF];
206 /* compensate for the top three bits of a */
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.
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)
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; */
244 void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
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.
252 int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
255 const BIGNUM *at, *bt;
260 if (a->top < b->top) {
268 if (bn_wexpand(r, at->top) == NULL)
271 for (i = 0; i < bt->top; i++) {
272 r->d[i] = at->d[i] ^ bt->d[i];
274 for (; i < at->top; i++) {
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.
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[])
301 /* reduction mod 1 => return 0 */
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.
311 if (!bn_wexpand(r, a->top))
313 for (j = 0; j < a->top; j++) {
320 /* start reduction */
321 dN = p[0] / BN_BITS2;
322 for (j = r->top - 1; j > dN;) {
330 for (k = 1; p[k] != 0; k++) {
331 /* reducing component t^p[k] */
336 z[j - n] ^= (zz >> d0);
338 z[j - n - 1] ^= (zz << d1);
341 /* reducing component t^0 */
343 d0 = p[0] % BN_BITS2;
345 z[j - n] ^= (zz >> d0);
347 z[j - n - 1] ^= (zz << d1);
350 /* final round of reduction */
353 d0 = p[0] % BN_BITS2;
359 /* clear up the top d1 bits */
361 z[dN] = (z[dN] << d1) >> d1;
364 z[0] ^= zz; /* reduction t^0 component */
366 for (k = 1; p[k] != 0; k++) {
369 /* reducing component t^p[k] */
371 d0 = p[k] % BN_BITS2;
374 if (d0 && (tmp_ulong = zz >> d1))
375 z[n + 1] ^= tmp_ulong;
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.
390 int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
396 ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
397 if (!ret || ret > (int)OSSL_NELEM(arr)) {
398 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
401 ret = BN_GF2m_mod_arr(r, a, arr);
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.
410 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
411 const int p[], BN_CTX *ctx)
413 int zlen, i, j, k, ret = 0;
415 BN_ULONG x1, x0, y1, y0, zz[4];
421 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
425 if ((s = BN_CTX_get(ctx)) == NULL)
428 zlen = a->top + b->top + 4;
429 if (!bn_wexpand(s, zlen))
433 for (i = 0; i < zlen; i++)
436 for (j = 0; j < b->top; j += 2) {
438 y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
439 for (i = 0; i < a->top; i += 2) {
441 x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
442 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
443 for (k = 0; k < 4; k++)
444 s->d[i + j + k] ^= zz[k];
449 if (BN_GF2m_mod_arr(r, s, p))
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.
465 int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
466 const BIGNUM *p, BN_CTX *ctx)
469 const int max = BN_num_bits(p) + 1;
476 arr = OPENSSL_malloc(sizeof(*arr) * max);
479 ret = BN_GF2m_poly2arr(p, arr, max);
480 if (!ret || ret > max) {
481 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
484 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
491 /* Square a, reduce the result mod p, and store it in a. r could be a. */
492 int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
500 if ((s = BN_CTX_get(ctx)) == NULL)
502 if (!bn_wexpand(s, 2 * a->top))
505 for (i = a->top - 1; i >= 0; i--) {
506 s->d[2 * i + 1] = SQR1(a->d[i]);
507 s->d[2 * i] = SQR0(a->d[i]);
512 if (!BN_GF2m_mod_arr(r, s, p))
522 * Square a, reduce the result mod p, and store it in a. r could be a. This
523 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
524 * wrapper function is only provided for convenience; for best performance,
525 * use the BN_GF2m_mod_sqr_arr function.
527 int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
530 const int max = BN_num_bits(p) + 1;
536 arr = OPENSSL_malloc(sizeof(*arr) * max);
539 ret = BN_GF2m_poly2arr(p, arr, max);
540 if (!ret || ret > max) {
541 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
544 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
552 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
553 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
554 * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
555 * Curve Cryptography Over Binary Fields".
557 static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
558 const BIGNUM *p, BN_CTX *ctx)
560 BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
575 if (!BN_GF2m_mod(u, a, p))
587 while (!BN_is_odd(u)) {
590 if (!BN_rshift1(u, u))
593 if (!BN_GF2m_add(b, b, p))
596 if (!BN_rshift1(b, b))
600 if (BN_abs_is_word(u, 1))
603 if (BN_num_bits(u) < BN_num_bits(v)) {
612 if (!BN_GF2m_add(u, u, v))
614 if (!BN_GF2m_add(b, b, c))
620 int ubits = BN_num_bits(u);
621 int vbits = BN_num_bits(v); /* v is copy of p */
623 BN_ULONG *udp, *bdp, *vdp, *cdp;
625 if (!bn_wexpand(u, top))
628 for (i = u->top; i < top; i++)
631 if (!bn_wexpand(b, top))
635 for (i = 1; i < top; i++)
638 if (!bn_wexpand(c, top))
641 for (i = 0; i < top; i++)
644 vdp = v->d; /* It pays off to "cache" *->d pointers,
645 * because it allows optimizer to be more
646 * aggressive. But we don't have to "cache"
647 * p->d, because *p is declared 'const'... */
649 while (ubits && !(udp[0] & 1)) {
650 BN_ULONG u0, u1, b0, b1, mask;
654 mask = (BN_ULONG)0 - (b0 & 1);
655 b0 ^= p->d[0] & mask;
656 for (i = 0; i < top - 1; i++) {
658 udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
660 b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
661 bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
669 if (ubits <= BN_BITS2) {
670 if (udp[0] == 0) /* poly was reducible */
691 for (i = 0; i < top; i++) {
695 if (ubits == vbits) {
697 int utop = (ubits - 1) / BN_BITS2;
699 while ((ul = udp[utop]) == 0 && utop)
701 ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
715 /* BN_CTX_end would complain about the expanded form */
725 * Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
726 * This is not constant time.
727 * But it does eliminate first order deduction on the input.
729 int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
736 if ((b = BN_CTX_get(ctx)) == NULL)
739 /* Fail on a non-sensical input p value */
740 numbits = BN_num_bits(p);
744 /* generate blinding value */
746 if (!BN_priv_rand_ex(b, numbits - 1,
747 BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
749 } while (BN_is_zero(b));
752 if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
756 if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
759 /* r := b/(a * b) = 1/a */
760 if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
771 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
772 * This function calls down to the BN_GF2m_mod_inv implementation; this
773 * wrapper function is only provided for convenience; for best performance,
774 * use the BN_GF2m_mod_inv function.
776 int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
784 if ((field = BN_CTX_get(ctx)) == NULL)
786 if (!BN_GF2m_arr2poly(p, field))
789 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
798 * Divide y by x, reduce modulo p, and store the result in r. r could be x
799 * or y, x could equal y.
801 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
802 const BIGNUM *p, BN_CTX *ctx)
812 xinv = BN_CTX_get(ctx);
816 if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
818 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
829 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
830 * * or yy, xx could equal yy. This function calls down to the
831 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
832 * convenience; for best performance, use the BN_GF2m_mod_div function.
834 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
835 const int p[], BN_CTX *ctx)
844 if ((field = BN_CTX_get(ctx)) == NULL)
846 if (!BN_GF2m_arr2poly(p, field))
849 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
858 * Compute the bth power of a, reduce modulo p, and store the result in r. r
859 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
862 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
863 const int p[], BN_CTX *ctx)
874 if (BN_abs_is_word(b, 1))
875 return (BN_copy(r, a) != NULL);
878 if ((u = BN_CTX_get(ctx)) == NULL)
881 if (!BN_GF2m_mod_arr(u, a, p))
884 n = BN_num_bits(b) - 1;
885 for (i = n - 1; i >= 0; i--) {
886 if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
888 if (BN_is_bit_set(b, i)) {
889 if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
903 * Compute the bth power of a, reduce modulo p, and store the result in r. r
904 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
905 * implementation; this wrapper function is only provided for convenience;
906 * for best performance, use the BN_GF2m_mod_exp_arr function.
908 int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
909 const BIGNUM *p, BN_CTX *ctx)
912 const int max = BN_num_bits(p) + 1;
919 arr = OPENSSL_malloc(sizeof(*arr) * max);
922 ret = BN_GF2m_poly2arr(p, arr, max);
923 if (!ret || ret > max) {
924 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
927 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
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.
938 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
947 /* reduction mod 1 => return 0 */
953 if ((u = BN_CTX_get(ctx)) == NULL)
956 if (!BN_set_bit(u, p[0] - 1))
958 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
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.
972 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
975 const int max = BN_num_bits(p) + 1;
981 arr = OPENSSL_malloc(sizeof(*arr) * max);
984 ret = BN_GF2m_poly2arr(p, arr, max);
985 if (!ret || ret > max) {
986 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
989 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
997 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
998 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
1000 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1003 int ret = 0, count = 0, j;
1004 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1009 /* reduction mod 1 => return 0 */
1015 a = BN_CTX_get(ctx);
1016 z = BN_CTX_get(ctx);
1017 w = BN_CTX_get(ctx);
1021 if (!BN_GF2m_mod_arr(a, a_, p))
1024 if (BN_is_zero(a)) {
1030 if (p[0] & 0x1) { /* m is odd */
1031 /* compute half-trace of a */
1034 for (j = 1; j <= (p[0] - 1) / 2; j++) {
1035 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1037 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1039 if (!BN_GF2m_add(z, z, a))
1043 } else { /* m is even */
1045 rho = BN_CTX_get(ctx);
1046 w2 = BN_CTX_get(ctx);
1047 tmp = BN_CTX_get(ctx);
1051 if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
1054 if (!BN_GF2m_mod_arr(rho, rho, p))
1057 if (!BN_copy(w, rho))
1059 for (j = 1; j <= p[0] - 1; j++) {
1060 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1062 if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1064 if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1066 if (!BN_GF2m_add(z, z, tmp))
1068 if (!BN_GF2m_add(w, w2, rho))
1072 } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1073 if (BN_is_zero(w)) {
1074 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
1079 if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1081 if (!BN_GF2m_add(w, z, w))
1083 if (BN_GF2m_cmp(w, a)) {
1084 ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
1100 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
1101 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1102 * implementation; this wrapper function is only provided for convenience;
1103 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1105 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1109 const int max = BN_num_bits(p) + 1;
1115 arr = OPENSSL_malloc(sizeof(*arr) * max);
1118 ret = BN_GF2m_poly2arr(p, arr, max);
1119 if (!ret || ret > max) {
1120 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
1123 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1131 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1132 * x^i) into an array of integers corresponding to the bits with non-zero
1133 * coefficient. Array is terminated with -1. Up to max elements of the array
1134 * will be filled. Return value is total number of array elements that would
1135 * be filled if array was large enough.
1137 int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1145 for (i = a->top - 1; i >= 0; i--) {
1147 /* skip word if a->d[i] == 0 */
1150 for (j = BN_BITS2 - 1; j >= 0; j--) {
1151 if (a->d[i] & mask) {
1153 p[k] = BN_BITS2 * i + j;
1169 * Convert the coefficient array representation of a polynomial to a
1170 * bit-string. The array must be terminated by -1.
1172 int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1178 for (i = 0; p[i] != -1; i++) {
1179 if (BN_set_bit(a, p[i]) == 0)