github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/crypto/secp256k1/libsecp256k1/src/ecmult_const_impl.h (about)

     1  /**********************************************************************
     2   * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra                  *
     3   * Distributed under the MIT software license, see the accompanying   *
     4   * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
     5   **********************************************************************/
     6  
     7  #ifndef _SECP256K1_ECMULT_CONST_IMPL_
     8  #define _SECP256K1_ECMULT_CONST_IMPL_
     9  
    10  #include "scalar.h"
    11  #include "group.h"
    12  #include "ecmult_const.h"
    13  #include "ecmult_impl.h"
    14  
    15  #ifdef USE_ENDOMORPHISM
    16      #define WNAF_BITS 128
    17  #else
    18      #define WNAF_BITS 256
    19  #endif
    20  #define WNAF_SIZE(w) ((WNAF_BITS + (w) - 1) / (w))
    21  
    22  /* This is like `ECMULT_TABLE_GET_GE` but is constant time */
    23  #define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \
    24      int m; \
    25      int abs_n = (n) * (((n) > 0) * 2 - 1); \
    26      int idx_n = abs_n / 2; \
    27      secp256k1_fe neg_y; \
    28      VERIFY_CHECK(((n) & 1) == 1); \
    29      VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
    30      VERIFY_CHECK((n) <=  ((1 << ((w)-1)) - 1)); \
    31      VERIFY_SETUP(secp256k1_fe_clear(&(r)->x)); \
    32      VERIFY_SETUP(secp256k1_fe_clear(&(r)->y)); \
    33      for (m = 0; m < ECMULT_TABLE_SIZE(w); m++) { \
    34          /* This loop is used to avoid secret data in array indices. See
    35           * the comment in ecmult_gen_impl.h for rationale. */ \
    36          secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \
    37          secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \
    38      } \
    39      (r)->infinity = 0; \
    40      secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
    41      secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
    42  } while(0)
    43  
    44  
    45  /** Convert a number to WNAF notation. The number becomes represented by sum(2^{wi} * wnaf[i], i=0..return_val)
    46   *  with the following guarantees:
    47   *  - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
    48   *  - each wnaf[i] is nonzero
    49   *  - the number of words set is returned; this is always (WNAF_BITS + w - 1) / w
    50   *
    51   *  Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar
    52   *  Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.)
    53   *  CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlagy Berlin Heidelberg 2003
    54   *
    55   *  Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335
    56   */
    57  static int secp256k1_wnaf_const(int *wnaf, secp256k1_scalar s, int w) {
    58      int global_sign;
    59      int skew = 0;
    60      int word = 0;
    61      /* 1 2 3 */
    62      int u_last;
    63      int u;
    64  
    65  #ifdef USE_ENDOMORPHISM
    66      int flip;
    67      int bit;
    68      secp256k1_scalar neg_s;
    69      int not_neg_one;
    70      /* If we are using the endomorphism, we cannot handle even numbers by negating
    71       * them, since we are working with 128-bit numbers whose negations would be 256
    72       * bits, eliminating the performance advantage. Instead we use a technique from
    73       * Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even)
    74       * or 2 (for odd) to the number we are encoding, then compensating after the
    75       * multiplication. */
    76      /* Negative 128-bit numbers will be negated, since otherwise they are 256-bit */
    77      flip = secp256k1_scalar_is_high(&s);
    78      /* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */
    79      bit = flip ^ (s.d[0] & 1);
    80      /* We check for negative one, since adding 2 to it will cause an overflow */
    81      secp256k1_scalar_negate(&neg_s, &s);
    82      not_neg_one = !secp256k1_scalar_is_one(&neg_s);
    83      secp256k1_scalar_cadd_bit(&s, bit, not_neg_one);
    84      /* If we had negative one, flip == 1, s.d[0] == 0, bit == 1, so caller expects
    85       * that we added two to it and flipped it. In fact for -1 these operations are
    86       * identical. We only flipped, but since skewing is required (in the sense that
    87       * the skew must be 1 or 2, never zero) and flipping is not, we need to change
    88       * our flags to claim that we only skewed. */
    89      global_sign = secp256k1_scalar_cond_negate(&s, flip);
    90      global_sign *= not_neg_one * 2 - 1;
    91      skew = 1 << bit;
    92  #else
    93      /* Otherwise, we just negate to force oddness */
    94      int is_even = secp256k1_scalar_is_even(&s);
    95      global_sign = secp256k1_scalar_cond_negate(&s, is_even);
    96  #endif
    97  
    98      /* 4 */
    99      u_last = secp256k1_scalar_shr_int(&s, w);
   100      while (word * w < WNAF_BITS) {
   101          int sign;
   102          int even;
   103  
   104          /* 4.1 4.4 */
   105          u = secp256k1_scalar_shr_int(&s, w);
   106          /* 4.2 */
   107          even = ((u & 1) == 0);
   108          sign = 2 * (u_last > 0) - 1;
   109          u += sign * even;
   110          u_last -= sign * even * (1 << w);
   111  
   112          /* 4.3, adapted for global sign change */
   113          wnaf[word++] = u_last * global_sign;
   114  
   115          u_last = u;
   116      }
   117      wnaf[word] = u * global_sign;
   118  
   119      VERIFY_CHECK(secp256k1_scalar_is_zero(&s));
   120      VERIFY_CHECK(word == WNAF_SIZE(w));
   121      return skew;
   122  }
   123  
   124  
   125  static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar) {
   126      secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
   127      secp256k1_ge tmpa;
   128      secp256k1_fe Z;
   129  
   130  #ifdef USE_ENDOMORPHISM
   131      secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
   132      int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
   133      int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)];
   134      int skew_1;
   135      int skew_lam;
   136      secp256k1_scalar q_1, q_lam;
   137  #else
   138      int wnaf[1 + WNAF_SIZE(WINDOW_A - 1)];
   139  #endif
   140  
   141      int i;
   142      secp256k1_scalar sc = *scalar;
   143  
   144      /* build wnaf representation for q. */
   145  #ifdef USE_ENDOMORPHISM
   146      /* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */
   147      secp256k1_scalar_split_lambda(&q_1, &q_lam, &sc);
   148      /* no need for zero correction when using endomorphism since even
   149       * numbers have one added to them anyway */
   150      skew_1   = secp256k1_wnaf_const(wnaf_1,   q_1,   WINDOW_A - 1);
   151      skew_lam = secp256k1_wnaf_const(wnaf_lam, q_lam, WINDOW_A - 1);
   152  #else
   153      int is_zero = secp256k1_scalar_is_zero(scalar);
   154      /* the wNAF ladder cannot handle zero, so bump this to one .. we will
   155       * correct the result after the fact */
   156      sc.d[0] += is_zero;
   157      VERIFY_CHECK(!secp256k1_scalar_is_zero(&sc));
   158  
   159      secp256k1_wnaf_const(wnaf, sc, WINDOW_A - 1);
   160  #endif
   161  
   162      /* Calculate odd multiples of a.
   163       * All multiples are brought to the same Z 'denominator', which is stored
   164       * in Z. Due to secp256k1' isomorphism we can do all operations pretending
   165       * that the Z coordinate was 1, use affine addition formulae, and correct
   166       * the Z coordinate of the result once at the end.
   167       */
   168      secp256k1_gej_set_ge(r, a);
   169      secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r);
   170      for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
   171          secp256k1_fe_normalize_weak(&pre_a[i].y);
   172      }
   173  #ifdef USE_ENDOMORPHISM
   174      for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
   175          secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
   176      }
   177  #endif
   178  
   179      /* first loop iteration (separated out so we can directly set r, rather
   180       * than having it start at infinity, get doubled several times, then have
   181       * its new value added to it) */
   182  #ifdef USE_ENDOMORPHISM
   183      i = wnaf_1[WNAF_SIZE(WINDOW_A - 1)];
   184      VERIFY_CHECK(i != 0);
   185      ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
   186      secp256k1_gej_set_ge(r, &tmpa);
   187  
   188      i = wnaf_lam[WNAF_SIZE(WINDOW_A - 1)];
   189      VERIFY_CHECK(i != 0);
   190      ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A);
   191      secp256k1_gej_add_ge(r, r, &tmpa);
   192  #else
   193      i = wnaf[WNAF_SIZE(WINDOW_A - 1)];
   194      VERIFY_CHECK(i != 0);
   195      ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
   196      secp256k1_gej_set_ge(r, &tmpa);
   197  #endif
   198      /* remaining loop iterations */
   199      for (i = WNAF_SIZE(WINDOW_A - 1) - 1; i >= 0; i--) {
   200          int n;
   201          int j;
   202          for (j = 0; j < WINDOW_A - 1; ++j) {
   203              secp256k1_gej_double_nonzero(r, r, NULL);
   204          }
   205  #ifdef USE_ENDOMORPHISM
   206          n = wnaf_1[i];
   207          ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
   208          VERIFY_CHECK(n != 0);
   209          secp256k1_gej_add_ge(r, r, &tmpa);
   210  
   211          n = wnaf_lam[i];
   212          ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
   213          VERIFY_CHECK(n != 0);
   214          secp256k1_gej_add_ge(r, r, &tmpa);
   215  #else
   216          n = wnaf[i];
   217          VERIFY_CHECK(n != 0);
   218          ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
   219          secp256k1_gej_add_ge(r, r, &tmpa);
   220  #endif
   221      }
   222  
   223      secp256k1_fe_mul(&r->z, &r->z, &Z);
   224  
   225  #ifdef USE_ENDOMORPHISM
   226      {
   227          /* Correct for wNAF skew */
   228          secp256k1_ge correction = *a;
   229          secp256k1_ge_storage correction_1_stor;
   230          secp256k1_ge_storage correction_lam_stor;
   231          secp256k1_ge_storage a2_stor;
   232          secp256k1_gej tmpj;
   233          secp256k1_gej_set_ge(&tmpj, &correction);
   234          secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
   235          secp256k1_ge_set_gej(&correction, &tmpj);
   236          secp256k1_ge_to_storage(&correction_1_stor, a);
   237          secp256k1_ge_to_storage(&correction_lam_stor, a);
   238          secp256k1_ge_to_storage(&a2_stor, &correction);
   239  
   240          /* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
   241          secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
   242          secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
   243  
   244          /* Apply the correction */
   245          secp256k1_ge_from_storage(&correction, &correction_1_stor);
   246          secp256k1_ge_neg(&correction, &correction);
   247          secp256k1_gej_add_ge(r, r, &correction);
   248  
   249          secp256k1_ge_from_storage(&correction, &correction_lam_stor);
   250          secp256k1_ge_neg(&correction, &correction);
   251          secp256k1_ge_mul_lambda(&correction, &correction);
   252          secp256k1_gej_add_ge(r, r, &correction);
   253      }
   254  #else
   255      /* correct for zero */
   256      r->infinity |= is_zero;
   257  #endif
   258  }
   259  
   260  #endif