Annotation of OpenXM_contrib2/asir2018/engine/C.c, Revision 1.2
1.1 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
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
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.2 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/engine/C.c,v 1.1 2018/09/19 05:45:06 noro Exp $
1.1 noro 49: */
50: #include "ca.h"
51: #include "inline.h"
52: #include "base.h"
53:
54: V up_var;
55:
56: /* binary has at least 32 leading 0 chars. */
57: void binarytoz(char *binary,Z *np)
58: {
59: int i,w,len;
60: int *b;
61: char buf[33];
62: mpz_t z;
63:
64: binary += strlen(binary)%32;
65: len = strlen(binary);
66: w = len/32; /* sufficient for holding binary */
67: b = CALLOC(w,sizeof(int));
68: for ( i = 0; i < w; i++ ) {
69: strncpy(buf,binary+len-32*(i+1),32); buf[32] = 0;
70: b[i] = strtoul(buf,0,2);
71: }
72: for ( i = w-1; i >= 0 && !b[i]; i-- );
73: if ( i < 0 )
74: *np = 0;
75: else {
76: len = i+1;
77: mpz_init(z);
78: mpz_import(z,len,-1,sizeof(int),0,0,b);
79: MPZTOZ(z,*np);
80: }
81: }
82:
83: /* hex has at least 8 leading 0 chars. */
84: void hextoz(char *hex,Z *np)
85: {
86: int i,w,len;
87: int *b;
88: mpz_t z;
89: char buf[9];
90:
91: hex += strlen(hex)%8;
92: len = strlen(hex);
93: w = len/8; /* sufficient for holding hex */
94: b = CALLOC(w,sizeof(int));
95: for ( i = 0; i < w; i++ ) {
96: strncpy(buf,hex+len-8*(i+1),8); buf[8] = 0;
97: b[i] = strtoul(buf,0,16);
98: }
99: for ( i = w-1; i >= 0 && !b[i]; i-- );
100: if ( i < 0 )
101: *np = 0;
102: else {
103: len = i+1;
104: mpz_init(z);
105: mpz_import(z,len,-1,sizeof(int),0,0,b);
106: MPZTOZ(z,*np);
107: }
108: }
109:
110: /* Z -> w[0]+w[1]*base+...+w[d-1]*base^(d-1) */
111:
112: int ztonadic(int base,Z n,unsigned int **nrp)
113: {
114: int i,plc;
115: size_t d;
116: unsigned int *c,*x,*w,*p;
117: unsigned int r;
118: L m;
119:
120: if ( !n ) {
121: *nrp = NULL;
122: return 0;
123: }
124: w = mpz_export(0,&d,-1,sizeof(int),0,0,BDY(n));
125: for ( i = 1, m = 1; m <= LBASE/(L)base; m *= base, i++ );
126:
127: c = (unsigned int *)W_ALLOC(d*i+1);
128: x = (unsigned int *)W_ALLOC(d+1);
129: for ( i = 0; i < d; i++ )
130: x[i] = w[i];
131: for ( plc = 0; d >= 1; plc++ ) {
132: for ( i = d - 1, r = 0; i >= 0; i-- ) {
133: DSAB((unsigned int)base,r,x[i],x[i],r)
134: }
135: c[plc] = r;
136: if ( !x[d-1] ) d--;
137: }
138:
139: *nrp = p = (unsigned int *)CALLOC(plc,sizeof(unsigned int));
140: for ( i = 0; i < plc; i++ )
141: p[i] = c[i];
142: return plc;
143: }
144:
145: /* w[0]+w[1]*base+...+w[d-1]*base^(d-1) -> Z */
146:
147: void nadictoz(int base,int d,unsigned int *w,Z *nrp)
148: {
149: unsigned int carry;
150: unsigned int *x;
151: int i,j,plc;
152: mpz_t z;
153:
154: if ( !d ) {
155: *nrp = 0;
156: return;
157: }
158: x = (unsigned int *)CALLOC(d + 1,sizeof(unsigned int));
159: for ( plc = 0, i = d - 1; i >= 0; i-- ) {
160: for ( carry = w[i],j = 0; j < plc; j++ ) {
161: DMA(x[j],(unsigned int)base,carry,carry,x[j])
162: }
163: if ( carry ) x[plc++] = carry;
164: }
165: mpz_init(z);
166: mpz_import(z,plc,-1,sizeof(int),0,0,x);
167: MPZTOZ(z,*nrp);
168: }
169:
170: void intarraytoz(int len,unsigned int *w,Z *nrp)
171: {
172: mpz_t z;
173: mpz_init(z);
174: mpz_import(z,len,-1,sizeof(int),0,0,w);
175: MPZTOZ(z,*nrp);
176: }
177:
178: void ptomp(int m,P p,P *pr)
179: {
180: DCP dc,dcr,dcr0;
181: unsigned int a,b;
182: P t;
183: MQ s;
184:
185: if ( !p )
186: *pr = 0;
187: else if ( NUM(p) ) {
188: a = remqi((Q)p,m);
189: STOMQ(a,s); *pr = (P)s;
190: } else {
191: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
192: ptomp(m,COEF(dc),&t);
193: if ( t ) {
194: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
195: }
196: }
197: if ( !dcr0 )
198: *pr = 0;
199: else {
200: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
201: }
202: }
203: }
204:
205: void mptop(P f,P *gp)
206: {
207: DCP dc,dcr,dcr0;
208: Z q;
209:
210: if ( !f )
211: *gp = 0;
212: else if ( NUM(f) )
1.2 ! noro 213: STOZ(CONT((MQ)f),q),*gp = (P)q;
1.1 noro 214: else {
215: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
216: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); mptop(COEF(dc),&COEF(dcr));
217: }
218: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
219: }
220: }
221:
222: void ptosfp(P p,P *pr)
223: {
224: DCP dc,dcr,dcr0;
225: GFS a;
226: P t;
227:
228: if ( !p )
229: *pr = 0;
230: else if ( NUM(p) ) {
231: if ( NID((Num)p) == N_GFS )
232: *pr = (P)p;
233: else {
234: qtogfs((Q)p,&a); *pr = (P)a;
235: }
236: } else {
237: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
238: ptosfp(COEF(dc),&t);
239: if ( t ) {
240: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
241: }
242: }
243: if ( !dcr0 )
244: *pr = 0;
245: else {
246: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
247: }
248: }
249: }
250:
251: void sfptop(P f,P *gp)
252: {
253: DCP dc,dcr,dcr0;
254: Z q;
255: MQ fq;
256:
257: if ( !f )
258: *gp = 0;
259: else if ( NUM(f) ) {
260: gfstomq((GFS)f,&fq);
1.2 ! noro 261: STOZ(CONT(fq),q);
1.1 noro 262: *gp = (P)q;
263: } else {
264: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
265: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); sfptop(COEF(dc),&COEF(dcr));
266: }
267: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
268: }
269: }
270:
271: void sfptopsfp(P f,V v,P *gp)
272: {
273: DCP dc,dcr,dcr0;
274: Q q;
275: P fq;
276:
277: if ( !f )
278: *gp = 0;
279: else if ( NUM(f) )
280: gfstopgfs((GFS)f,v,gp);
281: else {
282: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
283: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc);
284: sfptopsfp(COEF(dc),v,&COEF(dcr));
285: }
286: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
287: }
288: }
289:
290: void sf_galois_action(P p,Q e,P *pr)
291: {
292: DCP dc,dcr,dcr0;
293: GFS a;
294: P t;
295:
296: if ( !p )
297: *pr = 0;
298: else if ( NUM(p) ) {
299: gfs_galois_action((GFS)p,e,&a); *pr = (P)a;
300: } else {
301: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
302: sf_galois_action(COEF(dc),e,&t);
303: if ( t ) {
304: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
305: }
306: }
307: if ( !dcr0 )
308: *pr = 0;
309: else {
310: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
311: }
312: }
313: }
314:
315: /* GF(pn)={0,1,a,a^2,...} -> GF(pm)={0,1,b,b^2,..} ; a -> b^k */
316:
317: void sf_embed(P p,int k,int pm,P *pr)
318: {
319: DCP dc,dcr,dcr0;
320: GFS a;
321: P t;
322:
323: if ( !p )
324: *pr = 0;
325: else if ( NUM(p) ) {
326: gfs_embed((GFS)p,k,pm,&a); *pr = (P)a;
327: } else {
328: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
329: sf_embed(COEF(dc),k,pm,&t);
330: if ( t ) {
331: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
332: }
333: }
334: if ( !dcr0 )
335: *pr = 0;
336: else {
337: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
338: }
339: }
340: }
341:
342: void ptolmp(P p,P *pr)
343: {
344: DCP dc,dcr,dcr0;
345: LM a;
346: P t;
347:
348: if ( !p )
349: *pr = 0;
350: else if ( NUM(p) ) {
351: qtolm((Q)p,&a); *pr = (P)a;
352: } else {
353: for ( dc = DC(p), dcr0 = 0; dc; dc = NEXT(dc) ) {
354: ptolmp(COEF(dc),&t);
355: if ( t ) {
356: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); COEF(dcr) = t;
357: }
358: }
359: if ( !dcr0 )
360: *pr = 0;
361: else {
362: NEXT(dcr) = 0; MKP(VR(p),dcr0,*pr);
363: }
364: }
365: }
366:
367: void lmptop(P f,P *gp)
368: {
369: DCP dc,dcr,dcr0;
370: Z q;
371:
372: if ( !f )
373: *gp = 0;
374: else if ( NUM(f) ) {
375: MPZTOZ(((LM)f)->body,q); *gp = (P)q;
376: } else {
377: for ( dc = DC(f), dcr0 = 0; dc; dc = NEXT(dc) ) {
378: NEXTDC(dcr0,dcr); DEG(dcr) = DEG(dc); lmptop(COEF(dc),&COEF(dcr));
379: }
380: NEXT(dcr) = 0; MKP(VR(f),dcr0,*gp);
381: }
382: }
383:
384: void ptoum(int m,P f,UM wf)
385: {
386: unsigned int r;
387: int i;
388: DCP dc;
389:
390: for ( i = UDEG(f); i >= 0; i-- )
391: COEF(wf)[i] = 0;
392:
393: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
394: r = remqi(((Q)COEF(dc)),m);
1.2 ! noro 395: COEF(wf)[ZTOS(DEG(dc))] = r;
1.1 noro 396: }
397: degum(wf,UDEG(f));
398: }
399:
400: void umtop(V v,UM w,P *f)
401: {
402: int *c;
403: DCP dc,dc0;
404: int i;
405: Z q;
406:
407: if ( DEG(w) < 0 )
408: *f = 0;
409: else if ( DEG(w) == 0 )
1.2 ! noro 410: STOZ(COEF(w)[0],q), *f = (P)q;
1.1 noro 411: else {
412: for ( i = DEG(w), c = COEF(w), dc0 = 0; i >= 0; i-- )
413: if ( c[i] ) {
414: NEXTDC(dc0,dc);
1.2 ! noro 415: STOZ(i,DEG(dc));
! 416: STOZ(c[i],q), COEF(dc) = (P)q;
1.1 noro 417: }
418: NEXT(dc) = 0;
419: MKP(v,dc0,*f);
420: }
421: }
422:
423: void ptosfum(P f,UM wf)
424: {
425: GFS c;
426: int i;
427: DCP dc;
428:
429: if ( OID(f) == O_N ) {
430: DEG(wf) = 0;
431: ntogfs((Obj)f,&c);
432: COEF(wf)[0] = FTOIF(CONT(c));
433: return;
434: }
435:
436: for ( i = UDEG(f); i >= 0; i-- )
437: COEF(wf)[i] = 0;
438:
439: for ( dc = DC(f); dc; dc = NEXT(dc) ) {
440: ntogfs((Obj)COEF(dc),&c);
441: if ( c )
1.2 ! noro 442: COEF(wf)[ZTOS(DEG(dc))] = FTOIF(CONT(c));
1.1 noro 443: }
444: degum(wf,UDEG(f));
445: }
446:
447: void sfumtop(V v,UM w,P *f)
448: {
449: int *c;
450: DCP dc,dc0;
451: int i,t;
452: GFS q;
453:
454: if ( DEG(w) < 0 )
455: *f = 0;
456: else if ( DEG(w) == 0 ) {
457: t = COEF(w)[0];
458: t = IFTOF(t);
459: MKGFS(t,q);
460: *f = (P)q;
461: } else {
462: for ( i = DEG(w), c = COEF(w), dc0 = 0; i >= 0; i-- )
463: if ( c[i] ) {
464: NEXTDC(dc0,dc);
1.2 ! noro 465: STOZ(i,DEG(dc));
1.1 noro 466: t = COEF(w)[i];
467: t = IFTOF(t);
468: MKGFS(t,q);
469: COEF(dc) = (P)q;
470: }
471: NEXT(dc) = 0;
472: MKP(v,dc0,*f);
473: }
474: }
475:
476: void ptoup(P n,UP *nr)
477: {
478: DCP dc;
479: UP r;
480: int d;
481:
482: if ( !n )
483: *nr = 0;
484: else if ( OID(n) == O_N ) {
485: *nr = r = UPALLOC(0);
486: DEG(r) = 0; COEF(r)[0] = (Num)n;
487: } else {
488: d = UDEG(n);
489: up_var = VR(n);
490: *nr = r = UPALLOC(d); DEG(r) = d;
491: for ( dc = DC(n); dc; dc = NEXT(dc) ) {
1.2 ! noro 492: COEF(r)[ZTOS(DEG(dc))] = (Num)COEF(dc);
1.1 noro 493: }
494: }
495: }
496:
497: void uptop(UP n,P *nr)
498: {
499: int i;
500: DCP dc0,dc;
501:
502: if ( !n )
503: *nr = 0;
504: else if ( !DEG(n) )
505: *nr = (P)COEF(n)[0];
506: else {
507: for ( i = DEG(n), dc0 = 0; i >= 0; i-- )
508: if ( COEF(n)[i] ) {
1.2 ! noro 509: NEXTDC(dc0,dc); STOZ(i,DEG(dc)); COEF(dc) = (P)COEF(n)[i];
1.1 noro 510: }
511: if ( !up_var )
512: up_var = CO->v;
513: MKP(up_var,dc0,*nr);
514: }
515: }
516:
517: void ulmptoum(int m,UP f,UM wf)
518: {
519: int i,d;
520: LM *c;
521: Z z;
522:
523: if ( !f )
524: wf->d = -1;
525: else {
526: wf->d = d = f->d;
527: c = (LM *)f->c;
528: for ( i = 0, d = f->d; i <= d; i++ ) {
529: MPZTOZ(BDY(c[i]),z);
530: COEF(wf)[i] = remqi((Q)z,m);
531: }
532: }
533: }
534:
535: void mptoum(P p,UM pr)
536: {
537: DCP dc;
538:
539: if ( !p )
540: DEG(pr) = -1;
541: else if ( NUM(p) ) {
542: DEG(pr) = 0; COEF(pr)[0] = CONT((MQ)p);
543: } else {
544: bzero((char *)pr,(int)((UDEG(p)+2)*sizeof(int)));
545: for ( dc = DC(p); dc; dc = NEXT(dc) )
1.2 ! noro 546: COEF(pr)[ZTOS(DEG(dc))] = CONT((MQ)COEF(dc));
1.1 noro 547: degum(pr,UDEG(p));
548: }
549: }
550:
551: void umtomp(V v,UM p,P *pr)
552: {
553: DCP dc,dc0;
554: int i;
555: MQ q;
556:
557: if ( !p || (DEG(p) < 0) )
558: *pr = 0;
559: else if ( !DEG(p) )
560: STOMQ(COEF(p)[0],q), *pr = (P)q;
561: else {
562: for ( dc0 = 0, i = DEG(p); i >= 0; i-- )
563: if ( COEF(p)[i] ) {
1.2 ! noro 564: NEXTDC(dc0,dc); STOZ(i,DEG(dc));
1.1 noro 565: STOMQ(COEF(p)[i],q), COEF(dc) = (P)q;
566: }
567: NEXT(dc) = 0; MKP(v,dc0,*pr);
568: }
569: }
570:
571: /* f(p) -> f(x) */
572:
573: void enc_to_p(int p,int a,V v,P *pr)
574: {
575: DCP dc,dct;
576: int i,c;
577: Z dq,cq;
578:
579: dc = 0;
580: for ( i = 0; a; i++, a /= p ) {
581: c = a%p;
582: if ( c ) {
1.2 ! noro 583: STOZ(i,dq); STOZ(c,cq);
1.1 noro 584: NEWDC(dct); DEG(dct) = dq; COEF(dct) = (P)cq;
585: NEXT(dct) = dc; dc = dct;
586: }
587: }
588: MKP(v,dc,*pr);
589: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>