github.com/aquanetwork/aquachain@v1.7.8/crypto/secp256k1/libsecp256k1/src/group_impl.h (about)

     1  /**********************************************************************
     2   * Copyright (c) 2013, 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_GROUP_IMPL_H
     8  #define SECP256K1_GROUP_IMPL_H
     9  
    10  #include "num.h"
    11  #include "field.h"
    12  #include "group.h"
    13  
    14  /* These points can be generated in sage as follows:
    15   *
    16   * 0. Setup a worksheet with the following parameters.
    17   *   b = 4  # whatever CURVE_B will be set to
    18   *   F = FiniteField (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F)
    19   *   C = EllipticCurve ([F (0), F (b)])
    20   *
    21   * 1. Determine all the small orders available to you. (If there are
    22   *    no satisfactory ones, go back and change b.)
    23   *   print C.order().factor(limit=1000)
    24   *
    25   * 2. Choose an order as one of the prime factors listed in the above step.
    26   *    (You can also multiply some to get a composite order, though the
    27   *    tests will crash trying to invert scalars during signing.) We take a
    28   *    random point and scale it to drop its order to the desired value.
    29   *    There is some probability this won't work; just try again.
    30   *   order = 199
    31   *   P = C.random_point()
    32   *   P = (int(P.order()) / int(order)) * P
    33   *   assert(P.order() == order)
    34   *
    35   * 3. Print the values. You'll need to use a vim macro or something to
    36   *    split the hex output into 4-byte chunks.
    37   *   print "%x %x" % P.xy()
    38   */
    39  #if defined(EXHAUSTIVE_TEST_ORDER)
    40  #  if EXHAUSTIVE_TEST_ORDER == 199
    41  const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
    42      0xFA7CC9A7, 0x0737F2DB, 0xA749DD39, 0x2B4FB069,
    43      0x3B017A7D, 0xA808C2F1, 0xFB12940C, 0x9EA66C18,
    44      0x78AC123A, 0x5ED8AEF3, 0x8732BC91, 0x1F3A2868,
    45      0x48DF246C, 0x808DAE72, 0xCFE52572, 0x7F0501ED
    46  );
    47  
    48  const int CURVE_B = 4;
    49  #  elif EXHAUSTIVE_TEST_ORDER == 13
    50  const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
    51      0xedc60018, 0xa51a786b, 0x2ea91f4d, 0x4c9416c0,
    52      0x9de54c3b, 0xa1316554, 0x6cf4345c, 0x7277ef15,
    53      0x54cb1b6b, 0xdc8c1273, 0x087844ea, 0x43f4603e,
    54      0x0eaf9a43, 0xf6effe55, 0x939f806d, 0x37adf8ac
    55  );
    56  const int CURVE_B = 2;
    57  #  else
    58  #    error No known generator for the specified exhaustive test group order.
    59  #  endif
    60  #else
    61  /** Generator for secp256k1, value 'g' defined in
    62   *  "Standards for Efficient Cryptography" (SEC2) 2.7.1.
    63   */
    64  static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
    65      0x79BE667EUL, 0xF9DCBBACUL, 0x55A06295UL, 0xCE870B07UL,
    66      0x029BFCDBUL, 0x2DCE28D9UL, 0x59F2815BUL, 0x16F81798UL,
    67      0x483ADA77UL, 0x26A3C465UL, 0x5DA4FBFCUL, 0x0E1108A8UL,
    68      0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL
    69  );
    70  
    71  const int CURVE_B = 7;
    72  #endif
    73  
    74  static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
    75      secp256k1_fe zi2;
    76      secp256k1_fe zi3;
    77      secp256k1_fe_sqr(&zi2, zi);
    78      secp256k1_fe_mul(&zi3, &zi2, zi);
    79      secp256k1_fe_mul(&r->x, &a->x, &zi2);
    80      secp256k1_fe_mul(&r->y, &a->y, &zi3);
    81      r->infinity = a->infinity;
    82  }
    83  
    84  static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
    85      r->infinity = 0;
    86      r->x = *x;
    87      r->y = *y;
    88  }
    89  
    90  static int secp256k1_ge_is_infinity(const secp256k1_ge *a) {
    91      return a->infinity;
    92  }
    93  
    94  static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
    95      *r = *a;
    96      secp256k1_fe_normalize_weak(&r->y);
    97      secp256k1_fe_negate(&r->y, &r->y, 1);
    98  }
    99  
   100  static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {
   101      secp256k1_fe z2, z3;
   102      r->infinity = a->infinity;
   103      secp256k1_fe_inv(&a->z, &a->z);
   104      secp256k1_fe_sqr(&z2, &a->z);
   105      secp256k1_fe_mul(&z3, &a->z, &z2);
   106      secp256k1_fe_mul(&a->x, &a->x, &z2);
   107      secp256k1_fe_mul(&a->y, &a->y, &z3);
   108      secp256k1_fe_set_int(&a->z, 1);
   109      r->x = a->x;
   110      r->y = a->y;
   111  }
   112  
   113  static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
   114      secp256k1_fe z2, z3;
   115      r->infinity = a->infinity;
   116      if (a->infinity) {
   117          return;
   118      }
   119      secp256k1_fe_inv_var(&a->z, &a->z);
   120      secp256k1_fe_sqr(&z2, &a->z);
   121      secp256k1_fe_mul(&z3, &a->z, &z2);
   122      secp256k1_fe_mul(&a->x, &a->x, &z2);
   123      secp256k1_fe_mul(&a->y, &a->y, &z3);
   124      secp256k1_fe_set_int(&a->z, 1);
   125      r->x = a->x;
   126      r->y = a->y;
   127  }
   128  
   129  static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len, const secp256k1_callback *cb) {
   130      secp256k1_fe *az;
   131      secp256k1_fe *azi;
   132      size_t i;
   133      size_t count = 0;
   134      az = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * len);
   135      for (i = 0; i < len; i++) {
   136          if (!a[i].infinity) {
   137              az[count++] = a[i].z;
   138          }
   139      }
   140  
   141      azi = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * count);
   142      secp256k1_fe_inv_all_var(azi, az, count);
   143      free(az);
   144  
   145      count = 0;
   146      for (i = 0; i < len; i++) {
   147          r[i].infinity = a[i].infinity;
   148          if (!a[i].infinity) {
   149              secp256k1_ge_set_gej_zinv(&r[i], &a[i], &azi[count++]);
   150          }
   151      }
   152      free(azi);
   153  }
   154  
   155  static void secp256k1_ge_set_table_gej_var(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zr, size_t len) {
   156      size_t i = len - 1;
   157      secp256k1_fe zi;
   158  
   159      if (len > 0) {
   160          /* Compute the inverse of the last z coordinate, and use it to compute the last affine output. */
   161          secp256k1_fe_inv(&zi, &a[i].z);
   162          secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
   163  
   164          /* Work out way backwards, using the z-ratios to scale the x/y values. */
   165          while (i > 0) {
   166              secp256k1_fe_mul(&zi, &zi, &zr[i]);
   167              i--;
   168              secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
   169          }
   170      }
   171  }
   172  
   173  static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr) {
   174      size_t i = len - 1;
   175      secp256k1_fe zs;
   176  
   177      if (len > 0) {
   178          /* The z of the final point gives us the "global Z" for the table. */
   179          r[i].x = a[i].x;
   180          r[i].y = a[i].y;
   181          *globalz = a[i].z;
   182          r[i].infinity = 0;
   183          zs = zr[i];
   184  
   185          /* Work our way backwards, using the z-ratios to scale the x/y values. */
   186          while (i > 0) {
   187              if (i != len - 1) {
   188                  secp256k1_fe_mul(&zs, &zs, &zr[i]);
   189              }
   190              i--;
   191              secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
   192          }
   193      }
   194  }
   195  
   196  static void secp256k1_gej_set_infinity(secp256k1_gej *r) {
   197      r->infinity = 1;
   198      secp256k1_fe_clear(&r->x);
   199      secp256k1_fe_clear(&r->y);
   200      secp256k1_fe_clear(&r->z);
   201  }
   202  
   203  static void secp256k1_ge_set_infinity(secp256k1_ge *r) {
   204      r->infinity = 1;
   205      secp256k1_fe_clear(&r->x);
   206      secp256k1_fe_clear(&r->y);
   207  }
   208  
   209  static void secp256k1_gej_clear(secp256k1_gej *r) {
   210      r->infinity = 0;
   211      secp256k1_fe_clear(&r->x);
   212      secp256k1_fe_clear(&r->y);
   213      secp256k1_fe_clear(&r->z);
   214  }
   215  
   216  static void secp256k1_ge_clear(secp256k1_ge *r) {
   217      r->infinity = 0;
   218      secp256k1_fe_clear(&r->x);
   219      secp256k1_fe_clear(&r->y);
   220  }
   221  
   222  static int secp256k1_ge_set_xquad(secp256k1_ge *r, const secp256k1_fe *x) {
   223      secp256k1_fe x2, x3, c;
   224      r->x = *x;
   225      secp256k1_fe_sqr(&x2, x);
   226      secp256k1_fe_mul(&x3, x, &x2);
   227      r->infinity = 0;
   228      secp256k1_fe_set_int(&c, CURVE_B);
   229      secp256k1_fe_add(&c, &x3);
   230      return secp256k1_fe_sqrt(&r->y, &c);
   231  }
   232  
   233  static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
   234      if (!secp256k1_ge_set_xquad(r, x)) {
   235          return 0;
   236      }
   237      secp256k1_fe_normalize_var(&r->y);
   238      if (secp256k1_fe_is_odd(&r->y) != odd) {
   239          secp256k1_fe_negate(&r->y, &r->y, 1);
   240      }
   241      return 1;
   242  
   243  }
   244  
   245  static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a) {
   246     r->infinity = a->infinity;
   247     r->x = a->x;
   248     r->y = a->y;
   249     secp256k1_fe_set_int(&r->z, 1);
   250  }
   251  
   252  static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
   253      secp256k1_fe r, r2;
   254      VERIFY_CHECK(!a->infinity);
   255      secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
   256      r2 = a->x; secp256k1_fe_normalize_weak(&r2);
   257      return secp256k1_fe_equal_var(&r, &r2);
   258  }
   259  
   260  static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
   261      r->infinity = a->infinity;
   262      r->x = a->x;
   263      r->y = a->y;
   264      r->z = a->z;
   265      secp256k1_fe_normalize_weak(&r->y);
   266      secp256k1_fe_negate(&r->y, &r->y, 1);
   267  }
   268  
   269  static int secp256k1_gej_is_infinity(const secp256k1_gej *a) {
   270      return a->infinity;
   271  }
   272  
   273  static int secp256k1_gej_is_valid_var(const secp256k1_gej *a) {
   274      secp256k1_fe y2, x3, z2, z6;
   275      if (a->infinity) {
   276          return 0;
   277      }
   278      /** y^2 = x^3 + 7
   279       *  (Y/Z^3)^2 = (X/Z^2)^3 + 7
   280       *  Y^2 / Z^6 = X^3 / Z^6 + 7
   281       *  Y^2 = X^3 + 7*Z^6
   282       */
   283      secp256k1_fe_sqr(&y2, &a->y);
   284      secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
   285      secp256k1_fe_sqr(&z2, &a->z);
   286      secp256k1_fe_sqr(&z6, &z2); secp256k1_fe_mul(&z6, &z6, &z2);
   287      secp256k1_fe_mul_int(&z6, CURVE_B);
   288      secp256k1_fe_add(&x3, &z6);
   289      secp256k1_fe_normalize_weak(&x3);
   290      return secp256k1_fe_equal_var(&y2, &x3);
   291  }
   292  
   293  static int secp256k1_ge_is_valid_var(const secp256k1_ge *a) {
   294      secp256k1_fe y2, x3, c;
   295      if (a->infinity) {
   296          return 0;
   297      }
   298      /* y^2 = x^3 + 7 */
   299      secp256k1_fe_sqr(&y2, &a->y);
   300      secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
   301      secp256k1_fe_set_int(&c, CURVE_B);
   302      secp256k1_fe_add(&x3, &c);
   303      secp256k1_fe_normalize_weak(&x3);
   304      return secp256k1_fe_equal_var(&y2, &x3);
   305  }
   306  
   307  static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
   308      /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
   309       *
   310       * Note that there is an implementation described at
   311       *     https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
   312       * which trades a multiply for a square, but in practice this is actually slower,
   313       * mainly because it requires more normalizations.
   314       */
   315      secp256k1_fe t1,t2,t3,t4;
   316      /** For secp256k1, 2Q is infinity if and only if Q is infinity. This is because if 2Q = infinity,
   317       *  Q must equal -Q, or that Q.y == -(Q.y), or Q.y is 0. For a point on y^2 = x^3 + 7 to have
   318       *  y=0, x^3 must be -7 mod p. However, -7 has no cube root mod p.
   319       *
   320       *  Having said this, if this function receives a point on a sextic twist, e.g. by
   321       *  a fault attack, it is possible for y to be 0. This happens for y^2 = x^3 + 6,
   322       *  since -6 does have a cube root mod p. For this point, this function will not set
   323       *  the infinity flag even though the point doubles to infinity, and the result
   324       *  point will be gibberish (z = 0 but infinity = 0).
   325       */
   326      r->infinity = a->infinity;
   327      if (r->infinity) {
   328          if (rzr != NULL) {
   329              secp256k1_fe_set_int(rzr, 1);
   330          }
   331          return;
   332      }
   333  
   334      if (rzr != NULL) {
   335          *rzr = a->y;
   336          secp256k1_fe_normalize_weak(rzr);
   337          secp256k1_fe_mul_int(rzr, 2);
   338      }
   339  
   340      secp256k1_fe_mul(&r->z, &a->z, &a->y);
   341      secp256k1_fe_mul_int(&r->z, 2);       /* Z' = 2*Y*Z (2) */
   342      secp256k1_fe_sqr(&t1, &a->x);
   343      secp256k1_fe_mul_int(&t1, 3);         /* T1 = 3*X^2 (3) */
   344      secp256k1_fe_sqr(&t2, &t1);           /* T2 = 9*X^4 (1) */
   345      secp256k1_fe_sqr(&t3, &a->y);
   346      secp256k1_fe_mul_int(&t3, 2);         /* T3 = 2*Y^2 (2) */
   347      secp256k1_fe_sqr(&t4, &t3);
   348      secp256k1_fe_mul_int(&t4, 2);         /* T4 = 8*Y^4 (2) */
   349      secp256k1_fe_mul(&t3, &t3, &a->x);    /* T3 = 2*X*Y^2 (1) */
   350      r->x = t3;
   351      secp256k1_fe_mul_int(&r->x, 4);       /* X' = 8*X*Y^2 (4) */
   352      secp256k1_fe_negate(&r->x, &r->x, 4); /* X' = -8*X*Y^2 (5) */
   353      secp256k1_fe_add(&r->x, &t2);         /* X' = 9*X^4 - 8*X*Y^2 (6) */
   354      secp256k1_fe_negate(&t2, &t2, 1);     /* T2 = -9*X^4 (2) */
   355      secp256k1_fe_mul_int(&t3, 6);         /* T3 = 12*X*Y^2 (6) */
   356      secp256k1_fe_add(&t3, &t2);           /* T3 = 12*X*Y^2 - 9*X^4 (8) */
   357      secp256k1_fe_mul(&r->y, &t1, &t3);    /* Y' = 36*X^3*Y^2 - 27*X^6 (1) */
   358      secp256k1_fe_negate(&t2, &t4, 2);     /* T2 = -8*Y^4 (3) */
   359      secp256k1_fe_add(&r->y, &t2);         /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
   360  }
   361  
   362  static SECP256K1_INLINE void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
   363      VERIFY_CHECK(!secp256k1_gej_is_infinity(a));
   364      secp256k1_gej_double_var(r, a, rzr);
   365  }
   366  
   367  static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr) {
   368      /* Operations: 12 mul, 4 sqr, 2 normalize, 12 mul_int/add/negate */
   369      secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
   370  
   371      if (a->infinity) {
   372          VERIFY_CHECK(rzr == NULL);
   373          *r = *b;
   374          return;
   375      }
   376  
   377      if (b->infinity) {
   378          if (rzr != NULL) {
   379              secp256k1_fe_set_int(rzr, 1);
   380          }
   381          *r = *a;
   382          return;
   383      }
   384  
   385      r->infinity = 0;
   386      secp256k1_fe_sqr(&z22, &b->z);
   387      secp256k1_fe_sqr(&z12, &a->z);
   388      secp256k1_fe_mul(&u1, &a->x, &z22);
   389      secp256k1_fe_mul(&u2, &b->x, &z12);
   390      secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
   391      secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
   392      secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
   393      secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
   394      if (secp256k1_fe_normalizes_to_zero_var(&h)) {
   395          if (secp256k1_fe_normalizes_to_zero_var(&i)) {
   396              secp256k1_gej_double_var(r, a, rzr);
   397          } else {
   398              if (rzr != NULL) {
   399                  secp256k1_fe_set_int(rzr, 0);
   400              }
   401              r->infinity = 1;
   402          }
   403          return;
   404      }
   405      secp256k1_fe_sqr(&i2, &i);
   406      secp256k1_fe_sqr(&h2, &h);
   407      secp256k1_fe_mul(&h3, &h, &h2);
   408      secp256k1_fe_mul(&h, &h, &b->z);
   409      if (rzr != NULL) {
   410          *rzr = h;
   411      }
   412      secp256k1_fe_mul(&r->z, &a->z, &h);
   413      secp256k1_fe_mul(&t, &u1, &h2);
   414      r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
   415      secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
   416      secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
   417      secp256k1_fe_add(&r->y, &h3);
   418  }
   419  
   420  static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr) {
   421      /* 8 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
   422      secp256k1_fe z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
   423      if (a->infinity) {
   424          VERIFY_CHECK(rzr == NULL);
   425          secp256k1_gej_set_ge(r, b);
   426          return;
   427      }
   428      if (b->infinity) {
   429          if (rzr != NULL) {
   430              secp256k1_fe_set_int(rzr, 1);
   431          }
   432          *r = *a;
   433          return;
   434      }
   435      r->infinity = 0;
   436  
   437      secp256k1_fe_sqr(&z12, &a->z);
   438      u1 = a->x; secp256k1_fe_normalize_weak(&u1);
   439      secp256k1_fe_mul(&u2, &b->x, &z12);
   440      s1 = a->y; secp256k1_fe_normalize_weak(&s1);
   441      secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
   442      secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
   443      secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
   444      if (secp256k1_fe_normalizes_to_zero_var(&h)) {
   445          if (secp256k1_fe_normalizes_to_zero_var(&i)) {
   446              secp256k1_gej_double_var(r, a, rzr);
   447          } else {
   448              if (rzr != NULL) {
   449                  secp256k1_fe_set_int(rzr, 0);
   450              }
   451              r->infinity = 1;
   452          }
   453          return;
   454      }
   455      secp256k1_fe_sqr(&i2, &i);
   456      secp256k1_fe_sqr(&h2, &h);
   457      secp256k1_fe_mul(&h3, &h, &h2);
   458      if (rzr != NULL) {
   459          *rzr = h;
   460      }
   461      secp256k1_fe_mul(&r->z, &a->z, &h);
   462      secp256k1_fe_mul(&t, &u1, &h2);
   463      r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
   464      secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
   465      secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
   466      secp256k1_fe_add(&r->y, &h3);
   467  }
   468  
   469  static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
   470      /* 9 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
   471      secp256k1_fe az, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
   472  
   473      if (b->infinity) {
   474          *r = *a;
   475          return;
   476      }
   477      if (a->infinity) {
   478          secp256k1_fe bzinv2, bzinv3;
   479          r->infinity = b->infinity;
   480          secp256k1_fe_sqr(&bzinv2, bzinv);
   481          secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
   482          secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
   483          secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
   484          secp256k1_fe_set_int(&r->z, 1);
   485          return;
   486      }
   487      r->infinity = 0;
   488  
   489      /** We need to calculate (rx,ry,rz) = (ax,ay,az) + (bx,by,1/bzinv). Due to
   490       *  secp256k1's isomorphism we can multiply the Z coordinates on both sides
   491       *  by bzinv, and get: (rx,ry,rz*bzinv) = (ax,ay,az*bzinv) + (bx,by,1).
   492       *  This means that (rx,ry,rz) can be calculated as
   493       *  (ax,ay,az*bzinv) + (bx,by,1), when not applying the bzinv factor to rz.
   494       *  The variable az below holds the modified Z coordinate for a, which is used
   495       *  for the computation of rx and ry, but not for rz.
   496       */
   497      secp256k1_fe_mul(&az, &a->z, bzinv);
   498  
   499      secp256k1_fe_sqr(&z12, &az);
   500      u1 = a->x; secp256k1_fe_normalize_weak(&u1);
   501      secp256k1_fe_mul(&u2, &b->x, &z12);
   502      s1 = a->y; secp256k1_fe_normalize_weak(&s1);
   503      secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
   504      secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
   505      secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
   506      if (secp256k1_fe_normalizes_to_zero_var(&h)) {
   507          if (secp256k1_fe_normalizes_to_zero_var(&i)) {
   508              secp256k1_gej_double_var(r, a, NULL);
   509          } else {
   510              r->infinity = 1;
   511          }
   512          return;
   513      }
   514      secp256k1_fe_sqr(&i2, &i);
   515      secp256k1_fe_sqr(&h2, &h);
   516      secp256k1_fe_mul(&h3, &h, &h2);
   517      r->z = a->z; secp256k1_fe_mul(&r->z, &r->z, &h);
   518      secp256k1_fe_mul(&t, &u1, &h2);
   519      r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
   520      secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
   521      secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
   522      secp256k1_fe_add(&r->y, &h3);
   523  }
   524  
   525  
   526  static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
   527      /* Operations: 7 mul, 5 sqr, 4 normalize, 21 mul_int/add/negate/cmov */
   528      static const secp256k1_fe fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
   529      secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
   530      secp256k1_fe m_alt, rr_alt;
   531      int infinity, degenerate;
   532      VERIFY_CHECK(!b->infinity);
   533      VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
   534  
   535      /** In:
   536       *    Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
   537       *    In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
   538       *  we find as solution for a unified addition/doubling formula:
   539       *    lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
   540       *    x3 = lambda^2 - (x1 + x2)
   541       *    2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
   542       *
   543       *  Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
   544       *    U1 = X1*Z2^2, U2 = X2*Z1^2
   545       *    S1 = Y1*Z2^3, S2 = Y2*Z1^3
   546       *    Z = Z1*Z2
   547       *    T = U1+U2
   548       *    M = S1+S2
   549       *    Q = T*M^2
   550       *    R = T^2-U1*U2
   551       *    X3 = 4*(R^2-Q)
   552       *    Y3 = 4*(R*(3*Q-2*R^2)-M^4)
   553       *    Z3 = 2*M*Z
   554       *  (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
   555       *
   556       *  This formula has the benefit of being the same for both addition
   557       *  of distinct points and doubling. However, it breaks down in the
   558       *  case that either point is infinity, or that y1 = -y2. We handle
   559       *  these cases in the following ways:
   560       *
   561       *    - If b is infinity we simply bail by means of a VERIFY_CHECK.
   562       *
   563       *    - If a is infinity, we detect this, and at the end of the
   564       *      computation replace the result (which will be meaningless,
   565       *      but we compute to be constant-time) with b.x : b.y : 1.
   566       *
   567       *    - If a = -b, we have y1 = -y2, which is a degenerate case.
   568       *      But here the answer is infinity, so we simply set the
   569       *      infinity flag of the result, overriding the computed values
   570       *      without even needing to cmov.
   571       *
   572       *    - If y1 = -y2 but x1 != x2, which does occur thanks to certain
   573       *      properties of our curve (specifically, 1 has nontrivial cube
   574       *      roots in our field, and the curve equation has no x coefficient)
   575       *      then the answer is not infinity but also not given by the above
   576       *      equation. In this case, we cmov in place an alternate expression
   577       *      for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
   578       *      expressions for lambda are defined, they are equal, and can be
   579       *      obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
   580       *      then substitution of x^3 + 7 for y^2 (using the curve equation).
   581       *      For all pairs of nonzero points (a, b) at least one is defined,
   582       *      so this covers everything.
   583       */
   584  
   585      secp256k1_fe_sqr(&zz, &a->z);                       /* z = Z1^2 */
   586      u1 = a->x; secp256k1_fe_normalize_weak(&u1);        /* u1 = U1 = X1*Z2^2 (1) */
   587      secp256k1_fe_mul(&u2, &b->x, &zz);                  /* u2 = U2 = X2*Z1^2 (1) */
   588      s1 = a->y; secp256k1_fe_normalize_weak(&s1);        /* s1 = S1 = Y1*Z2^3 (1) */
   589      secp256k1_fe_mul(&s2, &b->y, &zz);                  /* s2 = Y2*Z1^2 (1) */
   590      secp256k1_fe_mul(&s2, &s2, &a->z);                  /* s2 = S2 = Y2*Z1^3 (1) */
   591      t = u1; secp256k1_fe_add(&t, &u2);                  /* t = T = U1+U2 (2) */
   592      m = s1; secp256k1_fe_add(&m, &s2);                  /* m = M = S1+S2 (2) */
   593      secp256k1_fe_sqr(&rr, &t);                          /* rr = T^2 (1) */
   594      secp256k1_fe_negate(&m_alt, &u2, 1);                /* Malt = -X2*Z1^2 */
   595      secp256k1_fe_mul(&tt, &u1, &m_alt);                 /* tt = -U1*U2 (2) */
   596      secp256k1_fe_add(&rr, &tt);                         /* rr = R = T^2-U1*U2 (3) */
   597      /** If lambda = R/M = 0/0 we have a problem (except in the "trivial"
   598       *  case that Z = z1z2 = 0, and this is special-cased later on). */
   599      degenerate = secp256k1_fe_normalizes_to_zero(&m) &
   600                   secp256k1_fe_normalizes_to_zero(&rr);
   601      /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
   602       * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
   603       * a nontrivial cube root of one. In either case, an alternate
   604       * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
   605       * so we set R/M equal to this. */
   606      rr_alt = s1;
   607      secp256k1_fe_mul_int(&rr_alt, 2);       /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
   608      secp256k1_fe_add(&m_alt, &u1);          /* Malt = X1*Z2^2 - X2*Z1^2 */
   609  
   610      secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
   611      secp256k1_fe_cmov(&m_alt, &m, !degenerate);
   612      /* Now Ralt / Malt = lambda and is guaranteed not to be 0/0.
   613       * From here on out Ralt and Malt represent the numerator
   614       * and denominator of lambda; R and M represent the explicit
   615       * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
   616      secp256k1_fe_sqr(&n, &m_alt);                       /* n = Malt^2 (1) */
   617      secp256k1_fe_mul(&q, &n, &t);                       /* q = Q = T*Malt^2 (1) */
   618      /* These two lines use the observation that either M == Malt or M == 0,
   619       * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
   620       * zero (which is "computed" by cmov). So the cost is one squaring
   621       * versus two multiplications. */
   622      secp256k1_fe_sqr(&n, &n);
   623      secp256k1_fe_cmov(&n, &m, degenerate);              /* n = M^3 * Malt (2) */
   624      secp256k1_fe_sqr(&t, &rr_alt);                      /* t = Ralt^2 (1) */
   625      secp256k1_fe_mul(&r->z, &a->z, &m_alt);             /* r->z = Malt*Z (1) */
   626      infinity = secp256k1_fe_normalizes_to_zero(&r->z) * (1 - a->infinity);
   627      secp256k1_fe_mul_int(&r->z, 2);                     /* r->z = Z3 = 2*Malt*Z (2) */
   628      secp256k1_fe_negate(&q, &q, 1);                     /* q = -Q (2) */
   629      secp256k1_fe_add(&t, &q);                           /* t = Ralt^2-Q (3) */
   630      secp256k1_fe_normalize_weak(&t);
   631      r->x = t;                                           /* r->x = Ralt^2-Q (1) */
   632      secp256k1_fe_mul_int(&t, 2);                        /* t = 2*x3 (2) */
   633      secp256k1_fe_add(&t, &q);                           /* t = 2*x3 - Q: (4) */
   634      secp256k1_fe_mul(&t, &t, &rr_alt);                  /* t = Ralt*(2*x3 - Q) (1) */
   635      secp256k1_fe_add(&t, &n);                           /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
   636      secp256k1_fe_negate(&r->y, &t, 3);                  /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
   637      secp256k1_fe_normalize_weak(&r->y);
   638      secp256k1_fe_mul_int(&r->x, 4);                     /* r->x = X3 = 4*(Ralt^2-Q) */
   639      secp256k1_fe_mul_int(&r->y, 4);                     /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */
   640  
   641      /** In case a->infinity == 1, replace r with (b->x, b->y, 1). */
   642      secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
   643      secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
   644      secp256k1_fe_cmov(&r->z, &fe_1, a->infinity);
   645      r->infinity = infinity;
   646  }
   647  
   648  static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s) {
   649      /* Operations: 4 mul, 1 sqr */
   650      secp256k1_fe zz;
   651      VERIFY_CHECK(!secp256k1_fe_is_zero(s));
   652      secp256k1_fe_sqr(&zz, s);
   653      secp256k1_fe_mul(&r->x, &r->x, &zz);                /* r->x *= s^2 */
   654      secp256k1_fe_mul(&r->y, &r->y, &zz);
   655      secp256k1_fe_mul(&r->y, &r->y, s);                  /* r->y *= s^3 */
   656      secp256k1_fe_mul(&r->z, &r->z, s);                  /* r->z *= s   */
   657  }
   658  
   659  static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a) {
   660      secp256k1_fe x, y;
   661      VERIFY_CHECK(!a->infinity);
   662      x = a->x;
   663      secp256k1_fe_normalize(&x);
   664      y = a->y;
   665      secp256k1_fe_normalize(&y);
   666      secp256k1_fe_to_storage(&r->x, &x);
   667      secp256k1_fe_to_storage(&r->y, &y);
   668  }
   669  
   670  static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a) {
   671      secp256k1_fe_from_storage(&r->x, &a->x);
   672      secp256k1_fe_from_storage(&r->y, &a->y);
   673      r->infinity = 0;
   674  }
   675  
   676  static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag) {
   677      secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
   678      secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
   679  }
   680  
   681  #ifdef USE_ENDOMORPHISM
   682  static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a) {
   683      static const secp256k1_fe beta = SECP256K1_FE_CONST(
   684          0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
   685          0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
   686      );
   687      *r = *a;
   688      secp256k1_fe_mul(&r->x, &r->x, &beta);
   689  }
   690  #endif
   691  
   692  static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a) {
   693      secp256k1_fe yz;
   694  
   695      if (a->infinity) {
   696          return 0;
   697      }
   698  
   699      /* We rely on the fact that the Jacobi symbol of 1 / a->z^3 is the same as
   700       * that of a->z. Thus a->y / a->z^3 is a quadratic residue iff a->y * a->z
   701         is */
   702      secp256k1_fe_mul(&yz, &a->y, &a->z);
   703      return secp256k1_fe_is_quad_var(&yz);
   704  }
   705  
   706  #endif /* SECP256K1_GROUP_IMPL_H */