Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bn/sqrt.c

raw
1 // Written by Lenka Fibikova <[email protected]> and Bodo Moeller for the OpenSSL project.
2 // Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/bn.h>
6
7 #include <openssl/err.h>
8
9 #include "internal.h"
10
11
12 BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
13 // Compute a square root of |a| mod |p| using the Tonelli/Shanks algorithm
14 // (cf. Henri Cohen, "A Course in Algebraic Computational Number Theory",
15 // algorithm 1.5.1). |p| must be prime, otherwise an error or
16 // an incorrect "result" will be returned.
17
18 BIGNUM *ret = in;
19 int err = 1;
20 int r;
21 BIGNUM *A, *b, *q, *t, *x, *y;
22 int e, i, j;
23
24 if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
25 if (BN_abs_is_word(p, 2)) {
26 if (ret == NULL) {
27 ret = BN_new();
28 }
29 if (ret == NULL ||
30 !BN_set_word(ret, BN_is_bit_set(a, 0))) {
31 if (ret != in) {
32 BN_free(ret);
33 }
34 return NULL;
35 }
36 return ret;
37 }
38
39 OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
40 return NULL;
41 }
42
43 if (BN_is_zero(a) || BN_is_one(a)) {
44 if (ret == NULL) {
45 ret = BN_new();
46 }
47 if (ret == NULL ||
48 !BN_set_word(ret, BN_is_one(a))) {
49 if (ret != in) {
50 BN_free(ret);
51 }
52 return NULL;
53 }
54 return ret;
55 }
56
57 BN_CTX_start(ctx);
58 A = BN_CTX_get(ctx);
59 b = BN_CTX_get(ctx);
60 q = BN_CTX_get(ctx);
61 t = BN_CTX_get(ctx);
62 x = BN_CTX_get(ctx);
63 y = BN_CTX_get(ctx);
64 if (y == NULL) {
65 goto end;
66 }
67
68 if (ret == NULL) {
69 ret = BN_new();
70 }
71 if (ret == NULL) {
72 goto end;
73 }
74
75 // A = a mod p
76 if (!BN_nnmod(A, a, p, ctx)) {
77 goto end;
78 }
79
80 // now write |p| - 1 as 2^e*q where q is odd
81 e = 1;
82 while (!BN_is_bit_set(p, e)) {
83 e++;
84 }
85 // we'll set q later (if needed)
86
87 if (e == 1) {
88 // The easy case: (|p|-1)/2 is odd, so 2 has an inverse
89 // modulo (|p|-1)/2, and square roots can be computed
90 // directly by modular exponentiation.
91 // We have
92 // 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
93 // so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
94 if (!BN_rshift(q, p, 2)) {
95 goto end;
96 }
97 q->neg = 0;
98 if (!BN_add_word(q, 1) ||
99 !BN_mod_exp_mont(ret, A, q, p, ctx, NULL)) {
100 goto end;
101 }
102 err = 0;
103 goto vrfy;
104 }
105
106 if (e == 2) {
107 // |p| == 5 (mod 8)
108 //
109 // In this case 2 is always a non-square since
110 // Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
111 // So if a really is a square, then 2*a is a non-square.
112 // Thus for
113 // b := (2*a)^((|p|-5)/8),
114 // i := (2*a)*b^2
115 // we have
116 // i^2 = (2*a)^((1 + (|p|-5)/4)*2)
117 // = (2*a)^((p-1)/2)
118 // = -1;
119 // so if we set
120 // x := a*b*(i-1),
121 // then
122 // x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
123 // = a^2 * b^2 * (-2*i)
124 // = a*(-i)*(2*a*b^2)
125 // = a*(-i)*i
126 // = a.
127 //
128 // (This is due to A.O.L. Atkin,
129 // <URL:
130 //http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
131 // November 1992.)
132
133 // t := 2*a
134 if (!bn_mod_lshift1_consttime(t, A, p, ctx)) {
135 goto end;
136 }
137
138 // b := (2*a)^((|p|-5)/8)
139 if (!BN_rshift(q, p, 3)) {
140 goto end;
141 }
142 q->neg = 0;
143 if (!BN_mod_exp_mont(b, t, q, p, ctx, NULL)) {
144 goto end;
145 }
146
147 // y := b^2
148 if (!BN_mod_sqr(y, b, p, ctx)) {
149 goto end;
150 }
151
152 // t := (2*a)*b^2 - 1
153 if (!BN_mod_mul(t, t, y, p, ctx) ||
154 !BN_sub_word(t, 1)) {
155 goto end;
156 }
157
158 // x = a*b*t
159 if (!BN_mod_mul(x, A, b, p, ctx) ||
160 !BN_mod_mul(x, x, t, p, ctx)) {
161 goto end;
162 }
163
164 if (!BN_copy(ret, x)) {
165 goto end;
166 }
167 err = 0;
168 goto vrfy;
169 }
170
171 // e > 2, so we really have to use the Tonelli/Shanks algorithm.
172 // First, find some y that is not a square.
173 if (!BN_copy(q, p)) {
174 goto end; // use 'q' as temp
175 }
176 q->neg = 0;
177 i = 2;
178 do {
179 // For efficiency, try small numbers first;
180 // if this fails, try random numbers.
181 if (i < 22) {
182 if (!BN_set_word(y, i)) {
183 goto end;
184 }
185 } else {
186 if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) {
187 goto end;
188 }
189 if (BN_ucmp(y, p) >= 0) {
190 if (!(p->neg ? BN_add : BN_sub)(y, y, p)) {
191 goto end;
192 }
193 }
194 // now 0 <= y < |p|
195 if (BN_is_zero(y)) {
196 if (!BN_set_word(y, i)) {
197 goto end;
198 }
199 }
200 }
201
202 r = bn_jacobi(y, q, ctx); // here 'q' is |p|
203 if (r < -1) {
204 goto end;
205 }
206 if (r == 0) {
207 // m divides p
208 OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
209 goto end;
210 }
211 } while (r == 1 && ++i < 82);
212
213 if (r != -1) {
214 // Many rounds and still no non-square -- this is more likely
215 // a bug than just bad luck.
216 // Even if p is not prime, we should have found some y
217 // such that r == -1.
218 OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS);
219 goto end;
220 }
221
222 // Here's our actual 'q':
223 if (!BN_rshift(q, q, e)) {
224 goto end;
225 }
226
227 // Now that we have some non-square, we can find an element
228 // of order 2^e by computing its q'th power.
229 if (!BN_mod_exp_mont(y, y, q, p, ctx, NULL)) {
230 goto end;
231 }
232 if (BN_is_one(y)) {
233 OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
234 goto end;
235 }
236
237 // Now we know that (if p is indeed prime) there is an integer
238 // k, 0 <= k < 2^e, such that
239 //
240 // a^q * y^k == 1 (mod p).
241 //
242 // As a^q is a square and y is not, k must be even.
243 // q+1 is even, too, so there is an element
244 //
245 // X := a^((q+1)/2) * y^(k/2),
246 //
247 // and it satisfies
248 //
249 // X^2 = a^q * a * y^k
250 // = a,
251 //
252 // so it is the square root that we are looking for.
253
254 // t := (q-1)/2 (note that q is odd)
255 if (!BN_rshift1(t, q)) {
256 goto end;
257 }
258
259 // x := a^((q-1)/2)
260 if (BN_is_zero(t)) { // special case: p = 2^e + 1
261 if (!BN_nnmod(t, A, p, ctx)) {
262 goto end;
263 }
264 if (BN_is_zero(t)) {
265 // special case: a == 0 (mod p)
266 BN_zero(ret);
267 err = 0;
268 goto end;
269 } else if (!BN_one(x)) {
270 goto end;
271 }
272 } else {
273 if (!BN_mod_exp_mont(x, A, t, p, ctx, NULL)) {
274 goto end;
275 }
276 if (BN_is_zero(x)) {
277 // special case: a == 0 (mod p)
278 BN_zero(ret);
279 err = 0;
280 goto end;
281 }
282 }
283
284 // b := a*x^2 (= a^q)
285 if (!BN_mod_sqr(b, x, p, ctx) ||
286 !BN_mod_mul(b, b, A, p, ctx)) {
287 goto end;
288 }
289
290 // x := a*x (= a^((q+1)/2))
291 if (!BN_mod_mul(x, x, A, p, ctx)) {
292 goto end;
293 }
294
295 while (1) {
296 // Now b is a^q * y^k for some even k (0 <= k < 2^E
297 // where E refers to the original value of e, which we
298 // don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
299 //
300 // We have a*b = x^2,
301 // y^2^(e-1) = -1,
302 // b^2^(e-1) = 1.
303 if (BN_is_one(b)) {
304 if (!BN_copy(ret, x)) {
305 goto end;
306 }
307 err = 0;
308 goto vrfy;
309 }
310
311 // Find the smallest i, 0 < i < e, such that b^(2^i) = 1
312 for (i = 1; i < e; i++) {
313 if (i == 1) {
314 if (!BN_mod_sqr(t, b, p, ctx)) {
315 goto end;
316 }
317 } else {
318 if (!BN_mod_mul(t, t, t, p, ctx)) {
319 goto end;
320 }
321 }
322 if (BN_is_one(t)) {
323 break;
324 }
325 }
326 // If not found, a is not a square or p is not a prime.
327 if (i >= e) {
328 OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
329 goto end;
330 }
331
332 // t := y^2^(e - i - 1)
333 if (!BN_copy(t, y)) {
334 goto end;
335 }
336 for (j = e - i - 1; j > 0; j--) {
337 if (!BN_mod_sqr(t, t, p, ctx)) {
338 goto end;
339 }
340 }
341 if (!BN_mod_mul(y, t, t, p, ctx) ||
342 !BN_mod_mul(x, x, t, p, ctx) ||
343 !BN_mod_mul(b, b, y, p, ctx)) {
344 goto end;
345 }
346
347 // e decreases each iteration, so this loop will terminate.
348 assert(i < e);
349 e = i;
350 }
351
352 vrfy:
353 if (!err) {
354 // Verify the result. The input might have been not a square.
355 if (!BN_mod_sqr(x, ret, p, ctx)) {
356 err = 1;
357 }
358
359 if (!err && 0 != BN_cmp(x, A)) {
360 OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
361 err = 1;
362 }
363 }
364
365 end:
366 if (err) {
367 if (ret != in) {
368 BN_clear_free(ret);
369 }
370 ret = NULL;
371 }
372 BN_CTX_end(ctx);
373 return ret;
374 }
375
376 int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) {
377 BIGNUM *estimate, *tmp, *delta, *last_delta, *tmp2;
378 int ok = 0, last_delta_valid = 0;
379
380 if (in->neg) {
381 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
382 return 0;
383 }
384 if (BN_is_zero(in)) {
385 BN_zero(out_sqrt);
386 return 1;
387 }
388
389 BN_CTX_start(ctx);
390 if (out_sqrt == in) {
391 estimate = BN_CTX_get(ctx);
392 } else {
393 estimate = out_sqrt;
394 }
395 tmp = BN_CTX_get(ctx);
396 last_delta = BN_CTX_get(ctx);
397 delta = BN_CTX_get(ctx);
398 if (estimate == NULL || tmp == NULL || last_delta == NULL || delta == NULL) {
399 goto err;
400 }
401
402 // We estimate that the square root of an n-bit number is 2^{n/2}.
403 if (!BN_lshift(estimate, BN_value_one(), BN_num_bits(in)/2)) {
404 goto err;
405 }
406
407 // This is Newton's method for finding a root of the equation |estimate|^2 -
408 // |in| = 0.
409 for (;;) {
410 // |estimate| = 1/2 * (|estimate| + |in|/|estimate|)
411 if (!BN_div(tmp, NULL, in, estimate, ctx) ||
412 !BN_add(tmp, tmp, estimate) ||
413 !BN_rshift1(estimate, tmp) ||
414 // |tmp| = |estimate|^2
415 !BN_sqr(tmp, estimate, ctx) ||
416 // |delta| = |in| - |tmp|
417 !BN_sub(delta, in, tmp)) {
418 OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
419 goto err;
420 }
421
422 delta->neg = 0;
423 // The difference between |in| and |estimate| squared is required to always
424 // decrease. This ensures that the loop always terminates, but I don't have
425 // a proof that it always finds the square root for a given square.
426 if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) {
427 break;
428 }
429
430 last_delta_valid = 1;
431
432 tmp2 = last_delta;
433 last_delta = delta;
434 delta = tmp2;
435 }
436
437 if (BN_cmp(tmp, in) != 0) {
438 OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
439 goto err;
440 }
441
442 ok = 1;
443
444 err:
445 if (ok && out_sqrt == in && !BN_copy(out_sqrt, estimate)) {
446 ok = 0;
447 }
448 BN_CTX_end(ctx);
449 return ok;
450 }