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 "ecp_fp.h" michael@0: #include michael@0: michael@0: #define ECFP_BSIZE 160 michael@0: #define ECFP_NUMDOUBLES 7 michael@0: michael@0: #include "ecp_fpinc.c" michael@0: michael@0: /* Performs a single step of reduction, just on the uppermost float michael@0: * (assumes already tidied), and then retidies. Note, this does not michael@0: * guarantee that the result will be less than p, but truncates the number michael@0: * of bits. */ michael@0: void michael@0: ecfp160_singleReduce(double *d, const EC_group_fp * group) michael@0: { michael@0: double q; michael@0: michael@0: ECFP_ASSERT(group->doubleBitSize == 24); michael@0: ECFP_ASSERT(group->primeBitSize == 160); michael@0: ECFP_ASSERT(ECFP_NUMDOUBLES == 7); michael@0: michael@0: q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_160; michael@0: q += group->bitSize_alpha; michael@0: q -= group->bitSize_alpha; michael@0: michael@0: d[ECFP_NUMDOUBLES - 1] -= q; michael@0: d[0] += q * ecfp_twom160; michael@0: d[1] += q * ecfp_twom129; michael@0: ecfp_positiveTidy(d, group); michael@0: michael@0: /* Assertions for the highest order term */ michael@0: ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] / ecfp_exp[ECFP_NUMDOUBLES - 1] == michael@0: (unsigned long long) (d[ECFP_NUMDOUBLES - 1] / michael@0: ecfp_exp[ECFP_NUMDOUBLES - 1])); michael@0: ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] >= 0); michael@0: } michael@0: michael@0: /* Performs imperfect reduction. This might leave some negative terms, michael@0: * and one more reduction might be required for the result to be between 0 michael@0: * and p-1. x should not already be reduced, i.e. should have michael@0: * 2*ECFP_NUMDOUBLES significant terms. x and r can be the same, but then michael@0: * the upper parts of r are not zeroed */ michael@0: void michael@0: ecfp160_reduce(double *r, double *x, const EC_group_fp * group) michael@0: { michael@0: michael@0: double x7, x8, q; michael@0: michael@0: ECFP_ASSERT(group->doubleBitSize == 24); michael@0: ECFP_ASSERT(group->primeBitSize == 160); michael@0: ECFP_ASSERT(ECFP_NUMDOUBLES == 7); michael@0: michael@0: /* Tidy just the upper bits, the lower bits can wait. */ michael@0: ecfp_tidyUpper(x, group); michael@0: michael@0: /* Assume that this is already tidied so that we have enough extra michael@0: * bits */ michael@0: x7 = x[7] + x[13] * ecfp_twom129; /* adds bits 15-39 */ michael@0: michael@0: /* Tidy x7, or we won't have enough bits later to add it in */ michael@0: q = x7 + group->alpha[8]; michael@0: q -= group->alpha[8]; michael@0: x7 -= q; /* holds bits 0-24 */ michael@0: x8 = x[8] + q; /* holds bits 0-25 */ michael@0: michael@0: r[6] = x[6] + x[13] * ecfp_twom160 + x[12] * ecfp_twom129; /* adds michael@0: * bits michael@0: * 8-39 */ michael@0: r[5] = x[5] + x[12] * ecfp_twom160 + x[11] * ecfp_twom129; michael@0: r[4] = x[4] + x[11] * ecfp_twom160 + x[10] * ecfp_twom129; michael@0: r[3] = x[3] + x[10] * ecfp_twom160 + x[9] * ecfp_twom129; michael@0: r[2] = x[2] + x[9] * ecfp_twom160 + x8 * ecfp_twom129; /* adds bits michael@0: * 8-40 */ michael@0: r[1] = x[1] + x8 * ecfp_twom160 + x7 * ecfp_twom129; /* adds bits michael@0: * 8-39 */ michael@0: r[0] = x[0] + x7 * ecfp_twom160; michael@0: michael@0: /* Tidy up just r[ECFP_NUMDOUBLES-2] so that the number of reductions michael@0: * is accurate plus or minus one. (Rather than tidy all to make it michael@0: * totally accurate, which is more costly.) */ michael@0: q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1]; michael@0: q -= group->alpha[ECFP_NUMDOUBLES - 1]; michael@0: r[ECFP_NUMDOUBLES - 2] -= q; michael@0: r[ECFP_NUMDOUBLES - 1] += q; michael@0: michael@0: /* Tidy up the excess bits on r[ECFP_NUMDOUBLES-1] using reduction */ michael@0: /* Use ecfp_beta so we get a positive result */ michael@0: q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_160; michael@0: q += group->bitSize_alpha; michael@0: q -= group->bitSize_alpha; michael@0: michael@0: r[ECFP_NUMDOUBLES - 1] -= q; michael@0: r[0] += q * ecfp_twom160; michael@0: r[1] += q * ecfp_twom129; michael@0: michael@0: /* Tidy the result */ michael@0: ecfp_tidyShort(r, group); michael@0: } michael@0: michael@0: /* Sets group to use optimized calculations in this file */ michael@0: mp_err michael@0: ec_group_set_secp160r1_fp(ECGroup *group) michael@0: { michael@0: michael@0: EC_group_fp *fpg = NULL; michael@0: michael@0: /* Allocate memory for floating point group data */ michael@0: fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp)); michael@0: if (fpg == NULL) { michael@0: return MP_MEM; michael@0: } michael@0: michael@0: fpg->numDoubles = ECFP_NUMDOUBLES; michael@0: fpg->primeBitSize = ECFP_BSIZE; michael@0: fpg->orderBitSize = 161; michael@0: fpg->doubleBitSize = 24; michael@0: fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS; michael@0: fpg->aIsM3 = 1; michael@0: fpg->ecfp_singleReduce = &ecfp160_singleReduce; michael@0: fpg->ecfp_reduce = &ecfp160_reduce; michael@0: fpg->ecfp_tidy = &ecfp_tidy; michael@0: michael@0: fpg->pt_add_jac_aff = &ecfp160_pt_add_jac_aff; michael@0: fpg->pt_add_jac = &ecfp160_pt_add_jac; michael@0: fpg->pt_add_jm_chud = &ecfp160_pt_add_jm_chud; michael@0: fpg->pt_add_chud = &ecfp160_pt_add_chud; michael@0: fpg->pt_dbl_jac = &ecfp160_pt_dbl_jac; michael@0: fpg->pt_dbl_jm = &ecfp160_pt_dbl_jm; michael@0: fpg->pt_dbl_aff2chud = &ecfp160_pt_dbl_aff2chud; michael@0: fpg->precompute_chud = &ecfp160_precompute_chud; michael@0: fpg->precompute_jac = &ecfp160_precompute_jac; michael@0: michael@0: group->point_mul = &ec_GFp_point_mul_wNAF_fp; michael@0: group->points_mul = &ec_pts_mul_basic; michael@0: group->extra1 = fpg; michael@0: group->extra_free = &ec_GFp_extra_free_fp; michael@0: michael@0: ec_set_fp_precision(fpg); michael@0: fpg->bitSize_alpha = ECFP_TWO160 * fpg->alpha[0]; michael@0: return MP_OKAY; michael@0: }