2020-12-29 17:15:51 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
#ifndef SECP256K1_ECMULT_CONST_IMPL_H
|
|
|
|
#define SECP256K1_ECMULT_CONST_IMPL_H
|
2015-10-26 14:54:21 +00:00
|
|
|
|
|
|
|
#include "scalar.h"
|
|
|
|
#include "group.h"
|
|
|
|
#include "ecmult_const.h"
|
|
|
|
#include "ecmult_impl.h"
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
/** Fill a table 'pre' with precomputed odd multiples of a.
|
|
|
|
*
|
|
|
|
* The resulting point set is brought to a single constant Z denominator, stores the X and Y
|
|
|
|
* coordinates as ge_storage points in pre, and stores the global Z in globalz.
|
|
|
|
* It only operates on tables sized for WINDOW_A wnaf multiples.
|
|
|
|
*/
|
|
|
|
static void rustsecp256k1_v0_5_0_ecmult_odd_multiples_table_globalz_windowa(rustsecp256k1_v0_5_0_ge *pre, rustsecp256k1_v0_5_0_fe *globalz, const rustsecp256k1_v0_5_0_gej *a) {
|
|
|
|
rustsecp256k1_v0_5_0_gej prej[ECMULT_TABLE_SIZE(WINDOW_A)];
|
|
|
|
rustsecp256k1_v0_5_0_fe zr[ECMULT_TABLE_SIZE(WINDOW_A)];
|
|
|
|
|
|
|
|
/* Compute the odd multiples in Jacobian form. */
|
|
|
|
rustsecp256k1_v0_5_0_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), prej, zr, a);
|
|
|
|
/* Bring them to the same Z denominator. */
|
|
|
|
rustsecp256k1_v0_5_0_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, globalz, prej, zr);
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:54:21 +00:00
|
|
|
/* This is like `ECMULT_TABLE_GET_GE` but is constant time */
|
|
|
|
#define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \
|
2020-08-26 17:35:27 +00:00
|
|
|
int m = 0; \
|
|
|
|
/* Extract the sign-bit for a constant time absolute-value. */ \
|
|
|
|
int mask = (n) >> (sizeof(n) * CHAR_BIT - 1); \
|
|
|
|
int abs_n = ((n) + mask) ^ mask; \
|
|
|
|
int idx_n = abs_n >> 1; \
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_fe neg_y; \
|
2015-10-26 14:54:21 +00:00
|
|
|
VERIFY_CHECK(((n) & 1) == 1); \
|
|
|
|
VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
|
|
|
|
VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
|
2022-03-08 19:45:41 +00:00
|
|
|
VERIFY_SETUP(rustsecp256k1_v0_5_0_fe_clear(&(r)->x)); \
|
|
|
|
VERIFY_SETUP(rustsecp256k1_v0_5_0_fe_clear(&(r)->y)); \
|
2020-08-26 17:35:27 +00:00
|
|
|
/* Unconditionally set r->x = (pre)[m].x. r->y = (pre)[m].y. because it's either the correct one \
|
|
|
|
* or will get replaced in the later iterations, this is needed to make sure `r` is initialized. */ \
|
|
|
|
(r)->x = (pre)[m].x; \
|
|
|
|
(r)->y = (pre)[m].y; \
|
|
|
|
for (m = 1; m < ECMULT_TABLE_SIZE(w); m++) { \
|
2015-10-26 14:54:21 +00:00
|
|
|
/* This loop is used to avoid secret data in array indices. See
|
|
|
|
* the comment in ecmult_gen_impl.h for rationale. */ \
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \
|
|
|
|
rustsecp256k1_v0_5_0_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \
|
2015-10-26 14:54:21 +00:00
|
|
|
} \
|
|
|
|
(r)->infinity = 0; \
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_fe_negate(&neg_y, &(r)->y, 1); \
|
|
|
|
rustsecp256k1_v0_5_0_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
|
2015-10-26 14:54:21 +00:00
|
|
|
} while(0)
|
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
/** Convert a number to WNAF notation.
|
|
|
|
* The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
|
|
|
|
* It has the following guarantees:
|
2015-10-26 14:54:21 +00:00
|
|
|
* - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
|
|
|
|
* - each wnaf[i] is nonzero
|
2018-07-09 11:17:44 +00:00
|
|
|
* - the number of words set is always WNAF_SIZE(w) + 1
|
2015-10-26 14:54:21 +00:00
|
|
|
*
|
|
|
|
* Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar
|
|
|
|
* Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.)
|
2020-08-26 17:35:27 +00:00
|
|
|
* CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlag Berlin Heidelberg 2003
|
2015-10-26 14:54:21 +00:00
|
|
|
*
|
|
|
|
* Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335
|
|
|
|
*/
|
2022-03-08 19:45:41 +00:00
|
|
|
static int rustsecp256k1_v0_5_0_wnaf_const(int *wnaf, const rustsecp256k1_v0_5_0_scalar *scalar, int w, int size) {
|
2015-10-26 14:54:21 +00:00
|
|
|
int global_sign;
|
2022-03-08 19:45:41 +00:00
|
|
|
int skew;
|
2015-10-26 14:54:21 +00:00
|
|
|
int word = 0;
|
2018-07-09 11:17:44 +00:00
|
|
|
|
2015-10-26 14:54:21 +00:00
|
|
|
/* 1 2 3 */
|
|
|
|
int u_last;
|
|
|
|
int u;
|
|
|
|
|
|
|
|
int flip;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar s = *scalar;
|
2019-05-28 12:23:28 +00:00
|
|
|
|
|
|
|
VERIFY_CHECK(w > 0);
|
|
|
|
VERIFY_CHECK(size > 0);
|
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
/* Note that we cannot handle even numbers by negating them to be odd, as is
|
|
|
|
* done in other implementations, since if our scalars were specified to have
|
|
|
|
* width < 256 for performance reasons, their negations would have width 256
|
2022-03-08 19:45:41 +00:00
|
|
|
* and we'd lose any performance benefit. Instead, we use a variation of a
|
|
|
|
* technique from Section 4.2 of the Okeya/Tagaki paper, which is to add 1 to the
|
|
|
|
* number we are encoding when it is even, returning a skew value indicating
|
2018-07-09 11:17:44 +00:00
|
|
|
* this, and having the caller compensate after doing the multiplication.
|
|
|
|
*
|
|
|
|
* In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
|
|
|
|
* particular, to ensure that the outputs from the endomorphism-split fit into
|
2022-03-08 19:45:41 +00:00
|
|
|
* 128 bits). If we negate, the parity of our number flips, affecting whether
|
|
|
|
* we want to add to the scalar to ensure that it's odd. */
|
|
|
|
flip = rustsecp256k1_v0_5_0_scalar_is_high(&s);
|
|
|
|
skew = flip ^ rustsecp256k1_v0_5_0_scalar_is_even(&s);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cadd_bit(&s, 0, skew);
|
|
|
|
global_sign = rustsecp256k1_v0_5_0_scalar_cond_negate(&s, flip);
|
2015-10-26 14:54:21 +00:00
|
|
|
|
|
|
|
/* 4 */
|
2022-03-08 19:45:41 +00:00
|
|
|
u_last = rustsecp256k1_v0_5_0_scalar_shr_int(&s, w);
|
2019-05-28 12:23:28 +00:00
|
|
|
do {
|
2015-10-26 14:54:21 +00:00
|
|
|
int even;
|
|
|
|
|
|
|
|
/* 4.1 4.4 */
|
2022-03-08 19:45:41 +00:00
|
|
|
u = rustsecp256k1_v0_5_0_scalar_shr_int(&s, w);
|
2015-10-26 14:54:21 +00:00
|
|
|
/* 4.2 */
|
|
|
|
even = ((u & 1) == 0);
|
2020-08-26 17:35:27 +00:00
|
|
|
/* In contrast to the original algorithm, u_last is always > 0 and
|
|
|
|
* therefore we do not need to check its sign. In particular, it's easy
|
|
|
|
* to see that u_last is never < 0 because u is never < 0. Moreover,
|
|
|
|
* u_last is never = 0 because u is never even after a loop
|
|
|
|
* iteration. The same holds analogously for the initial value of
|
|
|
|
* u_last (in the first loop iteration). */
|
|
|
|
VERIFY_CHECK(u_last > 0);
|
|
|
|
VERIFY_CHECK((u_last & 1) == 1);
|
|
|
|
u += even;
|
|
|
|
u_last -= even * (1 << w);
|
2015-10-26 14:54:21 +00:00
|
|
|
|
|
|
|
/* 4.3, adapted for global sign change */
|
|
|
|
wnaf[word++] = u_last * global_sign;
|
|
|
|
|
|
|
|
u_last = u;
|
2019-05-28 12:23:28 +00:00
|
|
|
} while (word * w < size);
|
2015-10-26 14:54:21 +00:00
|
|
|
wnaf[word] = u * global_sign;
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
VERIFY_CHECK(rustsecp256k1_v0_5_0_scalar_is_zero(&s));
|
2018-07-09 11:17:44 +00:00
|
|
|
VERIFY_CHECK(word == WNAF_SIZE_BITS(size, w));
|
2015-10-26 14:54:21 +00:00
|
|
|
return skew;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static void rustsecp256k1_v0_5_0_ecmult_const(rustsecp256k1_v0_5_0_gej *r, const rustsecp256k1_v0_5_0_ge *a, const rustsecp256k1_v0_5_0_scalar *scalar, int size) {
|
|
|
|
rustsecp256k1_v0_5_0_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
|
|
|
|
rustsecp256k1_v0_5_0_ge tmpa;
|
|
|
|
rustsecp256k1_v0_5_0_fe Z;
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
int skew_1;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
|
2015-10-26 14:54:21 +00:00
|
|
|
int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)];
|
|
|
|
int skew_lam;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar q_1, q_lam;
|
2018-07-09 11:17:44 +00:00
|
|
|
int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
|
2015-10-26 14:54:21 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* build wnaf representation for q. */
|
2018-07-09 11:17:44 +00:00
|
|
|
int rsize = size;
|
|
|
|
if (size > 128) {
|
|
|
|
rsize = 128;
|
|
|
|
/* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_split_lambda(&q_1, &q_lam, scalar);
|
|
|
|
skew_1 = rustsecp256k1_v0_5_0_wnaf_const(wnaf_1, &q_1, WINDOW_A - 1, 128);
|
|
|
|
skew_lam = rustsecp256k1_v0_5_0_wnaf_const(wnaf_lam, &q_lam, WINDOW_A - 1, 128);
|
2018-07-09 11:17:44 +00:00
|
|
|
} else
|
|
|
|
{
|
2022-03-08 19:45:41 +00:00
|
|
|
skew_1 = rustsecp256k1_v0_5_0_wnaf_const(wnaf_1, scalar, WINDOW_A - 1, size);
|
2018-07-09 11:17:44 +00:00
|
|
|
skew_lam = 0;
|
|
|
|
}
|
2015-10-26 14:54:21 +00:00
|
|
|
|
|
|
|
/* Calculate odd multiples of a.
|
|
|
|
* All multiples are brought to the same Z 'denominator', which is stored
|
|
|
|
* in Z. Due to secp256k1' isomorphism we can do all operations pretending
|
|
|
|
* that the Z coordinate was 1, use affine addition formulae, and correct
|
|
|
|
* the Z coordinate of the result once at the end.
|
|
|
|
*/
|
2022-03-08 19:45:41 +00:00
|
|
|
VERIFY_CHECK(!a->infinity);
|
|
|
|
rustsecp256k1_v0_5_0_gej_set_ge(r, a);
|
|
|
|
rustsecp256k1_v0_5_0_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r);
|
2015-10-26 14:54:21 +00:00
|
|
|
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_fe_normalize_weak(&pre_a[i].y);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
2018-07-09 11:17:44 +00:00
|
|
|
if (size > 128) {
|
|
|
|
for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* first loop iteration (separated out so we can directly set r, rather
|
|
|
|
* than having it start at infinity, get doubled several times, then have
|
|
|
|
* its new value added to it) */
|
2018-07-09 11:17:44 +00:00
|
|
|
i = wnaf_1[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
|
2015-10-26 14:54:21 +00:00
|
|
|
VERIFY_CHECK(i != 0);
|
|
|
|
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej_set_ge(r, &tmpa);
|
2018-07-09 11:17:44 +00:00
|
|
|
if (size > 128) {
|
|
|
|
i = wnaf_lam[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
|
|
|
|
VERIFY_CHECK(i != 0);
|
|
|
|
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej_add_ge(r, r, &tmpa);
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
2015-10-26 14:54:21 +00:00
|
|
|
/* remaining loop iterations */
|
2018-07-09 11:17:44 +00:00
|
|
|
for (i = WNAF_SIZE_BITS(rsize, WINDOW_A - 1) - 1; i >= 0; i--) {
|
2015-10-26 14:54:21 +00:00
|
|
|
int n;
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < WINDOW_A - 1; ++j) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej_double(r, r);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
2018-07-09 11:17:44 +00:00
|
|
|
|
2015-10-26 14:54:21 +00:00
|
|
|
n = wnaf_1[i];
|
|
|
|
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
|
|
|
|
VERIFY_CHECK(n != 0);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej_add_ge(r, r, &tmpa);
|
2018-07-09 11:17:44 +00:00
|
|
|
if (size > 128) {
|
|
|
|
n = wnaf_lam[i];
|
|
|
|
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
|
|
|
|
VERIFY_CHECK(n != 0);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej_add_ge(r, r, &tmpa);
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Correct for wNAF skew */
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej tmpj;
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge_neg(&tmpa, &pre_a[0]);
|
|
|
|
rustsecp256k1_v0_5_0_gej_add_ge(&tmpj, r, &tmpa);
|
|
|
|
rustsecp256k1_v0_5_0_gej_cmov(r, &tmpj, skew_1);
|
2015-10-26 14:54:21 +00:00
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
if (size > 128) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge_neg(&tmpa, &pre_a_lam[0]);
|
|
|
|
rustsecp256k1_v0_5_0_gej_add_ge(&tmpj, r, &tmpa);
|
|
|
|
rustsecp256k1_v0_5_0_gej_cmov(r, &tmpj, skew_lam);
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
|
|
|
|
rustsecp256k1_v0_5_0_fe_mul(&r->z, &r->z, &Z);
|
2015-10-26 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 11:17:44 +00:00
|
|
|
#endif /* SECP256K1_ECMULT_CONST_IMPL_H */
|