Standard curves and curve checking.
[u/mdw/catacomb] / mp-sqrt.c
CommitLineData
65397be7 1/* -*-c-*-
2 *
432c4e18 3 * $Id: mp-sqrt.c,v 1.4 2004/03/27 17:54:11 mdw Exp $
65397be7 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 $
432c4e18 33 * Revision 1.4 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
35 *
f1140c41 36 * Revision 1.3 2001/02/03 12:00:29 mdw
37 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
38 * it. Note that the macro version @MP_DROP@ doesn't do this.
39 *
4b536f42 40 * Revision 1.2 2000/10/08 12:02:21 mdw
41 * Use @MP_EQ@ instead of @MP_CMP@.
42 *
65397be7 43 * Revision 1.1 2000/06/22 19:01:44 mdw
44 * Compute (approximations to) integer square roots.
45 *
46 */
47
48/*----- Header files ------------------------------------------------------*/
49
50#include "mp.h"
51
52/*----- Main code ---------------------------------------------------------*/
53
54/* --- @mp_sqrt@ --- *
55 *
56 * Arguments: @mp *d@ = pointer to destination integer
57 * @mp *a@ = (nonnegative) integer to take square root of
58 *
59 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
60 *
61 * Use: Computes integer square roots.
62 *
63 * The current implementation isn't very good: it uses the
64 * Newton-Raphson method to find an approximation to %$a$%. If
65 * there's any demand for a better version, I'll write one.
66 */
67
68mp *mp_sqrt(mp *d, mp *a)
69{
70 unsigned long z;
71 mp *q = MP_NEW, *r = MP_NEW;
72
73 /* --- Sanity preservation --- */
74
75 assert(((void)"imaginary root in mp_sqrt", !(a->f & MP_NEG)));
76
77 /* --- Deal with trivial cases --- */
78
79 MP_SHRINK(a);
80 if (a->v == a->vl) {
f1140c41 81 mp_drop(d);
65397be7 82 return (MP_ZERO);
83 }
84
85 /* --- Find an initial guess of about the right size --- */
86
87 z = mp_bits(a);
88 z >>= 1;
89 mp_copy(a);
90 d = mp_lsr(d, a, z);
65397be7 91
92 /* --- Main approximation --- *
93 *
94 * We use the Newton-Raphson recurrence relation
95 *
96 * %$x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$%
97 *
98 * We inspect the term %$q = x^2 - a$% to see when to stop. Increasing
99 * %$x$% is pointless when %$-q < 2 x + 1$%.
100 */
101
102 for (;;) {
103 q = mp_sqr(q, d);
104 q = mp_sub(q, q, a);
105 if (q->v == q->vl)
106 break;
107 if (q->f & MP_NEG) {
108 r = mp_lsl(r, d, 1);
109 r->f |= MP_NEG;
110 if (MP_CMP(q, <=, r))
111 break;
112 }
113 mp_div(&r, &q, q, d);
114 r = mp_lsr(r, r, 1);
115 if (r->v == r->vl)
116 d = mp_sub(d, d, MP_ONE);
117 else
118 d = mp_sub(d, d, r);
119 }
120
121 /* --- Finished, at last --- */
122
432c4e18 123 mp_drop(a);
65397be7 124 mp_drop(q);
f1140c41 125 mp_drop(r);
65397be7 126 return (d);
127}
128
129/*----- Test rig ----------------------------------------------------------*/
130
131#ifdef TEST_RIG
132
133#include <mLib/testrig.h>
134
135static int verify(dstr *v)
136{
137 mp *a = *(mp **)v[0].buf;
138 mp *qq = *(mp **)v[1].buf;
139 mp *q = mp_sqrt(MP_NEW, a);
140 int ok = 1;
141
4b536f42 142 if (!MP_EQ(q, qq)) {
65397be7 143 ok = 0;
144 fputs("\n*** sqrt failed", stderr);
145 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
146 fputs("\n*** result = ", stderr); mp_writefile(q, stderr, 10);
147 fputs("\n*** expect = ", stderr); mp_writefile(qq, stderr, 10);
148 fputc('\n', stderr);
149 }
150
151 mp_drop(a);
152 mp_drop(q);
153 mp_drop(qq);
154 assert(mparena_count(MPARENA_GLOBAL) == 0);
155
156 return (ok);
157}
158
159static test_chunk tests[] = {
160 { "sqrt", verify, { &type_mp, &type_mp, 0 } },
161 { 0, 0, { 0 } },
162};
163
164int main(int argc, char *argv[])
165{
166 sub_init();
167 test_run(argc, argv, tests, SRCDIR "/tests/mp");
168 return (0);
169}
170
171#endif
172
173/*----- That's all, folks -------------------------------------------------*/