1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 #ifndef NO_FLOATING_POINT
9 #if (MP_DIGIT_BIT != 28) || (FLT_RADIX != 2) || (DBL_MANT_DIG != 53) || (DBL_MAX_EXP != 1024)
10 #define NO_FLOATING_POINT
14 /* this function is less generic than mp_n_root, simpler and faster */
15 mp_err mp_sqrt(const mp_int *arg, mp_int *ret)
19 #ifndef NO_FLOATING_POINT
25 /* must be positive */
26 if (arg->sign == MP_NEG) {
31 if (MP_IS_ZERO(arg)) {
36 #ifndef NO_FLOATING_POINT
38 i = (arg->used / 2) - 1;
40 if ((err = mp_init_size(&t1, i+2)) != MP_OKAY) {
44 if ((err = mp_init(&t2)) != MP_OKAY) {
48 for (k = 0; k < i; ++k) {
49 t1.dp[k] = (mp_digit) 0;
52 /* Estimate the square root using the hardware floating point unit. */
55 for (k = arg->used-1; k >= j; --k) {
56 d = ldexp(d, MP_DIGIT_BIT) + (double)(arg->dp[k]);
60 * At this point, d is the nearest floating point number to the most
61 * significant 1 or 2 mp_digits of arg. Extract its square root.
66 /* dig is the most significant mp_digit of the square root */
68 dig = (mp_digit) ldexp(d, -MP_DIGIT_BIT);
71 * If the most significant digit is nonzero, find the next digit down
72 * by subtracting MP_DIGIT_BIT times thie most significant digit.
73 * Subtract one from the result so that our initial estimate is always
79 d -= ldexp((double) dig, MP_DIGIT_BIT);
82 t1.dp[i] = ((mp_digit) d) - 1;
85 t1.dp[i] = MP_DIGIT_MAX;
89 t1.dp[i] = ((mp_digit) d) - 1;
94 if ((err = mp_init_copy(&t1, arg)) != MP_OKAY) {
98 if ((err = mp_init(&t2)) != MP_OKAY) {
102 /* First approx. (not very bad for large arg) */
103 mp_rshd(&t1, t1.used/2);
108 if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
111 if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
114 if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) {
117 /* And now t1 > sqrt(arg) */
119 if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
122 if ((err = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
125 if ((err = mp_div_2(&t1, &t1)) != MP_OKAY) {
128 /* t1 >= sqrt(arg) >= t2 at this point */
129 } while (mp_cmp_mag(&t1, &t2) == MP_GT);