Add an internal-representation no-op function.
[u/mdw/catacomb] / mp-modsqrt.c
CommitLineData
9f11b970 1/* -*-c-*-
2 *
f1140c41 3 * $Id: mp-modsqrt.c,v 1.3 2001/02/03 12:00:29 mdw Exp $
9f11b970 4 *
5 * Compute square roots modulo a prime
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-modsqrt.c,v $
f1140c41 33 * Revision 1.3 2001/02/03 12:00:29 mdw
34 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
35 * it. Note that the macro version @MP_DROP@ doesn't do this.
36 *
4b536f42 37 * Revision 1.2 2000/10/08 12:02:21 mdw
38 * Use @MP_EQ@ instead of @MP_CMP@.
39 *
9f11b970 40 * Revision 1.1 2000/06/22 19:01:31 mdw
41 * Compute square roots in a prime field.
42 *
43 */
44
45/*----- Header files ------------------------------------------------------*/
46
47#include "fibrand.h"
48#include "grand.h"
49#include "mp.h"
50#include "mpmont.h"
51#include "mprand.h"
52
53/*----- Main code ---------------------------------------------------------*/
54
55/* --- @mp_modsqrt@ --- *
56 *
57 * Arguments: @mp *d@ = destination integer
58 * @mp *a@ = source integer
59 * @mp *p@ = modulus (must be prime)
60 *
61 * Returns: If %$a$% is a quadratic residue, a square root of %$a$%; else
62 * a null pointer.
63 *
64 * Use: Returns an integer %$x$% such that %$x^2 \equiv a \pmod{p}$%,
65 * if one exists; else a null pointer. This function will not
66 * work if %$p$% is composite: you must factor the modulus, take
67 * a square root mod each factor, and recombine the results
68 * using the Chinese Remainder Theorem.
69 */
70
71mp *mp_modsqrt(mp *d, mp *a, mp *p)
72{
73 mpmont mm;
74 mp *t;
75 size_t s;
76 mp *b;
77 mp *ainv;
78 mp *c, *r;
79 size_t i, j;
80 mp *dd, *mone;
81
82 /* --- Cope if %$a \not\in Q_p$% --- */
83
84 if (mp_jacobi(a, p) != 1) {
f1140c41 85 mp_drop(d);
9f11b970 86 return (0);
87 }
88
89 /* --- Choose some quadratic non-residue --- */
90
91 {
92 grand *g = fibrand_create(0);
93
94 b = MP_NEW;
95 do
96 b = mprand_range(b, p, g, 0);
97 while (mp_jacobi(b, p) != -1);
98 g->ops->destroy(g);
99 }
100
101 /* --- Find the inverse of %$a$% --- */
102
103 ainv = MP_NEW;
104 mp_gcd(0, &ainv, 0, a, p);
105
106 /* --- Split %$p - 1$% into a power of two and an odd number --- */
107
108 t = mp_sub(MP_NEW, p, MP_ONE);
109 t = mp_odd(t, t, &s);
110
111 /* --- Now to really get going --- */
112
113 mpmont_create(&mm, p);
114 c = mpmont_expr(&mm, b, b, t);
115 t = mp_add(t, t, MP_ONE);
116 t = mp_lsr(t, t, 1);
117 r = mpmont_expr(&mm, t, a, t);
118 ainv = mpmont_mul(&mm, ainv, ainv, mm.r2);
119
120 mone = mp_sub(MP_NEW, p, mm.r);
121
122 dd = MP_NEW;
123
124 for (i = 1; i < s; i++) {
125
126 /* --- Compute %$d_0 = r^2a^{-1}$% --- */
127
128 dd = mp_sqr(dd, r);
129 dd = mpmont_reduce(&mm, dd, dd);
130 dd = mpmont_mul(&mm, dd, dd, ainv);
131
132 /* --- Now %$d = d_0^{s - i - 1}$% --- */
133
134 for (j = i; j < s - 1; j++) {
135 dd = mp_sqr(dd, dd);
136 dd = mpmont_reduce(&mm, dd, dd);
137 }
138
139 /* --- Fiddle at the end --- */
140
4b536f42 141 if (MP_EQ(dd, mone))
9f11b970 142 r = mpmont_mul(&mm, r, r, c);
143 c = mp_sqr(c, c);
144 c = mpmont_reduce(&mm, c, c);
145 }
146
147 /* --- Done, so tidy up --- */
148
149 d = mpmont_reduce(&mm, d, r);
150 mp_drop(ainv);
151 mp_drop(r); mp_drop(c);
f1140c41 152 mp_drop(dd);
9f11b970 153 mp_drop(mone);
154 mpmont_destroy(&mm);
155
156 return (d);
157}
158
159/*----- Test rig ----------------------------------------------------------*/
160
161#ifdef TEST_RIG
162
163#include <mLib/testrig.h>
164
165static int verify(dstr *v)
166{
167 mp *a = *(mp **)v[0].buf;
168 mp *p = *(mp **)v[1].buf;
169 mp *rr = *(mp **)v[2].buf;
170 mp *r = mp_modsqrt(MP_NEW, a, p);
171 int ok = 0;
172
173 if (!r)
174 ok = 0;
4b536f42 175 else if (MP_EQ(r, rr))
9f11b970 176 ok = 1;
177 else {
178 r = mp_sub(r, p, r);
4b536f42 179 if (MP_EQ(r, rr))
9f11b970 180 ok = 1;
181 }
182
183 if (!ok) {
184 fputs("\n*** fail\n", stderr);
185 fputs("a = ", stderr); mp_writefile(a, stderr, 10); fputc('\n', stderr);
186 fputs("p = ", stderr); mp_writefile(p, stderr, 10); fputc('\n', stderr);
187 if (r) {
188 fputs("r = ", stderr);
189 mp_writefile(r, stderr, 10);
190 fputc('\n', stderr);
191 } else
192 fputs("r = <undef>\n", stderr);
193 fputs("rr = ", stderr); mp_writefile(rr, stderr, 10); fputc('\n', stderr);
194 ok = 0;
195 }
196
197 mp_drop(a);
198 mp_drop(p);
f1140c41 199 mp_drop(r);
9f11b970 200 mp_drop(rr);
201 assert(mparena_count(MPARENA_GLOBAL) == 0);
202 return (ok);
203}
204
205static test_chunk tests[] = {
206 { "modsqrt", verify, { &type_mp, &type_mp, &type_mp, 0 } },
207 { 0, 0, { 0 } }
208};
209
210int main(int argc, char *argv[])
211{
212 sub_init();
213 test_run(argc, argv, tests, SRCDIR "/tests/mp");
214 return (0);
215}
216
217#endif
218
219/*----- That's all, folks -------------------------------------------------*/