michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ec2.h" michael@0: #include "mp_gf2m.h" michael@0: #include "mp_gf2m-priv.h" michael@0: #include "mpi.h" michael@0: #include "mpi-priv.h" michael@0: #include michael@0: michael@0: /* Fast reduction for polynomials over a 193-bit curve. Assumes reduction michael@0: * polynomial with terms {193, 15, 0}. */ michael@0: mp_err michael@0: ec_GF2m_193_mod(const mp_int *a, mp_int *r, const GFMethod *meth) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_digit *u, z; michael@0: michael@0: if (a != r) { michael@0: MP_CHECKOK(mp_copy(a, r)); michael@0: } michael@0: #ifdef ECL_SIXTY_FOUR_BIT michael@0: if (MP_USED(r) < 7) { michael@0: MP_CHECKOK(s_mp_pad(r, 7)); michael@0: } michael@0: u = MP_DIGITS(r); michael@0: MP_USED(r) = 7; michael@0: michael@0: /* u[6] only has 2 significant bits */ michael@0: z = u[6]; michael@0: u[3] ^= (z << 14) ^ (z >> 1); michael@0: u[2] ^= (z << 63); michael@0: z = u[5]; michael@0: u[3] ^= (z >> 50); michael@0: u[2] ^= (z << 14) ^ (z >> 1); michael@0: u[1] ^= (z << 63); michael@0: z = u[4]; michael@0: u[2] ^= (z >> 50); michael@0: u[1] ^= (z << 14) ^ (z >> 1); michael@0: u[0] ^= (z << 63); michael@0: z = u[3] >> 1; /* z only has 63 significant bits */ michael@0: u[1] ^= (z >> 49); michael@0: u[0] ^= (z << 15) ^ z; michael@0: /* clear bits above 193 */ michael@0: u[6] = u[5] = u[4] = 0; michael@0: u[3] ^= z << 1; michael@0: #else michael@0: if (MP_USED(r) < 13) { michael@0: MP_CHECKOK(s_mp_pad(r, 13)); michael@0: } michael@0: u = MP_DIGITS(r); michael@0: MP_USED(r) = 13; michael@0: michael@0: /* u[12] only has 2 significant bits */ michael@0: z = u[12]; michael@0: u[6] ^= (z << 14) ^ (z >> 1); michael@0: u[5] ^= (z << 31); michael@0: z = u[11]; michael@0: u[6] ^= (z >> 18); michael@0: u[5] ^= (z << 14) ^ (z >> 1); michael@0: u[4] ^= (z << 31); michael@0: z = u[10]; michael@0: u[5] ^= (z >> 18); michael@0: u[4] ^= (z << 14) ^ (z >> 1); michael@0: u[3] ^= (z << 31); michael@0: z = u[9]; michael@0: u[4] ^= (z >> 18); michael@0: u[3] ^= (z << 14) ^ (z >> 1); michael@0: u[2] ^= (z << 31); michael@0: z = u[8]; michael@0: u[3] ^= (z >> 18); michael@0: u[2] ^= (z << 14) ^ (z >> 1); michael@0: u[1] ^= (z << 31); michael@0: z = u[7]; michael@0: u[2] ^= (z >> 18); michael@0: u[1] ^= (z << 14) ^ (z >> 1); michael@0: u[0] ^= (z << 31); michael@0: z = u[6] >> 1; /* z only has 31 significant bits */ michael@0: u[1] ^= (z >> 17); michael@0: u[0] ^= (z << 15) ^ z; michael@0: /* clear bits above 193 */ michael@0: u[12] = u[11] = u[10] = u[9] = u[8] = u[7] = 0; michael@0: u[6] ^= z << 1; michael@0: #endif michael@0: s_mp_clamp(r); michael@0: michael@0: CLEANUP: michael@0: return res; michael@0: } michael@0: michael@0: /* Fast squaring for polynomials over a 193-bit curve. Assumes reduction michael@0: * polynomial with terms {193, 15, 0}. */ michael@0: mp_err michael@0: ec_GF2m_193_sqr(const mp_int *a, mp_int *r, const GFMethod *meth) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_digit *u, *v; michael@0: michael@0: v = MP_DIGITS(a); michael@0: michael@0: #ifdef ECL_SIXTY_FOUR_BIT michael@0: if (MP_USED(a) < 4) { michael@0: return mp_bsqrmod(a, meth->irr_arr, r); michael@0: } michael@0: if (MP_USED(r) < 7) { michael@0: MP_CHECKOK(s_mp_pad(r, 7)); michael@0: } michael@0: MP_USED(r) = 7; michael@0: #else michael@0: if (MP_USED(a) < 7) { michael@0: return mp_bsqrmod(a, meth->irr_arr, r); michael@0: } michael@0: if (MP_USED(r) < 13) { michael@0: MP_CHECKOK(s_mp_pad(r, 13)); michael@0: } michael@0: MP_USED(r) = 13; michael@0: #endif michael@0: u = MP_DIGITS(r); michael@0: michael@0: #ifdef ECL_THIRTY_TWO_BIT michael@0: u[12] = gf2m_SQR0(v[6]); michael@0: u[11] = gf2m_SQR1(v[5]); michael@0: u[10] = gf2m_SQR0(v[5]); michael@0: u[9] = gf2m_SQR1(v[4]); michael@0: u[8] = gf2m_SQR0(v[4]); michael@0: u[7] = gf2m_SQR1(v[3]); michael@0: #endif michael@0: u[6] = gf2m_SQR0(v[3]); michael@0: u[5] = gf2m_SQR1(v[2]); michael@0: u[4] = gf2m_SQR0(v[2]); michael@0: u[3] = gf2m_SQR1(v[1]); michael@0: u[2] = gf2m_SQR0(v[1]); michael@0: u[1] = gf2m_SQR1(v[0]); michael@0: u[0] = gf2m_SQR0(v[0]); michael@0: return ec_GF2m_193_mod(r, r, meth); michael@0: michael@0: CLEANUP: michael@0: return res; michael@0: } michael@0: michael@0: /* Fast multiplication for polynomials over a 193-bit curve. Assumes michael@0: * reduction polynomial with terms {193, 15, 0}. */ michael@0: mp_err michael@0: ec_GF2m_193_mul(const mp_int *a, const mp_int *b, mp_int *r, michael@0: const GFMethod *meth) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0; michael@0: michael@0: #ifdef ECL_THIRTY_TWO_BIT michael@0: mp_digit a6 = 0, a5 = 0, a4 = 0, b6 = 0, b5 = 0, b4 = 0; michael@0: mp_digit rm[8]; michael@0: #endif michael@0: michael@0: if (a == b) { michael@0: return ec_GF2m_193_sqr(a, r, meth); michael@0: } else { michael@0: switch (MP_USED(a)) { michael@0: #ifdef ECL_THIRTY_TWO_BIT michael@0: case 7: michael@0: a6 = MP_DIGIT(a, 6); michael@0: case 6: michael@0: a5 = MP_DIGIT(a, 5); michael@0: case 5: michael@0: a4 = MP_DIGIT(a, 4); michael@0: #endif michael@0: case 4: michael@0: a3 = MP_DIGIT(a, 3); michael@0: case 3: michael@0: a2 = MP_DIGIT(a, 2); michael@0: case 2: michael@0: a1 = MP_DIGIT(a, 1); michael@0: default: michael@0: a0 = MP_DIGIT(a, 0); michael@0: } michael@0: switch (MP_USED(b)) { michael@0: #ifdef ECL_THIRTY_TWO_BIT michael@0: case 7: michael@0: b6 = MP_DIGIT(b, 6); michael@0: case 6: michael@0: b5 = MP_DIGIT(b, 5); michael@0: case 5: michael@0: b4 = MP_DIGIT(b, 4); michael@0: #endif michael@0: case 4: michael@0: b3 = MP_DIGIT(b, 3); michael@0: case 3: michael@0: b2 = MP_DIGIT(b, 2); michael@0: case 2: michael@0: b1 = MP_DIGIT(b, 1); michael@0: default: michael@0: b0 = MP_DIGIT(b, 0); michael@0: } michael@0: #ifdef ECL_SIXTY_FOUR_BIT michael@0: MP_CHECKOK(s_mp_pad(r, 8)); michael@0: s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0); michael@0: MP_USED(r) = 8; michael@0: s_mp_clamp(r); michael@0: #else michael@0: MP_CHECKOK(s_mp_pad(r, 14)); michael@0: s_bmul_3x3(MP_DIGITS(r) + 8, a6, a5, a4, b6, b5, b4); michael@0: s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0); michael@0: s_bmul_4x4(rm, a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b3, b6 ^ b2, b5 ^ b1, michael@0: b4 ^ b0); michael@0: rm[7] ^= MP_DIGIT(r, 7); michael@0: rm[6] ^= MP_DIGIT(r, 6); michael@0: rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13); michael@0: rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12); michael@0: rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11); michael@0: rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10); michael@0: rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9); michael@0: rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8); michael@0: MP_DIGIT(r, 11) ^= rm[7]; michael@0: MP_DIGIT(r, 10) ^= rm[6]; michael@0: MP_DIGIT(r, 9) ^= rm[5]; michael@0: MP_DIGIT(r, 8) ^= rm[4]; michael@0: MP_DIGIT(r, 7) ^= rm[3]; michael@0: MP_DIGIT(r, 6) ^= rm[2]; michael@0: MP_DIGIT(r, 5) ^= rm[1]; michael@0: MP_DIGIT(r, 4) ^= rm[0]; michael@0: MP_USED(r) = 14; michael@0: s_mp_clamp(r); michael@0: #endif michael@0: return ec_GF2m_193_mod(r, r, meth); michael@0: } michael@0: michael@0: CLEANUP: michael@0: return res; michael@0: } michael@0: michael@0: /* Wire in fast field arithmetic for 193-bit curves. */ michael@0: mp_err michael@0: ec_group_set_gf2m193(ECGroup *group, ECCurveName name) michael@0: { michael@0: group->meth->field_mod = &ec_GF2m_193_mod; michael@0: group->meth->field_mul = &ec_GF2m_193_mul; michael@0: group->meth->field_sqr = &ec_GF2m_193_sqr; michael@0: return MP_OKAY; michael@0: }