Blame nss/lib/freebl/ecl/ecp_fp160.c

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