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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "ecp_fp.h"
     6 #include <stdlib.h>
     8 #define ECFP_BSIZE 160
     9 #define ECFP_NUMDOUBLES 7
    11 #include "ecp_fpinc.c"
    13 /* Performs a single step of reduction, just on the uppermost float
    14  * (assumes already tidied), and then retidies. Note, this does not
    15  * guarantee that the result will be less than p, but truncates the number 
    16  * of bits. */
    17 void
    18 ecfp160_singleReduce(double *d, const EC_group_fp * group)
    19 {
    20 	double q;
    22 	ECFP_ASSERT(group->doubleBitSize == 24);
    23 	ECFP_ASSERT(group->primeBitSize == 160);
    24 	ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
    26 	q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
    27 	q += group->bitSize_alpha;
    28 	q -= group->bitSize_alpha;
    30 	d[ECFP_NUMDOUBLES - 1] -= q;
    31 	d[0] += q * ecfp_twom160;
    32 	d[1] += q * ecfp_twom129;
    33 	ecfp_positiveTidy(d, group);
    35 	/* Assertions for the highest order term */
    36 	ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] / ecfp_exp[ECFP_NUMDOUBLES - 1] ==
    37 				(unsigned long long) (d[ECFP_NUMDOUBLES - 1] /
    38 									  ecfp_exp[ECFP_NUMDOUBLES - 1]));
    39 	ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] >= 0);
    40 }
    42 /* Performs imperfect reduction.  This might leave some negative terms,
    43  * and one more reduction might be required for the result to be between 0 
    44  * and p-1. x should not already be reduced, i.e. should have
    45  * 2*ECFP_NUMDOUBLES significant terms. x and r can be the same, but then
    46  * the upper parts of r are not zeroed */
    47 void
    48 ecfp160_reduce(double *r, double *x, const EC_group_fp * group)
    49 {
    51 	double x7, x8, q;
    53 	ECFP_ASSERT(group->doubleBitSize == 24);
    54 	ECFP_ASSERT(group->primeBitSize == 160);
    55 	ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
    57 	/* Tidy just the upper bits, the lower bits can wait. */
    58 	ecfp_tidyUpper(x, group);
    60 	/* Assume that this is already tidied so that we have enough extra
    61 	 * bits */
    62 	x7 = x[7] + x[13] * ecfp_twom129;	/* adds bits 15-39 */
    64 	/* Tidy x7, or we won't have enough bits later to add it in */
    65 	q = x7 + group->alpha[8];
    66 	q -= group->alpha[8];
    67 	x7 -= q;					/* holds bits 0-24 */
    68 	x8 = x[8] + q;				/* holds bits 0-25 */
    70 	r[6] = x[6] + x[13] * ecfp_twom160 + x[12] * ecfp_twom129;	/* adds
    71 																 * bits
    72 																 * 8-39 */
    73 	r[5] = x[5] + x[12] * ecfp_twom160 + x[11] * ecfp_twom129;
    74 	r[4] = x[4] + x[11] * ecfp_twom160 + x[10] * ecfp_twom129;
    75 	r[3] = x[3] + x[10] * ecfp_twom160 + x[9] * ecfp_twom129;
    76 	r[2] = x[2] + x[9] * ecfp_twom160 + x8 * ecfp_twom129;	/* adds bits
    77 															 * 8-40 */
    78 	r[1] = x[1] + x8 * ecfp_twom160 + x7 * ecfp_twom129;	/* adds bits
    79 															 * 8-39 */
    80 	r[0] = x[0] + x7 * ecfp_twom160;
    82 	/* Tidy up just r[ECFP_NUMDOUBLES-2] so that the number of reductions
    83 	 * is accurate plus or minus one.  (Rather than tidy all to make it
    84 	 * totally accurate, which is more costly.) */
    85 	q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1];
    86 	q -= group->alpha[ECFP_NUMDOUBLES - 1];
    87 	r[ECFP_NUMDOUBLES - 2] -= q;
    88 	r[ECFP_NUMDOUBLES - 1] += q;
    90 	/* Tidy up the excess bits on r[ECFP_NUMDOUBLES-1] using reduction */
    91 	/* Use ecfp_beta so we get a positive result */
    92 	q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
    93 	q += group->bitSize_alpha;
    94 	q -= group->bitSize_alpha;
    96 	r[ECFP_NUMDOUBLES - 1] -= q;
    97 	r[0] += q * ecfp_twom160;
    98 	r[1] += q * ecfp_twom129;
   100 	/* Tidy the result */
   101 	ecfp_tidyShort(r, group);
   102 }
   104 /* Sets group to use optimized calculations in this file */
   105 mp_err
   106 ec_group_set_secp160r1_fp(ECGroup *group)
   107 {
   109 	EC_group_fp *fpg = NULL;
   111 	/* Allocate memory for floating point group data */
   112 	fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
   113 	if (fpg == NULL) {
   114 		return MP_MEM;
   115 	}
   117 	fpg->numDoubles = ECFP_NUMDOUBLES;
   118 	fpg->primeBitSize = ECFP_BSIZE;
   119 	fpg->orderBitSize = 161;
   120 	fpg->doubleBitSize = 24;
   121 	fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS;
   122 	fpg->aIsM3 = 1;
   123 	fpg->ecfp_singleReduce = &ecfp160_singleReduce;
   124 	fpg->ecfp_reduce = &ecfp160_reduce;
   125 	fpg->ecfp_tidy = &ecfp_tidy;
   127 	fpg->pt_add_jac_aff = &ecfp160_pt_add_jac_aff;
   128 	fpg->pt_add_jac = &ecfp160_pt_add_jac;
   129 	fpg->pt_add_jm_chud = &ecfp160_pt_add_jm_chud;
   130 	fpg->pt_add_chud = &ecfp160_pt_add_chud;
   131 	fpg->pt_dbl_jac = &ecfp160_pt_dbl_jac;
   132 	fpg->pt_dbl_jm = &ecfp160_pt_dbl_jm;
   133 	fpg->pt_dbl_aff2chud = &ecfp160_pt_dbl_aff2chud;
   134 	fpg->precompute_chud = &ecfp160_precompute_chud;
   135 	fpg->precompute_jac = &ecfp160_precompute_jac;
   137 	group->point_mul = &ec_GFp_point_mul_wNAF_fp;
   138 	group->points_mul = &ec_pts_mul_basic;
   139 	group->extra1 = fpg;
   140 	group->extra_free = &ec_GFp_extra_free_fp;
   142 	ec_set_fp_precision(fpg);
   143 	fpg->bitSize_alpha = ECFP_TWO160 * fpg->alpha[0];
   144 	return MP_OKAY;
   145 }

mercurial