1 /* crypto/bn/bn_mod.c */
2 /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * and Bodo Moeller for the OpenSSL project. */
4 /* ====================================================================
5 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
62 BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
63 /* Returns 'ret' such that
65 * using the Tonelli/Shanks algorithm (cf. Henri Cohen, "A Course
66 * in Algebraic Computational Number Theory", algorithm 1.5.1).
73 BIGNUM *b, *q, *t, *x, *y;
76 if (!BN_is_odd(p) || BN_abs_is_word(p, 1))
78 if (BN_abs_is_word(p, 2))
84 if (!BN_set_word(ret, BN_is_bit_set(a, 0)))
92 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
96 if (BN_is_zero(a) || BN_is_one(a))
102 if (!BN_set_word(ret, BN_is_one(a)))
110 #if 0 /* if BN_mod_sqrt is used with correct input, this just wastes time */
111 r = BN_kronecker(a, p, ctx);
112 if (r < -1) return NULL;
115 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
126 if (y == NULL) goto end;
130 if (ret == NULL) goto end;
132 /* now write |p| - 1 as 2^e*q where q is odd */
134 while (!BN_is_bit_set(p, e))
136 /* we'll set q later (if needed) */
140 /* The easy case: (|p|-1)/2 is odd, so 2 has an inverse
141 * modulo (|p|-1)/2, and square roots can be computed
142 * directly by modular exponentiation.
144 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
145 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
147 if (!BN_rshift(q, p, 2)) goto end;
149 if (!BN_add_word(q, 1)) goto end;
150 if (!BN_mod_exp(ret, a, q, p, ctx)) goto end;
159 * In this case 2 is always a non-square since
160 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
161 * So if a really is a square, then 2*a is a non-square.
163 * b := (2*a)^((|p|-5)/8),
166 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
172 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
173 * = a^2 * b^2 * (-2*i)
178 * (This is due to A.O.L. Atkin,
179 * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
183 /* make sure that a is reduced modulo p */
184 if (a->neg || BN_ucmp(a, p) >= 0)
186 if (!BN_nnmod(x, a, p, ctx)) goto end;
187 a = x; /* use x as temporary variable */
191 if (!BN_mod_lshift1_quick(t, a, p)) goto end;
193 /* b := (2*a)^((|p|-5)/8) */
194 if (!BN_rshift(q, p, 3)) goto end;
196 if (!BN_mod_exp(b, t, q, p, ctx)) goto end;
199 if (!BN_mod_sqr(y, b, p, ctx)) goto end;
201 /* t := (2*a)*b^2 - 1*/
202 if (!BN_mod_mul(t, t, y, p, ctx)) goto end;
203 if (!BN_sub_word(t, 1)) goto end;
206 if (!BN_mod_mul(x, a, b, p, ctx)) goto end;
207 if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
209 if (!BN_copy(ret, x)) goto end;
214 /* e > 2, so we really have to use the Tonelli/Shanks algorithm.
215 * First, find some y that is not a square. */
216 if (!BN_copy(q, p)) goto end; /* use 'q' as temp */
221 /* For efficiency, try small numbers first;
222 * if this fails, try random numbers.
226 if (!BN_set_word(y, i)) goto end;
230 if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end;
231 if (BN_ucmp(y, p) >= 0)
233 if (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end;
235 /* now 0 <= y < |p| */
237 if (!BN_set_word(y, i)) goto end;
240 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
241 if (r < -1) goto end;
245 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
249 while (r == 1 && ++i < 82);
253 /* Many rounds and still no non-square -- this is more likely
254 * a bug than just bad luck.
255 * Even if p is not prime, we should have found some y
258 BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
262 /* Here's our actual 'q': */
263 if (!BN_rshift(q, q, e)) goto end;
265 /* Now that we have some non-square, we can find an element
266 * of order 2^e by computing its q'th power. */
267 if (!BN_mod_exp(y, y, q, p, ctx)) goto end;
270 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
274 /* Now we know that (if p is indeed prime) there is an integer
275 * k, 0 <= k < 2^e, such that
277 * a^q * y^k == 1 (mod p).
279 * As a^q is a square and y is not, k must be even.
280 * q+1 is even, too, so there is an element
282 * X := a^((q+1)/2) * y^(k/2),
286 * X^2 = a^q * a * y^k
289 * so it is the square root that we are looking for.
292 /* t := (q-1)/2 (note that q is odd) */
293 if (!BN_rshift1(t, q)) goto end;
295 /* x := a^((q-1)/2) */
296 if (BN_is_zero(t)) /* special case: p = 2^e + 1 */
298 if (!BN_nnmod(t, a, p, ctx)) goto end;
301 /* special case: a == 0 (mod p) */
302 if (!BN_zero(ret)) goto end;
307 if (!BN_one(x)) goto end;
311 if (!BN_mod_exp(x, a, t, p, ctx)) goto end;
314 /* special case: a == 0 (mod p) */
315 if (!BN_zero(ret)) goto end;
321 /* b := a*x^2 (= a^q) */
322 if (!BN_mod_sqr(b, x, p, ctx)) goto end;
323 if (!BN_mod_mul(b, b, a, p, ctx)) goto end;
325 /* x := a*x (= a^((q+1)/2)) */
326 if (!BN_mod_mul(x, x, a, p, ctx)) goto end;
330 /* Now b is a^q * y^k for some even k (0 <= k < 2^E
331 * where E refers to the original value of e, which we
332 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
341 if (!BN_copy(ret, x)) goto end;
347 /* find smallest i such that b^(2^i) = 1 */
349 if (!BN_mod_sqr(t, b, p, ctx)) goto end;
350 while (!BN_is_one(t))
355 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
358 if (!BN_mod_mul(t, t, t, p, ctx)) goto end;
362 /* t := y^2^(e - i - 1) */
363 if (!BN_copy(t, y)) goto end;
364 for (j = e - i - 1; j > 0; j--)
366 if (!BN_mod_sqr(t, t, p, ctx)) goto end;
368 if (!BN_mod_mul(y, t, t, p, ctx)) goto end;
369 if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
370 if (!BN_mod_mul(b, b, y, p, ctx)) goto end;
377 if (ret != NULL && ret != in)