2 * Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
13 BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
15 * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
16 * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
17 * Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or
18 * an incorrect "result" will be returned.
24 BIGNUM *A, *b, *q, *t, *x, *y;
27 if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
28 if (BN_abs_is_word(p, 2)) {
33 if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
42 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
46 if (BN_is_zero(a) || BN_is_one(a)) {
51 if (!BN_set_word(ret, BN_is_one(a))) {
76 if (!BN_nnmod(A, a, p, ctx))
79 /* now write |p| - 1 as 2^e*q where q is odd */
81 while (!BN_is_bit_set(p, e))
83 /* we'll set q later (if needed) */
87 * The easy case: (|p|-1)/2 is odd, so 2 has an inverse
88 * modulo (|p|-1)/2, and square roots can be computed
89 * directly by modular exponentiation.
91 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
92 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
94 if (!BN_rshift(q, p, 2))
97 if (!BN_add_word(q, 1))
99 if (!BN_mod_exp(ret, A, q, p, ctx))
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.
113 * b := (2*a)^((|p|-5)/8),
116 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
122 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
123 * = a^2 * b^2 * (-2*i)
128 * (This is due to A.O.L. Atkin,
129 * Subject: Square Roots and Cognate Matters modulo p=8n+5.
130 * URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026
135 if (!BN_mod_lshift1_quick(t, A, p))
138 /* b := (2*a)^((|p|-5)/8) */
139 if (!BN_rshift(q, p, 3))
142 if (!BN_mod_exp(b, t, q, p, ctx))
146 if (!BN_mod_sqr(y, b, p, ctx))
149 /* t := (2*a)*b^2 - 1 */
150 if (!BN_mod_mul(t, t, y, p, ctx))
152 if (!BN_sub_word(t, 1))
156 if (!BN_mod_mul(x, A, b, p, ctx))
158 if (!BN_mod_mul(x, x, t, p, ctx))
161 if (!BN_copy(ret, x))
168 * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
169 * find some y that is not a square.
172 goto end; /* use 'q' as temp */
177 * For efficiency, try small numbers first; if this fails, try random
181 if (!BN_set_word(y, i))
184 if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))
186 if (BN_ucmp(y, p) >= 0) {
187 if (!(p->neg ? BN_add : BN_sub) (y, y, p))
190 /* now 0 <= y < |p| */
192 if (!BN_set_word(y, i))
196 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
201 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
205 while (r == 1 && ++i < 82);
209 * Many rounds and still no non-square -- this is more likely a bug
210 * than just bad luck. Even if p is not prime, we should have found
211 * some y such that r == -1.
213 BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
217 /* Here's our actual 'q': */
218 if (!BN_rshift(q, q, e))
222 * Now that we have some non-square, we can find an element of order 2^e
223 * by computing its q'th power.
225 if (!BN_mod_exp(y, y, q, p, ctx))
228 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
233 * Now we know that (if p is indeed prime) there is an integer
234 * k, 0 <= k < 2^e, such that
236 * a^q * y^k == 1 (mod p).
238 * As a^q is a square and y is not, k must be even.
239 * q+1 is even, too, so there is an element
241 * X := a^((q+1)/2) * y^(k/2),
245 * X^2 = a^q * a * y^k
248 * so it is the square root that we are looking for.
251 /* t := (q-1)/2 (note that q is odd) */
252 if (!BN_rshift1(t, q))
255 /* x := a^((q-1)/2) */
256 if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
257 if (!BN_nnmod(t, A, p, ctx))
260 /* special case: a == 0 (mod p) */
264 } else if (!BN_one(x))
267 if (!BN_mod_exp(x, A, t, p, ctx))
270 /* special case: a == 0 (mod p) */
277 /* b := a*x^2 (= a^q) */
278 if (!BN_mod_sqr(b, x, p, ctx))
280 if (!BN_mod_mul(b, b, A, p, ctx))
283 /* x := a*x (= a^((q+1)/2)) */
284 if (!BN_mod_mul(x, x, A, p, ctx))
289 * Now b is a^q * y^k for some even k (0 <= k < 2^E
290 * where E refers to the original value of e, which we
291 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
299 if (!BN_copy(ret, x))
305 /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
306 for (i = 1; i < e; i++) {
308 if (!BN_mod_sqr(t, b, p, ctx))
312 if (!BN_mod_mul(t, t, t, p, ctx))
318 /* If not found, a is not a square or p is not prime. */
320 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
324 /* t := y^2^(e - i - 1) */
327 for (j = e - i - 1; j > 0; j--) {
328 if (!BN_mod_sqr(t, t, p, ctx))
331 if (!BN_mod_mul(y, t, t, p, ctx))
333 if (!BN_mod_mul(x, x, t, p, ctx))
335 if (!BN_mod_mul(b, b, y, p, ctx))
343 * verify the result -- the input might have been not a square (test
347 if (!BN_mod_sqr(x, ret, p, ctx))
350 if (!err && 0 != BN_cmp(x, A)) {
351 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);