Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / dropbear / libtommath / bn_mp_sqrt.c
blob4449625ae572c3e88a2028dd09fa3ec3d4eb92a4
1 #include <tommath.h>
2 #ifdef BN_MP_SQRT_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* this function is less generic than mp_n_root, simpler and faster */
19 int mp_sqrt(mp_int *arg, mp_int *ret)
21 int res;
22 mp_int t1,t2;
24 /* must be positive */
25 if (arg->sign == MP_NEG) {
26 return MP_VAL;
29 /* easy out */
30 if (mp_iszero(arg) == MP_YES) {
31 mp_zero(ret);
32 return MP_OKAY;
35 if ((res = mp_init_copy(&t1, arg)) != MP_OKAY) {
36 return res;
39 if ((res = mp_init(&t2)) != MP_OKAY) {
40 goto E2;
43 /* First approx. (not very bad for large arg) */
44 mp_rshd (&t1,t1.used/2);
46 /* t1 > 0 */
47 if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
48 goto E1;
50 if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
51 goto E1;
53 if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
54 goto E1;
56 /* And now t1 > sqrt(arg) */
57 do {
58 if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
59 goto E1;
61 if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
62 goto E1;
64 if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
65 goto E1;
67 /* t1 >= sqrt(arg) >= t2 at this point */
68 } while (mp_cmp_mag(&t1,&t2) == MP_GT);
70 mp_exch(&t1,ret);
72 E1: mp_clear(&t2);
73 E2: mp_clear(&t1);
74 return res;
77 #endif
79 /* $Source: /cvs/libtom/libtommath/bn_mp_sqrt.c,v $ */
80 /* $Revision: 1.3 $ */
81 /* $Date: 2006/03/31 14:18:44 $ */