chiark / gitweb /
ec-bin (ec_binproj): Make curve setup faster.
[catacomb] / mp-sqrt.c
1 /* -*-c-*-
2  *
3  * $Id$
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 /*----- Header files ------------------------------------------------------*/
31
32 #include "mp.h"
33
34 /*----- Main code ---------------------------------------------------------*/
35
36 /* --- @mp_sqrt@ --- *
37  *
38  * Arguments:   @mp *d@ = pointer to destination integer
39  *              @mp *a@ = (nonnegative) integer to take square root of
40  *
41  * Returns:     The largest integer %$x$% such that %$x^2 \le a$%.
42  *
43  * Use:         Computes integer square roots.
44  *
45  *              The current implementation isn't very good: it uses the
46  *              Newton-Raphson method to find an approximation to %$a$%.  If
47  *              there's any demand for a better version, I'll write one.
48  */
49
50 mp *mp_sqrt(mp *d, mp *a)
51 {
52   unsigned long z;
53   mp *q = MP_NEW, *r = MP_NEW;
54
55   /* --- Sanity preservation --- */
56
57   assert(!MP_NEGP(a));
58
59   /* --- Deal with trivial cases --- */
60
61   MP_SHRINK(a);
62   if (MP_ZEROP(a)) {
63     mp_drop(d);
64     return (MP_ZERO);
65   }
66
67   /* --- Find an initial guess of about the right size --- */
68
69   z = mp_bits(a);
70   z >>= 1;
71   mp_copy(a);
72   d = mp_lsr(d, a, z);
73
74   /* --- Main approximation --- *
75    *
76    * We use the Newton-Raphson recurrence relation
77    *
78    *   %$x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$%
79    *
80    * We inspect the term %$q = x^2 - a$% to see when to stop.  Increasing
81    * %$x$% is pointless when %$-q < 2 x + 1$%.
82    */
83
84   for (;;) {
85     q = mp_sqr(q, d);
86     q = mp_sub(q, q, a);
87     if (MP_ZEROP(q))
88       break;
89     if (MP_NEGP(q)) {
90       r = mp_lsl(r, d, 1);
91       r->f |= MP_NEG;
92       if (MP_CMP(q, >=, r))
93         break;
94     }
95     mp_div(&r, &q, q, d);
96     r = mp_lsr(r, r, 1);
97     if (r->v == r->vl)
98       d = mp_sub(d, d, MP_ONE);
99     else
100       d = mp_sub(d, d, r);
101   }
102
103   /* --- Finished, at last --- */
104
105   mp_drop(a);
106   mp_drop(q);
107   mp_drop(r);
108   return (d);
109 }
110
111 /*----- Test rig ----------------------------------------------------------*/
112
113 #ifdef TEST_RIG
114
115 #include <mLib/testrig.h>
116
117 static int verify(dstr *v)
118 {
119   mp *a = *(mp **)v[0].buf;
120   mp *qq = *(mp **)v[1].buf;
121   mp *q = mp_sqrt(MP_NEW, a);
122   int ok = 1;
123
124   if (!MP_EQ(q, qq)) {
125     ok = 0;
126     fputs("\n*** sqrt failed", stderr);
127     fputs("\n*** a      = ", stderr); mp_writefile(a, stderr, 10);
128     fputs("\n*** result = ", stderr); mp_writefile(q, stderr, 10);
129     fputs("\n*** expect = ", stderr); mp_writefile(qq, stderr, 10);
130     fputc('\n', stderr);
131   }
132
133   mp_drop(a);
134   mp_drop(q);
135   mp_drop(qq);
136   assert(mparena_count(MPARENA_GLOBAL) == 0);
137
138   return (ok);
139 }
140
141 static test_chunk tests[] = {
142   { "sqrt", verify, { &type_mp, &type_mp, 0 } },
143   { 0, 0, { 0 } },
144 };
145
146 int main(int argc, char *argv[])
147 {
148   sub_init();
149   test_run(argc, argv, tests, SRCDIR "/tests/mp");
150   return (0);
151 }
152
153 #endif
154
155 /*----- That's all, folks -------------------------------------------------*/