| 1 |
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/bn.h> |
| 5 |
#include <openssl/ec.h> |
| 6 |
#include <openssl/err.h> |
| 7 |
#include <openssl/mem.h> |
| 8 |
|
| 9 |
#include "../bn/internal.h" |
| 10 |
#include "../cpucap/internal.h" |
| 11 |
#include "../delocate.h" |
| 12 |
#include "internal.h" |
| 13 |
#include "ec_nistp.h" |
| 14 |
|
| 15 |
#if !defined(OPENSSL_SMALL) |
| 16 |
|
| 17 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 18 |
# include "../../../third_party/s2n-bignum/s2n-bignum_aws-lc.h" |
| 19 |
#else |
| 20 |
# if defined(EC_NISTP_USE_64BIT_LIMB) |
| 21 |
# include "../../../third_party/fiat/p384_64.h" |
| 22 |
# else |
| 23 |
# include "../../../third_party/fiat/p384_32.h" |
| 24 |
# endif |
| 25 |
#endif |
| 26 |
|
| 27 |
#if defined(EC_NISTP_USE_64BIT_LIMB) |
| 28 |
|
| 29 |
#define P384_NLIMBS (6) |
| 30 |
typedef uint64_t p384_limb_t; |
| 31 |
typedef uint64_t p384_felem[P384_NLIMBS]; |
| 32 |
static const p384_felem p384_felem_one = { |
| 33 |
0xffffffff00000001, 0xffffffff, 0x1, 0x0, 0x0, 0x0}; |
| 34 |
|
| 35 |
#else // 64BIT; else 32BIT |
| 36 |
|
| 37 |
#define P384_NLIMBS (12) |
| 38 |
typedef uint32_t p384_limb_t; |
| 39 |
typedef uint32_t p384_felem[P384_NLIMBS]; |
| 40 |
static const p384_felem p384_felem_one = { |
| 41 |
0x1, 0xffffffff, 0xffffffff, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 42 |
|
| 43 |
#endif // 64BIT |
| 44 |
|
| 45 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 46 |
|
| 47 |
#define p384_felem_add(out, in0, in1) bignum_add_p384(out, in0, in1) |
| 48 |
#define p384_felem_sub(out, in0, in1) bignum_sub_p384(out, in0, in1) |
| 49 |
#define p384_felem_opp(out, in0) bignum_neg_p384(out, in0) |
| 50 |
#define p384_felem_to_bytes(out, in0) bignum_tolebytes_6(out, in0) |
| 51 |
#define p384_felem_from_bytes(out, in0) bignum_fromlebytes_6(out, in0) |
| 52 |
#define p384_felem_to_mont(out, in0) bignum_tomont_p384_selector(out, in0) |
| 53 |
#define p384_felem_from_mont(out, in0) bignum_deamont_p384_selector(out, in0) |
| 54 |
#define p384_felem_mul(out, in0, in1) bignum_montmul_p384_selector(out, in0, in1) |
| 55 |
#define p384_felem_sqr(out, in0) bignum_montsqr_p384_selector(out, in0) |
| 56 |
|
| 57 |
static p384_limb_t p384_felem_nz(const p384_limb_t in1[P384_NLIMBS]) { |
| 58 |
return bignum_nonzero_6(in1); |
| 59 |
} |
| 60 |
|
| 61 |
#else // EC_NISTP_USE_S2N_BIGNUM |
| 62 |
|
| 63 |
// Fiat-crypto implementation of field arithmetic |
| 64 |
#define p384_felem_add(out, in0, in1) fiat_p384_add(out, in0, in1) |
| 65 |
#define p384_felem_sub(out, in0, in1) fiat_p384_sub(out, in0, in1) |
| 66 |
#define p384_felem_opp(out, in0) fiat_p384_opp(out, in0) |
| 67 |
#define p384_felem_mul(out, in0, in1) fiat_p384_mul(out, in0, in1) |
| 68 |
#define p384_felem_sqr(out, in0) fiat_p384_square(out, in0) |
| 69 |
#define p384_felem_to_mont(out, in0) fiat_p384_to_montgomery(out, in0) |
| 70 |
#define p384_felem_from_mont(out, in0) fiat_p384_from_montgomery(out, in0) |
| 71 |
#define p384_felem_to_bytes(out, in0) fiat_p384_to_bytes(out, in0) |
| 72 |
#define p384_felem_from_bytes(out, in0) fiat_p384_from_bytes(out, in0) |
| 73 |
|
| 74 |
static p384_limb_t p384_felem_nz(const p384_limb_t in1[P384_NLIMBS]) { |
| 75 |
p384_limb_t ret; |
| 76 |
fiat_p384_nonzero(&ret, in1); |
| 77 |
return ret; |
| 78 |
} |
| 79 |
|
| 80 |
#endif // EC_NISTP_USE_S2N_BIGNUM |
| 81 |
|
| 82 |
// The wrapper functions are needed for FIPS static build. |
| 83 |
// Otherwise, initializing ec_nistp_meth with pointers to s2n-bignum |
| 84 |
// functions directly generates :got: references that are also thought |
| 85 |
// to be local_target by the delocator. |
| 86 |
static inline void p384_felem_add_wrapper(ec_nistp_felem_limb *c, |
| 87 |
const ec_nistp_felem_limb *a, |
| 88 |
const ec_nistp_felem_limb *b) { |
| 89 |
p384_felem_add(c, a, b); |
| 90 |
} |
| 91 |
|
| 92 |
static inline void p384_felem_sub_wrapper(ec_nistp_felem_limb *c, |
| 93 |
const ec_nistp_felem_limb *a, |
| 94 |
const ec_nistp_felem_limb *b) { |
| 95 |
p384_felem_sub(c, a, b); |
| 96 |
} |
| 97 |
|
| 98 |
static inline void p384_felem_neg_wrapper(ec_nistp_felem_limb *c, |
| 99 |
const ec_nistp_felem_limb *a) { |
| 100 |
p384_felem_opp(c, a); |
| 101 |
} |
| 102 |
|
| 103 |
static void p384_from_generic(p384_felem out, const EC_FELEM *in) { |
| 104 |
#ifdef OPENSSL_BIG_ENDIAN |
| 105 |
uint8_t tmp[P384_EC_FELEM_BYTES]; |
| 106 |
bn_words_to_little_endian(tmp, P384_EC_FELEM_BYTES, in->words, P384_EC_FELEM_WORDS); |
| 107 |
p384_felem_from_bytes(out, tmp); |
| 108 |
#else |
| 109 |
p384_felem_from_bytes(out, (const uint8_t *)in->words); |
| 110 |
#endif |
| 111 |
} |
| 112 |
|
| 113 |
static void p384_to_generic(EC_FELEM *out, const p384_felem in) { |
| 114 |
// This works because 384 is a multiple of 64, so there are no excess bytes to |
| 115 |
// zero when rounding up to |BN_ULONG|s. |
| 116 |
OPENSSL_STATIC_ASSERT( |
| 117 |
384 / 8 == sizeof(BN_ULONG) * ((384 + BN_BITS2 - 1) / BN_BITS2), |
| 118 |
p384_felem_to_bytes_leaves_bytes_uninitialized); |
| 119 |
#ifdef OPENSSL_BIG_ENDIAN |
| 120 |
uint8_t tmp[P384_EC_FELEM_BYTES]; |
| 121 |
p384_felem_to_bytes(tmp, in); |
| 122 |
bn_little_endian_to_words(out->words, P384_EC_FELEM_WORDS, tmp, P384_EC_FELEM_BYTES); |
| 123 |
#else |
| 124 |
p384_felem_to_bytes((uint8_t *)out->words, in); |
| 125 |
#endif |
| 126 |
} |
| 127 |
|
| 128 |
static void p384_from_scalar(p384_felem out, const EC_SCALAR *in) { |
| 129 |
#ifdef OPENSSL_BIG_ENDIAN |
| 130 |
uint8_t tmp[P384_EC_FELEM_BYTES]; |
| 131 |
bn_words_to_little_endian(tmp, P384_EC_FELEM_BYTES, in->words, P384_EC_FELEM_WORDS); |
| 132 |
p384_felem_from_bytes(out, tmp); |
| 133 |
#else |
| 134 |
p384_felem_from_bytes(out, (const uint8_t *)in->words); |
| 135 |
#endif |
| 136 |
} |
| 137 |
|
| 138 |
// p384_inv_square calculates |out| = |in|^{-2} |
| 139 |
// |
| 140 |
// Based on Fermat's Little Theorem: |
| 141 |
// a^p = a (mod p) |
| 142 |
// a^{p-1} = 1 (mod p) |
| 143 |
// a^{p-3} = a^{-2} (mod p) |
| 144 |
// p = 2^384 - 2^128 - 2^96 + 2^32 - 1 |
| 145 |
// Hexadecimal representation of p − 3: |
| 146 |
// p-3 = ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe |
| 147 |
// ffffffff 00000000 00000000 fffffffc |
| 148 |
static void p384_inv_square(p384_felem out, |
| 149 |
const p384_felem in) { |
| 150 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 151 |
ec_nistp_felem_limb in_sqr[P384_NLIMBS]; |
| 152 |
p384_felem_sqr(in_sqr, in); |
| 153 |
bignum_montinv_p384(out, in_sqr); |
| 154 |
#else |
| 155 |
// This implements the addition chain described in |
| 156 |
// https://briansmith.org/ecc-inversion-addition-chains-01#p384_field_inversion |
| 157 |
// The side comments show the value of the exponent: |
| 158 |
// squaring the element => doubling the exponent |
| 159 |
// multiplying by an element => adding to the exponent the power of that element |
| 160 |
p384_felem x2, x3, x6, x12, x15, x30, x60, x120; |
| 161 |
p384_felem_sqr(x2, in); // 2^2 - 2^1 |
| 162 |
p384_felem_mul(x2, x2, in); // 2^2 - 2^0 |
| 163 |
|
| 164 |
p384_felem_sqr(x3, x2); // 2^3 - 2^1 |
| 165 |
p384_felem_mul(x3, x3, in); // 2^3 - 2^0 |
| 166 |
|
| 167 |
p384_felem_sqr(x6, x3); |
| 168 |
for (int i = 1; i < 3; i++) { |
| 169 |
p384_felem_sqr(x6, x6); |
| 170 |
} // 2^6 - 2^3 |
| 171 |
p384_felem_mul(x6, x6, x3); // 2^6 - 2^0 |
| 172 |
|
| 173 |
p384_felem_sqr(x12, x6); |
| 174 |
for (int i = 1; i < 6; i++) { |
| 175 |
p384_felem_sqr(x12, x12); |
| 176 |
} // 2^12 - 2^6 |
| 177 |
p384_felem_mul(x12, x12, x6); // 2^12 - 2^0 |
| 178 |
|
| 179 |
p384_felem_sqr(x15, x12); |
| 180 |
for (int i = 1; i < 3; i++) { |
| 181 |
p384_felem_sqr(x15, x15); |
| 182 |
} // 2^15 - 2^3 |
| 183 |
p384_felem_mul(x15, x15, x3); // 2^15 - 2^0 |
| 184 |
|
| 185 |
p384_felem_sqr(x30, x15); |
| 186 |
for (int i = 1; i < 15; i++) { |
| 187 |
p384_felem_sqr(x30, x30); |
| 188 |
} // 2^30 - 2^15 |
| 189 |
p384_felem_mul(x30, x30, x15); // 2^30 - 2^0 |
| 190 |
|
| 191 |
p384_felem_sqr(x60, x30); |
| 192 |
for (int i = 1; i < 30; i++) { |
| 193 |
p384_felem_sqr(x60, x60); |
| 194 |
} // 2^60 - 2^30 |
| 195 |
p384_felem_mul(x60, x60, x30); // 2^60 - 2^0 |
| 196 |
|
| 197 |
p384_felem_sqr(x120, x60); |
| 198 |
for (int i = 1; i < 60; i++) { |
| 199 |
p384_felem_sqr(x120, x120); |
| 200 |
} // 2^120 - 2^60 |
| 201 |
p384_felem_mul(x120, x120, x60); // 2^120 - 2^0 |
| 202 |
|
| 203 |
p384_felem ret; |
| 204 |
p384_felem_sqr(ret, x120); |
| 205 |
for (int i = 1; i < 120; i++) { |
| 206 |
p384_felem_sqr(ret, ret); |
| 207 |
} // 2^240 - 2^120 |
| 208 |
p384_felem_mul(ret, ret, x120); // 2^240 - 2^0 |
| 209 |
|
| 210 |
for (int i = 0; i < 15; i++) { |
| 211 |
p384_felem_sqr(ret, ret); |
| 212 |
} // 2^255 - 2^15 |
| 213 |
p384_felem_mul(ret, ret, x15); // 2^255 - 2^0 |
| 214 |
|
| 215 |
// Why (1 + 30) in the loop? |
| 216 |
// This is as expressed in: |
| 217 |
// https://briansmith.org/ecc-inversion-addition-chains-01#p384_field_inversion |
| 218 |
// My guess is to say that we're going to shift 31 bits, but this time we |
| 219 |
// won't add x31 to make all the new bits 1s, as was done in previous steps, |
| 220 |
// but we're going to add x30 so there will be 255 1s, then a 0, then 30 1s |
| 221 |
// to form this pattern: |
| 222 |
// ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe ffffffff |
| 223 |
// (the last 2 1s are appended in the following step). |
| 224 |
for (int i = 0; i < (1 + 30); i++) { |
| 225 |
p384_felem_sqr(ret, ret); |
| 226 |
} // 2^286 - 2^31 |
| 227 |
p384_felem_mul(ret, ret, x30); // 2^286 - 2^30 - 2^0 |
| 228 |
|
| 229 |
p384_felem_sqr(ret, ret); |
| 230 |
p384_felem_sqr(ret, ret); // 2^288 - 2^32 - 2^2 |
| 231 |
p384_felem_mul(ret, ret, x2); // 2^288 - 2^32 - 2^0 |
| 232 |
|
| 233 |
// Why not 94 instead of (64 + 30) in the loop? |
| 234 |
// Similarly to the comment above, there is a shift of 94 bits |
| 235 |
// but what will be added is x30, which will cause 64 of those bits |
| 236 |
// to be 64 0s and 30 1s to complete the pattern above with: |
| 237 |
// 00000000 00000000 fffffffc |
| 238 |
// (the last 2 0s are appended by the last 2 shifts). |
| 239 |
for (int i = 0; i < (64 + 30); i++) { |
| 240 |
p384_felem_sqr(ret, ret); |
| 241 |
} // 2^382 - 2^126 - 2^94 |
| 242 |
p384_felem_mul(ret, ret, x30); // 2^382 - 2^126 - 2^94 + 2^30 - 2^0 |
| 243 |
|
| 244 |
p384_felem_sqr(ret, ret); |
| 245 |
p384_felem_sqr(out, ret); // 2^384 - 2^128 - 2^96 + 2^32 - 2^2 = p - 3 |
| 246 |
#endif |
| 247 |
} |
| 248 |
|
| 249 |
static void p384_point_double(p384_felem x_out, |
| 250 |
p384_felem y_out, |
| 251 |
p384_felem z_out, |
| 252 |
const p384_felem x_in, |
| 253 |
const p384_felem y_in, |
| 254 |
const p384_felem z_in) { |
| 255 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 256 |
ec_nistp_felem_limb in[P384_NLIMBS * 3]; |
| 257 |
ec_nistp_felem_limb out[P384_NLIMBS * 3]; |
| 258 |
ec_nistp_coordinates_to_point(in, x_in, y_in, z_in, P384_NLIMBS); |
| 259 |
p384_montjdouble_selector(out, in); |
| 260 |
ec_nistp_point_to_coordinates(x_out, y_out, z_out, out, P384_NLIMBS); |
| 261 |
#else |
| 262 |
ec_nistp_point_double(p384_methods(), x_out, y_out, z_out, x_in, y_in, z_in); |
| 263 |
#endif |
| 264 |
} |
| 265 |
|
| 266 |
// p384_point_add calculates (x1, y1, z1) + (x2, y2, z2) |
| 267 |
// |
| 268 |
// The method is taken from: |
| 269 |
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-add-2007-bl |
| 270 |
// adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity). |
| 271 |
// |
| 272 |
// Coq transcription and correctness proof: |
| 273 |
// <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L467> |
| 274 |
// <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L544> |
| 275 |
static void p384_point_add(p384_felem x3, p384_felem y3, p384_felem z3, |
| 276 |
const p384_felem x1, |
| 277 |
const p384_felem y1, |
| 278 |
const p384_felem z1, |
| 279 |
const int mixed, |
| 280 |
const p384_felem x2, |
| 281 |
const p384_felem y2, |
| 282 |
const p384_felem z2) { |
| 283 |
ec_nistp_point_add(p384_methods(), x3, y3, z3, x1, y1, z1, mixed, x2, y2, z2); |
| 284 |
} |
| 285 |
|
| 286 |
#include "p384_table.h" |
| 287 |
|
| 288 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 289 |
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p384_methods) { |
| 290 |
out->felem_num_limbs = P384_NLIMBS; |
| 291 |
out->felem_num_bits = 384; |
| 292 |
out->felem_add = p384_felem_add_wrapper; |
| 293 |
out->felem_sub = p384_felem_sub_wrapper; |
| 294 |
out->felem_mul = bignum_montmul_p384_selector; |
| 295 |
out->felem_sqr = bignum_montsqr_p384_selector; |
| 296 |
out->felem_neg = p384_felem_neg_wrapper; |
| 297 |
out->felem_nz = p384_felem_nz; |
| 298 |
out->felem_one = p384_felem_one; |
| 299 |
out->point_dbl = p384_point_double; |
| 300 |
out->point_add = p384_point_add; |
| 301 |
out->scalar_mul_base_table = (const ec_nistp_felem_limb*) p384_g_pre_comp; |
| 302 |
} |
| 303 |
#else |
| 304 |
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p384_methods) { |
| 305 |
out->felem_num_limbs = P384_NLIMBS; |
| 306 |
out->felem_num_bits = 384; |
| 307 |
out->felem_add = fiat_p384_add; |
| 308 |
out->felem_sub = fiat_p384_sub; |
| 309 |
out->felem_mul = fiat_p384_mul; |
| 310 |
out->felem_sqr = fiat_p384_square; |
| 311 |
out->felem_neg = fiat_p384_opp; |
| 312 |
out->felem_nz = p384_felem_nz; |
| 313 |
out->felem_one = p384_felem_one; |
| 314 |
out->point_dbl = p384_point_double; |
| 315 |
out->point_add = p384_point_add; |
| 316 |
out->scalar_mul_base_table = (const ec_nistp_felem_limb*) p384_g_pre_comp; |
| 317 |
} |
| 318 |
#endif |
| 319 |
|
| 320 |
// OPENSSL EC_METHOD FUNCTIONS |
| 321 |
|
| 322 |
// Takes the Jacobian coordinates (X, Y, Z) of a point and returns: |
| 323 |
// (X', Y') = (X/Z^2, Y/Z^3). |
| 324 |
static int ec_GFp_nistp384_point_get_affine_coordinates( |
| 325 |
const EC_GROUP *group, const EC_JACOBIAN *point, |
| 326 |
EC_FELEM *x_out, EC_FELEM *y_out) { |
| 327 |
|
| 328 |
if (constant_time_declassify_w(ec_GFp_simple_is_at_infinity(group, point))) { |
| 329 |
OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
p384_felem z1, z2; |
| 334 |
p384_from_generic(z1, &point->Z); |
| 335 |
p384_inv_square(z2, z1); |
| 336 |
|
| 337 |
if (x_out != NULL) { |
| 338 |
p384_felem x; |
| 339 |
p384_from_generic(x, &point->X); |
| 340 |
p384_felem_mul(x, x, z2); |
| 341 |
p384_to_generic(x_out, x); |
| 342 |
} |
| 343 |
|
| 344 |
if (y_out != NULL) { |
| 345 |
p384_felem y; |
| 346 |
p384_from_generic(y, &point->Y); |
| 347 |
p384_felem_sqr(z2, z2); // z^-4 |
| 348 |
p384_felem_mul(y, y, z1); // y * z |
| 349 |
p384_felem_mul(y, y, z2); // y * z^-3 |
| 350 |
p384_to_generic(y_out, y); |
| 351 |
} |
| 352 |
|
| 353 |
return 1; |
| 354 |
} |
| 355 |
|
| 356 |
static void ec_GFp_nistp384_add(const EC_GROUP *group, EC_JACOBIAN *r, |
| 357 |
const EC_JACOBIAN *a, const EC_JACOBIAN *b) { |
| 358 |
p384_felem x1, y1, z1, x2, y2, z2; |
| 359 |
p384_from_generic(x1, &a->X); |
| 360 |
p384_from_generic(y1, &a->Y); |
| 361 |
p384_from_generic(z1, &a->Z); |
| 362 |
p384_from_generic(x2, &b->X); |
| 363 |
p384_from_generic(y2, &b->Y); |
| 364 |
p384_from_generic(z2, &b->Z); |
| 365 |
p384_point_add(x1, y1, z1, x1, y1, z1, 0 /* both Jacobian */, x2, y2, z2); |
| 366 |
p384_to_generic(&r->X, x1); |
| 367 |
p384_to_generic(&r->Y, y1); |
| 368 |
p384_to_generic(&r->Z, z1); |
| 369 |
} |
| 370 |
|
| 371 |
static void ec_GFp_nistp384_dbl(const EC_GROUP *group, EC_JACOBIAN *r, |
| 372 |
const EC_JACOBIAN *a) { |
| 373 |
p384_felem x, y, z; |
| 374 |
p384_from_generic(x, &a->X); |
| 375 |
p384_from_generic(y, &a->Y); |
| 376 |
p384_from_generic(z, &a->Z); |
| 377 |
p384_point_double(x, y, z, x, y, z); |
| 378 |
p384_to_generic(&r->X, x); |
| 379 |
p384_to_generic(&r->Y, y); |
| 380 |
p384_to_generic(&r->Z, z); |
| 381 |
} |
| 382 |
|
| 383 |
// The calls to from/to_generic are needed for the case |
| 384 |
// when BORINGSSL_HAS_UINT128 is undefined, i.e. p384_32.h fiat code is used; |
| 385 |
// while OPENSSL_64_BIT is defined, i.e. BN_ULONG is uint64_t |
| 386 |
static void ec_GFp_nistp384_mont_felem_to_bytes( |
| 387 |
const EC_GROUP *group, uint8_t *out, size_t *out_len, const EC_FELEM *in) { |
| 388 |
|
| 389 |
size_t len = BN_num_bytes(&group->field.N); |
| 390 |
EC_FELEM felem_tmp; |
| 391 |
p384_felem tmp; |
| 392 |
p384_from_generic(tmp, in); |
| 393 |
p384_felem_from_mont(tmp, tmp); |
| 394 |
p384_to_generic(&felem_tmp, tmp); |
| 395 |
|
| 396 |
bn_words_to_big_endian(out, len, felem_tmp.words, group->order.N.width); |
| 397 |
|
| 398 |
*out_len = len; |
| 399 |
} |
| 400 |
|
| 401 |
static int ec_GFp_nistp384_mont_felem_from_bytes( |
| 402 |
const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, size_t len) { |
| 403 |
|
| 404 |
EC_FELEM felem_tmp; |
| 405 |
p384_felem tmp; |
| 406 |
// This function calls bn_cmp_words_consttime |
| 407 |
if (!ec_GFp_simple_felem_from_bytes(group, &felem_tmp, in, len)) { |
| 408 |
return 0; |
| 409 |
} |
| 410 |
p384_from_generic(tmp, &felem_tmp); |
| 411 |
p384_felem_to_mont(tmp, tmp); |
| 412 |
p384_to_generic(out, tmp); |
| 413 |
return 1; |
| 414 |
} |
| 415 |
|
| 416 |
static int ec_GFp_nistp384_cmp_x_coordinate(const EC_GROUP *group, |
| 417 |
const EC_JACOBIAN *p, |
| 418 |
const EC_SCALAR *r) { |
| 419 |
if (ec_GFp_simple_is_at_infinity(group, p)) { |
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
// We wish to compare X/Z^2 with r. This is equivalent to comparing X with |
| 424 |
// r*Z^2. Note that X and Z are represented in Montgomery form, while r is |
| 425 |
// not. |
| 426 |
p384_felem Z2_mont; |
| 427 |
p384_from_generic(Z2_mont, &p->Z); |
| 428 |
p384_felem_mul(Z2_mont, Z2_mont, Z2_mont); |
| 429 |
|
| 430 |
p384_felem r_Z2; |
| 431 |
p384_from_scalar(r_Z2, r); // r < order < p, so this is valid. |
| 432 |
p384_felem_mul(r_Z2, r_Z2, Z2_mont); |
| 433 |
|
| 434 |
p384_felem X; |
| 435 |
p384_from_generic(X, &p->X); |
| 436 |
p384_felem_from_mont(X, X); |
| 437 |
|
| 438 |
if (OPENSSL_memcmp(&r_Z2, &X, sizeof(r_Z2)) == 0) { |
| 439 |
return 1; |
| 440 |
} |
| 441 |
|
| 442 |
// During signing the x coefficient is reduced modulo the group order. |
| 443 |
// Therefore there is a small possibility, less than 2^189/2^384 = 1/2^195, |
| 444 |
// that group_order < p.x < p. |
| 445 |
// In that case, we need not only to compare against |r| but also to |
| 446 |
// compare against r+group_order. |
| 447 |
assert(group->field.N.width == group->order.N.width); |
| 448 |
EC_FELEM tmp; |
| 449 |
BN_ULONG carry = |
| 450 |
bn_add_words(tmp.words, r->words, group->order.N.d, group->field.N.width); |
| 451 |
if (carry == 0 && |
| 452 |
bn_less_than_words(tmp.words, group->field.N.d, group->field.N.width)) { |
| 453 |
p384_from_generic(r_Z2, &tmp); |
| 454 |
p384_felem_mul(r_Z2, r_Z2, Z2_mont); |
| 455 |
if (OPENSSL_memcmp(&r_Z2, &X, sizeof(r_Z2)) == 0) { |
| 456 |
return 1; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
// Multiplication of an arbitrary point by a scalar, r = [scalar]P. |
| 464 |
static void ec_GFp_nistp384_point_mul(const EC_GROUP *group, EC_JACOBIAN *r, |
| 465 |
const EC_JACOBIAN *p, |
| 466 |
const EC_SCALAR *scalar) { |
| 467 |
|
| 468 |
p384_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}}; |
| 469 |
|
| 470 |
p384_from_generic(tmp[0], &p->X); |
| 471 |
p384_from_generic(tmp[1], &p->Y); |
| 472 |
p384_from_generic(tmp[2], &p->Z); |
| 473 |
|
| 474 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 475 |
p384_montjscalarmul_selector((uint64_t*)res, scalar->words, (uint64_t*)tmp); |
| 476 |
#else |
| 477 |
ec_nistp_scalar_mul(p384_methods(), res[0], res[1], res[2], tmp[0], tmp[1], tmp[2], scalar); |
| 478 |
#endif |
| 479 |
|
| 480 |
p384_to_generic(&r->X, res[0]); |
| 481 |
p384_to_generic(&r->Y, res[1]); |
| 482 |
p384_to_generic(&r->Z, res[2]); |
| 483 |
} |
| 484 |
|
| 485 |
// Multiplication of the base point G of P-384 curve with the given scalar. |
| 486 |
static void ec_GFp_nistp384_point_mul_base(const EC_GROUP *group, |
| 487 |
EC_JACOBIAN *r, |
| 488 |
const EC_SCALAR *scalar) { |
| 489 |
p384_felem res[3] = {{0}, {0}, {0}}; |
| 490 |
|
| 491 |
ec_nistp_scalar_mul_base(p384_methods(), res[0], res[1], res[2], scalar); |
| 492 |
|
| 493 |
p384_to_generic(&r->X, res[0]); |
| 494 |
p384_to_generic(&r->Y, res[1]); |
| 495 |
p384_to_generic(&r->Z, res[2]); |
| 496 |
} |
| 497 |
|
| 498 |
// Computes [g_scalar]G + [p_scalar]P, where G is the base point of the P-384 |
| 499 |
// curve, and P is the given point |p|. |
| 500 |
// Note: this function is NOT constant-time. |
| 501 |
static void ec_GFp_nistp384_point_mul_public(const EC_GROUP *group, |
| 502 |
EC_JACOBIAN *r, |
| 503 |
const EC_SCALAR *g_scalar, |
| 504 |
const EC_JACOBIAN *p, |
| 505 |
const EC_SCALAR *p_scalar) { |
| 506 |
|
| 507 |
p384_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}}; |
| 508 |
|
| 509 |
p384_from_generic(tmp[0], &p->X); |
| 510 |
p384_from_generic(tmp[1], &p->Y); |
| 511 |
p384_from_generic(tmp[2], &p->Z); |
| 512 |
|
| 513 |
ec_nistp_scalar_mul_public(p384_methods(), res[0], res[1], res[2], g_scalar, tmp[0], tmp[1], tmp[2], p_scalar); |
| 514 |
|
| 515 |
p384_to_generic(&r->X, res[0]); |
| 516 |
p384_to_generic(&r->Y, res[1]); |
| 517 |
p384_to_generic(&r->Z, res[2]); |
| 518 |
} |
| 519 |
|
| 520 |
DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp384_method) { |
| 521 |
out->point_get_affine_coordinates = |
| 522 |
ec_GFp_nistp384_point_get_affine_coordinates; |
| 523 |
out->jacobian_to_affine_batch = |
| 524 |
ec_GFp_mont_jacobian_to_affine_batch; // needed for TrustToken tests |
| 525 |
out->add = ec_GFp_nistp384_add; |
| 526 |
out->dbl = ec_GFp_nistp384_dbl; |
| 527 |
out->mul = ec_GFp_nistp384_point_mul; |
| 528 |
out->mul_base = ec_GFp_nistp384_point_mul_base; |
| 529 |
out->mul_public = ec_GFp_nistp384_point_mul_public; |
| 530 |
out->mul_batch = ec_GFp_mont_mul_batch; // needed for TrustToken tests |
| 531 |
out->mul_public_batch = ec_GFp_mont_mul_public_batch; |
| 532 |
out->init_precomp = ec_GFp_mont_init_precomp; // needed for TrustToken tests |
| 533 |
out->mul_precomp = ec_GFp_mont_mul_precomp; // needed for TrustToken tests |
| 534 |
out->felem_mul = ec_GFp_mont_felem_mul; |
| 535 |
out->felem_sqr = ec_GFp_mont_felem_sqr; |
| 536 |
out->felem_to_bytes = ec_GFp_nistp384_mont_felem_to_bytes; |
| 537 |
out->felem_from_bytes = ec_GFp_nistp384_mont_felem_from_bytes; |
| 538 |
out->felem_reduce = ec_GFp_mont_felem_reduce; // needed for ECTest.HashToCurve |
| 539 |
out->felem_exp = ec_GFp_mont_felem_exp; // needed for ECTest.HashToCurve |
| 540 |
out->scalar_inv0_montgomery = ec_simple_scalar_inv0_montgomery; |
| 541 |
out->scalar_to_montgomery_inv_vartime = |
| 542 |
ec_simple_scalar_to_montgomery_inv_vartime; |
| 543 |
out->cmp_x_coordinate = ec_GFp_nistp384_cmp_x_coordinate; |
| 544 |
} |
| 545 |
|
| 546 |
// ---------------------------------------------------------------------------- |
| 547 |
// Analysis of the doubling case occurrence in the Joye-Tunstall recoding: |
| 548 |
// p384_felem_mul_scalar_rwnaf() |
| 549 |
// ---------------------------------------------------------------------------- |
| 550 |
// |
| 551 |
// The JT scalar recoding is Algorithm 6: (Odd) Signed-Digit Recoding Algorithm in |
| 552 |
// Joye, Tunstall, "Exponent Recoding and Regular Exponentiation Algorithms", |
| 553 |
// AfricaCrypt 2009, available from |
| 554 |
// https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.477.1245&rep=rep1&type=pdf |
| 555 |
// |
| 556 |
// We write the algorithm using variables similar to those used in the code and |
| 557 |
// in the proof detailed in util.c (t_i in the algorithm below is d in |
| 558 |
// p384_felem_mul_scalar_rwnaf()): |
| 559 |
// |
| 560 |
// Input: k: odd scalar, where k = (b_{l-1}, ..., b_1, b_0) in binary form, |
| 561 |
// w: window width |
| 562 |
// Output: k = (t_{h-1}, ..., t_1, t_0) |
| 563 |
// with t_i \in {\pm 1, \pm 3, ..., \pm (2^{w} - 1)}, |
| 564 |
// h = ceil(t/w), i.e. t_i are positive and negative odd digits |
| 565 |
// which absolute value is less than (2^{w} - 1). |
| 566 |
// i := 0 |
| 567 |
// j := 0 |
| 568 |
// while (k > 2^w): |
| 569 |
// window := (b_{j+w}, ..., b_j) # (w+1)-bit window in k where |
| 570 |
// # the least significant bit is b_j |
| 571 |
// t_i := window - 2^w |
| 572 |
// k := k - t_i |
| 573 |
// k := k / 2^w # k >> w |
| 574 |
// i += 1 |
| 575 |
// j += w |
| 576 |
// t_{h-1} := k |
| 577 |
// |
| 578 |
// Note that if b_{j+w} = 0, t_i will be negative; |
| 579 |
// otherwise, if b_{j+w} = 1, t_i will be positive. |
| 580 |
// |
| 581 |
// The algorithm recodes the least (w+1) bits into a (odd) digit in the range |
| 582 |
// [-(2^{w}-1), (2^{w}-1)] by subtracting 2^w from that digit and adding it back |
| 583 |
// to the remaining bits of the scalar. This ensures that, after the w-bit right |
| 584 |
// shift, the next least significant bit is 1, i.e. next digit is odd. |
| 585 |
// |
| 586 |
// In the following we will show that the non-trivial doubling case in |
| 587 |
// single-point left-to-right windowed (or m-ary, m = 2^w) scalar multiplication |
| 588 |
// may occur if and only if the (2^w)th bit of the group order is 1. This only |
| 589 |
// holds if the scalar is fully reduced and the group order is a prime that is |
| 590 |
// much larger than 2^{w+1}. |
| 591 |
// |
| 592 |
// PROOF: |
| 593 |
// |
| 594 |
// Let n be the group order. Let l be the number of bits needed to represent n. |
| 595 |
// Assume there exists some 0 <= k < n such that signed w-bit windowed |
| 596 |
// multiplication hits the doubling case. |
| 597 |
// |
| 598 |
// Windowed multiplication consists of iterating over the digits t_i defined |
| 599 |
// above by the algorithm from most to least significant. At iteration i |
| 600 |
// (for i = ..., 3w, 2w, w, 0, starting from the most significant window), we: |
| 601 |
// |
| 602 |
// 1. Double the accumulator A, w times. Let A_i be the value of A at this |
| 603 |
// point, and it corresponds to a value [a_i]P. |
| 604 |
// |
| 605 |
// 2. Set A to T_i + A_i, where T_i is a precomputed multiple of P, [t_i]P |
| 606 |
|
| 607 |
// From the algorithm steps we can see that the current digit |
| 608 |
// t_i = (b_{w+i} ... b_i) - 2^w, b_i = 1 => -2^w < t_i < 2^w, t_i: odd |
| 609 |
// which can also be written using C notation as |
| 610 |
// t_i = [(k >> i) & ((1<<(w+1)) - 1)] - (1 << w) -- (1) |
| 611 |
// |
| 612 |
// and the accumulator value |
| 613 |
// a_i = b_{l-1} ... b_{i+w+1} 1 |
| 614 |
// when written as C notation |
| 615 |
// a_i = (k >> (i+w+1)) << (w+1)) + (1 << w) -- (2) |
| 616 |
// |
| 617 |
// Similarly to the recoding in util.c, a_i is bounded by |
| 618 |
// 0 <= a_i < n + 2^w. Additionally, a_i can only be zero if b_(i+w-1) and up |
| 619 |
// are all zero. (Note this implies a non-trivial P + (-P) is unreachable for |
| 620 |
// all groups. That would imply the subsequent a_i is zero, which means all |
| 621 |
// terms thus far were zero.) |
| 622 |
// |
| 623 |
// Let j be the index such that A_j = T_j ≠ ∞. We have a_j = t_j (mod n). We now |
| 624 |
// determine the value of a_j - t_j, which must be divisible by n. |
| 625 |
// Our bounds on a_j and t_j imply a_j - t_j is 0 or n. |
| 626 |
// If it is 0, a_j = t_j. However, 2^w divides a_j and -2^w < t_i < 2^w, so this |
| 627 |
// can only happen if a_j = t_j = 0, which is a trivial doubling. |
| 628 |
// Therefore, a_j - t_j = n. |
| 629 |
// |
| 630 |
// Now we determine j. Suppose j > 0. w divides j, so j >= w. Then, |
| 631 |
// |
| 632 |
// n = a_j - t_j = (k >> (j+w+1)) << (w+1)) + (1 << w) - t_j |
| 633 |
// = k/2^j + 2^w - t_j |
| 634 |
// < n/2^w + 2^w + 2^w-1 |
| 635 |
// |
| 636 |
// n is much larger than 2^{w+1}, so this is impossible. Thus, j = 0: only the |
| 637 |
// final addition may hit the doubling case. |
| 638 |
// |
| 639 |
// Finally, we consider bit patterns for n and k. Divide k into k_H + k_M + k_L |
| 640 |
// such that k_H is the contribution from b_(l-1) .. b_{w+1}, |
| 641 |
// k_M is the contribution from b_w, |
| 642 |
// and k_L is the contribution from b_(w-1) ... b_0. |
| 643 |
// That is: |
| 644 |
// |
| 645 |
// - 2^{w+1} divides k_H |
| 646 |
// - k_M is 0 or 2^w |
| 647 |
// - 0 <= k_L < 2^w |
| 648 |
// |
| 649 |
// Divide n into n_H + n_M + n_L similarly. |
| 650 |
// From (1) and (2), we have |
| 651 |
// |
| 652 |
// t_0 = (k_M + k_L) - 2^w |
| 653 |
// a_0 = k_H + 2^w |
| 654 |
// |
| 655 |
// We try to find t_0 and a_0 such that |
| 656 |
// |
| 657 |
// n = a_0 - t_0 |
| 658 |
// n_H + n_M + n_L = k_H + 2^w - (k_M + k_L - 2^w) |
| 659 |
// = k_H + 2^{w+1} - (k_M + k_L) |
| 660 |
// |
| 661 |
// We know that k_H <= n_H. |
| 662 |
// |
| 663 |
// If k_H < n_H, then k_H <= n_H - 2^{w+1} (Note that 2^{w+1} divides both k_H |
| 664 |
// and n_H). Then we would have |
| 665 |
// |
| 666 |
// n_H + n_M + n_L <= n_H - 2^{w+1} + 2^{w+1} - (k_M + k_L) |
| 667 |
// n_M + n_L <= - (k_M + k_L) |
| 668 |
// |
| 669 |
// Contradiction. Thus, |
| 670 |
// |
| 671 |
// k_H = n_H -- (3) |
| 672 |
// => n_M + n_L = 2^{w+1} - (k_M + k_L) -- (4) |
| 673 |
// |
| 674 |
// We also have n > k; hence, |
| 675 |
// n_M + n_L > k_M + k_L -- (5) |
| 676 |
// |
| 677 |
// For (3), (4) and (5) to hold, |
| 678 |
// n_M = 2^w, k_M = 0. |
| 679 |
// |
| 680 |
// Otherwise, if n_M = 0 and k_M = 0 |
| 681 |
// n_L = 2^{w+1} - k_L |
| 682 |
// n_L >= 2^{w+1} - (2^w - 1) |
| 683 |
// n_L >= 2^w + 1 |
| 684 |
// Contradiction since n_L < 2^w. |
| 685 |
// |
| 686 |
// And if n_M = 0 and k_M = 2^w, (5) would not hold. |
| 687 |
// |
| 688 |
// Since n_M = 2^w, n_L >= 1, k_L >= 1, from (4) we have |
| 689 |
// k_M + k_L = 2^{w+1} - (n_M + n_L) |
| 690 |
// <= 2^{w+1} - 2^w - 1 |
| 691 |
// <= 2^{w} - 1 |
| 692 |
// => k_M = 0 |
| 693 |
// |
| 694 |
// Putting this together, from the group order of the curve, n, we can construct |
| 695 |
// the scalar, k, that would incur a doubling in the last iteration as: |
| 696 |
// |
| 697 |
// if n_M = 2^w, |
| 698 |
// k_H = n_H and |
| 699 |
// k_M + k_L = 2^{w+1} - (n_M + n_L) |
| 700 |
// |
| 701 |
// COMMON CURVES: |
| 702 |
// |
| 703 |
// The group orders for common curves end in the following bit patterns: |
| 704 |
// |
| 705 |
// P-521: ...00001001; w = 4, 5, 6, 7 are okay |
| 706 |
// P-384: ...01110011; w = 2, 3, 7 are okay |
| 707 |
// P-256: ...01010001; w = 2, 3, 5, 7 are okay |
| 708 |
// |
| 709 |
// |
| 710 |
// CAN DOUBLING OCCUR IN RIGHT-TO-LEFT ALGORITHMS OR COMB ALGORITHMS? |
| 711 |
// |
| 712 |
// This question was answered empirically for P-384 group order n, w = 5, |
| 713 |
// by asking: |
| 714 |
// Is there a value d_76, such that |
| 715 |
// - d_{76} * 2^{380} - a_{76} = n and |
| 716 |
// - d_{76} * 2^{380} + a_{76} = k < n ? |
| 717 |
// |
| 718 |
// Setting |
| 719 |
// d_76 = 0xf |
| 720 |
// a_76 = (d_76 << 380) - n = |
| 721 |
// -0xfffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973 |
| 722 |
// k = a_76 + (d_76 << 380) = |
| 723 |
// 0xe00000000000000000000000000000000000000000000000389cb27e0bc8d220a7e5f24db74f58851313e695333ad68d |
| 724 |
// |
| 725 |
// n-k = |
| 726 |
// 0x1fffffffffffffffffffffffffffffffffffffffffffffff8ec69b03e86e5bbeb0341b6491614ef5d9d832d5998a52e6 |
| 727 |
// => 0 < k < n |
| 728 |
// |
| 729 |
// -(1<<380)-a_76 = -0x389cb27e0bc8d220a7e5f24db74f58851313e695333ad68d |
| 730 |
// => -2^380 < a_76 < 2^380 |
| 731 |
// |
| 732 |
// This shows that such a k value exists. |
| 733 |
// |
| 734 |
// This resulted in modifying the comb algorithm used in |
| 735 |
// ec_GFp_nistp384_point_mul_base() to proceed in a left-to-right fashion in |
| 736 |
// order to add the least significant digit in the last iteration. |
| 737 |
// |
| 738 |
// We can probably construct values of k that would incur doubling for whenever |
| 739 |
// any of the higher digits, t_{j-1}, (down to the middle digit, roughly) is |
| 740 |
// added last. This is because the upper half of the group order of P-384 is all |
| 741 |
// 1s, therefore we can find a value k < n, having a 0 at the (j*w)th bit which |
| 742 |
// would become 1 in the recoding of t_j (being the least significant bit in |
| 743 |
// t_j) and making t_{j-1} a negative digit. Hence, the difference between the |
| 744 |
// accumulator value containing all digits and t_{j-1} * 2^{(j-1)*w} can be n. |
| 745 |
// |
| 746 |
// This was tested as follows in Python for j = 75, i.e. the second last digit: |
| 747 |
// let that digit's value be the smallest possible value for w = 5, i.e. -31 |
| 748 |
// d_75 = -31 |
| 749 |
// # assuming the accumulator contains the most significant digit d_76 |
| 750 |
// a = n + (d_75 << 375); hex(a) |
| 751 |
// '0xf07fffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973L' |
| 752 |
// hex(n) |
| 753 |
// '0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973L' |
| 754 |
// k = a + (d_75 << 375); hex(k) |
| 755 |
// '0xe0ffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973L' |
| 756 |
// |
| 757 |
// # Checks |
| 758 |
// hex(n-k) |
| 759 |
// '0x1f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L' |
| 760 |
// hex(n - (a - (d_75 << 375))) |
| 761 |
// '0x0L' |
| 762 |
// => n > k |
| 763 |
// and a value k was found such that when adding d_75 last, the difference |
| 764 |
// between the accumulator a and (d_75 << 375) is n |
| 765 |
// |
| 766 |
// |
| 767 |
// ---------------------------------------------------------------------------- |
| 768 |
// Python code showing the doubling case occurrence in the Joye-Tunstall |
| 769 |
// recoding: |
| 770 |
// ---------------------------------------------------------------------------- |
| 771 |
// |
| 772 |
// from array import * |
| 773 |
// |
| 774 |
// # P-384 group order |
| 775 |
// n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973 |
| 776 |
// |
| 777 |
// # k value that causes a doubling case in left-to-right reconstruction |
| 778 |
// k = 0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc5294d |
| 779 |
// # k value that causes a doubling case in right-to-left reconstruction |
| 780 |
// k_r2l = 0xe00000000000000000000000000000000000000000000000389cb27e0bc8d220a7e5f24db74f58851313e695333ad68d |
| 781 |
// |
| 782 |
// |
| 783 |
// def recode(k, w): |
| 784 |
// rec = array('i', []) |
| 785 |
// while k > (2 ** w): |
| 786 |
// window = k & ((2 ** (w + 1)) - 1) |
| 787 |
// d = window - (2 ** w) |
| 788 |
// k = k - d |
| 789 |
// k = (k >> w) |
| 790 |
// rec.append(d) |
| 791 |
// rec.append(k) |
| 792 |
// return rec |
| 793 |
// |
| 794 |
// # Rebuild k from the recoded scalar proceeding from left to right |
| 795 |
// def rebuild_l2r(rec, w): |
| 796 |
// l = rec.buffer_info()[1] # length of the recoded scalar array |
| 797 |
// # initialise accumulator |
| 798 |
// a = rec[l-1] |
| 799 |
// # for i from l-2 downto 0 |
| 800 |
// for i in range(l-2,-1,-1): |
| 801 |
// a = (a << w) |
| 802 |
// if (a - rec[i]) == n: |
| 803 |
// print("L2R Doubling case: ") |
| 804 |
// print(" i =", i, " digit =", hex(rec[i])) |
| 805 |
// print(" a =", hex(a)) |
| 806 |
// a += rec[i] |
| 807 |
// return a |
| 808 |
// |
| 809 |
// # Rebuild k from the recoded scalar proceeding from right to left |
| 810 |
// def rebuild_r2l(rec, w): |
| 811 |
// l = rec.buffer_info()[1] # length of the recoded scalar array |
| 812 |
// # initialise accumulator |
| 813 |
// a = rec[0] |
| 814 |
// # for i from 1 to l-1 |
| 815 |
// for i in range(1,l): |
| 816 |
// shifted_d = rec[i] << (w*i) |
| 817 |
// if (shifted_d - a) == n: |
| 818 |
// print("R2L Doubling case: ") |
| 819 |
// print(" i =", i, " digit =", hex(rec[i])) |
| 820 |
// print(" a =", hex(a)) |
| 821 |
// a += shifted_d |
| 822 |
// return a |
| 823 |
// |
| 824 |
// def test_recode(): |
| 825 |
// w = 5 |
| 826 |
// |
| 827 |
// # Left-to-right recoding of k which causes a doubling case |
| 828 |
// assert k < n |
| 829 |
// print("k = ", hex(k)) |
| 830 |
// # recode k |
| 831 |
// rec_k = recode(k,w) |
| 832 |
// # print(rec_k) |
| 833 |
// print("Digits of the recoded scalar:") |
| 834 |
// for a in rec_k: |
| 835 |
// print(hex(a), end=', ') |
| 836 |
// print() |
| 837 |
// # rebuild k |
| 838 |
// out_k = rebuild_l2r(rec_k, w) |
| 839 |
// if out_k != k: |
| 840 |
// print("ERROR: rebuilt value is different from recoded value") |
| 841 |
// print() |
| 842 |
// |
| 843 |
// # Right-to-left recoding of k_r2l which causes a doubling case |
| 844 |
// assert k_r2l < n |
| 845 |
// print("k = ", hex(k_r2l)) |
| 846 |
// # recode k_r2l |
| 847 |
// rec_k_r2l = recode(k_r2l,w) |
| 848 |
// # print(rec_k_r2l) |
| 849 |
// print("Digits of the recoded scalar:") |
| 850 |
// for a in rec_k_r2l: |
| 851 |
// print(hex(a), end=', ') |
| 852 |
// print() |
| 853 |
// # rebuild k_r2l |
| 854 |
// out_k_r2l = rebuild_r2l(rec_k_r2l, w) |
| 855 |
// if out_k_r2l != k_r2l: |
| 856 |
// print("ERROR: rebuilt R2L value is different from recoded value") |
| 857 |
// print() |
| 858 |
// |
| 859 |
// test_recode() |
| 860 |
// ''' |
| 861 |
// Output: |
| 862 |
// ------- |
| 863 |
// k = 0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc5294d |
| 864 |
// Digits of the recoded scalar: |
| 865 |
// -0x13, -0x15, -0x15, -0x15, -0x13, 0x7, 0xb, 0xd, -0x7, 0x1, 0x1b, -0x7, 0xf, 0x1d, -0x3, -0xb, 0x11, -0x1b, -0xd, 0x5, -0x5, -0x19, 0x9, -0x1d, -0x7, 0x1b, 0x17, -0x5, 0x13, -0x5, -0xf, 0x1f, -0x1f, 0xd, -0xd, -0x19, 0x17, 0x3, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0xf, |
| 866 |
// L2R Doubling case: |
| 867 |
// i = 0 digit = -0x13 |
| 868 |
// a = 0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52960 |
| 869 |
// |
| 870 |
// k = 0xe00000000000000000000000000000000000000000000000389cb27e0bc8d220a7e5f24db74f58851313e695333ad68d |
| 871 |
// Digits of the recoded scalar: |
| 872 |
// -0x13, 0x15, 0x15, 0x15, 0x13, -0x7, -0xb, -0xd, 0x7, -0x1, -0x1b, 0x7, -0xf, -0x1d, 0x3, 0xb, -0x11, 0x1b, 0xd, -0x5, 0x5, 0x19, -0x9, 0x1d, 0x7, -0x1b, -0x17, 0x5, -0x13, 0x5, 0xf, -0x1f, 0x1f, -0xd, 0xd, 0x19, -0x17, -0x3, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, -0x1f, 0xf, |
| 873 |
// R2L Doubling case: |
| 874 |
// i = 76 digit = 0xf |
| 875 |
// a = -0xfffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973 |
| 876 |
// ''' |
| 877 |
// |
| 878 |
#endif // !defined(OPENSSL_SMALL) |