You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/base.h>
  15. #if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
  16. #include <openssl/ec.h>
  17. #include "internal.h"
  18. /* Convert an array of points into affine coordinates. (If the point at
  19. * infinity is found (Z = 0), it remains unchanged.) This function is
  20. * essentially an equivalent to EC_POINTs_make_affine(), but works with the
  21. * internal representation of points as used by ecp_nistp###.c rather than
  22. * with (BIGNUM-based) EC_POINT data structures. point_array is the
  23. * input/output buffer ('num' points in projective form, i.e. three
  24. * coordinates each), based on an internal representation of field elements
  25. * of size 'felem_size'. tmp_felems needs to point to a temporary array of
  26. * 'num'+1 field elements for storage of intermediate values. */
  27. void ec_GFp_nistp_points_make_affine_internal(
  28. size_t num, void *point_array, size_t felem_size, void *tmp_felems,
  29. void (*felem_one)(void *out), int (*felem_is_zero)(const void *in),
  30. void (*felem_assign)(void *out, const void *in),
  31. void (*felem_square)(void *out, const void *in),
  32. void (*felem_mul)(void *out, const void *in1, const void *in2),
  33. void (*felem_inv)(void *out, const void *in),
  34. void (*felem_contract)(void *out, const void *in)) {
  35. int i = 0;
  36. #define tmp_felem(I) (&((char *)tmp_felems)[(I)*felem_size])
  37. #define X(I) (&((char *)point_array)[3 * (I)*felem_size])
  38. #define Y(I) (&((char *)point_array)[(3 * (I) + 1) * felem_size])
  39. #define Z(I) (&((char *)point_array)[(3 * (I) + 2) * felem_size])
  40. if (!felem_is_zero(Z(0))) {
  41. felem_assign(tmp_felem(0), Z(0));
  42. } else {
  43. felem_one(tmp_felem(0));
  44. }
  45. for (i = 1; i < (int)num; i++) {
  46. if (!felem_is_zero(Z(i))) {
  47. felem_mul(tmp_felem(i), tmp_felem(i - 1), Z(i));
  48. } else {
  49. felem_assign(tmp_felem(i), tmp_felem(i - 1));
  50. }
  51. }
  52. /* Now each tmp_felem(i) is the product of Z(0) .. Z(i), skipping any
  53. * zero-valued factors: if Z(i) = 0, we essentially pretend that Z(i) = 1. */
  54. felem_inv(tmp_felem(num - 1), tmp_felem(num - 1));
  55. for (i = num - 1; i >= 0; i--) {
  56. if (i > 0) {
  57. /* tmp_felem(i-1) is the product of Z(0) .. Z(i-1), tmp_felem(i)
  58. * is the inverse of the product of Z(0) .. Z(i). */
  59. /* 1/Z(i) */
  60. felem_mul(tmp_felem(num), tmp_felem(i - 1), tmp_felem(i));
  61. } else {
  62. felem_assign(tmp_felem(num), tmp_felem(0)); /* 1/Z(0) */
  63. }
  64. if (!felem_is_zero(Z(i))) {
  65. if (i > 0) {
  66. /* For next iteration, replace tmp_felem(i-1) by its inverse. */
  67. felem_mul(tmp_felem(i - 1), tmp_felem(i), Z(i));
  68. }
  69. /* Convert point (X, Y, Z) into affine form (X/(Z^2), Y/(Z^3), 1). */
  70. felem_square(Z(i), tmp_felem(num)); /* 1/(Z^2) */
  71. felem_mul(X(i), X(i), Z(i)); /* X/(Z^2) */
  72. felem_mul(Z(i), Z(i), tmp_felem(num)); /* 1/(Z^3) */
  73. felem_mul(Y(i), Y(i), Z(i)); /* Y/(Z^3) */
  74. felem_contract(X(i), X(i));
  75. felem_contract(Y(i), Y(i));
  76. felem_one(Z(i));
  77. } else {
  78. if (i > 0) {
  79. /* For next iteration, replace tmp_felem(i-1) by its inverse. */
  80. felem_assign(tmp_felem(i - 1), tmp_felem(i));
  81. }
  82. }
  83. }
  84. }
  85. /* This function looks at 5+1 scalar bits (5 current, 1 adjacent less
  86. * significant bit), and recodes them into a signed digit for use in fast point
  87. * multiplication: the use of signed rather than unsigned digits means that
  88. * fewer points need to be precomputed, given that point inversion is easy (a
  89. * precomputed point dP makes -dP available as well).
  90. *
  91. * BACKGROUND:
  92. *
  93. * Signed digits for multiplication were introduced by Booth ("A signed binary
  94. * multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
  95. * pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
  96. * Booth's original encoding did not generally improve the density of nonzero
  97. * digits over the binary representation, and was merely meant to simplify the
  98. * handling of signed factors given in two's complement; but it has since been
  99. * shown to be the basis of various signed-digit representations that do have
  100. * further advantages, including the wNAF, using the following general
  101. * approach:
  102. *
  103. * (1) Given a binary representation
  104. *
  105. * b_k ... b_2 b_1 b_0,
  106. *
  107. * of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
  108. * by using bit-wise subtraction as follows:
  109. *
  110. * b_k b_(k-1) ... b_2 b_1 b_0
  111. * - b_k ... b_3 b_2 b_1 b_0
  112. * -------------------------------------
  113. * s_k b_(k-1) ... s_3 s_2 s_1 s_0
  114. *
  115. * A left-shift followed by subtraction of the original value yields a new
  116. * representation of the same value, using signed bits s_i = b_(i+1) - b_i.
  117. * This representation from Booth's paper has since appeared in the
  118. * literature under a variety of different names including "reversed binary
  119. * form", "alternating greedy expansion", "mutual opposite form", and
  120. * "sign-alternating {+-1}-representation".
  121. *
  122. * An interesting property is that among the nonzero bits, values 1 and -1
  123. * strictly alternate.
  124. *
  125. * (2) Various window schemes can be applied to the Booth representation of
  126. * integers: for example, right-to-left sliding windows yield the wNAF
  127. * (a signed-digit encoding independently discovered by various researchers
  128. * in the 1990s), and left-to-right sliding windows yield a left-to-right
  129. * equivalent of the wNAF (independently discovered by various researchers
  130. * around 2004).
  131. *
  132. * To prevent leaking information through side channels in point multiplication,
  133. * we need to recode the given integer into a regular pattern: sliding windows
  134. * as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
  135. * decades older: we'll be using the so-called "modified Booth encoding" due to
  136. * MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
  137. * (1961), pp. 67-91), in a radix-2^5 setting. That is, we always combine five
  138. * signed bits into a signed digit:
  139. *
  140. * s_(4j + 4) s_(4j + 3) s_(4j + 2) s_(4j + 1) s_(4j)
  141. *
  142. * The sign-alternating property implies that the resulting digit values are
  143. * integers from -16 to 16.
  144. *
  145. * Of course, we don't actually need to compute the signed digits s_i as an
  146. * intermediate step (that's just a nice way to see how this scheme relates
  147. * to the wNAF): a direct computation obtains the recoded digit from the
  148. * six bits b_(4j + 4) ... b_(4j - 1).
  149. *
  150. * This function takes those five bits as an integer (0 .. 63), writing the
  151. * recoded digit to *sign (0 for positive, 1 for negative) and *digit (absolute
  152. * value, in the range 0 .. 8). Note that this integer essentially provides the
  153. * input bits "shifted to the left" by one position: for example, the input to
  154. * compute the least significant recoded digit, given that there's no bit b_-1,
  155. * has to be b_4 b_3 b_2 b_1 b_0 0. */
  156. void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit,
  157. uint8_t in) {
  158. uint8_t s, d;
  159. s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
  160. * 6-bit value */
  161. d = (1 << 6) - in - 1;
  162. d = (d & s) | (in & ~s);
  163. d = (d >> 1) + (d & 1);
  164. *sign = s & 1;
  165. *digit = d;
  166. }
  167. #endif /* 64_BIT && !WINDOWS */