util-64.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // This function looks at 5+1 scalar bits (5 current, 1 adjacent less
  19. // significant bit), and recodes them into a signed digit for use in fast point
  20. // multiplication: the use of signed rather than unsigned digits means that
  21. // fewer points need to be precomputed, given that point inversion is easy (a
  22. // precomputed point dP makes -dP available as well).
  23. //
  24. // BACKGROUND:
  25. //
  26. // Signed digits for multiplication were introduced by Booth ("A signed binary
  27. // multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
  28. // pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
  29. // Booth's original encoding did not generally improve the density of nonzero
  30. // digits over the binary representation, and was merely meant to simplify the
  31. // handling of signed factors given in two's complement; but it has since been
  32. // shown to be the basis of various signed-digit representations that do have
  33. // further advantages, including the wNAF, using the following general
  34. // approach:
  35. //
  36. // (1) Given a binary representation
  37. //
  38. // b_k ... b_2 b_1 b_0,
  39. //
  40. // of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
  41. // by using bit-wise subtraction as follows:
  42. //
  43. // b_k b_(k-1) ... b_2 b_1 b_0
  44. // - b_k ... b_3 b_2 b_1 b_0
  45. // -------------------------------------
  46. // s_k b_(k-1) ... s_3 s_2 s_1 s_0
  47. //
  48. // A left-shift followed by subtraction of the original value yields a new
  49. // representation of the same value, using signed bits s_i = b_(i+1) - b_i.
  50. // This representation from Booth's paper has since appeared in the
  51. // literature under a variety of different names including "reversed binary
  52. // form", "alternating greedy expansion", "mutual opposite form", and
  53. // "sign-alternating {+-1}-representation".
  54. //
  55. // An interesting property is that among the nonzero bits, values 1 and -1
  56. // strictly alternate.
  57. //
  58. // (2) Various window schemes can be applied to the Booth representation of
  59. // integers: for example, right-to-left sliding windows yield the wNAF
  60. // (a signed-digit encoding independently discovered by various researchers
  61. // in the 1990s), and left-to-right sliding windows yield a left-to-right
  62. // equivalent of the wNAF (independently discovered by various researchers
  63. // around 2004).
  64. //
  65. // To prevent leaking information through side channels in point multiplication,
  66. // we need to recode the given integer into a regular pattern: sliding windows
  67. // as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
  68. // decades older: we'll be using the so-called "modified Booth encoding" due to
  69. // MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
  70. // (1961), pp. 67-91), in a radix-2^5 setting. That is, we always combine five
  71. // signed bits into a signed digit:
  72. //
  73. // s_(4j + 4) s_(4j + 3) s_(4j + 2) s_(4j + 1) s_(4j)
  74. //
  75. // The sign-alternating property implies that the resulting digit values are
  76. // integers from -16 to 16.
  77. //
  78. // Of course, we don't actually need to compute the signed digits s_i as an
  79. // intermediate step (that's just a nice way to see how this scheme relates
  80. // to the wNAF): a direct computation obtains the recoded digit from the
  81. // six bits b_(4j + 4) ... b_(4j - 1).
  82. //
  83. // This function takes those five bits as an integer (0 .. 63), writing the
  84. // recoded digit to *sign (0 for positive, 1 for negative) and *digit (absolute
  85. // value, in the range 0 .. 8). Note that this integer essentially provides the
  86. // input bits "shifted to the left" by one position: for example, the input to
  87. // compute the least significant recoded digit, given that there's no bit b_-1,
  88. // has to be b_4 b_3 b_2 b_1 b_0 0.
  89. void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit,
  90. uint8_t in) {
  91. uint8_t s, d;
  92. s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
  93. * 6-bit value */
  94. d = (1 << 6) - in - 1;
  95. d = (d & s) | (in & ~s);
  96. d = (d >> 1) + (d & 1);
  97. *sign = s & 1;
  98. *digit = d;
  99. }
  100. #endif // 64_BIT && !WINDOWS