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