1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/ecl/ecp_fp192.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 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 192 1.12 +#define ECFP_NUMDOUBLES 8 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. */ 1.19 +void 1.20 +ecfp192_singleReduce(double *d, const EC_group_fp * group) 1.21 +{ 1.22 + double q; 1.23 + 1.24 + ECFP_ASSERT(group->doubleBitSize == 24); 1.25 + ECFP_ASSERT(group->primeBitSize == 192); 1.26 + ECFP_ASSERT(group->numDoubles == 8); 1.27 + 1.28 + q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_192; 1.29 + q += group->bitSize_alpha; 1.30 + q -= group->bitSize_alpha; 1.31 + 1.32 + d[ECFP_NUMDOUBLES - 1] -= q; 1.33 + d[0] += q * ecfp_twom192; 1.34 + d[2] += q * ecfp_twom128; 1.35 + ecfp_positiveTidy(d, group); 1.36 +} 1.37 + 1.38 +/* 1.39 + * Performs imperfect reduction. This might leave some negative terms, 1.40 + * and one more reduction might be required for the result to be between 0 1.41 + * and p-1. x should be be an array of at least 16, and r at least 8 x and 1.42 + * r can be the same, but then the upper parts of r are not zeroed */ 1.43 +void 1.44 +ecfp_reduce_192(double *r, double *x, const EC_group_fp * group) 1.45 +{ 1.46 + double x8, x9, x10, q; 1.47 + 1.48 + ECFP_ASSERT(group->doubleBitSize == 24); 1.49 + ECFP_ASSERT(group->primeBitSize == 192); 1.50 + ECFP_ASSERT(group->numDoubles == 8); 1.51 + 1.52 + /* Tidy just the upper portion, the lower part can wait */ 1.53 + ecfp_tidyUpper(x, group); 1.54 + 1.55 + x8 = x[8] + x[14] * ecfp_twom128; /* adds bits 16-40 */ 1.56 + x9 = x[9] + x[15] * ecfp_twom128; /* adds bits 16-40 */ 1.57 + 1.58 + /* Tidy up, or we won't have enough bits later to add it in */ 1.59 + 1.60 + q = x8 + group->alpha[9]; 1.61 + q -= group->alpha[9]; 1.62 + x8 -= q; 1.63 + x9 += q; 1.64 + 1.65 + q = x9 + group->alpha[10]; 1.66 + q -= group->alpha[10]; 1.67 + x9 -= q; 1.68 + x10 = x[10] + q; 1.69 + 1.70 + r[7] = x[7] + x[15] * ecfp_twom192 + x[13] * ecfp_twom128; /* adds 1.71 + * bits 1.72 + * 0-40 */ 1.73 + r[6] = x[6] + x[14] * ecfp_twom192 + x[12] * ecfp_twom128; 1.74 + r[5] = x[5] + x[13] * ecfp_twom192 + x[11] * ecfp_twom128; 1.75 + r[4] = x[4] + x[12] * ecfp_twom192 + x10 * ecfp_twom128; 1.76 + r[3] = x[3] + x[11] * ecfp_twom192 + x9 * ecfp_twom128; /* adds bits 1.77 + * 0-40 */ 1.78 + r[2] = x[2] + x10 * ecfp_twom192 + x8 * ecfp_twom128; 1.79 + r[1] = x[1] + x9 * ecfp_twom192; /* adds bits 16-40 */ 1.80 + r[0] = x[0] + x8 * ecfp_twom192; 1.81 + 1.82 + /* 1.83 + * Tidy up just r[group->numDoubles-2] so that the number of 1.84 + * reductions is accurate plus or minus one. (Rather than tidy all to 1.85 + * make it totally accurate) */ 1.86 + q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1]; 1.87 + q -= group->alpha[ECFP_NUMDOUBLES - 1]; 1.88 + r[ECFP_NUMDOUBLES - 2] -= q; 1.89 + r[ECFP_NUMDOUBLES - 1] += q; 1.90 + 1.91 + /* Tidy up the excess bits on r[group->numDoubles-1] using reduction */ 1.92 + /* Use ecfp_beta so we get a positive res */ 1.93 + q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_192; 1.94 + q += group->bitSize_alpha; 1.95 + q -= group->bitSize_alpha; 1.96 + 1.97 + r[ECFP_NUMDOUBLES - 1] -= q; 1.98 + r[0] += q * ecfp_twom192; 1.99 + r[2] += q * ecfp_twom128; 1.100 + 1.101 + /* Tidy the result */ 1.102 + ecfp_tidyShort(r, group); 1.103 +} 1.104 + 1.105 +/* Sets group to use optimized calculations in this file */ 1.106 +mp_err 1.107 +ec_group_set_nistp192_fp(ECGroup *group) 1.108 +{ 1.109 + EC_group_fp *fpg; 1.110 + 1.111 + /* Allocate memory for floating point group data */ 1.112 + fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp)); 1.113 + if (fpg == NULL) { 1.114 + return MP_MEM; 1.115 + } 1.116 + 1.117 + fpg->numDoubles = ECFP_NUMDOUBLES; 1.118 + fpg->primeBitSize = ECFP_BSIZE; 1.119 + fpg->orderBitSize = 192; 1.120 + fpg->doubleBitSize = 24; 1.121 + fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS; 1.122 + fpg->aIsM3 = 1; 1.123 + fpg->ecfp_singleReduce = &ecfp192_singleReduce; 1.124 + fpg->ecfp_reduce = &ecfp_reduce_192; 1.125 + fpg->ecfp_tidy = &ecfp_tidy; 1.126 + 1.127 + fpg->pt_add_jac_aff = &ecfp192_pt_add_jac_aff; 1.128 + fpg->pt_add_jac = &ecfp192_pt_add_jac; 1.129 + fpg->pt_add_jm_chud = &ecfp192_pt_add_jm_chud; 1.130 + fpg->pt_add_chud = &ecfp192_pt_add_chud; 1.131 + fpg->pt_dbl_jac = &ecfp192_pt_dbl_jac; 1.132 + fpg->pt_dbl_jm = &ecfp192_pt_dbl_jm; 1.133 + fpg->pt_dbl_aff2chud = &ecfp192_pt_dbl_aff2chud; 1.134 + fpg->precompute_chud = &ecfp192_precompute_chud; 1.135 + fpg->precompute_jac = &ecfp192_precompute_jac; 1.136 + 1.137 + group->point_mul = &ec_GFp_point_mul_wNAF_fp; 1.138 + group->points_mul = &ec_pts_mul_basic; 1.139 + group->extra1 = fpg; 1.140 + group->extra_free = &ec_GFp_extra_free_fp; 1.141 + 1.142 + ec_set_fp_precision(fpg); 1.143 + fpg->bitSize_alpha = ECFP_TWO192 * fpg->alpha[0]; 1.144 + 1.145 + return MP_OKAY; 1.146 +}