Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | #ifndef __ecp_fp_h_ |
michael@0 | 6 | #define __ecp_fp_h_ |
michael@0 | 7 | |
michael@0 | 8 | #include "mpi.h" |
michael@0 | 9 | #include "ecl.h" |
michael@0 | 10 | #include "ecp.h" |
michael@0 | 11 | |
michael@0 | 12 | #include <sys/types.h> |
michael@0 | 13 | #include "mpi-priv.h" |
michael@0 | 14 | |
michael@0 | 15 | #ifdef ECL_DEBUG |
michael@0 | 16 | #include <assert.h> |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | /* Largest number of doubles to store one reduced number in floating |
michael@0 | 20 | * point. Used for memory allocation on the stack. */ |
michael@0 | 21 | #define ECFP_MAXDOUBLES 10 |
michael@0 | 22 | |
michael@0 | 23 | /* For debugging purposes */ |
michael@0 | 24 | #ifndef ECL_DEBUG |
michael@0 | 25 | #define ECFP_ASSERT(x) |
michael@0 | 26 | #else |
michael@0 | 27 | #define ECFP_ASSERT(x) assert(x) |
michael@0 | 28 | #endif |
michael@0 | 29 | |
michael@0 | 30 | /* ECFP_Ti = 2^(i*24) Define as preprocessor constants so we can use in |
michael@0 | 31 | * multiple static constants */ |
michael@0 | 32 | #define ECFP_T0 1.0 |
michael@0 | 33 | #define ECFP_T1 16777216.0 |
michael@0 | 34 | #define ECFP_T2 281474976710656.0 |
michael@0 | 35 | #define ECFP_T3 4722366482869645213696.0 |
michael@0 | 36 | #define ECFP_T4 79228162514264337593543950336.0 |
michael@0 | 37 | #define ECFP_T5 1329227995784915872903807060280344576.0 |
michael@0 | 38 | #define ECFP_T6 22300745198530623141535718272648361505980416.0 |
michael@0 | 39 | #define ECFP_T7 374144419156711147060143317175368453031918731001856.0 |
michael@0 | 40 | #define ECFP_T8 6277101735386680763835789423207666416102355444464034512896.0 |
michael@0 | 41 | #define ECFP_T9 105312291668557186697918027683670432318895095400549111254310977536.0 |
michael@0 | 42 | #define ECFP_T10 1766847064778384329583297500742918515827483896875618958121606201292619776.0 |
michael@0 | 43 | #define ECFP_T11 29642774844752946028434172162224104410437116074403984394101141506025761187823616.0 |
michael@0 | 44 | #define ECFP_T12 497323236409786642155382248146820840100456150797347717440463976893159497012533375533056.0 |
michael@0 | 45 | #define ECFP_T13 8343699359066055009355553539724812947666814540455674882605631280555545803830627148527195652096.0 |
michael@0 | 46 | #define ECFP_T14 139984046386112763159840142535527767382602843577165595931249318810236991948760059086304843329475444736.0 |
michael@0 | 47 | #define ECFP_T15 2348542582773833227889480596789337027375682548908319870707290971532209025114608443463698998384768703031934976.0 |
michael@0 | 48 | #define ECFP_T16 39402006196394479212279040100143613805079739270465446667948293404245\ |
michael@0 | 49 | 721771497210611414266254884915640806627990306816.0 |
michael@0 | 50 | #define ECFP_T17 66105596879024859895191530803277103982840468296428121928464879527440\ |
michael@0 | 51 | 5791236311345825189210439715284847591212025023358304256.0 |
michael@0 | 52 | #define ECFP_T18 11090678776483259438313656736572334813745748301503266300681918322458\ |
michael@0 | 53 | 485231222502492159897624416558312389564843845614287315896631296.0 |
michael@0 | 54 | #define ECFP_T19 18607071341967536398062689481932916079453218833595342343206149099024\ |
michael@0 | 55 | 36577570298683715049089827234727835552055312041415509848580169253519\ |
michael@0 | 56 | 36.0 |
michael@0 | 57 | |
michael@0 | 58 | #define ECFP_TWO160 1461501637330902918203684832716283019655932542976.0 |
michael@0 | 59 | #define ECFP_TWO192 6277101735386680763835789423207666416102355444464034512896.0 |
michael@0 | 60 | #define ECFP_TWO224 26959946667150639794667015087019630673637144422540572481103610249216.0 |
michael@0 | 61 | |
michael@0 | 62 | /* Multiplicative constants */ |
michael@0 | 63 | static const double ecfp_two32 = 4294967296.0; |
michael@0 | 64 | static const double ecfp_two64 = 18446744073709551616.0; |
michael@0 | 65 | static const double ecfp_twom16 = .0000152587890625; |
michael@0 | 66 | static const double ecfp_twom128 = |
michael@0 | 67 | .00000000000000000000000000000000000000293873587705571876992184134305561419454666389193021880377187926569604314863681793212890625; |
michael@0 | 68 | static const double ecfp_twom129 = |
michael@0 | 69 | .000000000000000000000000000000000000001469367938527859384960920671527807097273331945965109401885939632848021574318408966064453125; |
michael@0 | 70 | static const double ecfp_twom160 = |
michael@0 | 71 | .0000000000000000000000000000000000000000000000006842277657836020854119773355907793609766904013068924666782559979930620520927053718196475529111921787261962890625; |
michael@0 | 72 | static const double ecfp_twom192 = |
michael@0 | 73 | .000000000000000000000000000000000000000000000000000000000159309191113245227702888039776771180559110455519261878607388585338616290151305816094308987472018268594098344692611135542392730712890625; |
michael@0 | 74 | static const double ecfp_twom224 = |
michael@0 | 75 | .00000000000000000000000000000000000000000000000000000000000000000003709206150687421385731735261547639513367564778757791002453039058917581340095629358997312082723208437536338919136001159027049567384892725385725498199462890625; |
michael@0 | 76 | |
michael@0 | 77 | /* ecfp_exp[i] = 2^(i*ECFP_DSIZE) */ |
michael@0 | 78 | static const double ecfp_exp[2 * ECFP_MAXDOUBLES] = { |
michael@0 | 79 | ECFP_T0, ECFP_T1, ECFP_T2, ECFP_T3, ECFP_T4, ECFP_T5, |
michael@0 | 80 | ECFP_T6, ECFP_T7, ECFP_T8, ECFP_T9, ECFP_T10, ECFP_T11, |
michael@0 | 81 | ECFP_T12, ECFP_T13, ECFP_T14, ECFP_T15, ECFP_T16, ECFP_T17, ECFP_T18, |
michael@0 | 82 | ECFP_T19 |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | /* 1.1 * 2^52 Uses 2^52 to truncate, the .1 is an extra 2^51 to protect |
michael@0 | 86 | * the 2^52 bit, so that adding alphas to a negative number won't borrow |
michael@0 | 87 | * and empty the important 2^52 bit */ |
michael@0 | 88 | #define ECFP_ALPHABASE_53 6755399441055744.0 |
michael@0 | 89 | /* Special case: On some platforms, notably x86 Linux, there is an |
michael@0 | 90 | * extended-precision floating point representation with 64-bits of |
michael@0 | 91 | * precision in the mantissa. These extra bits of precision require a |
michael@0 | 92 | * larger value of alpha to truncate, i.e. 1.1 * 2^63. */ |
michael@0 | 93 | #define ECFP_ALPHABASE_64 13835058055282163712.0 |
michael@0 | 94 | |
michael@0 | 95 | /* |
michael@0 | 96 | * ecfp_alpha[i] = 1.5 * 2^(52 + i*ECFP_DSIZE) we add and subtract alpha |
michael@0 | 97 | * to truncate floating point numbers to a certain number of bits for |
michael@0 | 98 | * tidying */ |
michael@0 | 99 | static const double ecfp_alpha_53[2 * ECFP_MAXDOUBLES] = { |
michael@0 | 100 | ECFP_ALPHABASE_53 * ECFP_T0, |
michael@0 | 101 | ECFP_ALPHABASE_53 * ECFP_T1, |
michael@0 | 102 | ECFP_ALPHABASE_53 * ECFP_T2, |
michael@0 | 103 | ECFP_ALPHABASE_53 * ECFP_T3, |
michael@0 | 104 | ECFP_ALPHABASE_53 * ECFP_T4, |
michael@0 | 105 | ECFP_ALPHABASE_53 * ECFP_T5, |
michael@0 | 106 | ECFP_ALPHABASE_53 * ECFP_T6, |
michael@0 | 107 | ECFP_ALPHABASE_53 * ECFP_T7, |
michael@0 | 108 | ECFP_ALPHABASE_53 * ECFP_T8, |
michael@0 | 109 | ECFP_ALPHABASE_53 * ECFP_T9, |
michael@0 | 110 | ECFP_ALPHABASE_53 * ECFP_T10, |
michael@0 | 111 | ECFP_ALPHABASE_53 * ECFP_T11, |
michael@0 | 112 | ECFP_ALPHABASE_53 * ECFP_T12, |
michael@0 | 113 | ECFP_ALPHABASE_53 * ECFP_T13, |
michael@0 | 114 | ECFP_ALPHABASE_53 * ECFP_T14, |
michael@0 | 115 | ECFP_ALPHABASE_53 * ECFP_T15, |
michael@0 | 116 | ECFP_ALPHABASE_53 * ECFP_T16, |
michael@0 | 117 | ECFP_ALPHABASE_53 * ECFP_T17, |
michael@0 | 118 | ECFP_ALPHABASE_53 * ECFP_T18, |
michael@0 | 119 | ECFP_ALPHABASE_53 * ECFP_T19 |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | /* |
michael@0 | 123 | * ecfp_alpha[i] = 1.5 * 2^(63 + i*ECFP_DSIZE) we add and subtract alpha |
michael@0 | 124 | * to truncate floating point numbers to a certain number of bits for |
michael@0 | 125 | * tidying */ |
michael@0 | 126 | static const double ecfp_alpha_64[2 * ECFP_MAXDOUBLES] = { |
michael@0 | 127 | ECFP_ALPHABASE_64 * ECFP_T0, |
michael@0 | 128 | ECFP_ALPHABASE_64 * ECFP_T1, |
michael@0 | 129 | ECFP_ALPHABASE_64 * ECFP_T2, |
michael@0 | 130 | ECFP_ALPHABASE_64 * ECFP_T3, |
michael@0 | 131 | ECFP_ALPHABASE_64 * ECFP_T4, |
michael@0 | 132 | ECFP_ALPHABASE_64 * ECFP_T5, |
michael@0 | 133 | ECFP_ALPHABASE_64 * ECFP_T6, |
michael@0 | 134 | ECFP_ALPHABASE_64 * ECFP_T7, |
michael@0 | 135 | ECFP_ALPHABASE_64 * ECFP_T8, |
michael@0 | 136 | ECFP_ALPHABASE_64 * ECFP_T9, |
michael@0 | 137 | ECFP_ALPHABASE_64 * ECFP_T10, |
michael@0 | 138 | ECFP_ALPHABASE_64 * ECFP_T11, |
michael@0 | 139 | ECFP_ALPHABASE_64 * ECFP_T12, |
michael@0 | 140 | ECFP_ALPHABASE_64 * ECFP_T13, |
michael@0 | 141 | ECFP_ALPHABASE_64 * ECFP_T14, |
michael@0 | 142 | ECFP_ALPHABASE_64 * ECFP_T15, |
michael@0 | 143 | ECFP_ALPHABASE_64 * ECFP_T16, |
michael@0 | 144 | ECFP_ALPHABASE_64 * ECFP_T17, |
michael@0 | 145 | ECFP_ALPHABASE_64 * ECFP_T18, |
michael@0 | 146 | ECFP_ALPHABASE_64 * ECFP_T19 |
michael@0 | 147 | }; |
michael@0 | 148 | |
michael@0 | 149 | /* 0.011111111111111111111111 (binary) = 0.5 - 2^25 (24 ones) */ |
michael@0 | 150 | #define ECFP_BETABASE 0.4999999701976776123046875 |
michael@0 | 151 | |
michael@0 | 152 | /* |
michael@0 | 153 | * We subtract beta prior to using alpha to simulate rounding down. We |
michael@0 | 154 | * make this close to 0.5 to round almost everything down, but exactly 0.5 |
michael@0 | 155 | * would cause some incorrect rounding. */ |
michael@0 | 156 | static const double ecfp_beta[2 * ECFP_MAXDOUBLES] = { |
michael@0 | 157 | ECFP_BETABASE * ECFP_T0, |
michael@0 | 158 | ECFP_BETABASE * ECFP_T1, |
michael@0 | 159 | ECFP_BETABASE * ECFP_T2, |
michael@0 | 160 | ECFP_BETABASE * ECFP_T3, |
michael@0 | 161 | ECFP_BETABASE * ECFP_T4, |
michael@0 | 162 | ECFP_BETABASE * ECFP_T5, |
michael@0 | 163 | ECFP_BETABASE * ECFP_T6, |
michael@0 | 164 | ECFP_BETABASE * ECFP_T7, |
michael@0 | 165 | ECFP_BETABASE * ECFP_T8, |
michael@0 | 166 | ECFP_BETABASE * ECFP_T9, |
michael@0 | 167 | ECFP_BETABASE * ECFP_T10, |
michael@0 | 168 | ECFP_BETABASE * ECFP_T11, |
michael@0 | 169 | ECFP_BETABASE * ECFP_T12, |
michael@0 | 170 | ECFP_BETABASE * ECFP_T13, |
michael@0 | 171 | ECFP_BETABASE * ECFP_T14, |
michael@0 | 172 | ECFP_BETABASE * ECFP_T15, |
michael@0 | 173 | ECFP_BETABASE * ECFP_T16, |
michael@0 | 174 | ECFP_BETABASE * ECFP_T17, |
michael@0 | 175 | ECFP_BETABASE * ECFP_T18, |
michael@0 | 176 | ECFP_BETABASE * ECFP_T19 |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | static const double ecfp_beta_160 = ECFP_BETABASE * ECFP_TWO160; |
michael@0 | 180 | static const double ecfp_beta_192 = ECFP_BETABASE * ECFP_TWO192; |
michael@0 | 181 | static const double ecfp_beta_224 = ECFP_BETABASE * ECFP_TWO224; |
michael@0 | 182 | |
michael@0 | 183 | /* Affine EC Point. This is the basic representation (x, y) of an elliptic |
michael@0 | 184 | * curve point. */ |
michael@0 | 185 | typedef struct { |
michael@0 | 186 | double x[ECFP_MAXDOUBLES]; |
michael@0 | 187 | double y[ECFP_MAXDOUBLES]; |
michael@0 | 188 | } ecfp_aff_pt; |
michael@0 | 189 | |
michael@0 | 190 | /* Jacobian EC Point. This coordinate system uses X = x/z^2, Y = y/z^3, |
michael@0 | 191 | * which enables calculations with fewer inversions than affine |
michael@0 | 192 | * coordinates. */ |
michael@0 | 193 | typedef struct { |
michael@0 | 194 | double x[ECFP_MAXDOUBLES]; |
michael@0 | 195 | double y[ECFP_MAXDOUBLES]; |
michael@0 | 196 | double z[ECFP_MAXDOUBLES]; |
michael@0 | 197 | } ecfp_jac_pt; |
michael@0 | 198 | |
michael@0 | 199 | /* Chudnovsky Jacobian EC Point. This coordinate system is the same as |
michael@0 | 200 | * Jacobian, except it keeps z^2, z^3 for faster additions. */ |
michael@0 | 201 | typedef struct { |
michael@0 | 202 | double x[ECFP_MAXDOUBLES]; |
michael@0 | 203 | double y[ECFP_MAXDOUBLES]; |
michael@0 | 204 | double z[ECFP_MAXDOUBLES]; |
michael@0 | 205 | double z2[ECFP_MAXDOUBLES]; |
michael@0 | 206 | double z3[ECFP_MAXDOUBLES]; |
michael@0 | 207 | } ecfp_chud_pt; |
michael@0 | 208 | |
michael@0 | 209 | /* Modified Jacobian EC Point. This coordinate system is the same as |
michael@0 | 210 | * Jacobian, except it keeps a*z^4 for faster doublings. */ |
michael@0 | 211 | typedef struct { |
michael@0 | 212 | double x[ECFP_MAXDOUBLES]; |
michael@0 | 213 | double y[ECFP_MAXDOUBLES]; |
michael@0 | 214 | double z[ECFP_MAXDOUBLES]; |
michael@0 | 215 | double az4[ECFP_MAXDOUBLES]; |
michael@0 | 216 | } ecfp_jm_pt; |
michael@0 | 217 | |
michael@0 | 218 | struct EC_group_fp_str; |
michael@0 | 219 | |
michael@0 | 220 | typedef struct EC_group_fp_str EC_group_fp; |
michael@0 | 221 | struct EC_group_fp_str { |
michael@0 | 222 | int fpPrecision; /* Set to number of bits in mantissa, 53 |
michael@0 | 223 | * or 64 */ |
michael@0 | 224 | int numDoubles; |
michael@0 | 225 | int primeBitSize; |
michael@0 | 226 | int orderBitSize; |
michael@0 | 227 | int doubleBitSize; |
michael@0 | 228 | int numInts; |
michael@0 | 229 | int aIsM3; /* True if curvea == -3 (mod p), then we |
michael@0 | 230 | * can optimize doubling */ |
michael@0 | 231 | double curvea[ECFP_MAXDOUBLES]; |
michael@0 | 232 | /* Used to truncate a double to the number of bits in the curve */ |
michael@0 | 233 | double bitSize_alpha; |
michael@0 | 234 | /* Pointer to either ecfp_alpha_53 or ecfp_alpha_64 */ |
michael@0 | 235 | const double *alpha; |
michael@0 | 236 | |
michael@0 | 237 | void (*ecfp_singleReduce) (double *r, const EC_group_fp * group); |
michael@0 | 238 | void (*ecfp_reduce) (double *r, double *x, const EC_group_fp * group); |
michael@0 | 239 | /* Performs a "tidy" operation, which performs carrying, moving excess |
michael@0 | 240 | * bits from one double to the next double, so that the precision of |
michael@0 | 241 | * the doubles is reduced to the regular precision ECFP_DSIZE. This |
michael@0 | 242 | * might result in some float digits being negative. */ |
michael@0 | 243 | void (*ecfp_tidy) (double *t, const double *alpha, |
michael@0 | 244 | const EC_group_fp * group); |
michael@0 | 245 | /* Perform a point addition using coordinate system Jacobian + Affine |
michael@0 | 246 | * -> Jacobian. Input and output should be multi-precision floating |
michael@0 | 247 | * point integers. */ |
michael@0 | 248 | void (*pt_add_jac_aff) (const ecfp_jac_pt * p, const ecfp_aff_pt * q, |
michael@0 | 249 | ecfp_jac_pt * r, const EC_group_fp * group); |
michael@0 | 250 | /* Perform a point doubling in Jacobian coordinates. Input and output |
michael@0 | 251 | * should be multi-precision floating point integers. */ |
michael@0 | 252 | void (*pt_dbl_jac) (const ecfp_jac_pt * dp, ecfp_jac_pt * dr, |
michael@0 | 253 | const EC_group_fp * group); |
michael@0 | 254 | /* Perform a point addition using Jacobian coordinate system. Input |
michael@0 | 255 | * and output should be multi-precision floating point integers. */ |
michael@0 | 256 | void (*pt_add_jac) (const ecfp_jac_pt * p, const ecfp_jac_pt * q, |
michael@0 | 257 | ecfp_jac_pt * r, const EC_group_fp * group); |
michael@0 | 258 | /* Perform a point doubling in Modified Jacobian coordinates. Input |
michael@0 | 259 | * and output should be multi-precision floating point integers. */ |
michael@0 | 260 | void (*pt_dbl_jm) (const ecfp_jm_pt * p, ecfp_jm_pt * r, |
michael@0 | 261 | const EC_group_fp * group); |
michael@0 | 262 | /* Perform a point doubling using coordinates Affine -> Chudnovsky |
michael@0 | 263 | * Jacobian. Input and output should be multi-precision floating point |
michael@0 | 264 | * integers. */ |
michael@0 | 265 | void (*pt_dbl_aff2chud) (const ecfp_aff_pt * p, ecfp_chud_pt * r, |
michael@0 | 266 | const EC_group_fp * group); |
michael@0 | 267 | /* Perform a point addition using coordinates: Modified Jacobian + |
michael@0 | 268 | * Chudnovsky Jacobian -> Modified Jacobian. Input and output should |
michael@0 | 269 | * be multi-precision floating point integers. */ |
michael@0 | 270 | void (*pt_add_jm_chud) (ecfp_jm_pt * p, ecfp_chud_pt * q, |
michael@0 | 271 | ecfp_jm_pt * r, const EC_group_fp * group); |
michael@0 | 272 | /* Perform a point addition using Chudnovsky Jacobian coordinates. |
michael@0 | 273 | * Input and output should be multi-precision floating point integers. |
michael@0 | 274 | */ |
michael@0 | 275 | void (*pt_add_chud) (const ecfp_chud_pt * p, const ecfp_chud_pt * q, |
michael@0 | 276 | ecfp_chud_pt * r, const EC_group_fp * group); |
michael@0 | 277 | /* Expects out to be an array of size 16 of Chudnovsky Jacobian |
michael@0 | 278 | * points. Fills in Chudnovsky Jacobian form (x, y, z, z^2, z^3), for |
michael@0 | 279 | * -15P, -13P, -11P, -9P, -7P, -5P, -3P, -P, P, 3P, 5P, 7P, 9P, 11P, |
michael@0 | 280 | * 13P, 15P */ |
michael@0 | 281 | void (*precompute_chud) (ecfp_chud_pt * out, const ecfp_aff_pt * p, |
michael@0 | 282 | const EC_group_fp * group); |
michael@0 | 283 | /* Expects out to be an array of size 16 of Jacobian points. Fills in |
michael@0 | 284 | * Chudnovsky Jacobian form (x, y, z), for O, P, 2P, ... 15P */ |
michael@0 | 285 | void (*precompute_jac) (ecfp_jac_pt * out, const ecfp_aff_pt * p, |
michael@0 | 286 | const EC_group_fp * group); |
michael@0 | 287 | |
michael@0 | 288 | }; |
michael@0 | 289 | |
michael@0 | 290 | /* Computes r = x*y. |
michael@0 | 291 | * r must be different (point to different memory) than x and y. |
michael@0 | 292 | * Does not tidy or reduce. */ |
michael@0 | 293 | void ecfp_multiply(double *r, const double *x, const double *y); |
michael@0 | 294 | |
michael@0 | 295 | /* Performs a "tidy" operation, which performs carrying, moving excess |
michael@0 | 296 | * bits from one double to the next double, so that the precision of the |
michael@0 | 297 | * doubles is reduced to the regular precision group->doubleBitSize. This |
michael@0 | 298 | * might result in some float digits being negative. */ |
michael@0 | 299 | void ecfp_tidy(double *t, const double *alpha, const EC_group_fp * group); |
michael@0 | 300 | |
michael@0 | 301 | /* Performs tidying on only the upper float digits of a multi-precision |
michael@0 | 302 | * floating point integer, i.e. the digits beyond the regular length which |
michael@0 | 303 | * are removed in the reduction step. */ |
michael@0 | 304 | void ecfp_tidyUpper(double *t, const EC_group_fp * group); |
michael@0 | 305 | |
michael@0 | 306 | /* Performs tidying on a short multi-precision floating point integer (the |
michael@0 | 307 | * lower group->numDoubles floats). */ |
michael@0 | 308 | void ecfp_tidyShort(double *t, const EC_group_fp * group); |
michael@0 | 309 | |
michael@0 | 310 | /* Performs a more mathematically precise "tidying" so that each term is |
michael@0 | 311 | * positive. This is slower than the regular tidying, and is used for |
michael@0 | 312 | * conversion from floating point to integer. */ |
michael@0 | 313 | void ecfp_positiveTidy(double *t, const EC_group_fp * group); |
michael@0 | 314 | |
michael@0 | 315 | /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters |
michael@0 | 316 | * a, b and p are the elliptic curve coefficients and the prime that |
michael@0 | 317 | * determines the field GFp. Elliptic curve points P and R can be |
michael@0 | 318 | * identical. Uses mixed Jacobian-affine coordinates. Uses 4-bit window |
michael@0 | 319 | * method. */ |
michael@0 | 320 | mp_err |
michael@0 | 321 | ec_GFp_point_mul_jac_4w_fp(const mp_int *n, const mp_int *px, |
michael@0 | 322 | const mp_int *py, mp_int *rx, mp_int *ry, |
michael@0 | 323 | const ECGroup *ecgroup); |
michael@0 | 324 | |
michael@0 | 325 | /* Computes R = nP where R is (rx, ry) and P is the base point. The |
michael@0 | 326 | * parameters a, b and p are the elliptic curve coefficients and the prime |
michael@0 | 327 | * that determines the field GFp. Elliptic curve points P and R can be |
michael@0 | 328 | * identical. Uses mixed Jacobian-affine coordinates (Jacobian |
michael@0 | 329 | * coordinates for doubles and affine coordinates for additions; based on |
michael@0 | 330 | * recommendation from Brown et al.). Uses window NAF method (algorithm |
michael@0 | 331 | * 11) for scalar-point multiplication from Brown, Hankerson, Lopez, |
michael@0 | 332 | * Menezes. Software Implementation of the NIST Elliptic Curves Over Prime |
michael@0 | 333 | * Fields. */ |
michael@0 | 334 | mp_err ec_GFp_point_mul_wNAF_fp(const mp_int *n, const mp_int *px, |
michael@0 | 335 | const mp_int *py, mp_int *rx, mp_int *ry, |
michael@0 | 336 | const ECGroup *ecgroup); |
michael@0 | 337 | |
michael@0 | 338 | /* Uses mixed Jacobian-affine coordinates to perform a point |
michael@0 | 339 | * multiplication: R = n * P, n scalar. Uses mixed Jacobian-affine |
michael@0 | 340 | * coordinates (Jacobian coordinates for doubles and affine coordinates |
michael@0 | 341 | * for additions; based on recommendation from Brown et al.). Not very |
michael@0 | 342 | * time efficient but quite space efficient, no precomputation needed. |
michael@0 | 343 | * group contains the elliptic curve coefficients and the prime that |
michael@0 | 344 | * determines the field GFp. Elliptic curve points P and R can be |
michael@0 | 345 | * identical. Performs calculations in floating point number format, since |
michael@0 | 346 | * this is faster than the integer operations on the ULTRASPARC III. |
michael@0 | 347 | * Uses left-to-right binary method (double & add) (algorithm 9) for |
michael@0 | 348 | * scalar-point multiplication from Brown, Hankerson, Lopez, Menezes. |
michael@0 | 349 | * Software Implementation of the NIST Elliptic Curves Over Prime Fields. */ |
michael@0 | 350 | mp_err |
michael@0 | 351 | ec_GFp_pt_mul_jac_fp(const mp_int *n, const mp_int *px, const mp_int *py, |
michael@0 | 352 | mp_int *rx, mp_int *ry, const ECGroup *ecgroup); |
michael@0 | 353 | |
michael@0 | 354 | /* Cleans up extra memory allocated in ECGroup for this implementation. */ |
michael@0 | 355 | void ec_GFp_extra_free_fp(ECGroup *group); |
michael@0 | 356 | |
michael@0 | 357 | /* Converts from a floating point representation into an mp_int. Expects |
michael@0 | 358 | * that d is already reduced. */ |
michael@0 | 359 | void |
michael@0 | 360 | ecfp_fp2i(mp_int *mpout, double *d, const ECGroup *ecgroup); |
michael@0 | 361 | |
michael@0 | 362 | /* Converts from an mpint into a floating point representation. */ |
michael@0 | 363 | void |
michael@0 | 364 | ecfp_i2fp(double *out, const mp_int *x, const ECGroup *ecgroup); |
michael@0 | 365 | |
michael@0 | 366 | /* Tests what precision floating point arithmetic is set to. This should |
michael@0 | 367 | * be either a 53-bit mantissa (IEEE standard) or a 64-bit mantissa |
michael@0 | 368 | * (extended precision on x86) and sets it into the EC_group_fp. Returns |
michael@0 | 369 | * either 53 or 64 accordingly. */ |
michael@0 | 370 | int ec_set_fp_precision(EC_group_fp * group); |
michael@0 | 371 | |
michael@0 | 372 | #endif |