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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/ecl/ecp_fp160.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "ecp_fp.h"
     1.9 +#include <stdlib.h>
    1.10 +
    1.11 +#define ECFP_BSIZE 160
    1.12 +#define ECFP_NUMDOUBLES 7
    1.13 +
    1.14 +#include "ecp_fpinc.c"
    1.15 +
    1.16 +/* Performs a single step of reduction, just on the uppermost float
    1.17 + * (assumes already tidied), and then retidies. Note, this does not
    1.18 + * guarantee that the result will be less than p, but truncates the number 
    1.19 + * of bits. */
    1.20 +void
    1.21 +ecfp160_singleReduce(double *d, const EC_group_fp * group)
    1.22 +{
    1.23 +	double q;
    1.24 +
    1.25 +	ECFP_ASSERT(group->doubleBitSize == 24);
    1.26 +	ECFP_ASSERT(group->primeBitSize == 160);
    1.27 +	ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
    1.28 +
    1.29 +	q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
    1.30 +	q += group->bitSize_alpha;
    1.31 +	q -= group->bitSize_alpha;
    1.32 +
    1.33 +	d[ECFP_NUMDOUBLES - 1] -= q;
    1.34 +	d[0] += q * ecfp_twom160;
    1.35 +	d[1] += q * ecfp_twom129;
    1.36 +	ecfp_positiveTidy(d, group);
    1.37 +
    1.38 +	/* Assertions for the highest order term */
    1.39 +	ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] / ecfp_exp[ECFP_NUMDOUBLES - 1] ==
    1.40 +				(unsigned long long) (d[ECFP_NUMDOUBLES - 1] /
    1.41 +									  ecfp_exp[ECFP_NUMDOUBLES - 1]));
    1.42 +	ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] >= 0);
    1.43 +}
    1.44 +
    1.45 +/* Performs imperfect reduction.  This might leave some negative terms,
    1.46 + * and one more reduction might be required for the result to be between 0 
    1.47 + * and p-1. x should not already be reduced, i.e. should have
    1.48 + * 2*ECFP_NUMDOUBLES significant terms. x and r can be the same, but then
    1.49 + * the upper parts of r are not zeroed */
    1.50 +void
    1.51 +ecfp160_reduce(double *r, double *x, const EC_group_fp * group)
    1.52 +{
    1.53 +
    1.54 +	double x7, x8, q;
    1.55 +
    1.56 +	ECFP_ASSERT(group->doubleBitSize == 24);
    1.57 +	ECFP_ASSERT(group->primeBitSize == 160);
    1.58 +	ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
    1.59 +
    1.60 +	/* Tidy just the upper bits, the lower bits can wait. */
    1.61 +	ecfp_tidyUpper(x, group);
    1.62 +
    1.63 +	/* Assume that this is already tidied so that we have enough extra
    1.64 +	 * bits */
    1.65 +	x7 = x[7] + x[13] * ecfp_twom129;	/* adds bits 15-39 */
    1.66 +
    1.67 +	/* Tidy x7, or we won't have enough bits later to add it in */
    1.68 +	q = x7 + group->alpha[8];
    1.69 +	q -= group->alpha[8];
    1.70 +	x7 -= q;					/* holds bits 0-24 */
    1.71 +	x8 = x[8] + q;				/* holds bits 0-25 */
    1.72 +
    1.73 +	r[6] = x[6] + x[13] * ecfp_twom160 + x[12] * ecfp_twom129;	/* adds
    1.74 +																 * bits
    1.75 +																 * 8-39 */
    1.76 +	r[5] = x[5] + x[12] * ecfp_twom160 + x[11] * ecfp_twom129;
    1.77 +	r[4] = x[4] + x[11] * ecfp_twom160 + x[10] * ecfp_twom129;
    1.78 +	r[3] = x[3] + x[10] * ecfp_twom160 + x[9] * ecfp_twom129;
    1.79 +	r[2] = x[2] + x[9] * ecfp_twom160 + x8 * ecfp_twom129;	/* adds bits
    1.80 +															 * 8-40 */
    1.81 +	r[1] = x[1] + x8 * ecfp_twom160 + x7 * ecfp_twom129;	/* adds bits
    1.82 +															 * 8-39 */
    1.83 +	r[0] = x[0] + x7 * ecfp_twom160;
    1.84 +
    1.85 +	/* Tidy up just r[ECFP_NUMDOUBLES-2] so that the number of reductions
    1.86 +	 * is accurate plus or minus one.  (Rather than tidy all to make it
    1.87 +	 * totally accurate, which is more costly.) */
    1.88 +	q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1];
    1.89 +	q -= group->alpha[ECFP_NUMDOUBLES - 1];
    1.90 +	r[ECFP_NUMDOUBLES - 2] -= q;
    1.91 +	r[ECFP_NUMDOUBLES - 1] += q;
    1.92 +
    1.93 +	/* Tidy up the excess bits on r[ECFP_NUMDOUBLES-1] using reduction */
    1.94 +	/* Use ecfp_beta so we get a positive result */
    1.95 +	q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
    1.96 +	q += group->bitSize_alpha;
    1.97 +	q -= group->bitSize_alpha;
    1.98 +
    1.99 +	r[ECFP_NUMDOUBLES - 1] -= q;
   1.100 +	r[0] += q * ecfp_twom160;
   1.101 +	r[1] += q * ecfp_twom129;
   1.102 +
   1.103 +	/* Tidy the result */
   1.104 +	ecfp_tidyShort(r, group);
   1.105 +}
   1.106 +
   1.107 +/* Sets group to use optimized calculations in this file */
   1.108 +mp_err
   1.109 +ec_group_set_secp160r1_fp(ECGroup *group)
   1.110 +{
   1.111 +
   1.112 +	EC_group_fp *fpg = NULL;
   1.113 +
   1.114 +	/* Allocate memory for floating point group data */
   1.115 +	fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
   1.116 +	if (fpg == NULL) {
   1.117 +		return MP_MEM;
   1.118 +	}
   1.119 +
   1.120 +	fpg->numDoubles = ECFP_NUMDOUBLES;
   1.121 +	fpg->primeBitSize = ECFP_BSIZE;
   1.122 +	fpg->orderBitSize = 161;
   1.123 +	fpg->doubleBitSize = 24;
   1.124 +	fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS;
   1.125 +	fpg->aIsM3 = 1;
   1.126 +	fpg->ecfp_singleReduce = &ecfp160_singleReduce;
   1.127 +	fpg->ecfp_reduce = &ecfp160_reduce;
   1.128 +	fpg->ecfp_tidy = &ecfp_tidy;
   1.129 +
   1.130 +	fpg->pt_add_jac_aff = &ecfp160_pt_add_jac_aff;
   1.131 +	fpg->pt_add_jac = &ecfp160_pt_add_jac;
   1.132 +	fpg->pt_add_jm_chud = &ecfp160_pt_add_jm_chud;
   1.133 +	fpg->pt_add_chud = &ecfp160_pt_add_chud;
   1.134 +	fpg->pt_dbl_jac = &ecfp160_pt_dbl_jac;
   1.135 +	fpg->pt_dbl_jm = &ecfp160_pt_dbl_jm;
   1.136 +	fpg->pt_dbl_aff2chud = &ecfp160_pt_dbl_aff2chud;
   1.137 +	fpg->precompute_chud = &ecfp160_precompute_chud;
   1.138 +	fpg->precompute_jac = &ecfp160_precompute_jac;
   1.139 +
   1.140 +	group->point_mul = &ec_GFp_point_mul_wNAF_fp;
   1.141 +	group->points_mul = &ec_pts_mul_basic;
   1.142 +	group->extra1 = fpg;
   1.143 +	group->extra_free = &ec_GFp_extra_free_fp;
   1.144 +
   1.145 +	ec_set_fp_precision(fpg);
   1.146 +	fpg->bitSize_alpha = ECFP_TWO160 * fpg->alpha[0];
   1.147 +	return MP_OKAY;
   1.148 +}

mercurial