comparison libtommath/bn_mp_sqrt.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_SQRT_C 2 #ifdef BN_MP_SQRT_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* this function is less generic than mp_n_root, simpler and faster */ 15 /* this function is less generic than mp_n_root, simpler and faster */
19 int mp_sqrt(mp_int *arg, mp_int *ret) 16 int mp_sqrt(const mp_int *arg, mp_int *ret)
20 { 17 {
21 int res; 18 int res;
22 mp_int t1,t2; 19 mp_int t1, t2;
23 20
24 /* must be positive */ 21 /* must be positive */
25 if (arg->sign == MP_NEG) { 22 if (arg->sign == MP_NEG) {
26 return MP_VAL; 23 return MP_VAL;
27 } 24 }
28 25
29 /* easy out */ 26 /* easy out */
30 if (mp_iszero(arg) == MP_YES) { 27 if (mp_iszero(arg) == MP_YES) {
31 mp_zero(ret); 28 mp_zero(ret);
32 return MP_OKAY; 29 return MP_OKAY;
33 } 30 }
34 31
35 if ((res = mp_init_copy(&t1, arg)) != MP_OKAY) { 32 if ((res = mp_init_copy(&t1, arg)) != MP_OKAY) {
36 return res; 33 return res;
37 } 34 }
38 35
39 if ((res = mp_init(&t2)) != MP_OKAY) { 36 if ((res = mp_init(&t2)) != MP_OKAY) {
40 goto E2; 37 goto E2;
41 } 38 }
42 39
43 /* First approx. (not very bad for large arg) */ 40 /* First approx. (not very bad for large arg) */
44 mp_rshd (&t1,t1.used/2); 41 mp_rshd(&t1, t1.used/2);
45 42
46 /* t1 > 0 */ 43 /* t1 > 0 */
47 if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { 44 if ((res = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
48 goto E1;
49 }
50 if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
51 goto E1;
52 }
53 if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
54 goto E1;
55 }
56 /* And now t1 > sqrt(arg) */
57 do {
58 if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
59 goto E1; 45 goto E1;
60 } 46 }
61 if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) { 47 if ((res = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
62 goto E1; 48 goto E1;
63 } 49 }
64 if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) { 50 if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) {
65 goto E1; 51 goto E1;
66 } 52 }
67 /* t1 >= sqrt(arg) >= t2 at this point */ 53 /* And now t1 > sqrt(arg) */
68 } while (mp_cmp_mag(&t1,&t2) == MP_GT); 54 do {
55 if ((res = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
56 goto E1;
57 }
58 if ((res = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
59 goto E1;
60 }
61 if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) {
62 goto E1;
63 }
64 /* t1 >= sqrt(arg) >= t2 at this point */
65 } while (mp_cmp_mag(&t1, &t2) == MP_GT);
69 66
70 mp_exch(&t1,ret); 67 mp_exch(&t1, ret);
71 68
72 E1: mp_clear(&t2); 69 E1:
73 E2: mp_clear(&t1); 70 mp_clear(&t2);
74 return res; 71 E2:
72 mp_clear(&t1);
73 return res;
75 } 74 }
76 75
77 #endif 76 #endif
78 77
79 /* ref: $Format:%D$ */ 78 /* ref: HEAD -> master, tag: v1.1.0 */
80 /* git commit: $Format:%H$ */ 79 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
81 /* commit time: $Format:%ai$ */ 80 /* commit time: 2019-01-28 20:32:32 +0100 */