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!
23 BIGNUM *A, *b, *q, *t, *x, *y;
26 if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
27 if (BN_abs_is_word(p, 2)) {
32 if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
41 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
45 if (BN_is_zero(a) || BN_is_one(a)) {
50 if (!BN_set_word(ret, BN_is_one(a))) {
75 if (!BN_nnmod(A, a, p, ctx))
78 /* now write |p| - 1 as 2^e*q where q is odd */
80 while (!BN_is_bit_set(p, e))
82 /* we'll set q later (if needed) */
86 * The easy case: (|p|-1)/2 is odd, so 2 has an inverse
87 * modulo (|p|-1)/2, and square roots can be computed
88 * directly by modular exponentiation.
90 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
91 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
93 if (!BN_rshift(q, p, 2))
96 if (!BN_add_word(q, 1))
98 if (!BN_mod_exp(ret, A, q, p, ctx))
108 * In this case 2 is always a non-square since
109 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
110 * So if a really is a square, then 2*a is a non-square.
112 * b := (2*a)^((|p|-5)/8),
115 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
121 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
122 * = a^2 * b^2 * (-2*i)
127 * (This is due to A.O.L. Atkin,
128 * Subject: Square Roots and Cognate Matters modulo p=8n+5.
129 * URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026
134 if (!BN_mod_lshift1_quick(t, A, p))
137 /* b := (2*a)^((|p|-5)/8) */
138 if (!BN_rshift(q, p, 3))
141 if (!BN_mod_exp(b, t, q, p, ctx))
145 if (!BN_mod_sqr(y, b, p, ctx))
148 /* t := (2*a)*b^2 - 1 */
149 if (!BN_mod_mul(t, t, y, p, ctx))
151 if (!BN_sub_word(t, 1))
155 if (!BN_mod_mul(x, A, b, p, ctx))
157 if (!BN_mod_mul(x, x, t, p, ctx))
160 if (!BN_copy(ret, x))
167 * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
168 * find some y that is not a square.
171 goto end; /* use 'q' as temp */
176 * For efficiency, try small numbers first; if this fails, try random
180 if (!BN_set_word(y, i))
183 if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))
185 if (BN_ucmp(y, p) >= 0) {
186 if (!(p->neg ? BN_add : BN_sub) (y, y, p))
189 /* now 0 <= y < |p| */
191 if (!BN_set_word(y, i))
195 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
200 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
204 while (r == 1 && ++i < 82);
208 * Many rounds and still no non-square -- this is more likely a bug
209 * than just bad luck. Even if p is not prime, we should have found
210 * some y such that r == -1.
212 BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
216 /* Here's our actual 'q': */
217 if (!BN_rshift(q, q, e))
221 * Now that we have some non-square, we can find an element of order 2^e
222 * by computing its q'th power.
224 if (!BN_mod_exp(y, y, q, p, ctx))
227 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
232 * Now we know that (if p is indeed prime) there is an integer
233 * k, 0 <= k < 2^e, such that
235 * a^q * y^k == 1 (mod p).
237 * As a^q is a square and y is not, k must be even.
238 * q+1 is even, too, so there is an element
240 * X := a^((q+1)/2) * y^(k/2),
244 * X^2 = a^q * a * y^k
247 * so it is the square root that we are looking for.
250 /* t := (q-1)/2 (note that q is odd) */
251 if (!BN_rshift1(t, q))
254 /* x := a^((q-1)/2) */
255 if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
256 if (!BN_nnmod(t, A, p, ctx))
259 /* special case: a == 0 (mod p) */
263 } else if (!BN_one(x))
266 if (!BN_mod_exp(x, A, t, p, ctx))
269 /* special case: a == 0 (mod p) */
276 /* b := a*x^2 (= a^q) */
277 if (!BN_mod_sqr(b, x, p, ctx))
279 if (!BN_mod_mul(b, b, A, p, ctx))
282 /* x := a*x (= a^((q+1)/2)) */
283 if (!BN_mod_mul(x, x, A, p, ctx))
288 * Now b is a^q * y^k for some even k (0 <= k < 2^E
289 * where E refers to the original value of e, which we
290 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
298 if (!BN_copy(ret, x))
304 /* find smallest i such that b^(2^i) = 1 */
306 if (!BN_mod_sqr(t, b, p, ctx))
308 while (!BN_is_one(t)) {
311 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
314 if (!BN_mod_mul(t, t, t, p, ctx))
318 /* t := y^2^(e - i - 1) */
321 for (j = e - i - 1; j > 0; j--) {
322 if (!BN_mod_sqr(t, t, p, ctx))
325 if (!BN_mod_mul(y, t, t, p, ctx))
327 if (!BN_mod_mul(x, x, t, p, ctx))
329 if (!BN_mod_mul(b, b, y, p, ctx))
337 * verify the result -- the input might have been not a square (test
341 if (!BN_mod_sqr(x, ret, p, ctx))
344 if (!err && 0 != BN_cmp(x, A)) {
345 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);