Renamed from `rsa-decrypt', since the name was no longer appropriate.
[u/mdw/catacomb] / mp-sqrt.c
1 /* -*-c-*-
2 *
3 * $Id: mp-sqrt.c,v 1.1 2000/06/22 19:01:44 mdw Exp $
4 *
5 * Compute integer square roots
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mp-sqrt.c,v $
33 * Revision 1.1 2000/06/22 19:01:44 mdw
34 * Compute (approximations to) integer square roots.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include "mp.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 /* --- @mp_sqrt@ --- *
45 *
46 * Arguments: @mp *d@ = pointer to destination integer
47 * @mp *a@ = (nonnegative) integer to take square root of
48 *
49 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
50 *
51 * Use: Computes integer square roots.
52 *
53 * The current implementation isn't very good: it uses the
54 * Newton-Raphson method to find an approximation to %$a$%. If
55 * there's any demand for a better version, I'll write one.
56 */
57
58 mp *mp_sqrt(mp *d, mp *a)
59 {
60 unsigned long z;
61 mp *q = MP_NEW, *r = MP_NEW;
62
63 /* --- Sanity preservation --- */
64
65 assert(((void)"imaginary root in mp_sqrt", !(a->f & MP_NEG)));
66
67 /* --- Deal with trivial cases --- */
68
69 MP_SHRINK(a);
70 if (a->v == a->vl) {
71 if (d)
72 mp_drop(d);
73 return (MP_ZERO);
74 }
75
76 /* --- Find an initial guess of about the right size --- */
77
78 z = mp_bits(a);
79 z >>= 1;
80 mp_copy(a);
81 d = mp_lsr(d, a, z);
82 mp_drop(a);
83
84 /* --- Main approximation --- *
85 *
86 * We use the Newton-Raphson recurrence relation
87 *
88 * %$x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$%
89 *
90 * We inspect the term %$q = x^2 - a$% to see when to stop. Increasing
91 * %$x$% is pointless when %$-q < 2 x + 1$%.
92 */
93
94 for (;;) {
95 q = mp_sqr(q, d);
96 q = mp_sub(q, q, a);
97 if (q->v == q->vl)
98 break;
99 if (q->f & MP_NEG) {
100 r = mp_lsl(r, d, 1);
101 r->f |= MP_NEG;
102 if (MP_CMP(q, <=, r))
103 break;
104 }
105 mp_div(&r, &q, q, d);
106 r = mp_lsr(r, r, 1);
107 if (r->v == r->vl)
108 d = mp_sub(d, d, MP_ONE);
109 else
110 d = mp_sub(d, d, r);
111 }
112
113 /* --- Finished, at last --- */
114
115 mp_drop(q);
116 if (r)
117 mp_drop(r);
118 return (d);
119 }
120
121 /*----- Test rig ----------------------------------------------------------*/
122
123 #ifdef TEST_RIG
124
125 #include <mLib/testrig.h>
126
127 static int verify(dstr *v)
128 {
129 mp *a = *(mp **)v[0].buf;
130 mp *qq = *(mp **)v[1].buf;
131 mp *q = mp_sqrt(MP_NEW, a);
132 int ok = 1;
133
134 if (MP_CMP(q, !=, qq)) {
135 ok = 0;
136 fputs("\n*** sqrt failed", stderr);
137 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
138 fputs("\n*** result = ", stderr); mp_writefile(q, stderr, 10);
139 fputs("\n*** expect = ", stderr); mp_writefile(qq, stderr, 10);
140 fputc('\n', stderr);
141 }
142
143 mp_drop(a);
144 mp_drop(q);
145 mp_drop(qq);
146 assert(mparena_count(MPARENA_GLOBAL) == 0);
147
148 return (ok);
149 }
150
151 static test_chunk tests[] = {
152 { "sqrt", verify, { &type_mp, &type_mp, 0 } },
153 { 0, 0, { 0 } },
154 };
155
156 int main(int argc, char *argv[])
157 {
158 sub_init();
159 test_run(argc, argv, tests, SRCDIR "/tests/mp");
160 return (0);
161 }
162
163 #endif
164
165 /*----- That's all, folks -------------------------------------------------*/