Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ec/util.c

raw
1 // Copyright (c) 2015, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/base.h>
5
6 #include <openssl/ec.h>
7
8 #include "internal.h"
9
10
11 // This function looks at 5+1 scalar bits (5 current, 1 adjacent less
12 // significant bit), and recodes them into a signed digit for use in fast point
13 // multiplication: the use of signed rather than unsigned digits means that
14 // fewer points need to be precomputed, given that point inversion is easy (a
15 // precomputed point dP makes -dP available as well).
16 //
17 // BACKGROUND:
18 //
19 // Signed digits for multiplication were introduced by Booth ("A signed binary
20 // multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
21 // pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
22 // Booth's original encoding did not generally improve the density of nonzero
23 // digits over the binary representation, and was merely meant to simplify the
24 // handling of signed factors given in two's complement; but it has since been
25 // shown to be the basis of various signed-digit representations that do have
26 // further advantages, including the wNAF, using the following general
27 // approach:
28 //
29 // (1) Given a binary representation
30 //
31 // b_k ... b_2 b_1 b_0,
32 //
33 // of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
34 // by using bit-wise subtraction as follows:
35 //
36 // b_k b_(k-1) ... b_2 b_1 b_0
37 // - b_k ... b_3 b_2 b_1 b_0
38 // -----------------------------------------
39 // s_(k+1) s_k ... s_3 s_2 s_1 s_0
40 //
41 // A left-shift followed by subtraction of the original value yields a new
42 // representation of the same value, using signed bits s_i = b_(i-1) - b_i.
43 // This representation from Booth's paper has since appeared in the
44 // literature under a variety of different names including "reversed binary
45 // form", "alternating greedy expansion", "mutual opposite form", and
46 // "sign-alternating {+-1}-representation".
47 //
48 // An interesting property is that among the nonzero bits, values 1 and -1
49 // strictly alternate.
50 //
51 // (2) Various window schemes can be applied to the Booth representation of
52 // integers: for example, right-to-left sliding windows yield the wNAF
53 // (a signed-digit encoding independently discovered by various researchers
54 // in the 1990s), and left-to-right sliding windows yield a left-to-right
55 // equivalent of the wNAF (independently discovered by various researchers
56 // around 2004).
57 //
58 // To prevent leaking information through side channels in point multiplication,
59 // we need to recode the given integer into a regular pattern: sliding windows
60 // as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
61 // decades older: we'll be using the so-called "modified Booth encoding" due to
62 // MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
63 // (1961), pp. 67-91), in a radix-2^5 setting. That is, we always combine five
64 // signed bits into a signed digit:
65 //
66 // s_(5j + 4) s_(5j + 3) s_(5j + 2) s_(5j + 1) s_(5j)
67 //
68 // The sign-alternating property implies that the resulting digit values are
69 // integers from -16 to 16.
70 //
71 // Of course, we don't actually need to compute the signed digits s_i as an
72 // intermediate step (that's just a nice way to see how this scheme relates
73 // to the wNAF): a direct computation obtains the recoded digit from the
74 // six bits b_(5j + 4) ... b_(5j - 1).
75 //
76 // This function takes those six bits as an integer (0 .. 63), writing the
77 // recoded digit to *sign (0 for positive, 1 for negative) and *digit (absolute
78 // value, in the range 0 .. 16). Note that this integer essentially provides
79 // the input bits "shifted to the left" by one position: for example, the input
80 // to compute the least significant recoded digit, given that there's no bit
81 // b_-1, has to be b_4 b_3 b_2 b_1 b_0 0.
82 //
83 // DOUBLING CASE:
84 //
85 // Point addition formulas for short Weierstrass curves are often incomplete.
86 // Edge cases such as P + P or P + ∞ must be handled separately. This
87 // complicates constant-time requirements. P + ∞ cannot be avoided (any window
88 // may be zero) and is handled with constant-time selects. P + P (where P is not
89 // ∞) usually is not. Instead, windowing strategies are chosen to avoid this
90 // case. Whether this happens depends on the group order.
91 //
92 // Let w be the window width (in this function, w = 5). The non-trivial doubling
93 // case in single-point scalar multiplication may occur if and only if the
94 // 2^(w-1) bit of the group order is zero.
95 //
96 // Note the above only holds if the scalar is fully reduced and the group order
97 // is a prime that is much larger than 2^w. It also only holds when windows
98 // are applied from most significant to least significant, doubling between each
99 // window. It does not apply to more complex table strategies such as
100 // |EC_GFp_nistz256_method|.
101 //
102 // PROOF:
103 //
104 // Let n be the group order. Let l be the number of bits needed to represent n.
105 // Assume there exists some 0 <= k < n such that signed w-bit windowed
106 // multiplication hits the doubling case.
107 //
108 // Windowed multiplication consists of iterating over groups of s_i (defined
109 // above based on k's binary representation) from most to least significant. At
110 // iteration i (for i = ..., 3w, 2w, w, 0, starting from the most significant
111 // window), we:
112 //
113 // 1. Double the accumulator A, w times. Let A_i be the value of A at this
114 // point.
115 //
116 // 2. Set A to T_i + A_i, where T_i is a precomputed multiple of P
117 // corresponding to the window s_(i+w-1) ... s_i.
118 //
119 // Let j be the index such that A_j = T_j ≠ ∞. Looking at A_i and T_i as
120 // multiples of P, define a_i and t_i to be scalar coefficients of A_i and T_i.
121 // Thus a_j = t_j ≠ 0 (mod n). Note a_i and t_i may not be reduced mod n. t_i is
122 // the value of the w signed bits s_(i+w-1) ... s_i. a_i is computed as a_i =
123 // 2^w * (a_(i+w) + t_(i+w)).
124 //
125 // t_i is bounded by -2^(w-1) <= t_i <= 2^(w-1). Additionally, we may write it
126 // in terms of unsigned bits b_i. t_i consists of signed bits s_(i+w-1) ... s_i.
127 // This is computed as:
128 //
129 // b_(i+w-2) b_(i+w-3) ... b_i b_(i-1)
130 // - b_(i+w-1) b_(i+w-2) ... b_(i+1) b_i
131 // --------------------------------------------
132 // t_i = s_(i+w-1) s_(i+w-2) ... s_(i+1) s_i
133 //
134 // Observe that b_(i+w-2) through b_i occur in both terms. Let x be the integer
135 // represented by that bit string, i.e. 2^(w-2)*b_(i+w-2) + ... + b_i.
136 //
137 // t_i = (2*x + b_(i-1)) - (2^(w-1)*b_(i+w-1) + x)
138 // = x - 2^(w-1)*b_(i+w-1) + b_(i-1)
139 //
140 // Or, using C notation for bit operations:
141 //
142 // t_i = (k>>i) & ((1<<(w-1)) - 1) - (k>>i) & (1<<(w-1)) + (k>>(i-1)) & 1
143 //
144 // Note b_(i-1) is added in left-shifted by one (or doubled) from its place.
145 // This is compensated by t_(i-w)'s subtraction term. Thus, a_i may be computed
146 // by adding b_l b_(l-1) ... b_(i+1) b_i and an extra copy of b_(i-1). In C
147 // notation, this is:
148 //
149 // a_i = (k>>(i+w)) << w + ((k>>(i+w-1)) & 1) << w
150 //
151 // Observe that, while t_i may be positive or negative, a_i is bounded by
152 // 0 <= a_i < n + 2^w. Additionally, a_i can only be zero if b_(i+w-1) and up
153 // are all zero. (Note this implies a non-trivial P + (-P) is unreachable for
154 // all groups. That would imply the subsequent a_i is zero, which means all
155 // terms thus far were zero.)
156 //
157 // Returning to our doubling position, we have a_j = t_j (mod n). We now
158 // determine the value of a_j - t_j, which must be divisible by n. Our bounds on
159 // a_j and t_j imply a_j - t_j is 0 or n. If it is 0, a_j = t_j. However, 2^w
160 // divides a_j and -2^(w-1) <= t_j <= 2^(w-1), so this can only happen if
161 // a_j = t_j = 0, which is a trivial doubling. Therefore, a_j - t_j = n.
162 //
163 // Now we determine j. Suppose j > 0. w divides j, so j >= w. Then,
164 //
165 // n = a_j - t_j = (k>>(j+w)) << w + ((k>>(j+w-1)) & 1) << w - t_j
166 // <= k/2^j + 2^w - t_j
167 // < n/2^w + 2^w + 2^(w-1)
168 //
169 // n is much larger than 2^w, so this is impossible. Thus, j = 0: only the final
170 // addition may hit the doubling case.
171 //
172 // Finally, we consider bit patterns for n and k. Divide k into k_H + k_M + k_L
173 // such that k_H is the contribution from b_(l-1) .. b_w, k_M is the
174 // contribution from b_(w-1), and k_L is the contribution from b_(w-2) ... b_0.
175 // That is:
176 //
177 // - 2^w divides k_H
178 // - k_M is 0 or 2^(w-1)
179 // - 0 <= k_L < 2^(w-1)
180 //
181 // Divide n into n_H + n_M + n_L similarly. We thus have:
182 //
183 // t_0 = (k>>0) & ((1<<(w-1)) - 1) - (k>>0) & (1<<(w-1)) + (k>>(0-1)) & 1
184 // = k & ((1<<(w-1)) - 1) - k & (1<<(w-1))
185 // = k_L - k_M
186 //
187 // a_0 = (k>>(0+w)) << w + ((k>>(0+w-1)) & 1) << w
188 // = (k>>w) << w + ((k>>(w-1)) & 1) << w
189 // = k_H + 2*k_M
190 //
191 // n = a_0 - t_0
192 // n_H + n_M + n_L = (k_H + 2*k_M) - (k_L - k_M)
193 // = k_H + 3*k_M - k_L
194 //
195 // k_H - k_L < k and k < n, so k_H - k_L ≠ n. Therefore k_M is not 0 and must be
196 // 2^(w-1). Now we consider k_H and n_H. We know k_H <= n_H. Suppose k_H = n_H.
197 // Then,
198 //
199 // n_M + n_L = 3*(2^(w-1)) - k_L
200 // > 3*(2^(w-1)) - 2^(w-1)
201 // = 2^w
202 //
203 // Contradiction (n_M + n_L is the bottom w bits of n). Thus k_H < n_H. Suppose
204 // k_H < n_H - 2*2^w. Then,
205 //
206 // n_H + n_M + n_L = k_H + 3*(2^(w-1)) - k_L
207 // < n_H - 2*2^w + 3*(2^(w-1)) - k_L
208 // n_M + n_L < -2^(w-1) - k_L
209 //
210 // Contradiction. Thus, k_H = n_H - 2^w. (Note 2^w divides n_H and k_H.) Thus,
211 //
212 // n_H + n_M + n_L = k_H + 3*(2^(w-1)) - k_L
213 // = n_H - 2^w + 3*(2^(w-1)) - k_L
214 // n_M + n_L = 2^(w-1) - k_L
215 // <= 2^(w-1)
216 //
217 // Equality would mean 2^(w-1) divides n, which is impossible if n is prime.
218 // Thus n_M + n_L < 2^(w-1), so n_M is zero, proving our condition.
219 //
220 // This proof constructs k, so, to show the converse, let k_H = n_H - 2^w,
221 // k_M = 2^(w-1), k_L = 2^(w-1) - n_L. This will result in a non-trivial point
222 // doubling in the final addition and is the only such scalar.
223 //
224 // COMMON CURVES:
225 //
226 // The group orders for common curves end in the following bit patterns:
227 //
228 // P-521: ...00001001; w = 4 is okay
229 // P-384: ...01110011; w = 2, 5, 6, 7 are okay
230 // P-256: ...01010001; w = 5, 7 are okay
231 // P-224: ...00111101; w = 3, 4, 5, 6 are okay
232 void ec_GFp_nistp_recode_scalar_bits(crypto_word_t *sign, crypto_word_t *digit,
233 crypto_word_t in) {
234 crypto_word_t s, d;
235
236 s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
237 * 6-bit value */
238 d = (1 << 6) - in - 1;
239 d = (d & s) | (in & ~s);
240 d = (d >> 1) + (d & 1);
241
242 *sign = s & 1;
243 *digit = d;
244 }