Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "ecp_fp.h" |
michael@0 | 6 | #include <stdlib.h> |
michael@0 | 7 | |
michael@0 | 8 | #define ECFP_BSIZE 192 |
michael@0 | 9 | #define ECFP_NUMDOUBLES 8 |
michael@0 | 10 | |
michael@0 | 11 | #include "ecp_fpinc.c" |
michael@0 | 12 | |
michael@0 | 13 | /* Performs a single step of reduction, just on the uppermost float |
michael@0 | 14 | * (assumes already tidied), and then retidies. Note, this does not |
michael@0 | 15 | * guarantee that the result will be less than p. */ |
michael@0 | 16 | void |
michael@0 | 17 | ecfp192_singleReduce(double *d, const EC_group_fp * group) |
michael@0 | 18 | { |
michael@0 | 19 | double q; |
michael@0 | 20 | |
michael@0 | 21 | ECFP_ASSERT(group->doubleBitSize == 24); |
michael@0 | 22 | ECFP_ASSERT(group->primeBitSize == 192); |
michael@0 | 23 | ECFP_ASSERT(group->numDoubles == 8); |
michael@0 | 24 | |
michael@0 | 25 | q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_192; |
michael@0 | 26 | q += group->bitSize_alpha; |
michael@0 | 27 | q -= group->bitSize_alpha; |
michael@0 | 28 | |
michael@0 | 29 | d[ECFP_NUMDOUBLES - 1] -= q; |
michael@0 | 30 | d[0] += q * ecfp_twom192; |
michael@0 | 31 | d[2] += q * ecfp_twom128; |
michael@0 | 32 | ecfp_positiveTidy(d, group); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | /* |
michael@0 | 36 | * Performs imperfect reduction. This might leave some negative terms, |
michael@0 | 37 | * and one more reduction might be required for the result to be between 0 |
michael@0 | 38 | * and p-1. x should be be an array of at least 16, and r at least 8 x and |
michael@0 | 39 | * r can be the same, but then the upper parts of r are not zeroed */ |
michael@0 | 40 | void |
michael@0 | 41 | ecfp_reduce_192(double *r, double *x, const EC_group_fp * group) |
michael@0 | 42 | { |
michael@0 | 43 | double x8, x9, x10, q; |
michael@0 | 44 | |
michael@0 | 45 | ECFP_ASSERT(group->doubleBitSize == 24); |
michael@0 | 46 | ECFP_ASSERT(group->primeBitSize == 192); |
michael@0 | 47 | ECFP_ASSERT(group->numDoubles == 8); |
michael@0 | 48 | |
michael@0 | 49 | /* Tidy just the upper portion, the lower part can wait */ |
michael@0 | 50 | ecfp_tidyUpper(x, group); |
michael@0 | 51 | |
michael@0 | 52 | x8 = x[8] + x[14] * ecfp_twom128; /* adds bits 16-40 */ |
michael@0 | 53 | x9 = x[9] + x[15] * ecfp_twom128; /* adds bits 16-40 */ |
michael@0 | 54 | |
michael@0 | 55 | /* Tidy up, or we won't have enough bits later to add it in */ |
michael@0 | 56 | |
michael@0 | 57 | q = x8 + group->alpha[9]; |
michael@0 | 58 | q -= group->alpha[9]; |
michael@0 | 59 | x8 -= q; |
michael@0 | 60 | x9 += q; |
michael@0 | 61 | |
michael@0 | 62 | q = x9 + group->alpha[10]; |
michael@0 | 63 | q -= group->alpha[10]; |
michael@0 | 64 | x9 -= q; |
michael@0 | 65 | x10 = x[10] + q; |
michael@0 | 66 | |
michael@0 | 67 | r[7] = x[7] + x[15] * ecfp_twom192 + x[13] * ecfp_twom128; /* adds |
michael@0 | 68 | * bits |
michael@0 | 69 | * 0-40 */ |
michael@0 | 70 | r[6] = x[6] + x[14] * ecfp_twom192 + x[12] * ecfp_twom128; |
michael@0 | 71 | r[5] = x[5] + x[13] * ecfp_twom192 + x[11] * ecfp_twom128; |
michael@0 | 72 | r[4] = x[4] + x[12] * ecfp_twom192 + x10 * ecfp_twom128; |
michael@0 | 73 | r[3] = x[3] + x[11] * ecfp_twom192 + x9 * ecfp_twom128; /* adds bits |
michael@0 | 74 | * 0-40 */ |
michael@0 | 75 | r[2] = x[2] + x10 * ecfp_twom192 + x8 * ecfp_twom128; |
michael@0 | 76 | r[1] = x[1] + x9 * ecfp_twom192; /* adds bits 16-40 */ |
michael@0 | 77 | r[0] = x[0] + x8 * ecfp_twom192; |
michael@0 | 78 | |
michael@0 | 79 | /* |
michael@0 | 80 | * Tidy up just r[group->numDoubles-2] so that the number of |
michael@0 | 81 | * reductions is accurate plus or minus one. (Rather than tidy all to |
michael@0 | 82 | * make it totally accurate) */ |
michael@0 | 83 | q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1]; |
michael@0 | 84 | q -= group->alpha[ECFP_NUMDOUBLES - 1]; |
michael@0 | 85 | r[ECFP_NUMDOUBLES - 2] -= q; |
michael@0 | 86 | r[ECFP_NUMDOUBLES - 1] += q; |
michael@0 | 87 | |
michael@0 | 88 | /* Tidy up the excess bits on r[group->numDoubles-1] using reduction */ |
michael@0 | 89 | /* Use ecfp_beta so we get a positive res */ |
michael@0 | 90 | q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_192; |
michael@0 | 91 | q += group->bitSize_alpha; |
michael@0 | 92 | q -= group->bitSize_alpha; |
michael@0 | 93 | |
michael@0 | 94 | r[ECFP_NUMDOUBLES - 1] -= q; |
michael@0 | 95 | r[0] += q * ecfp_twom192; |
michael@0 | 96 | r[2] += q * ecfp_twom128; |
michael@0 | 97 | |
michael@0 | 98 | /* Tidy the result */ |
michael@0 | 99 | ecfp_tidyShort(r, group); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /* Sets group to use optimized calculations in this file */ |
michael@0 | 103 | mp_err |
michael@0 | 104 | ec_group_set_nistp192_fp(ECGroup *group) |
michael@0 | 105 | { |
michael@0 | 106 | EC_group_fp *fpg; |
michael@0 | 107 | |
michael@0 | 108 | /* Allocate memory for floating point group data */ |
michael@0 | 109 | fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp)); |
michael@0 | 110 | if (fpg == NULL) { |
michael@0 | 111 | return MP_MEM; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | fpg->numDoubles = ECFP_NUMDOUBLES; |
michael@0 | 115 | fpg->primeBitSize = ECFP_BSIZE; |
michael@0 | 116 | fpg->orderBitSize = 192; |
michael@0 | 117 | fpg->doubleBitSize = 24; |
michael@0 | 118 | fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS; |
michael@0 | 119 | fpg->aIsM3 = 1; |
michael@0 | 120 | fpg->ecfp_singleReduce = &ecfp192_singleReduce; |
michael@0 | 121 | fpg->ecfp_reduce = &ecfp_reduce_192; |
michael@0 | 122 | fpg->ecfp_tidy = &ecfp_tidy; |
michael@0 | 123 | |
michael@0 | 124 | fpg->pt_add_jac_aff = &ecfp192_pt_add_jac_aff; |
michael@0 | 125 | fpg->pt_add_jac = &ecfp192_pt_add_jac; |
michael@0 | 126 | fpg->pt_add_jm_chud = &ecfp192_pt_add_jm_chud; |
michael@0 | 127 | fpg->pt_add_chud = &ecfp192_pt_add_chud; |
michael@0 | 128 | fpg->pt_dbl_jac = &ecfp192_pt_dbl_jac; |
michael@0 | 129 | fpg->pt_dbl_jm = &ecfp192_pt_dbl_jm; |
michael@0 | 130 | fpg->pt_dbl_aff2chud = &ecfp192_pt_dbl_aff2chud; |
michael@0 | 131 | fpg->precompute_chud = &ecfp192_precompute_chud; |
michael@0 | 132 | fpg->precompute_jac = &ecfp192_precompute_jac; |
michael@0 | 133 | |
michael@0 | 134 | group->point_mul = &ec_GFp_point_mul_wNAF_fp; |
michael@0 | 135 | group->points_mul = &ec_pts_mul_basic; |
michael@0 | 136 | group->extra1 = fpg; |
michael@0 | 137 | group->extra_free = &ec_GFp_extra_free_fp; |
michael@0 | 138 | |
michael@0 | 139 | ec_set_fp_precision(fpg); |
michael@0 | 140 | fpg->bitSize_alpha = ECFP_TWO192 * fpg->alpha[0]; |
michael@0 | 141 | |
michael@0 | 142 | return MP_OKAY; |
michael@0 | 143 | } |