Various changes. Kinda in the middle of it here, but it seems to work.
[catacomb-perl] / ec.xs
1 # ---?---
2 #
3 # $Id$
4 #
5 # Elliptic curves
6 #
7 # (c) 2001 Straylight/Edgeware
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of the Perl interface to Catacomb.
13 #
14 # Catacomb/Perl is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # Catacomb/Perl 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 General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with Catacomb/Perl; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28 MODULE = Catacomb PACKAGE = Catacomb::EC::Point PREFIX = ec_
29
30 EC_Point *
31 new(x = 0, y = 0, z = 0)
32 mp *x
33 mp *y
34 mp *z
35 CODE:
36 RETVAL = CREATE(EC_Point);
37 EC_CREATE(RETVAL);
38 if (x && y) {
39 RETVAL->x = MP_COPY(x);
40 RETVAL->y = MP_COPY(y);
41 if (z)
42 RETVAL->z = MP_COPY(z);
43 }
44 OUTPUT:
45 RETVAL
46
47 bool
48 atinfp(p)
49 EC_Point *p
50 CODE:
51 RETVAL = EC_ATINF(p);
52 OUTPUT:
53 RETVAL
54
55 mp *
56 x(p)
57 EC_Point *p
58 CODE:
59 RETVAL = p->x ? MP_COPY(p->x) : 0;
60 OUTPUT:
61 RETVAL
62
63 mp *
64 y(p)
65 EC_Point *p
66 CODE:
67 RETVAL = p->y ? MP_COPY(p->y) : 0;
68 OUTPUT:
69 RETVAL
70
71 mp *
72 z(p)
73 EC_Point *p
74 CODE:
75 RETVAL = p->z ? MP_COPY(p->z) : 0;
76 OUTPUT:
77 RETVAL
78
79 bool
80 ec_eq(p, q)
81 EC_Point *p
82 EC_Point *q
83
84 SV *
85 DESTROY(p)
86 EC_Point *p
87 CODE:
88 EC_DESTROY(p);
89 DESTROY(p);
90 XSRETURN_YES;
91
92 MODULE = Catacomb PACKAGE = Catacomb::EC::Curve PREFIX = ec_
93
94 EC_Curve *
95 ec_prime(me, f, a, b)
96 SV *me
97 Field *f
98 mp *a
99 mp *b
100 C_ARGS:
101 f, a, b
102
103 EC_Curve *
104 ec_primeproj(me, f, a, b)
105 SV *me
106 Field *f
107 mp *a
108 mp *b
109 C_ARGS:
110 f, a, b
111
112 EC_Curve *
113 ec_bin(me, f, a, b)
114 SV *me
115 Field *f
116 gf *a
117 gf *b
118 C_ARGS:
119 f, a, b
120
121 EC_Curve *
122 ec_binproj(me, f, a, b)
123 SV *me
124 Field *f
125 gf *a
126 gf *b
127 C_ARGS:
128 f, a, b
129
130 char *
131 name(c)
132 EC_Curve *c
133 CODE:
134 RETVAL = (char *)EC_NAME(c);
135 OUTPUT:
136 RETVAL
137
138 bool
139 ec_samep(me, c)
140 EC_Curve *me
141 EC_Curve *c
142
143 EC_Point *
144 find(c, x)
145 EC_Curve *c
146 mp *x
147 CODE:
148 RETVAL = CREATE(EC_Point);
149 if (!ec_find(c, RETVAL, x)) {
150 DESTROY(RETVAL);
151 RETVAL = 0;
152 }
153 OUTPUT:
154 RETVAL
155
156 EC_Point *
157 rand(c, r = &rand_global)
158 EC_Curve *c
159 grand *r
160 CODE:
161 RETVAL = CREATE(EC_Point);
162 ec_rand(c, RETVAL, r);
163 OUTPUT:
164 RETVAL
165
166 EC_Point *
167 neg(c, p)
168 EC_Curve *c
169 EC_Point *p
170 CODE:
171 RETVAL = CREATE(EC_Point);
172 EC_CREATE(RETVAL);
173 ec_neg(c, RETVAL, p);
174 OUTPUT:
175 RETVAL
176
177 EC_Point *
178 add(c, p, q)
179 EC_Curve *c
180 EC_Point *p
181 EC_Point *q
182 CODE:
183 RETVAL = CREATE(EC_Point);
184 EC_CREATE(RETVAL);
185 ec_add(c, RETVAL, p, q);
186 OUTPUT:
187 RETVAL
188
189 EC_Point *
190 sub(c, p, q)
191 EC_Curve *c
192 EC_Point *p
193 EC_Point *q
194 CODE:
195 RETVAL = CREATE(EC_Point);
196 EC_CREATE(RETVAL);
197 ec_sub(c, RETVAL, p, q);
198 OUTPUT:
199 RETVAL
200
201 EC_Point *
202 dbl(c, p)
203 EC_Curve *c
204 EC_Point *p
205 CODE:
206 RETVAL = CREATE(EC_Point);
207 EC_CREATE(RETVAL);
208 ec_dbl(c, RETVAL, p);
209 OUTPUT:
210 RETVAL
211
212 bool
213 ec_check(c, p)
214 EC_Curve *c
215 EC_Point *p
216
217 EC_Point *
218 mul(c, p, x)
219 EC_Curve *c
220 EC_Point *p
221 mp *x
222 CODE:
223 RETVAL = CREATE(EC_Point);
224 EC_CREATE(RETVAL);
225 ec_mul(c, RETVAL, p, x);
226 OUTPUT:
227 RETVAL
228
229 EC_Point *
230 mmul(c, ...)
231 EC_Curve *c
232 PREINIT:
233 ec_mulfactor *v;
234 size_t i, j, n;
235 CODE:
236 if (items < 3 || !(items & 1)) {
237 croak("Usage: Catacomb::EC::Curve::mmul"
238 "(c, p_0, x_0, p_1, x_1, ...");
239 }
240 n = (items - 1)/2;
241 v = xmalloc(n * sizeof(mp_expfactor));
242 for (i = 1, j = 0; i < items; i += 2, j++) {
243 v[j].base = *(ec *)ptrfromsv(ST(i), "Catacomb::EC::Point", "p_i");
244 v[j].exp = mp_fromsv(ST(i + 1), "x_i", "Catacomb::MP", 0, 0);
245 }
246 RETVAL = CREATE(RETVAL);
247 EC_CREATE(RETVAL);
248 ec_mmul(c, RETVAL, v, n);
249 xfree(v);
250 OUTPUT:
251 RETVAL
252
253 EC_Point *
254 in(c, p)
255 EC_Curve *c
256 EC_Point *p
257 CODE:
258 RETVAL = CREATE(EC_Point);
259 EC_CREATE(RETVAL);
260 EC_IN(c, RETVAL, p);
261 OUTPUT:
262 RETVAL
263
264 EC_Point *
265 out(c, p)
266 EC_Curve *c
267 EC_Point *p
268 CODE:
269 RETVAL = CREATE(EC_Point);
270 EC_CREATE(RETVAL);
271 EC_OUT(c, RETVAL, p);
272 OUTPUT:
273 RETVAL
274
275 EC_Point *
276 fix(c, p)
277 EC_Curve *c
278 EC_Point *p
279 CODE:
280 RETVAL = CREATE(EC_Point);
281 EC_CREATE(RETVAL);
282 EC_FIX(c, RETVAL, p);
283 OUTPUT:
284 RETVAL
285
286 EC_Point *
287 ifind(c, x)
288 EC_Curve *c
289 mp *x
290 CODE:
291 RETVAL = CREATE(EC_Point);
292 if (!EC_FIND(c, RETVAL, x)) {
293 DESTROY(RETVAL);
294 RETVAL = 0;
295 }
296 OUTPUT:
297 RETVAL
298
299 EC_Point *
300 ineg(c, p)
301 EC_Curve *c
302 EC_Point *p
303 CODE:
304 RETVAL = CREATE(EC_Point);
305 EC_CREATE(RETVAL);
306 EC_NEG(c, RETVAL, p);
307 OUTPUT:
308 RETVAL
309
310 EC_Point *
311 iadd(c, p, q)
312 EC_Curve *c
313 EC_Point *p
314 EC_Point *q
315 CODE:
316 RETVAL = CREATE(EC_Point);
317 EC_CREATE(RETVAL);
318 EC_ADD(c, RETVAL, p, q);
319 OUTPUT:
320 RETVAL
321
322 EC_Point *
323 isub(c, p, q)
324 EC_Curve *c
325 EC_Point *p
326 EC_Point *q
327 CODE:
328 RETVAL = CREATE(EC_Point);
329 EC_CREATE(RETVAL);
330 EC_SUB(c, RETVAL, p, q);
331 OUTPUT:
332 RETVAL
333
334 EC_Point *
335 idbl(c, p)
336 EC_Curve *c
337 EC_Point *p
338 CODE:
339 RETVAL = CREATE(EC_Point);
340 EC_CREATE(RETVAL);
341 EC_DBL(c, RETVAL, p);
342 OUTPUT:
343 RETVAL
344
345 bool
346 icheck(c, p)
347 EC_Curve *c
348 EC_Point *p
349 CODE:
350 RETVAL = EC_CHECK(c, p);
351 OUTPUT:
352 RETVAL
353
354 EC_Point *
355 imul(c, p, x)
356 EC_Curve *c
357 EC_Point *p
358 mp *x
359 CODE:
360 RETVAL = CREATE(EC_Point);
361 EC_CREATE(RETVAL);
362 ec_imul(c, RETVAL, p, x);
363 OUTPUT:
364 RETVAL
365
366 EC_Point *
367 immul(c, ...)
368 EC_Curve *c
369 PREINIT:
370 ec_mulfactor *v;
371 size_t i, j, n;
372 CODE:
373 if (items < 3 || !(items & 1)) {
374 croak("Usage: Catacomb::EC::Curve::immul"
375 "(c, p_0, x_0, p_1, x_1, ...");
376 }
377 n = (items - 1)/2;
378 v = xmalloc(n * sizeof(mp_expfactor));
379 for (i = 1, j = 0; i < items; i += 2, j++) {
380 v[j].base = *(ec *)ptrfromsv(ST(i), "Catacomb::EC::Point", "p_i");
381 v[j].exp = mp_fromsv(ST(i + 1), "x_i", "Catacomb::MP", 0, 0);
382 }
383 RETVAL = CREATE(RETVAL);
384 EC_CREATE(RETVAL);
385 ec_mmul(c, RETVAL, v, n);
386 xfree(v);
387 OUTPUT:
388 RETVAL
389
390 void
391 getinfo(me, p)
392 char *p
393 PREINIT:
394 ec_info i;
395 const char *e;
396 EC_Point *pt;
397 PPCODE:
398 if ((e = ec_getinfo(&i, p)) != 0)
399 croak("bad curve spec: %s", e);
400 pt = CREATE(EC_Point);
401 *pt = i.g;
402 XPUSHs(RET(i.c, "Catacomb::EC::Curve"));
403 XPUSHs(RET(pt, "Catacomb::EC::Point"));
404 XPUSHs(RET(i.r, "Catacomb::MP"));
405 XPUSHs(RET(i.h, "Catacomb::MP"));
406