andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "mp_gf2m.h" andre@0: #include "mp_gf2m-priv.h" andre@0: #include "mplogic.h" andre@0: #include "mpi-priv.h" andre@0: andre@0: const mp_digit mp_gf2m_sqr_tb[16] = andre@0: { andre@0: 0, 1, 4, 5, 16, 17, 20, 21, andre@0: 64, 65, 68, 69, 80, 81, 84, 85 andre@0: }; andre@0: andre@0: /* Multiply two binary polynomials mp_digits a, b. andre@0: * Result is a polynomial with degree < 2 * MP_DIGIT_BITS - 1. andre@0: * Output in two mp_digits rh, rl. andre@0: */ andre@0: #if MP_DIGIT_BITS == 32 andre@0: void andre@0: s_bmul_1x1(mp_digit *rh, mp_digit *rl, const mp_digit a, const mp_digit b) andre@0: { andre@0: register mp_digit h, l, s; andre@0: mp_digit tab[8], top2b = a >> 30; andre@0: register mp_digit a1, a2, a4; andre@0: andre@0: a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1; andre@0: andre@0: tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2; andre@0: tab[4] = a4; tab[5] = a1^a4; tab[6] = a2^a4; tab[7] = a1^a2^a4; andre@0: andre@0: s = tab[b & 0x7]; l = s; andre@0: s = tab[b >> 3 & 0x7]; l ^= s << 3; h = s >> 29; andre@0: s = tab[b >> 6 & 0x7]; l ^= s << 6; h ^= s >> 26; andre@0: s = tab[b >> 9 & 0x7]; l ^= s << 9; h ^= s >> 23; andre@0: s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20; andre@0: s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17; andre@0: s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14; andre@0: s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11; andre@0: s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >> 8; andre@0: s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >> 5; andre@0: s = tab[b >> 30 ]; l ^= s << 30; h ^= s >> 2; andre@0: andre@0: /* compensate for the top two bits of a */ andre@0: andre@0: if (top2b & 01) { l ^= b << 30; h ^= b >> 2; } andre@0: if (top2b & 02) { l ^= b << 31; h ^= b >> 1; } andre@0: andre@0: *rh = h; *rl = l; andre@0: } andre@0: #else andre@0: void andre@0: s_bmul_1x1(mp_digit *rh, mp_digit *rl, const mp_digit a, const mp_digit b) andre@0: { andre@0: register mp_digit h, l, s; andre@0: mp_digit tab[16], top3b = a >> 61; andre@0: register mp_digit a1, a2, a4, a8; andre@0: andre@0: a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; andre@0: a4 = a2 << 1; a8 = a4 << 1; andre@0: tab[ 0] = 0; tab[ 1] = a1; tab[ 2] = a2; tab[ 3] = a1^a2; andre@0: tab[ 4] = a4; tab[ 5] = a1^a4; tab[ 6] = a2^a4; tab[ 7] = a1^a2^a4; andre@0: tab[ 8] = a8; tab[ 9] = a1^a8; tab[10] = a2^a8; tab[11] = a1^a2^a8; andre@0: tab[12] = a4^a8; tab[13] = a1^a4^a8; tab[14] = a2^a4^a8; tab[15] = a1^a2^a4^a8; andre@0: andre@0: s = tab[b & 0xF]; l = s; andre@0: s = tab[b >> 4 & 0xF]; l ^= s << 4; h = s >> 60; andre@0: s = tab[b >> 8 & 0xF]; l ^= s << 8; h ^= s >> 56; andre@0: s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52; andre@0: s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48; andre@0: s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44; andre@0: s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40; andre@0: s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36; andre@0: s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32; andre@0: s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28; andre@0: s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24; andre@0: s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20; andre@0: s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16; andre@0: s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12; andre@0: s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >> 8; andre@0: s = tab[b >> 60 ]; l ^= s << 60; h ^= s >> 4; andre@0: andre@0: /* compensate for the top three bits of a */ andre@0: andre@0: if (top3b & 01) { l ^= b << 61; h ^= b >> 3; } andre@0: if (top3b & 02) { l ^= b << 62; h ^= b >> 2; } andre@0: if (top3b & 04) { l ^= b << 63; h ^= b >> 1; } andre@0: andre@0: *rh = h; *rl = l; andre@0: } andre@0: #endif andre@0: andre@0: /* Compute xor-multiply of two binary polynomials (a1, a0) x (b1, b0) andre@0: * result is a binary polynomial in 4 mp_digits r[4]. andre@0: * The caller MUST ensure that r has the right amount of space allocated. andre@0: */ andre@0: void andre@0: s_bmul_2x2(mp_digit *r, const mp_digit a1, const mp_digit a0, const mp_digit b1, andre@0: const mp_digit b0) andre@0: { andre@0: mp_digit m1, m0; andre@0: /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */ andre@0: s_bmul_1x1(r+3, r+2, a1, b1); andre@0: s_bmul_1x1(r+1, r, a0, b0); andre@0: s_bmul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1); andre@0: /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */ andre@0: r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */ andre@0: r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */ andre@0: } andre@0: andre@0: /* Compute xor-multiply of two binary polynomials (a2, a1, a0) x (b2, b1, b0) andre@0: * result is a binary polynomial in 6 mp_digits r[6]. andre@0: * The caller MUST ensure that r has the right amount of space allocated. andre@0: */ andre@0: void andre@0: s_bmul_3x3(mp_digit *r, const mp_digit a2, const mp_digit a1, const mp_digit a0, andre@0: const mp_digit b2, const mp_digit b1, const mp_digit b0) andre@0: { andre@0: mp_digit zm[4]; andre@0: andre@0: s_bmul_1x1(r+5, r+4, a2, b2); /* fill top 2 words */ andre@0: s_bmul_2x2(zm, a1, a2^a0, b1, b2^b0); /* fill middle 4 words */ andre@0: s_bmul_2x2(r, a1, a0, b1, b0); /* fill bottom 4 words */ andre@0: andre@0: zm[3] ^= r[3]; andre@0: zm[2] ^= r[2]; andre@0: zm[1] ^= r[1] ^ r[5]; andre@0: zm[0] ^= r[0] ^ r[4]; andre@0: andre@0: r[5] ^= zm[3]; andre@0: r[4] ^= zm[2]; andre@0: r[3] ^= zm[1]; andre@0: r[2] ^= zm[0]; andre@0: } andre@0: andre@0: /* Compute xor-multiply of two binary polynomials (a3, a2, a1, a0) x (b3, b2, b1, b0) andre@0: * result is a binary polynomial in 8 mp_digits r[8]. andre@0: * The caller MUST ensure that r has the right amount of space allocated. andre@0: */ andre@0: void s_bmul_4x4(mp_digit *r, const mp_digit a3, const mp_digit a2, const mp_digit a1, andre@0: const mp_digit a0, const mp_digit b3, const mp_digit b2, const mp_digit b1, andre@0: const mp_digit b0) andre@0: { andre@0: mp_digit zm[4]; andre@0: andre@0: s_bmul_2x2(r+4, a3, a2, b3, b2); /* fill top 4 words */ andre@0: s_bmul_2x2(zm, a3^a1, a2^a0, b3^b1, b2^b0); /* fill middle 4 words */ andre@0: s_bmul_2x2(r, a1, a0, b1, b0); /* fill bottom 4 words */ andre@0: andre@0: zm[3] ^= r[3] ^ r[7]; andre@0: zm[2] ^= r[2] ^ r[6]; andre@0: zm[1] ^= r[1] ^ r[5]; andre@0: zm[0] ^= r[0] ^ r[4]; andre@0: andre@0: r[5] ^= zm[3]; andre@0: r[4] ^= zm[2]; andre@0: r[3] ^= zm[1]; andre@0: r[2] ^= zm[0]; andre@0: } andre@0: andre@0: /* Compute addition of two binary polynomials a and b, andre@0: * store result in c; c could be a or b, a and b could be equal; andre@0: * c is the bitwise XOR of a and b. andre@0: */ andre@0: mp_err andre@0: mp_badd(const mp_int *a, const mp_int *b, mp_int *c) andre@0: { andre@0: mp_digit *pa, *pb, *pc; andre@0: mp_size ix; andre@0: mp_size used_pa, used_pb; andre@0: mp_err res = MP_OKAY; andre@0: andre@0: /* Add all digits up to the precision of b. If b had more andre@0: * precision than a initially, swap a, b first andre@0: */ andre@0: if (MP_USED(a) >= MP_USED(b)) { andre@0: pa = MP_DIGITS(a); andre@0: pb = MP_DIGITS(b); andre@0: used_pa = MP_USED(a); andre@0: used_pb = MP_USED(b); andre@0: } else { andre@0: pa = MP_DIGITS(b); andre@0: pb = MP_DIGITS(a); andre@0: used_pa = MP_USED(b); andre@0: used_pb = MP_USED(a); andre@0: } andre@0: andre@0: /* Make sure c has enough precision for the output value */ andre@0: MP_CHECKOK( s_mp_pad(c, used_pa) ); andre@0: andre@0: /* Do word-by-word xor */ andre@0: pc = MP_DIGITS(c); andre@0: for (ix = 0; ix < used_pb; ix++) { andre@0: (*pc++) = (*pa++) ^ (*pb++); andre@0: } andre@0: andre@0: /* Finish the rest of digits until we're actually done */ andre@0: for (; ix < used_pa; ++ix) { andre@0: *pc++ = *pa++; andre@0: } andre@0: andre@0: MP_USED(c) = used_pa; andre@0: MP_SIGN(c) = ZPOS; andre@0: s_mp_clamp(c); andre@0: andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: #define s_mp_div2(a) MP_CHECKOK( mpl_rsh((a), (a), 1) ); andre@0: andre@0: /* Compute binary polynomial multiply d = a * b */ andre@0: static void andre@0: s_bmul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *d) andre@0: { andre@0: mp_digit a_i, a0b0, a1b1, carry = 0; andre@0: while (a_len--) { andre@0: a_i = *a++; andre@0: s_bmul_1x1(&a1b1, &a0b0, a_i, b); andre@0: *d++ = a0b0 ^ carry; andre@0: carry = a1b1; andre@0: } andre@0: *d = carry; andre@0: } andre@0: andre@0: /* Compute binary polynomial xor multiply accumulate d ^= a * b */ andre@0: static void andre@0: s_bmul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *d) andre@0: { andre@0: mp_digit a_i, a0b0, a1b1, carry = 0; andre@0: while (a_len--) { andre@0: a_i = *a++; andre@0: s_bmul_1x1(&a1b1, &a0b0, a_i, b); andre@0: *d++ ^= a0b0 ^ carry; andre@0: carry = a1b1; andre@0: } andre@0: *d ^= carry; andre@0: } andre@0: andre@0: /* Compute binary polynomial xor multiply c = a * b. andre@0: * All parameters may be identical. andre@0: */ andre@0: mp_err andre@0: mp_bmul(const mp_int *a, const mp_int *b, mp_int *c) andre@0: { andre@0: mp_digit *pb, b_i; andre@0: mp_int tmp; andre@0: mp_size ib, a_used, b_used; andre@0: mp_err res = MP_OKAY; andre@0: andre@0: MP_DIGITS(&tmp) = 0; andre@0: andre@0: ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); andre@0: andre@0: if (a == c) { andre@0: MP_CHECKOK( mp_init_copy(&tmp, a) ); andre@0: if (a == b) andre@0: b = &tmp; andre@0: a = &tmp; andre@0: } else if (b == c) { andre@0: MP_CHECKOK( mp_init_copy(&tmp, b) ); andre@0: b = &tmp; andre@0: } andre@0: andre@0: if (MP_USED(a) < MP_USED(b)) { andre@0: const mp_int *xch = b; /* switch a and b if b longer */ andre@0: b = a; andre@0: a = xch; andre@0: } andre@0: andre@0: MP_USED(c) = 1; MP_DIGIT(c, 0) = 0; andre@0: MP_CHECKOK( s_mp_pad(c, USED(a) + USED(b)) ); andre@0: andre@0: pb = MP_DIGITS(b); andre@0: s_bmul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c)); andre@0: andre@0: /* Outer loop: Digits of b */ andre@0: a_used = MP_USED(a); andre@0: b_used = MP_USED(b); andre@0: MP_USED(c) = a_used + b_used; andre@0: for (ib = 1; ib < b_used; ib++) { andre@0: b_i = *pb++; andre@0: andre@0: /* Inner product: Digits of a */ andre@0: if (b_i) andre@0: s_bmul_d_add(MP_DIGITS(a), a_used, b_i, MP_DIGITS(c) + ib); andre@0: else andre@0: MP_DIGIT(c, ib + a_used) = b_i; andre@0: } andre@0: andre@0: s_mp_clamp(c); andre@0: andre@0: SIGN(c) = ZPOS; andre@0: andre@0: CLEANUP: andre@0: mp_clear(&tmp); andre@0: return res; andre@0: } andre@0: andre@0: andre@0: /* Compute modular reduction of a and store result in r. andre@0: * r could be a. andre@0: * For modular arithmetic, the irreducible polynomial f(t) is represented andre@0: * as an array of int[], where f(t) is of the form: andre@0: * f(t) = t^p[0] + t^p[1] + ... + t^p[k] andre@0: * where m = p[0] > p[1] > ... > p[k] = 0. andre@0: */ andre@0: mp_err andre@0: mp_bmod(const mp_int *a, const unsigned int p[], mp_int *r) andre@0: { andre@0: int j, k; andre@0: int n, dN, d0, d1; andre@0: mp_digit zz, *z, tmp; andre@0: mp_size used; andre@0: mp_err res = MP_OKAY; andre@0: andre@0: /* The algorithm does the reduction in place in r, andre@0: * if a != r, copy a into r first so reduction can be done in r andre@0: */ andre@0: if (a != r) { andre@0: MP_CHECKOK( mp_copy(a, r) ); andre@0: } andre@0: z = MP_DIGITS(r); andre@0: andre@0: /* start reduction */ andre@0: /*dN = p[0] / MP_DIGIT_BITS; */ andre@0: dN = p[0] >> MP_DIGIT_BITS_LOG_2; andre@0: used = MP_USED(r); andre@0: andre@0: for (j = used - 1; j > dN;) { andre@0: andre@0: zz = z[j]; andre@0: if (zz == 0) { andre@0: j--; continue; andre@0: } andre@0: z[j] = 0; andre@0: andre@0: for (k = 1; p[k] > 0; k++) { andre@0: /* reducing component t^p[k] */ andre@0: n = p[0] - p[k]; andre@0: /*d0 = n % MP_DIGIT_BITS; */ andre@0: d0 = n & MP_DIGIT_BITS_MASK; andre@0: d1 = MP_DIGIT_BITS - d0; andre@0: /*n /= MP_DIGIT_BITS; */ andre@0: n >>= MP_DIGIT_BITS_LOG_2; andre@0: z[j-n] ^= (zz>>d0); andre@0: if (d0) andre@0: z[j-n-1] ^= (zz<> d0); andre@0: if (d0) andre@0: z[j-n-1] ^= (zz << d1); andre@0: andre@0: } andre@0: andre@0: /* final round of reduction */ andre@0: while (j == dN) { andre@0: andre@0: /* d0 = p[0] % MP_DIGIT_BITS; */ andre@0: d0 = p[0] & MP_DIGIT_BITS_MASK; andre@0: zz = z[dN] >> d0; andre@0: if (zz == 0) break; andre@0: d1 = MP_DIGIT_BITS - d0; andre@0: andre@0: /* clear up the top d1 bits */ andre@0: if (d0) { andre@0: z[dN] = (z[dN] << d1) >> d1; andre@0: } else { andre@0: z[dN] = 0; andre@0: } andre@0: *z ^= zz; /* reduction t^0 component */ andre@0: andre@0: for (k = 1; p[k] > 0; k++) { andre@0: /* reducing component t^p[k]*/ andre@0: /* n = p[k] / MP_DIGIT_BITS; */ andre@0: n = p[k] >> MP_DIGIT_BITS_LOG_2; andre@0: /* d0 = p[k] % MP_DIGIT_BITS; */ andre@0: d0 = p[k] & MP_DIGIT_BITS_MASK; andre@0: d1 = MP_DIGIT_BITS - d0; andre@0: z[n] ^= (zz << d0); andre@0: tmp = zz >> d1; andre@0: if (d0 && tmp) andre@0: z[n+1] ^= tmp; andre@0: } andre@0: } andre@0: andre@0: s_mp_clamp(r); andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Compute the product of two polynomials a and b, reduce modulo p, andre@0: * Store the result in r. r could be a or b; a could be b. andre@0: */ andre@0: mp_err andre@0: mp_bmulmod(const mp_int *a, const mp_int *b, const unsigned int p[], mp_int *r) andre@0: { andre@0: mp_err res; andre@0: andre@0: if (a == b) return mp_bsqrmod(a, p, r); andre@0: if ((res = mp_bmul(a, b, r) ) != MP_OKAY) andre@0: return res; andre@0: return mp_bmod(r, p, r); andre@0: } andre@0: andre@0: /* Compute binary polynomial squaring c = a*a mod p . andre@0: * Parameter r and a can be identical. andre@0: */ andre@0: andre@0: mp_err andre@0: mp_bsqrmod(const mp_int *a, const unsigned int p[], mp_int *r) andre@0: { andre@0: mp_digit *pa, *pr, a_i; andre@0: mp_int tmp; andre@0: mp_size ia, a_used; andre@0: mp_err res; andre@0: andre@0: ARGCHK(a != NULL && r != NULL, MP_BADARG); andre@0: MP_DIGITS(&tmp) = 0; andre@0: andre@0: if (a == r) { andre@0: MP_CHECKOK( mp_init_copy(&tmp, a) ); andre@0: a = &tmp; andre@0: } andre@0: andre@0: MP_USED(r) = 1; MP_DIGIT(r, 0) = 0; andre@0: MP_CHECKOK( s_mp_pad(r, 2*USED(a)) ); andre@0: andre@0: pa = MP_DIGITS(a); andre@0: pr = MP_DIGITS(r); andre@0: a_used = MP_USED(a); andre@0: MP_USED(r) = 2 * a_used; andre@0: andre@0: for (ia = 0; ia < a_used; ia++) { andre@0: a_i = *pa++; andre@0: *pr++ = gf2m_SQR0(a_i); andre@0: *pr++ = gf2m_SQR1(a_i); andre@0: } andre@0: andre@0: MP_CHECKOK( mp_bmod(r, p, r) ); andre@0: s_mp_clamp(r); andre@0: SIGN(r) = ZPOS; andre@0: andre@0: CLEANUP: andre@0: mp_clear(&tmp); andre@0: return res; andre@0: } andre@0: andre@0: /* Compute binary polynomial y/x mod p, y divided by x, reduce modulo p. andre@0: * Store the result in r. r could be x or y, and x could equal y. andre@0: * Uses algorithm Modular_Division_GF(2^m) from andre@0: * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to andre@0: * the Great Divide". andre@0: */ andre@0: int andre@0: mp_bdivmod(const mp_int *y, const mp_int *x, const mp_int *pp, andre@0: const unsigned int p[], mp_int *r) andre@0: { andre@0: mp_int aa, bb, uu; andre@0: mp_int *a, *b, *u, *v; andre@0: mp_err res = MP_OKAY; andre@0: andre@0: MP_DIGITS(&aa) = 0; andre@0: MP_DIGITS(&bb) = 0; andre@0: MP_DIGITS(&uu) = 0; andre@0: andre@0: MP_CHECKOK( mp_init_copy(&aa, x) ); andre@0: MP_CHECKOK( mp_init_copy(&uu, y) ); andre@0: MP_CHECKOK( mp_init_copy(&bb, pp) ); andre@0: MP_CHECKOK( s_mp_pad(r, USED(pp)) ); andre@0: MP_USED(r) = 1; MP_DIGIT(r, 0) = 0; andre@0: andre@0: a = &aa; b= &bb; u=&uu; v=r; andre@0: /* reduce x and y mod p */ andre@0: MP_CHECKOK( mp_bmod(a, p, a) ); andre@0: MP_CHECKOK( mp_bmod(u, p, u) ); andre@0: andre@0: while (!mp_isodd(a)) { andre@0: s_mp_div2(a); andre@0: if (mp_isodd(u)) { andre@0: MP_CHECKOK( mp_badd(u, pp, u) ); andre@0: } andre@0: s_mp_div2(u); andre@0: } andre@0: andre@0: do { andre@0: if (mp_cmp_mag(b, a) > 0) { andre@0: MP_CHECKOK( mp_badd(b, a, b) ); andre@0: MP_CHECKOK( mp_badd(v, u, v) ); andre@0: do { andre@0: s_mp_div2(b); andre@0: if (mp_isodd(v)) { andre@0: MP_CHECKOK( mp_badd(v, pp, v) ); andre@0: } andre@0: s_mp_div2(v); andre@0: } while (!mp_isodd(b)); andre@0: } andre@0: else if ((MP_DIGIT(a,0) == 1) && (MP_USED(a) == 1)) andre@0: break; andre@0: else { andre@0: MP_CHECKOK( mp_badd(a, b, a) ); andre@0: MP_CHECKOK( mp_badd(u, v, u) ); andre@0: do { andre@0: s_mp_div2(a); andre@0: if (mp_isodd(u)) { andre@0: MP_CHECKOK( mp_badd(u, pp, u) ); andre@0: } andre@0: s_mp_div2(u); andre@0: } while (!mp_isodd(a)); andre@0: } andre@0: } while (1); andre@0: andre@0: MP_CHECKOK( mp_copy(u, r) ); andre@0: andre@0: CLEANUP: andre@0: mp_clear(&aa); andre@0: mp_clear(&bb); andre@0: mp_clear(&uu); andre@0: return res; andre@0: andre@0: } andre@0: andre@0: /* Convert the bit-string representation of a polynomial a into an array andre@0: * of integers corresponding to the bits with non-zero coefficient. andre@0: * Up to max elements of the array will be filled. Return value is total andre@0: * number of coefficients that would be extracted if array was large enough. andre@0: */ andre@0: int andre@0: mp_bpoly2arr(const mp_int *a, unsigned int p[], int max) andre@0: { andre@0: int i, j, k; andre@0: mp_digit top_bit, mask; andre@0: andre@0: top_bit = 1; andre@0: top_bit <<= MP_DIGIT_BIT - 1; andre@0: andre@0: for (k = 0; k < max; k++) p[k] = 0; andre@0: k = 0; andre@0: andre@0: for (i = MP_USED(a) - 1; i >= 0; i--) { andre@0: mask = top_bit; andre@0: for (j = MP_DIGIT_BIT - 1; j >= 0; j--) { andre@0: if (MP_DIGITS(a)[i] & mask) { andre@0: if (k < max) p[k] = MP_DIGIT_BIT * i + j; andre@0: k++; andre@0: } andre@0: mask >>= 1; andre@0: } andre@0: } andre@0: andre@0: return k; andre@0: } andre@0: andre@0: /* Convert the coefficient array representation of a polynomial to a andre@0: * bit-string. The array must be terminated by 0. andre@0: */ andre@0: mp_err andre@0: mp_barr2poly(const unsigned int p[], mp_int *a) andre@0: { andre@0: andre@0: mp_err res = MP_OKAY; andre@0: int i; andre@0: andre@0: mp_zero(a); andre@0: for (i = 0; p[i] > 0; i++) { andre@0: MP_CHECKOK( mpl_set_bit(a, p[i], 1) ); andre@0: } andre@0: MP_CHECKOK( mpl_set_bit(a, 0, 1) ); andre@0: andre@0: CLEANUP: andre@0: return res; andre@0: }