2 * Copyright 2000-2022 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, otherwise an error or
18 * an incorrect "result" will be returned.
24 BIGNUM *A, *b, *q, *t, *x, *y;
28 if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
29 if (BN_abs_is_word(p, 2)) {
34 if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
43 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
47 if (BN_is_zero(a) || BN_is_one(a)) {
52 if (!BN_set_word(ret, BN_is_one(a))) {
78 if (!BN_nnmod(A, a, p, ctx))
81 /* now write |p| - 1 as 2^e*q where q is odd */
83 while (!BN_is_bit_set(p, e))
85 /* we'll set q later (if needed) */
89 * The easy case: (|p|-1)/2 is odd, so 2 has an inverse
90 * modulo (|p|-1)/2, and square roots can be computed
91 * directly by modular exponentiation.
93 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
94 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
96 if (!BN_rshift(q, p, 2))
99 if (!BN_add_word(q, 1))
101 if (!BN_mod_exp(ret, A, q, p, ctx))
111 * In this case 2 is always a non-square since
112 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
113 * So if a really is a square, then 2*a is a non-square.
115 * b := (2*a)^((|p|-5)/8),
118 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
124 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
125 * = a^2 * b^2 * (-2*i)
130 * (This is due to A.O.L. Atkin,
131 * Subject: Square Roots and Cognate Matters modulo p=8n+5.
132 * URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026
137 if (!BN_mod_lshift1_quick(t, A, p))
140 /* b := (2*a)^((|p|-5)/8) */
141 if (!BN_rshift(q, p, 3))
144 if (!BN_mod_exp(b, t, q, p, ctx))
148 if (!BN_mod_sqr(y, b, p, ctx))
151 /* t := (2*a)*b^2 - 1 */
152 if (!BN_mod_mul(t, t, y, p, ctx))
154 if (!BN_sub_word(t, 1))
158 if (!BN_mod_mul(x, A, b, p, ctx))
160 if (!BN_mod_mul(x, x, t, p, ctx))
163 if (!BN_copy(ret, x))
170 * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
171 * find some y that is not a square.
174 goto end; /* use 'q' as temp */
179 * For efficiency, try small numbers first; if this fails, try random
183 if (!BN_set_word(y, i))
186 if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx))
188 if (BN_ucmp(y, p) >= 0) {
189 if (!(p->neg ? BN_add : BN_sub) (y, y, p))
192 /* now 0 <= y < |p| */
194 if (!BN_set_word(y, i))
198 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
203 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
207 while (r == 1 && ++i < 82);
211 * Many rounds and still no non-square -- this is more likely a bug
212 * than just bad luck. Even if p is not prime, we should have found
213 * some y such that r == -1.
215 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
219 /* Here's our actual 'q': */
220 if (!BN_rshift(q, q, e))
224 * Now that we have some non-square, we can find an element of order 2^e
225 * by computing its q'th power.
227 if (!BN_mod_exp(y, y, q, p, ctx))
230 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
235 * Now we know that (if p is indeed prime) there is an integer
236 * k, 0 <= k < 2^e, such that
238 * a^q * y^k == 1 (mod p).
240 * As a^q is a square and y is not, k must be even.
241 * q+1 is even, too, so there is an element
243 * X := a^((q+1)/2) * y^(k/2),
247 * X^2 = a^q * a * y^k
250 * so it is the square root that we are looking for.
253 /* t := (q-1)/2 (note that q is odd) */
254 if (!BN_rshift1(t, q))
257 /* x := a^((q-1)/2) */
258 if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
259 if (!BN_nnmod(t, A, p, ctx))
262 /* special case: a == 0 (mod p) */
266 } else if (!BN_one(x))
269 if (!BN_mod_exp(x, A, t, p, ctx))
272 /* special case: a == 0 (mod p) */
279 /* b := a*x^2 (= a^q) */
280 if (!BN_mod_sqr(b, x, p, ctx))
282 if (!BN_mod_mul(b, b, A, p, ctx))
285 /* x := a*x (= a^((q+1)/2)) */
286 if (!BN_mod_mul(x, x, A, p, ctx))
291 * Now b is a^q * y^k for some even k (0 <= k < 2^E
292 * where E refers to the original value of e, which we
293 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
301 if (!BN_copy(ret, x))
307 /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
308 for (i = 1; i < e; i++) {
310 if (!BN_mod_sqr(t, b, p, ctx))
314 if (!BN_mod_mul(t, t, t, p, ctx))
320 /* If not found, a is not a square or p is not prime. */
322 ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
326 /* t := y^2^(e - i - 1) */
329 for (j = e - i - 1; j > 0; j--) {
330 if (!BN_mod_sqr(t, t, p, ctx))
333 if (!BN_mod_mul(y, t, t, p, ctx))
335 if (!BN_mod_mul(x, x, t, p, ctx))
337 if (!BN_mod_mul(b, b, y, p, ctx))
345 * verify the result -- the input might have been not a square (test
349 if (!BN_mod_sqr(x, ret, p, ctx))
352 if (!err && 0 != BN_cmp(x, A)) {
353 ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);