xref: /openbsd/lib/libcrypto/bn/bn_isqrt.c (revision a29d9d67)
1 /*	$OpenBSD: bn_isqrt.c,v 1.10 2023/06/04 17:28:35 tb Exp $ */
2 /*
3  * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include <openssl/bn.h>
22 #include <openssl/err.h>
23 
24 #include "bn_local.h"
25 #include "crypto_internal.h"
26 
27 /*
28  * Calculate integer square root of |n| using a variant of Newton's method.
29  *
30  * Returns the integer square root of |n| in the caller-provided |out_sqrt|;
31  * |*out_perfect| is set to 1 if and only if |n| is a perfect square.
32  * One of |out_sqrt| and |out_perfect| can be NULL; |in_ctx| can be NULL.
33  *
34  * Returns 0 on error, 1 on success.
35  *
36  * Adapted from pure Python describing cpython's math.isqrt(), without bothering
37  * with any of the optimizations in the C code. A correctness proof is here:
38  * https://github.com/mdickinson/snippets/blob/master/proofs/isqrt/src/isqrt.lean
39  * The comments in the Python code also give a rather detailed proof.
40  */
41 
42 int
bn_isqrt(BIGNUM * out_sqrt,int * out_perfect,const BIGNUM * n,BN_CTX * in_ctx)43 bn_isqrt(BIGNUM *out_sqrt, int *out_perfect, const BIGNUM *n, BN_CTX *in_ctx)
44 {
45 	BN_CTX *ctx = NULL;
46 	BIGNUM *a, *b;
47 	int c, d, e, s;
48 	int cmp, perfect;
49 	int ret = 0;
50 
51 	if (out_perfect == NULL && out_sqrt == NULL) {
52 		BNerror(ERR_R_PASSED_NULL_PARAMETER);
53 		goto err;
54 	}
55 
56 	if (BN_is_negative(n)) {
57 		BNerror(BN_R_INVALID_RANGE);
58 		goto err;
59 	}
60 
61 	if ((ctx = in_ctx) == NULL)
62 		ctx = BN_CTX_new();
63 	if (ctx == NULL)
64 		goto err;
65 
66 	BN_CTX_start(ctx);
67 
68 	if ((a = BN_CTX_get(ctx)) == NULL)
69 		goto err;
70 	if ((b = BN_CTX_get(ctx)) == NULL)
71 		goto err;
72 
73 	if (BN_is_zero(n)) {
74 		perfect = 1;
75 		BN_zero(a);
76 		goto done;
77 	}
78 
79 	if (!BN_one(a))
80 		goto err;
81 
82 	c = (BN_num_bits(n) - 1) / 2;
83 	d = 0;
84 
85 	/* Calculate s = floor(log(c)). */
86 	if (!BN_set_word(b, c))
87 		goto err;
88 	s = BN_num_bits(b) - 1;
89 
90 	/*
91 	 * By definition, the loop below is run <= floor(log(log(n))) times.
92 	 * Comments in the cpython code establish the loop invariant that
93 	 *
94 	 *	(a - 1)^2 < n / 4^(c - d) < (a + 1)^2
95 	 *
96 	 * holds true in every iteration. Once this is proved via induction,
97 	 * correctness of the algorithm is easy.
98 	 *
99 	 * Roughly speaking, A = (a << (d - e)) is used for one Newton step
100 	 * "a = (A >> 1) + (m >> 1) / A" approximating m = (n >> 2 * (c - d)).
101 	 */
102 
103 	for (; s >= 0; s--) {
104 		e = d;
105 		d = c >> s;
106 
107 		if (!BN_rshift(b, n, 2 * c - d - e + 1))
108 			goto err;
109 
110 		if (!BN_div_ct(b, NULL, b, a, ctx))
111 			goto err;
112 
113 		if (!BN_lshift(a, a, d - e - 1))
114 			goto err;
115 
116 		if (!BN_add(a, a, b))
117 			goto err;
118 	}
119 
120 	/*
121 	 * The loop invariant implies that either a or a - 1 is isqrt(n).
122 	 * Figure out which one it is. The invariant also implies that for
123 	 * a perfect square n, a must be the square root.
124 	 */
125 
126 	if (!BN_sqr(b, a, ctx))
127 		goto err;
128 
129 	/* If a^2 > n, we must have isqrt(n) == a - 1. */
130 	if ((cmp = BN_cmp(b, n)) > 0) {
131 		if (!BN_sub_word(a, 1))
132 			goto err;
133 	}
134 
135 	perfect = cmp == 0;
136 
137  done:
138 	if (out_perfect != NULL)
139 		*out_perfect = perfect;
140 
141 	if (out_sqrt != NULL) {
142 		if (!bn_copy(out_sqrt, a))
143 			goto err;
144 	}
145 
146 	ret = 1;
147 
148  err:
149 	BN_CTX_end(ctx);
150 
151 	if (ctx != in_ctx)
152 		BN_CTX_free(ctx);
153 
154 	return ret;
155 }
156 
157 /*
158  * is_square_mod_N[r % N] indicates whether r % N has a square root modulo N.
159  * The tables are generated in regress/lib/libcrypto/bn/bn_isqrt.c.
160  */
161 
162 const uint8_t is_square_mod_11[] = {
163 	1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0,
164 };
165 CTASSERT(sizeof(is_square_mod_11) == 11);
166 
167 const uint8_t is_square_mod_63[] = {
168 	1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
169 	1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0,
170 	0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
171 	0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
172 };
173 CTASSERT(sizeof(is_square_mod_63) == 63);
174 
175 const uint8_t is_square_mod_64[] = {
176 	1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
177 	1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
178 	0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
179 	0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
180 };
181 CTASSERT(sizeof(is_square_mod_64) == 64);
182 
183 const uint8_t is_square_mod_65[] = {
184 	1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0,
185 	1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
186 	0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
187 	0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0,
188 	1,
189 };
190 CTASSERT(sizeof(is_square_mod_65) == 65);
191 
192 /*
193  * Determine whether n is a perfect square or not.
194  *
195  * Returns 1 on success and 0 on error. In case of success, |*out_perfect| is
196  * set to 1 if and only if |n| is a perfect square.
197  */
198 
199 int
bn_is_perfect_square(int * out_perfect,const BIGNUM * n,BN_CTX * ctx)200 bn_is_perfect_square(int *out_perfect, const BIGNUM *n, BN_CTX *ctx)
201 {
202 	BN_ULONG r;
203 
204 	*out_perfect = 0;
205 
206 	if (BN_is_negative(n))
207 		return 1;
208 
209 	/*
210 	 * Before performing an expensive bn_isqrt() operation, weed out many
211 	 * obvious non-squares. See H. Cohen, "A course in computational
212 	 * algebraic number theory", Algorithm 1.7.3.
213 	 *
214 	 * The idea is that a square remains a square when reduced modulo any
215 	 * number. The moduli are chosen in such a way that a non-square has
216 	 * probability < 1% of passing the four table lookups.
217 	 */
218 
219 	/* n % 64 */
220 	r = BN_lsw(n) & 0x3f;
221 
222 	if (!is_square_mod_64[r % 64])
223 		return 1;
224 
225 	if ((r = BN_mod_word(n, 11 * 63 * 65)) == (BN_ULONG)-1)
226 		return 0;
227 
228 	if (!is_square_mod_63[r % 63] ||
229 	    !is_square_mod_65[r % 65] ||
230 	    !is_square_mod_11[r % 11])
231 		return 1;
232 
233 	return bn_isqrt(NULL, out_perfect, n, ctx);
234 }
235