2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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;
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 ERR_raise(ERR_LIB_BN, 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))) {
77 if (!BN_nnmod(A, a, p, ctx))
80 /* now write |p| - 1 as 2^e*q where q is odd */
82 while (!BN_is_bit_set(p, e))
84 /* we'll set q later (if needed) */
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.
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.
95 if (!BN_rshift(q, p, 2))
98 if (!BN_add_word(q, 1))
100 if (!BN_mod_exp(ret, A, q, p, ctx))
110 * In this case 2 is always a non-square since
111 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
112 * So if a really is a square, then 2*a is a non-square.
114 * b := (2*a)^((|p|-5)/8),
117 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
123 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
124 * = a^2 * b^2 * (-2*i)
129 * (This is due to A.O.L. Atkin,
130 * Subject: Square Roots and Cognate Matters modulo p=8n+5.
131 * URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026
136 if (!BN_mod_lshift1_quick(t, A, p))
139 /* b := (2*a)^((|p|-5)/8) */
140 if (!BN_rshift(q, p, 3))
143 if (!BN_mod_exp(b, t, q, p, ctx))
147 if (!BN_mod_sqr(y, b, p, ctx))
150 /* t := (2*a)*b^2 - 1 */
151 if (!BN_mod_mul(t, t, y, p, ctx))
153 if (!BN_sub_word(t, 1))
157 if (!BN_mod_mul(x, A, b, p, ctx))
159 if (!BN_mod_mul(x, x, t, p, ctx))
162 if (!BN_copy(ret, x))
169 * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
170 * find some y that is not a square.
173 goto end; /* use 'q' as temp */
178 * For efficiency, try small numbers first; if this fails, try random
182 if (!BN_set_word(y, i))
185 if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, ctx))
187 if (BN_ucmp(y, p) >= 0) {
188 if (!(p->neg ? BN_add : BN_sub) (y, y, p))
191 /* now 0 <= y < |p| */
193 if (!BN_set_word(y, i))
197 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
202 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
206 while (r == 1 && ++i < 82);
210 * Many rounds and still no non-square -- this is more likely a bug
211 * than just bad luck. Even if p is not prime, we should have found
212 * some y such that r == -1.
214 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
218 /* Here's our actual 'q': */
219 if (!BN_rshift(q, q, e))
223 * Now that we have some non-square, we can find an element of order 2^e
224 * by computing its q'th power.
226 if (!BN_mod_exp(y, y, q, p, ctx))
229 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
234 * Now we know that (if p is indeed prime) there is an integer
235 * k, 0 <= k < 2^e, such that
237 * a^q * y^k == 1 (mod p).
239 * As a^q is a square and y is not, k must be even.
240 * q+1 is even, too, so there is an element
242 * X := a^((q+1)/2) * y^(k/2),
246 * X^2 = a^q * a * y^k
249 * so it is the square root that we are looking for.
252 /* t := (q-1)/2 (note that q is odd) */
253 if (!BN_rshift1(t, q))
256 /* x := a^((q-1)/2) */
257 if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
258 if (!BN_nnmod(t, A, p, ctx))
261 /* special case: a == 0 (mod p) */
265 } else if (!BN_one(x))
268 if (!BN_mod_exp(x, A, t, p, ctx))
271 /* special case: a == 0 (mod p) */
278 /* b := a*x^2 (= a^q) */
279 if (!BN_mod_sqr(b, x, p, ctx))
281 if (!BN_mod_mul(b, b, A, p, ctx))
284 /* x := a*x (= a^((q+1)/2)) */
285 if (!BN_mod_mul(x, x, A, p, ctx))
290 * Now b is a^q * y^k for some even k (0 <= k < 2^E
291 * where E refers to the original value of e, which we
292 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
300 if (!BN_copy(ret, x))
306 /* find smallest i such that b^(2^i) = 1 */
308 if (!BN_mod_sqr(t, b, p, ctx))
310 while (!BN_is_one(t)) {
313 ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
316 if (!BN_mod_mul(t, t, t, p, ctx))
320 /* t := y^2^(e - i - 1) */
323 for (j = e - i - 1; j > 0; j--) {
324 if (!BN_mod_sqr(t, t, p, ctx))
327 if (!BN_mod_mul(y, t, t, p, ctx))
329 if (!BN_mod_mul(x, x, t, p, ctx))
331 if (!BN_mod_mul(b, b, y, p, ctx))
339 * verify the result -- the input might have been not a square (test
343 if (!BN_mod_sqr(x, ret, p, ctx))
346 if (!err && 0 != BN_cmp(x, A)) {
347 ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);