Annotation of OpenXM_contrib2/asir2000/builtin/ec.c, Revision 1.4
1.3 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.4 ! noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.4 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/ec.c,v 1.3 2000/08/21 08:31:19 noro Exp $
1.3 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52: #include "inline.h"
53:
54: /*
55: * Elliptic curve related functions
56: *
57: * Argument specifications
58: * Point = vector(x,y,z) (projective representation)
59: * ECInfo = vector(a,b,p)
60: * a,b : integer for GF(p), GF2N for GF(2^n)
61: * p : integer for GF(p), polynomial for GF(2^n)
62: *
63: */
64:
65: struct oKeyIndexPair {
66: unsigned int key;
67: int index;
68: };
69:
70: void Psha1();
71: void Psha1_ec();
72: void Pecm_add_ff();
73: void Pecm_chsgn_ff();
74: void Pecm_sub_ff();
75: void Pecm_compute_all_key_homo_ff();
76: void Pnextvect1(),Psort_ktarray(),Pecm_find_match(),Pseparate_vect();
77: void Pecm_set_addcounter();
78: void Pecm_count_order();
79:
80: void ecm_add_ff(VECT,VECT,VECT,VECT *);
81: void ecm_add_gfp(VECT,VECT,VECT,VECT *);
82: void ecm_add_gf2n(VECT,VECT,VECT,VECT *);
83:
84: void ecm_chsgn_ff(VECT,VECT *);
85: void ecm_sub_ff(VECT,VECT,VECT,VECT *);
86:
87: void compute_all_key_homo_gfp(VECT *,int,unsigned int *);
88: void compute_all_key_homo_gf2n(VECT *,int,unsigned int *);
89:
90: unsigned int separate_vect(double *,int);
91: void ecm_find_match(unsigned int *,int,unsigned int *,int,LIST *);
92: int find_match(unsigned int,unsigned int *,int);
93:
94: void sort_ktarray(VECT,VECT,LIST *);
95: int comp_kip(struct oKeyIndexPair *,struct oKeyIndexPair *);
96:
97: int nextvect1(VECT,VECT);
98:
99: unsigned int ecm_count_order_gfp(unsigned int,unsigned int,unsigned int);
100: unsigned int ecm_count_order_gf2n(UP2,GF2N,GF2N);
101:
102: void sha_memory(unsigned char *,unsigned int,unsigned int *);
103:
104: unsigned int ecm_addcounter;
105: extern int current_ff,lm_lazy;
106:
107: struct ftab ec_tab[] = {
108: /* point addition */
109: {"ecm_add_ff",Pecm_add_ff,3},
110:
111: /* point change sign */
112: {"ecm_chsgn_ff",Pecm_chsgn_ff,1},
113:
114: /* point subtraction */
115: {"ecm_sub_ff",Pecm_sub_ff,3},
116:
117: /* key computation for sort and match */
118: {"ecm_compute_all_key_homo_ff",Pecm_compute_all_key_homo_ff,1},
119:
120: /* exhausitve search of rational points */
121: {"ecm_count_order",Pecm_count_order,1},
122:
123: /* common functions */
124: {"nextvect1",Pnextvect1,2},
125: {"sort_ktarray",Psort_ktarray,2},
126: {"separate_vect",Pseparate_vect,1},
127: {"ecm_find_match",Pecm_find_match,2},
128: {"ecm_set_addcounter",Pecm_set_addcounter,-1},
1.2 noro 129: {"sha1",Psha1,-2},
1.1 noro 130: #if 0
131: {"sha1_free",Psha1_free,1},
132: #endif
133: {0,0,0},
134: };
135:
136: void Psha1(arg,rp)
137: NODE arg;
138: Q *rp;
139: {
140: unsigned char *s;
141: unsigned int digest[5];
142: int i,j,l,bl,n;
143: unsigned int t;
144: N z,r;
145:
146: asir_assert(ARG0(arg),O_N,"sha1_free");
147: z = NM((Q)ARG0(arg));
148: n = PL(z);
149: l = n_bits(z);
1.2 noro 150: if ( argc(arg) == 2 )
151: bl = QTOS((Q)ARG1(arg));
152: else
153: bl = (l+7)/8;
1.1 noro 154: s = (unsigned char *)MALLOC(bl);
155: for ( i = 0, j = bl-1; i < n; i++ ) {
156: t = BD(z)[i];
157: if ( j >= 0 ) s[j--] = t&0xff; t>>=8;
158: if ( j >= 0 ) s[j--] = t&0xff; t>>=8;
159: if ( j >= 0 ) s[j--] = t&0xff; t>>=8;
160: if ( j >= 0 ) s[j--] = t;
161: }
162: sha_memory(s,bl,digest);
163: r = NALLOC(5);
164: for ( i = 0; i < 5; i++ )
165: BD(r)[i] = digest[4-i];
166: for ( i = 4; i >= 0 && !BD(r)[i]; i-- );
167: if ( i < 0 )
168: *rp = 0;
169: else {
170: PL(r) = i+1;
171: NTOQ(r,1,*rp);
172: }
173: }
174:
175: #if 0
176: void Psha1_ec(arg,rp)
177: NODE arg;
178: Q *rp;
179: {
180: #include <fj_crypt.h>
181: SHS_CTX context;
182: unsigned char *s;
183: int i,j,l,bl,n;
184: unsigned int t;
185: N z,r;
186: extern int little_endian;
187:
188: asir_assert(ARG0(arg),O_N,"sha1");
189: z = NM((Q)ARG0(arg));
190: n = PL(z);
191: l = n_bits(z);
192: bl = (l+7)/8;
193: s = (unsigned char *)MALLOC(bl);
194: for ( i = 0, j = bl-1; i < n; i++ ) {
195: t = BD(z)[i];
196: if ( j >= 0 ) s[j--] = t&0xff; t>>=8;
197: if ( j >= 0 ) s[j--] = t&0xff; t>>=8;
198: if ( j >= 0 ) s[j--] = t&0xff; t>>=8;
199: if ( j >= 0 ) s[j--] = t;
200: }
201: SHSInit(&context);
202: SHSUpdate(&context,s,bl);
203: SHSFinal(&context);
204: r = NALLOC(5);
205: if ( little_endian )
206: for ( i = 0; i < 5; i++ ) {
207: t = context.digest[4-i];
208: BD(r)[i] = (t>>24)|((t&0xff0000)>>8)|((t&0xff00)<<8)|(t<<24);
209: } else
210: for ( i = 0; i < 5; i++ )
211: BD(r)[i] = context.digest[4-i];
212: for ( i = 4; i >= 0 && !BD(r)[i]; i-- );
213: if ( i < 0 )
214: *rp = 0;
215: else {
216: PL(r) = i+1;
217: NTOQ(r,1,*rp);
218: }
219: }
220: #endif
221:
222: void Pecm_count_order(arg,rp)
223: NODE arg;
224: Q *rp;
225: {
226: N p;
227: UP2 d;
228: Obj a,b;
229: unsigned int p0,a0,b0,ord;
230: VECT ec;
231: Obj *vb;
232:
233: switch ( current_ff ) {
234: case FF_GFP:
235: getmod_lm(&p);
236: if ( n_bits(p) > 32 )
237: error("ecm_count_order : ground field too large");
238: p0 = BD(p)[0];
239: ec = (VECT)ARG0(arg);
240: vb = (Obj *)BDY(ec); simp_ff(vb[0],&a); simp_ff(vb[1],&b);
241: a0 = a?BD(BDY((LM)a))[0]:0;
242: b0 = b?BD(BDY((LM)b))[0]:0;
243: ord = ecm_count_order_gfp(p0,a0,b0);
244: UTOQ(ord,*rp);
245: break;
246: case FF_GF2N:
247: getmod_gf2n(&d);
248: if ( degup2(d) > 10 )
249: error("ecm_count_order : ground field too large");
250: ec = (VECT)ARG0(arg);
251: vb = (Obj *)BDY(ec); simp_ff(vb[0],&a); simp_ff(vb[1],&b);
252: ord = ecm_count_order_gf2n(d,(GF2N)a,(GF2N)b);
253: UTOQ(ord,*rp);
254: break;
255: default:
256: error("ecm_count_order : current_ff is not set");
257: }
258: }
259:
260: void Pecm_set_addcounter(arg,rp)
261: NODE arg;
262: Q *rp;
263: {
264: if ( arg )
265: ecm_addcounter = QTOS((Q)ARG0(arg));
266: UTOQ(ecm_addcounter,*rp);
267: }
268:
269: void Pecm_compute_all_key_homo_ff(arg,rp)
270: NODE arg;
271: VECT *rp;
272: {
273: Obj mod;
274: unsigned int *ka;
275: int len,i;
276: VECT *pa;
277: VECT r,v;
278: LIST *vb;
279: USINT *b;
280:
281: v = (VECT)ARG0(arg);
282: len = v->len;
283: vb = (LIST *)v->body;
284: pa = (VECT *)ALLOCA(len*sizeof(VECT));
285: ka = (unsigned int *)ALLOCA(len*sizeof(unsigned int));
286: for ( i = 0; i < len; i++ )
287: pa[i] = (VECT)BDY(NEXT(BDY(vb[i])));
288: switch ( current_ff ) {
289: case FF_GFP:
290: compute_all_key_homo_gfp(pa,len,ka); break;
291: case FF_GF2N:
292: compute_all_key_homo_gf2n(pa,len,ka); break;
293: default:
294: error("ecm_compute_all_key_homo_ff : current_ff is not set");
295: }
296: MKVECT(r,len); *rp = r;
297: b = (USINT *)r->body;
298: for ( i = 0; i < len; i++ )
299: MKUSINT(b[i],ka[i]);
300: }
301:
302: void Psort_ktarray(arg,rp)
303: NODE arg;
304: LIST *rp;
305: {
306: sort_ktarray((VECT)ARG0(arg),(VECT)ARG1(arg),rp);
307: }
308:
309: void Pecm_add_ff(arg,rp)
310: NODE arg;
311: VECT *rp;
312: {
313: ecm_add_ff(ARG0(arg),ARG1(arg),ARG2(arg),rp);
314: }
315:
316: void Pecm_sub_ff(arg,rp)
317: NODE arg;
318: VECT *rp;
319: {
320: ecm_sub_ff(ARG0(arg),ARG1(arg),ARG2(arg),rp);
321: }
322:
323: void Pecm_chsgn_ff(arg,rp)
324: NODE arg;
325: VECT *rp;
326: {
327: ecm_chsgn_ff(ARG0(arg),rp);
328: }
329:
330: void Pnextvect1(arg,rp)
331: NODE arg;
332: Q *rp;
333: {
334: int index;
335:
336: index = nextvect1(ARG0(arg),ARG1(arg));
337: STOQ(index,*rp);
338: }
339:
340: /* XXX at least n < 32 must hold. What is the strict restriction for n ? */
341:
342: void Pseparate_vect(arg,rp)
343: NODE arg;
344: LIST *rp;
345: {
346: VECT v;
347: int n,i;
348: Q *b;
349: double *w;
350: unsigned int s;
351: NODE ns,nc,t,t1;
352: Q iq;
353: LIST ls,lc;
354:
355: v = (VECT)ARG0(arg);
356: n = v->len; b = (Q *)v->body;
357: w = (double *)ALLOCA(n*sizeof(double));
358: for ( i = 0; i < n; i++ )
359: w[i] = (double)QTOS(b[i]);
360: s = separate_vect(w,n);
361: ns = nc = 0;
362: for ( i = n-1; i >= 0; i-- )
363: if ( s & (1<<i) ) {
364: STOQ(i,iq); MKNODE(t,iq,ns); ns = t;
365: } else {
366: STOQ(i,iq); MKNODE(t,iq,nc); nc = t;
367: }
368: MKLIST(ls,ns); MKLIST(lc,nc);
369: MKNODE(t,lc,0); MKNODE(t1,ls,t);
370: MKLIST(*rp,t1);
371: }
372:
373: void Pecm_find_match(arg,rp)
374: NODE arg;
375: LIST *rp;
376: {
377: VECT g,b;
378: int ng,nb,i;
379: USINT *p;
380: unsigned int *kg,*kb;
381:
382: g = (VECT)ARG0(arg); ng = g->len;
383: kg = (unsigned int *)ALLOCA(ng*sizeof(unsigned int));
384: for ( i = 0, p = (USINT *)g->body; i < ng; i++ )
385: kg[i] = p[i]?BDY(p[i]):0;
386: b = (VECT)ARG1(arg); nb = b->len;
387: kb = (unsigned int *)ALLOCA(nb*sizeof(unsigned int));
388: for ( i = 0, p = (USINT *)b->body; i < nb; i++ )
389: kb[i] = p[i]?BDY(p[i]):0;
390: ecm_find_match(kg,ng,kb,nb,rp);
391: }
392:
393: void ecm_add_ff(p1,p2,ec,pr)
394: VECT p1,p2,ec;
395: VECT *pr;
396: {
397: if ( !p1 )
398: *pr = p2;
399: else if ( !p2 )
400: *pr = p1;
401: else {
402: switch ( current_ff ) {
403: case FF_GFP:
404: ecm_add_gfp(p1,p2,ec,pr); break;
405: case FF_GF2N:
406: ecm_add_gf2n(p1,p2,ec,pr); break;
407: default:
408: error("ecm_add_ff : current_ff is not set");
409: }
410: }
411: }
412:
413: /* ec = [AX,BC] */
414:
415: void ecm_add_gf2n(p1,p2,ec,rp)
416: VECT p1,p2,ec;
417: VECT *rp;
418: {
419: GF2N ax,bc,a0,a1,a2,b0,b1,b2;
420: GF2N a2b0,a0b2,a2b1,a1b2,a02,a04,a22,a24,a0a2,a0a22,a1a2;
421: GF2N t,s,u,r0,r1,r00,r01,r02,r002,r003,r022,r02q;
422: VECT r;
423: GF2N *vb,*rb;
424:
425: ecm_addcounter++;
426: /* addition with O */
427: if ( !p1 ) {
428: *rp = p2;
429: return;
430: }
431: if ( !p2 ) {
432: *rp = p1;
433: return;
434: }
435: vb = (GF2N *)BDY(ec); ax = vb[0]; bc = vb[1];
436: vb = (GF2N *)BDY(p1); a0 = vb[0]; a1 = vb[1]; a2 = vb[2];
437: vb = (GF2N *)BDY(p2); b0 = vb[0]; b1 = vb[1]; b2 = vb[2];
438:
439: mulgf2n(a2,b0,&a2b0); mulgf2n(a0,b2,&a0b2);
440: if ( !cmpgf2n(a2b0,a0b2) ) {
441: mulgf2n(a2,b1,&a2b1);
442: mulgf2n(a1,b2,&a1b2);
443: if ( !cmpgf2n(a2b1,a1b2) ) {
444: if ( !a0 )
445: *rp = 0;
446: else {
447: squaregf2n(a0,&a02); squaregf2n(a02,&a04);
448: squaregf2n(a2,&a22); squaregf2n(a22,&a24);
449: mulgf2n(a0,a2,&a0a2); squaregf2n(a0a2,&a0a22);
450: mulgf2n(bc,a24,&t); addgf2n(a04,t,&r0);
451: mulgf2n(a04,a0a2,&t); mulgf2n(a1,a2,&a1a2);
452: addgf2n(a02,a1a2,&s); addgf2n(s,a0a2,&u);
453: mulgf2n(u,r0,&s); addgf2n(t,s,&r1);
454:
455: MKVECT(r,3); rb = (GF2N *)r->body;
456: mulgf2n(r0,a0a2,&rb[0]); rb[1] = r1; mulgf2n(a0a22,a0a2,&rb[2]);
457: *rp = r;
458: }
459: } else
460: *rp = 0;
461: } else {
462: mulgf2n(a1,b2,&a1b2); addgf2n(a0b2,a2b0,&r00);
463: mulgf2n(a2,b1,&t); addgf2n(a1b2,t,&r01); mulgf2n(a2,b2,&r02);
464: squaregf2n(r00,&r002); mulgf2n(r002,r00,&r003);
465:
466: addgf2n(r00,r01,&t); mulgf2n(t,r01,&s); mulgf2n(s,r02,&t);
467: if ( ax ) {
468: mulgf2n(r02,ax,&r02q);
469: addgf2n(t,r003,&s); mulgf2n(r02q,r002,&t); addgf2n(s,t,&r0);
470: } else
471: addgf2n(t,r003,&r0);
472:
473: mulgf2n(a0b2,r002,&t); addgf2n(t,r0,&s); mulgf2n(r01,s,&t);
474: mulgf2n(r002,a1b2,&s); addgf2n(r0,s,&u); mulgf2n(r00,u,&s);
475: addgf2n(t,s,&r1);
476:
477: MKVECT(r,3); rb = (GF2N *)r->body;
478: mulgf2n(r0,r00,&rb[0]); rb[1] = r1; mulgf2n(r003,r02,&rb[2]);
479: *rp = r;
480: }
481: }
482:
483: extern LM THREE_LM,FOUR_LM,EIGHT_LM;
484:
485: /* 0 < p < 2^16, 0 <= a,b < p */
486:
487: unsigned int ecm_count_order_gfp(p,a,b)
488: unsigned int p,a,b;
489: {
490: unsigned int x,y,rhs,ord,t;
491:
492: for ( x = 0, ord = 1; x < p; x++ ) {
493: DMAR(x,x,a,p,t) /* t = x^2+a mod p */
494: DMAR(t,x,b,p,rhs) /* rhs = x*(x^2+a)+b mod p */
495: if ( !rhs )
496: ord++;
497: else if ( small_jacobi(rhs,p)==1 )
498: ord+=2;
499: }
500: return ord;
501: }
502:
503: unsigned int ecm_count_order_gf2n(d,a,b)
504: UP2 d;
505: GF2N a,b;
506: {
507: error("ecm_count_order_gf2n : not implemented yet");
508: }
509:
510: /* ec = [AX,BC] */
511:
512: void ecm_add_gfp(p1,p2,ec,pr)
513: VECT p1,p2,ec;
514: VECT *pr;
515: {
516: LM aa,bb,x1,y1,z1,x2,y2,z2,x1z2,v1,y1z2,u1,u2,v2,v3,z1z2;
517: LM v2x1z2,a1,x3,y3,z3,w1,s1,s2,s3,s1y1,b1,h1;
518: LM t,s,u;
519: LM *vb;
520: VECT r;
521:
522: ecm_addcounter++;
523: /* addition with O */
524: if( !p1 ) {
525: *pr = p2;
526: return;
527: }
528: if( !p2 ) {
529: *pr = p1;
530: return;
531: }
532:
533: /* set parameters */
534:
535: /* aa = ec[0]; bb = ec[1]; */
536: vb = (LM *)BDY(ec); aa = vb[0]; bb = vb[1];
537:
538: /* x1 = p1[0]; y1 = p1[1]; z1 = p1[2]; */
539: vb = (LM *)BDY(p1); x1 = vb[0]; y1 = vb[1]; z1 = vb[2];
540:
541: /* x2 = p2[0]; y2 = p2[1]; z2 = p2[2]; */
542: vb = (LM *)BDY(p2); x2 = vb[0]; y2 = vb[1]; z2 = vb[2];
543:
544: /* addition */
545:
546: /* x1z2 = (x1*z2) %p; */
547: mullm(x1,z2,&x1z2);
548:
549: /* v1 = (x2*z1-x1z2) %p; */
550: lm_lazy = 1;
551: mullm(x2,z1,&t); sublm(t,x1z2,&s);
552: lm_lazy = 0; simplm(s,&v1);
553:
554: /* y1z2 = (y1*z2) %p; */
555: mullm(y1,z2,&y1z2);
556:
557: /* u1 = (y2*z1-y1z2) %p; */
558: lm_lazy = 1;
559: mullm(y2,z1,&t); sublm(t,y1z2,&s);
560: lm_lazy = 0; simplm(s,&u1);
561:
562: if( v1 != 0 ) {
563: /* u2 = (u1*u1) %p; */
564: mullm(u1,u1,&u2);
565:
566: /* v2 = (v1*v1) %p; */
567: mullm(v1,v1,&v2);
568:
569: /* v3 = (v1*v2) %p; */
570: mullm(v1,v2,&v3);
571:
572: /* z1z2 = (z1*z2) %p; */
573: mullm(z1,z2,&z1z2);
574:
575: /* v2x1z2 = (v2*x1z2) %p; */
576: mullm(v2,x1z2,&v2x1z2);
577:
578: /* a1 = (u2*z1z2-v3-2*v2x1z2) %p; */
579: lm_lazy = 1;
580: mullm(u2,z1z2,&t); addlm(v2x1z2,v2x1z2,&s);
581: addlm(v3,s,&u); sublm(t,u,&s);
582: lm_lazy = 0; simplm(s,&a1);
583:
584: /* x3 = ( v1 * a1 ) %p; */
585: mullm(v1,a1,&x3);
586:
587: /* y3 = ( u1 * ( v2x1z2 - a1 ) - v3 * y1z2 ) %p; */
588: lm_lazy = 1;
589: sublm(v2x1z2,a1,&t); mullm(u1,t,&s); mullm(v3,y1z2,&u); sublm(s,u,&t);
590: lm_lazy = 0; simplm(t,&y3);
591:
592: /* z3 = ( v3 * z1z2 ) %p; */
593: mullm(v3,z1z2,&z3);
594: } else if( u1 == 0 ) {
595: /* w1 = (aa*z1*z1+3*x1*x1) %p; */
596: lm_lazy = 1;
597: mullm(z1,z1,&t); mullm(aa,t,&s);
598: mullm(x1,x1,&t); mullm(THREE_LM,t,&u); addlm(s,u,&t);
599: lm_lazy = 0; simplm(t,&w1);
600:
601: /* s1 = (y1*z1) %p; */
602: mullm(y1,z1,&s1);
603:
604: /* s2 = (s1*s1) %p; */
605: mullm(s1,s1,&s2);
606:
607: /* s3 = (s1*s2) %p; */
608: mullm(s1,s2,&s3);
609:
610: /* s1y1 = (s1*y1) %p; */
611: mullm(s1,y1,&s1y1);
612:
613: /* b1 = (s1y1*x1) %p; */
614: mullm(s1y1,x1,&b1);
615:
616: /* h1 = (w1*w1-8*b1) %p; */
617: lm_lazy = 1;
618: mullm(w1,w1,&t); mullm(EIGHT_LM,b1,&s); sublm(t,s,&u);
619: lm_lazy = 0; simplm(u,&h1);
620:
621: /* x3 = ( 2 * h1 * s1 ) %p; */
622: lm_lazy = 1;
623: mullm(h1,s1,&t); addlm(t,t,&s);
624: lm_lazy = 0; simplm(s,&x3);
625:
626: /* y3 = ( w1 * ( 4 * b1 - h1 ) - 8 * s1y1 * s1y1 ) %p; */
627: lm_lazy = 1;
628: mullm(FOUR_LM,b1,&t); sublm(t,h1,&s); mullm(w1,s,&u);
629: mullm(s1y1,s1y1,&t); mullm(EIGHT_LM,t,&s); sublm(u,s,&t);
630: lm_lazy = 0; simplm(t,&y3);
631:
632: /* z3 = ( 8 * s3 ) %p; */
633: mullm(EIGHT_LM,s3,&z3);
634: } else {
635: *pr = 0;
636: return;
637: }
638: if ( !z3 )
639: *pr = 0;
640: else {
641: MKVECT(r,3); *pr = r;
642: vb = (LM *)BDY(r); vb[0] = x3; vb[1] = y3; vb[2] = z3;
643: }
644: }
645:
646: void ecm_chsgn_ff(p,pr)
647: VECT p;
648: VECT *pr;
649: {
650: Obj m,x,y,z;
651: LM tl;
652: GF2N tg;
653: Obj *vb;
654: VECT r;
655:
656: if( !p ) {
657: *pr = 0;
658: return;
659: }
660:
661: /* x = p[0]; y = p[1]; z = p[2]; */
662: vb = (Obj *)BDY(p); x = vb[0]; y = vb[1]; z = vb[2];
663: switch ( current_ff ) {
664: case FF_GFP:
665: if ( !y )
666: *pr = p;
667: else {
668: chsgnlm((LM)y,&tl);
669: MKVECT(r,3); *pr = r;
670: vb = (Obj *)BDY(r); vb[0] = x; vb[1] = (Obj)tl; vb[2] = z;
671: }
672: break;
673: case FF_GF2N:
674: addgf2n((GF2N)x,(GF2N)y,&tg);
675: MKVECT(r,3); *pr = r;
676: vb = (Obj *)BDY(r); vb[0] = x; vb[1] = (Obj)tg; vb[2] = z;
677: break;
678: default:
679: error("ecm_chsgn_ff : current_ff is not set");
680: }
681: }
682:
683: void ecm_sub_ff(p1,p2,ec,pr)
684: VECT p1,p2,ec;
685: VECT *pr;
686: {
687: VECT mp2;
688:
689: ecm_chsgn_ff(p2,&mp2);
690: ecm_add_ff(p1,mp2,ec,pr);
691: }
692:
693: /* tplist = [[t,p],...]; t:interger, p=[p0,p1]:point (vector) */
694:
695: int comp_kip(a,b)
696: struct oKeyIndexPair *a,*b;
697: {
698: unsigned int ka,kb;
699:
700: ka = a->key; kb = b->key;
701: if ( ka > kb )
702: return 1;
703: else if ( ka < kb )
704: return -1;
705: else
706: return 0;
707: }
708:
709: #define EC_GET_XZ(p,x,z) \
710: if ( !(p) ) {\
711: (x)=0; (z)=(LM)ONE;\
712: } else { \
713: LM *vb;\
714: vb = (LM *)BDY((VECT)(p));\
715: (x) = vb[0]; (z) = vb[2];\
716: }
717:
718: #define EC_GET_XZ_GF2N(p,x,z) \
719: if ( !(p) ) {\
720: (x)=0; (z)=(GF2N)ONE;\
721: } else { \
722: GF2N *vb;\
723: vb = (GF2N *)BDY((VECT)(p));\
724: (x) = vb[0]; (z) = vb[2];\
725: }
726:
727: void compute_all_key_homo_gfp(pa,len,ka)
728: VECT *pa;
729: int len;
730: unsigned int *ka;
731: {
732: LM *b,*x,*z;
733: int i;
734: LM t,s,m;
735: N lm;
736: Q mod;
737:
738: b = (LM *)ALLOCA((len+1)*sizeof(LM));
739: x = (LM *)ALLOCA(len*sizeof(LM));
740: z = (LM *)ALLOCA(len*sizeof(LM));
741: MKLM(ONEN,b[0]);
742: for ( i = 1; i <= len; i++ ) {
743: EC_GET_XZ(pa[i-1],x[i-1],z[i-1]);
744: mullm(b[i-1],z[i-1],&b[i]);
745: }
746: /* b[0] = 1 */
747: divlm(b[0],b[len],&m);
748: for ( i = len-1; i >= 0; i-- ) {
749: mullm(m,b[i],&s); mullm(s,x[i],&t); s = t;
750: ka[i] = s ? s->body->b[0] : 0; ka[i] |= 0x80000000;
751: mullm(m,z[i],&s); m = s;
752: }
753: }
754:
755: void compute_all_key_homo_gf2n(pa,len,ka)
756: VECT *pa;
757: int len;
758: unsigned int *ka;
759: {
760: GF2N *b,*x,*z;
761: int i;
762: GF2N t,s,m;
763:
764: b = (GF2N *)ALLOCA((len+1)*sizeof(Q));
765: x = (GF2N *)ALLOCA(len*sizeof(Q));
766: z = (GF2N *)ALLOCA(len*sizeof(Q));
767: MKGF2N(ONEUP2,b[0]);
768: for ( i = 1; i <= len; i++ ) {
769: EC_GET_XZ_GF2N(pa[i-1],x[i-1],z[i-1]);
770: mulgf2n(b[i-1],z[i-1],&b[i]);
771: }
772: invgf2n(b[len],&m);
773: for ( i = len-1; i >= 0; i-- ) {
774: mulgf2n(m,b[i],&s); mulgf2n(s,x[i],&t); s = t;
775: ka[i] = s ? s->body->b[0] : 0; ka[i] |= 0x80000000;
776: mulgf2n(m,z[i],&s); m = s;
777: }
778: }
779:
780: unsigned int separate_vect(v,n)
781: double *v;
782: int n;
783: {
784: unsigned int max = 1<<n;
785: unsigned int i,j,i0;
786: double all,a,total,m;
787:
788: for ( i = 0, all = 1; i < (unsigned int)n; i++ )
789: all *= v[i];
790:
791: for ( i = 0, m = 0; i < max; i++ ) {
792: for ( a = 1, j = 0; j < (unsigned int)n; j++ )
793: if ( i & (1<<j) )
794: a *= v[j];
795: total = a+(all/a)*2;
796: if ( !m || total < m ) {
797: m = total;
798: i0 = i;
799: }
800: }
801: return i0;
802: }
803:
804: void ecm_find_match(g,ng,b,nb,r)
805: unsigned int *g;
806: int ng;
807: unsigned int *b;
808: int nb;
809: LIST *r;
810: {
811: int i,j;
812: Q iq,jq;
813: NODE n0,n1,c0,c;
814: LIST l;
815:
816: for ( i = 0, c0 = 0; i < ng; i++ ) {
817: j = find_match(g[i],b,nb);
818: if ( j >= 0 ) {
819: STOQ(i,iq); STOQ(j,jq);
820: MKNODE(n1,jq,0); MKNODE(n0,iq,n1); MKLIST(l,n0);
821: NEXTNODE(c0,c);
822: BDY(c) = (pointer)l;
823: }
824: }
825: if ( c0 )
826: NEXT(c) = 0;
827: MKLIST(*r,c0);
828: }
829:
830: int find_match(k,key,n)
831: unsigned int k;
832: unsigned int *key;
833: int n;
834: {
835: int s,e,m;
836:
837: for ( s = 0, e = n; (e-s) > 1; ) {
838: m = (s+e)/2;
839: if ( k==key[m] )
840: return m;
841: else if ( k > key[m] )
842: s = m;
843: else
844: e = m;
845: }
846: if ( k == key[s] )
847: return s;
848: else
849: return -1;
850: }
851:
852: int nextvect1(vect,bound)
853: VECT vect,bound;
854: {
855: int size,i,a;
856: Q *vb,*bb;
857:
858: size = vect->len;
859: vb = (Q *)vect->body;
860: bb = (Q *)bound->body;
861: for ( i = size-1; i >= 0; i-- )
862: if ( (a=QTOS(vb[i])) < QTOS(bb[i]) ) {
863: a++; STOQ(a,vb[i]);
864: break;
865: } else
866: vb[i] = 0;
867: return i;
868: }
869:
870: void sort_ktarray(karray,tarray,rp)
871: VECT karray,tarray;
872: LIST *rp;
873: {
874: LIST *lb;
875: NODE r,r1;
876: int i,i0,k,len,same,tsame;
877: struct oKeyIndexPair *kip;
878: VECT key,value,v;
879: Q *tb,*samebuf;
880: USINT *kb;
881: Obj *svb;
882: USINT *skb;
883:
884: len = karray->len;
885: kb = (USINT *)karray->body;
886:
887: kip = (struct oKeyIndexPair *)ALLOCA(len*sizeof(struct oKeyIndexPair));
888: for ( i = 0; i < len; i++ ) {
889: kip[i].key = BDY(kb[i]); kip[i].index = i;
890: }
891: qsort((void *)kip,len,sizeof(struct oKeyIndexPair),
892: (int (*)(const void *,const void *))comp_kip);
893:
894: for ( same = tsame = i = i0 = 0, k = 1; i < len; i++, tsame++ )
895: if ( kip[i0].key != kip[i].key ) {
896: i0 = i; k++;
897: same = MAX(tsame,same);
898: tsame = 0;
899: }
900: same = MAX(tsame,same);
901: samebuf = (Q *)ALLOCA(same*sizeof(Q));
902:
903: MKVECT(key,k); skb = (USINT *)BDY(key);
904: MKVECT(value,k); svb = (Obj *)BDY(value);
905:
906: tb = (Q *)tarray->body;
907: for ( same = i = i0 = k = 0; i <= len; i++ ) {
908: if ( i == len || kip[i0].key != kip[i].key ) {
909: skb[k] = kb[kip[i0].index];
910: if ( same > 1 ) {
911: MKVECT(v,same);
912: bcopy((char *)samebuf,(char *)v->body,same*sizeof(Q));
913: svb[k] = (Obj)v;
914: } else
915: svb[k] = (Obj)samebuf[0];
916: i0 = i;
917: k++;
918: same = 0;
919: if ( i == len )
920: break;
921: }
922: samebuf[same++] = tb[kip[i].index];
923: }
924: MKNODE(r1,value,0); MKNODE(r,key,r1); MKLIST(*rp,r);
925: }
926:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>