github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/crypto/secp256k1/libsecp256k1/src/scalar_impl.h (about)

     1  /**********************************************************************
     2   * Copyright (c) 2014 Pieter Wuille                                   *
     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_SCALAR_IMPL_H_
     8  #define _SECP256K1_SCALAR_IMPL_H_
     9  
    10  #include "group.h"
    11  #include "scalar.h"
    12  
    13  #if defined HAVE_CONFIG_H
    14  #include "libsecp256k1-config.h"
    15  #endif
    16  
    17  #if defined(EXHAUSTIVE_TEST_ORDER)
    18  #include "scalar_low_impl.h"
    19  #elif defined(USE_SCALAR_4X64)
    20  #include "scalar_4x64_impl.h"
    21  #elif defined(USE_SCALAR_8X32)
    22  #include "scalar_8x32_impl.h"
    23  #else
    24  #error "Please select scalar implementation"
    25  #endif
    26  
    27  #ifndef USE_NUM_NONE
    28  static void secp256k1_scalar_get_num(secp256k1_num *r, const secp256k1_scalar *a) {
    29      unsigned char c[32];
    30      secp256k1_scalar_get_b32(c, a);
    31      secp256k1_num_set_bin(r, c, 32);
    32  }
    33  
    34  /** secp256k1 curve order, see secp256k1_ecdsa_const_order_as_fe in ecdsa_impl.h */
    35  static void secp256k1_scalar_order_get_num(secp256k1_num *r) {
    36  #if defined(EXHAUSTIVE_TEST_ORDER)
    37      static const unsigned char order[32] = {
    38          0,0,0,0,0,0,0,0,
    39          0,0,0,0,0,0,0,0,
    40          0,0,0,0,0,0,0,0,
    41          0,0,0,0,0,0,0,EXHAUSTIVE_TEST_ORDER
    42      };
    43  #else
    44      static const unsigned char order[32] = {
    45          0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    46          0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
    47          0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
    48          0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
    49      };
    50  #endif
    51      secp256k1_num_set_bin(r, order, 32);
    52  }
    53  #endif
    54  
    55  static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x) {
    56  #if defined(EXHAUSTIVE_TEST_ORDER)
    57      int i;
    58      *r = 0;
    59      for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++)
    60          if ((i * *x) % EXHAUSTIVE_TEST_ORDER == 1)
    61              *r = i;
    62      /* If this VERIFY_CHECK triggers we were given a noninvertible scalar (and thus
    63       * have a composite group order; fix it in exhaustive_tests.c). */
    64      VERIFY_CHECK(*r != 0);
    65  }
    66  #else
    67      secp256k1_scalar *t;
    68      int i;
    69      /* First compute x ^ (2^N - 1) for some values of N. */
    70      secp256k1_scalar x2, x3, x4, x6, x7, x8, x15, x30, x60, x120, x127;
    71  
    72      secp256k1_scalar_sqr(&x2,  x);
    73      secp256k1_scalar_mul(&x2, &x2,  x);
    74  
    75      secp256k1_scalar_sqr(&x3, &x2);
    76      secp256k1_scalar_mul(&x3, &x3,  x);
    77  
    78      secp256k1_scalar_sqr(&x4, &x3);
    79      secp256k1_scalar_mul(&x4, &x4,  x);
    80  
    81      secp256k1_scalar_sqr(&x6, &x4);
    82      secp256k1_scalar_sqr(&x6, &x6);
    83      secp256k1_scalar_mul(&x6, &x6, &x2);
    84  
    85      secp256k1_scalar_sqr(&x7, &x6);
    86      secp256k1_scalar_mul(&x7, &x7,  x);
    87  
    88      secp256k1_scalar_sqr(&x8, &x7);
    89      secp256k1_scalar_mul(&x8, &x8,  x);
    90  
    91      secp256k1_scalar_sqr(&x15, &x8);
    92      for (i = 0; i < 6; i++) {
    93          secp256k1_scalar_sqr(&x15, &x15);
    94      }
    95      secp256k1_scalar_mul(&x15, &x15, &x7);
    96  
    97      secp256k1_scalar_sqr(&x30, &x15);
    98      for (i = 0; i < 14; i++) {
    99          secp256k1_scalar_sqr(&x30, &x30);
   100      }
   101      secp256k1_scalar_mul(&x30, &x30, &x15);
   102  
   103      secp256k1_scalar_sqr(&x60, &x30);
   104      for (i = 0; i < 29; i++) {
   105          secp256k1_scalar_sqr(&x60, &x60);
   106      }
   107      secp256k1_scalar_mul(&x60, &x60, &x30);
   108  
   109      secp256k1_scalar_sqr(&x120, &x60);
   110      for (i = 0; i < 59; i++) {
   111          secp256k1_scalar_sqr(&x120, &x120);
   112      }
   113      secp256k1_scalar_mul(&x120, &x120, &x60);
   114  
   115      secp256k1_scalar_sqr(&x127, &x120);
   116      for (i = 0; i < 6; i++) {
   117          secp256k1_scalar_sqr(&x127, &x127);
   118      }
   119      secp256k1_scalar_mul(&x127, &x127, &x7);
   120  
   121      /* Then accumulate the final result (t starts at x127). */
   122      t = &x127;
   123      for (i = 0; i < 2; i++) { /* 0 */
   124          secp256k1_scalar_sqr(t, t);
   125      }
   126      secp256k1_scalar_mul(t, t, x); /* 1 */
   127      for (i = 0; i < 4; i++) { /* 0 */
   128          secp256k1_scalar_sqr(t, t);
   129      }
   130      secp256k1_scalar_mul(t, t, &x3); /* 111 */
   131      for (i = 0; i < 2; i++) { /* 0 */
   132          secp256k1_scalar_sqr(t, t);
   133      }
   134      secp256k1_scalar_mul(t, t, x); /* 1 */
   135      for (i = 0; i < 2; i++) { /* 0 */
   136          secp256k1_scalar_sqr(t, t);
   137      }
   138      secp256k1_scalar_mul(t, t, x); /* 1 */
   139      for (i = 0; i < 2; i++) { /* 0 */
   140          secp256k1_scalar_sqr(t, t);
   141      }
   142      secp256k1_scalar_mul(t, t, x); /* 1 */
   143      for (i = 0; i < 4; i++) { /* 0 */
   144          secp256k1_scalar_sqr(t, t);
   145      }
   146      secp256k1_scalar_mul(t, t, &x3); /* 111 */
   147      for (i = 0; i < 3; i++) { /* 0 */
   148          secp256k1_scalar_sqr(t, t);
   149      }
   150      secp256k1_scalar_mul(t, t, &x2); /* 11 */
   151      for (i = 0; i < 4; i++) { /* 0 */
   152          secp256k1_scalar_sqr(t, t);
   153      }
   154      secp256k1_scalar_mul(t, t, &x3); /* 111 */
   155      for (i = 0; i < 5; i++) { /* 00 */
   156          secp256k1_scalar_sqr(t, t);
   157      }
   158      secp256k1_scalar_mul(t, t, &x3); /* 111 */
   159      for (i = 0; i < 4; i++) { /* 00 */
   160          secp256k1_scalar_sqr(t, t);
   161      }
   162      secp256k1_scalar_mul(t, t, &x2); /* 11 */
   163      for (i = 0; i < 2; i++) { /* 0 */
   164          secp256k1_scalar_sqr(t, t);
   165      }
   166      secp256k1_scalar_mul(t, t, x); /* 1 */
   167      for (i = 0; i < 2; i++) { /* 0 */
   168          secp256k1_scalar_sqr(t, t);
   169      }
   170      secp256k1_scalar_mul(t, t, x); /* 1 */
   171      for (i = 0; i < 5; i++) { /* 0 */
   172          secp256k1_scalar_sqr(t, t);
   173      }
   174      secp256k1_scalar_mul(t, t, &x4); /* 1111 */
   175      for (i = 0; i < 2; i++) { /* 0 */
   176          secp256k1_scalar_sqr(t, t);
   177      }
   178      secp256k1_scalar_mul(t, t, x); /* 1 */
   179      for (i = 0; i < 3; i++) { /* 00 */
   180          secp256k1_scalar_sqr(t, t);
   181      }
   182      secp256k1_scalar_mul(t, t, x); /* 1 */
   183      for (i = 0; i < 4; i++) { /* 000 */
   184          secp256k1_scalar_sqr(t, t);
   185      }
   186      secp256k1_scalar_mul(t, t, x); /* 1 */
   187      for (i = 0; i < 2; i++) { /* 0 */
   188          secp256k1_scalar_sqr(t, t);
   189      }
   190      secp256k1_scalar_mul(t, t, x); /* 1 */
   191      for (i = 0; i < 10; i++) { /* 0000000 */
   192          secp256k1_scalar_sqr(t, t);
   193      }
   194      secp256k1_scalar_mul(t, t, &x3); /* 111 */
   195      for (i = 0; i < 4; i++) { /* 0 */
   196          secp256k1_scalar_sqr(t, t);
   197      }
   198      secp256k1_scalar_mul(t, t, &x3); /* 111 */
   199      for (i = 0; i < 9; i++) { /* 0 */
   200          secp256k1_scalar_sqr(t, t);
   201      }
   202      secp256k1_scalar_mul(t, t, &x8); /* 11111111 */
   203      for (i = 0; i < 2; i++) { /* 0 */
   204          secp256k1_scalar_sqr(t, t);
   205      }
   206      secp256k1_scalar_mul(t, t, x); /* 1 */
   207      for (i = 0; i < 3; i++) { /* 00 */
   208          secp256k1_scalar_sqr(t, t);
   209      }
   210      secp256k1_scalar_mul(t, t, x); /* 1 */
   211      for (i = 0; i < 3; i++) { /* 00 */
   212          secp256k1_scalar_sqr(t, t);
   213      }
   214      secp256k1_scalar_mul(t, t, x); /* 1 */
   215      for (i = 0; i < 5; i++) { /* 0 */
   216          secp256k1_scalar_sqr(t, t);
   217      }
   218      secp256k1_scalar_mul(t, t, &x4); /* 1111 */
   219      for (i = 0; i < 2; i++) { /* 0 */
   220          secp256k1_scalar_sqr(t, t);
   221      }
   222      secp256k1_scalar_mul(t, t, x); /* 1 */
   223      for (i = 0; i < 5; i++) { /* 000 */
   224          secp256k1_scalar_sqr(t, t);
   225      }
   226      secp256k1_scalar_mul(t, t, &x2); /* 11 */
   227      for (i = 0; i < 4; i++) { /* 00 */
   228          secp256k1_scalar_sqr(t, t);
   229      }
   230      secp256k1_scalar_mul(t, t, &x2); /* 11 */
   231      for (i = 0; i < 2; i++) { /* 0 */
   232          secp256k1_scalar_sqr(t, t);
   233      }
   234      secp256k1_scalar_mul(t, t, x); /* 1 */
   235      for (i = 0; i < 8; i++) { /* 000000 */
   236          secp256k1_scalar_sqr(t, t);
   237      }
   238      secp256k1_scalar_mul(t, t, &x2); /* 11 */
   239      for (i = 0; i < 3; i++) { /* 0 */
   240          secp256k1_scalar_sqr(t, t);
   241      }
   242      secp256k1_scalar_mul(t, t, &x2); /* 11 */
   243      for (i = 0; i < 3; i++) { /* 00 */
   244          secp256k1_scalar_sqr(t, t);
   245      }
   246      secp256k1_scalar_mul(t, t, x); /* 1 */
   247      for (i = 0; i < 6; i++) { /* 00000 */
   248          secp256k1_scalar_sqr(t, t);
   249      }
   250      secp256k1_scalar_mul(t, t, x); /* 1 */
   251      for (i = 0; i < 8; i++) { /* 00 */
   252          secp256k1_scalar_sqr(t, t);
   253      }
   254      secp256k1_scalar_mul(r, t, &x6); /* 111111 */
   255  }
   256  
   257  SECP256K1_INLINE static int secp256k1_scalar_is_even(const secp256k1_scalar *a) {
   258      return !(a->d[0] & 1);
   259  }
   260  #endif
   261  
   262  static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x) {
   263  #if defined(USE_SCALAR_INV_BUILTIN)
   264      secp256k1_scalar_inverse(r, x);
   265  #elif defined(USE_SCALAR_INV_NUM)
   266      unsigned char b[32];
   267      secp256k1_num n, m;
   268      secp256k1_scalar t = *x;
   269      secp256k1_scalar_get_b32(b, &t);
   270      secp256k1_num_set_bin(&n, b, 32);
   271      secp256k1_scalar_order_get_num(&m);
   272      secp256k1_num_mod_inverse(&n, &n, &m);
   273      secp256k1_num_get_bin(b, 32, &n);
   274      secp256k1_scalar_set_b32(r, b, NULL);
   275      /* Verify that the inverse was computed correctly, without GMP code. */
   276      secp256k1_scalar_mul(&t, &t, r);
   277      CHECK(secp256k1_scalar_is_one(&t));
   278  #else
   279  #error "Please select scalar inverse implementation"
   280  #endif
   281  }
   282  
   283  #ifdef USE_ENDOMORPHISM
   284  #if defined(EXHAUSTIVE_TEST_ORDER)
   285  /**
   286   * Find k1 and k2 given k, such that k1 + k2 * lambda == k mod n; unlike in the
   287   * full case we don't bother making k1 and k2 be small, we just want them to be
   288   * nontrivial to get full test coverage for the exhaustive tests. We therefore
   289   * (arbitrarily) set k2 = k + 5 and k1 = k - k2 * lambda.
   290   */
   291  static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a) {
   292      *r2 = (*a + 5) % EXHAUSTIVE_TEST_ORDER;
   293      *r1 = (*a + (EXHAUSTIVE_TEST_ORDER - *r2) * EXHAUSTIVE_TEST_LAMBDA) % EXHAUSTIVE_TEST_ORDER;
   294  }
   295  #else
   296  /**
   297   * The Secp256k1 curve has an endomorphism, where lambda * (x, y) = (beta * x, y), where
   298   * lambda is {0x53,0x63,0xad,0x4c,0xc0,0x5c,0x30,0xe0,0xa5,0x26,0x1c,0x02,0x88,0x12,0x64,0x5a,
   299   *            0x12,0x2e,0x22,0xea,0x20,0x81,0x66,0x78,0xdf,0x02,0x96,0x7c,0x1b,0x23,0xbd,0x72}
   300   *
   301   * "Guide to Elliptic Curve Cryptography" (Hankerson, Menezes, Vanstone) gives an algorithm
   302   * (algorithm 3.74) to find k1 and k2 given k, such that k1 + k2 * lambda == k mod n, and k1
   303   * and k2 have a small size.
   304   * It relies on constants a1, b1, a2, b2. These constants for the value of lambda above are:
   305   *
   306   * - a1 =      {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
   307   * - b1 =     -{0xe4,0x43,0x7e,0xd6,0x01,0x0e,0x88,0x28,0x6f,0x54,0x7f,0xa9,0x0a,0xbf,0xe4,0xc3}
   308   * - a2 = {0x01,0x14,0xca,0x50,0xf7,0xa8,0xe2,0xf3,0xf6,0x57,0xc1,0x10,0x8d,0x9d,0x44,0xcf,0xd8}
   309   * - b2 =      {0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,0xe8,0x6c,0x90,0xe4,0x92,0x84,0xeb,0x15}
   310   *
   311   * The algorithm then computes c1 = round(b1 * k / n) and c2 = round(b2 * k / n), and gives
   312   * k1 = k - (c1*a1 + c2*a2) and k2 = -(c1*b1 + c2*b2). Instead, we use modular arithmetic, and
   313   * compute k1 as k - k2 * lambda, avoiding the need for constants a1 and a2.
   314   *
   315   * g1, g2 are precomputed constants used to replace division with a rounded multiplication
   316   * when decomposing the scalar for an endomorphism-based point multiplication.
   317   *
   318   * The possibility of using precomputed estimates is mentioned in "Guide to Elliptic Curve
   319   * Cryptography" (Hankerson, Menezes, Vanstone) in section 3.5.
   320   *
   321   * The derivation is described in the paper "Efficient Software Implementation of Public-Key
   322   * Cryptography on Sensor Networks Using the MSP430X Microcontroller" (Gouvea, Oliveira, Lopez),
   323   * Section 4.3 (here we use a somewhat higher-precision estimate):
   324   * d = a1*b2 - b1*a2
   325   * g1 = round((2^272)*b2/d)
   326   * g2 = round((2^272)*b1/d)
   327   *
   328   * (Note that 'd' is also equal to the curve order here because [a1,b1] and [a2,b2] are found
   329   * as outputs of the Extended Euclidean Algorithm on inputs 'order' and 'lambda').
   330   *
   331   * The function below splits a in r1 and r2, such that r1 + lambda * r2 == a (mod order).
   332   */
   333  
   334  static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a) {
   335      secp256k1_scalar c1, c2;
   336      static const secp256k1_scalar minus_lambda = SECP256K1_SCALAR_CONST(
   337          0xAC9C52B3UL, 0x3FA3CF1FUL, 0x5AD9E3FDUL, 0x77ED9BA4UL,
   338          0xA880B9FCUL, 0x8EC739C2UL, 0xE0CFC810UL, 0xB51283CFUL
   339      );
   340      static const secp256k1_scalar minus_b1 = SECP256K1_SCALAR_CONST(
   341          0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL,
   342          0xE4437ED6UL, 0x010E8828UL, 0x6F547FA9UL, 0x0ABFE4C3UL
   343      );
   344      static const secp256k1_scalar minus_b2 = SECP256K1_SCALAR_CONST(
   345          0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
   346          0x8A280AC5UL, 0x0774346DUL, 0xD765CDA8UL, 0x3DB1562CUL
   347      );
   348      static const secp256k1_scalar g1 = SECP256K1_SCALAR_CONST(
   349          0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00003086UL,
   350          0xD221A7D4UL, 0x6BCDE86CUL, 0x90E49284UL, 0xEB153DABUL
   351      );
   352      static const secp256k1_scalar g2 = SECP256K1_SCALAR_CONST(
   353          0x00000000UL, 0x00000000UL, 0x00000000UL, 0x0000E443UL,
   354          0x7ED6010EUL, 0x88286F54UL, 0x7FA90ABFUL, 0xE4C42212UL
   355      );
   356      VERIFY_CHECK(r1 != a);
   357      VERIFY_CHECK(r2 != a);
   358      /* these _var calls are constant time since the shift amount is constant */
   359      secp256k1_scalar_mul_shift_var(&c1, a, &g1, 272);
   360      secp256k1_scalar_mul_shift_var(&c2, a, &g2, 272);
   361      secp256k1_scalar_mul(&c1, &c1, &minus_b1);
   362      secp256k1_scalar_mul(&c2, &c2, &minus_b2);
   363      secp256k1_scalar_add(r2, &c1, &c2);
   364      secp256k1_scalar_mul(r1, r2, &minus_lambda);
   365      secp256k1_scalar_add(r1, r1, a);
   366  }
   367  #endif
   368  #endif
   369  
   370  #endif