Annotation of OpenXM_contrib2/asir2000/engine/gf2n.c, Revision 1.3
1.2 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.3 ! noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 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.3 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/gf2n.c,v 1.2 2000/08/21 08:31:27 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "base.h"
52:
53: extern int lm_lazy;
54:
55: GEN_UP2 current_mod_gf2n;
56:
57: void setmod_gf2n(p)
58: P p;
59: {
60: if ( !current_mod_gf2n ) {
61: current_mod_gf2n = (GEN_UP2)MALLOC(sizeof(struct oGEN_UP2));
62: current_mod_gf2n->id = UP2_SPARSE; /* use sparse rep. by default */
63: }
64: ptoup2(p,¤t_mod_gf2n->dense);
65: ptoup2_sparse(p,¤t_mod_gf2n->sparse);
66: }
67:
68: void getmod_gf2n(p)
69: UP2 *p;
70: {
71: if ( current_mod_gf2n )
72: *p = current_mod_gf2n->dense;
73: else
74: *p = 0;
75: }
76:
77: void simpgf2n(n,r)
78: GF2N n;
79: GF2N *r;
80: {
81: UP2 rem;
82:
83: if ( !n )
84: *r = 0;
85: else if ( NID(n) != N_GF2N )
86: *r = n;
87: else {
88: gen_simpup2(n->body,current_mod_gf2n,&rem);
89: MKGF2N(rem,*r);
90: }
91: }
92:
93: void ptogf2n(q,l)
94: Obj q;
95: GF2N *l;
96: {
97: UP2 q1;
98:
99: if ( !q || (OID(q)==O_N && NID(q)==N_GF2N) ) {
100: *l = (GF2N)q;
101: } else if ( (OID(q)==O_N && NID(q)==N_Q) || OID(q)==O_P ) {
102: ptoup2((P)q,&q1);
103: MKGF2N(q1,*l);
104: } else
105: error("ptogf2n : invalid argument");
106: }
107:
108: void gf2ntop(q,l)
109: GF2N q;
110: P *l;
111: {
112: if ( !q )
113: *l = 0;
114: else
115: up2top(q->body,l);
116: }
117:
118: void gf2ntovect(q,l)
119: GF2N q;
120: VECT *l;
121: {
122: if ( !q )
123: *l = 0;
124: else
125: up2tovect(q->body,l);
126: }
127:
128: #define NZGF2N(a) ((a)&&(OID(a)==O_N)&&(NID(a)==N_GF2N))
129:
130: void addgf2n(a,b,c)
131: GF2N a,b;
132: GF2N *c;
133: {
134: UP2 t,t1;
135: GF2N z;
136:
137: ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
138: if ( !a )
139: *c = b;
140: else if ( !b )
141: *c = a;
142: else {
143: addup2(a->body,b->body,&t);
144: gen_simpup2(t,current_mod_gf2n,&t1);
145: MKGF2N(t1,*c);
146: }
147: }
148:
149: void subgf2n(a,b,c)
150: GF2N a,b;
151: GF2N *c;
152: {
153: addgf2n(a,b,c);
154: }
155:
156: void mulgf2n(a,b,c)
157: GF2N a,b;
158: GF2N *c;
159: {
160: UP2 t;
161: GF2N z;
162:
163: ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
164: if ( !a || !b )
165: *c = 0;
166: else {
167: mulup2(a->body,b->body,&t);
168: #if 0
169: gen_simpup2(t,current_mod_gf2n,&t1);
170: MKGF2N(t1,*c);
171: #else
172: gen_simpup2_destructive(t,current_mod_gf2n);
173: if ( !t || !t->w )
174: *c = 0;
175: else
176: MKGF2N(t,*c);
177: #endif
178: }
179: }
180:
181: void squaregf2n(a,c)
182: GF2N a;
183: GF2N *c;
184: {
185: UP2 t;
186: GF2N z;
187:
188: ptogf2n((Obj)a,&z); a = z;
189: if ( !a )
190: *c = 0;
191: else {
192: squareup2(a->body,&t);
193: #if 0
194: gen_simpup2(t,current_mod_gf2n,&t1);
195: MKGF2N(t1,*c);
196: #else
197: gen_simpup2_destructive(t,current_mod_gf2n);
198: if ( !t || !t->w )
199: *c = 0;
200: else
201: MKGF2N(t,*c);
202: #endif
203: }
204: }
205:
206: void divgf2n(a,b,c)
207: GF2N a,b;
208: GF2N *c;
209: {
210: UP2 t,i,s;
211: GF2N z;
212:
213: ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
214: if ( !b )
215: error("divgf2n: division by 0");
216: else if ( !a )
217: *c = 0;
218: else {
219: gen_invup2(b->body,current_mod_gf2n,&i);
220: mulup2(a->body,i,&t);
221: gen_simpup2(t,current_mod_gf2n,&s);
222: MKGF2N(s,*c);
223: }
224: }
225:
226: void invgf2n(b,c)
227: GF2N b;
228: GF2N *c;
229: {
230: UP2 i;
231: GF2N z;
232:
233: ptogf2n((Obj)b,&z); b = z;
234: if ( !b )
235: error("divgf2n: division by 0");
236: else {
237: gen_invup2(b->body,current_mod_gf2n,&i);
238: MKGF2N(i,*c);
239: }
240: }
241:
242: void chsgngf2n(a,c)
243: GF2N a,*c;
244: {
245: *c = a;
246: }
247:
248: void pwrgf2n(a,b,c)
249: GF2N a;
250: Q b;
251: GF2N *c;
252: {
253: UP2 t;
254: GF2N r;
255:
256: if ( !b ) {
257: MKGF2N(ONEUP2,*c);
258: } else if ( !a )
259: *c = 0;
260: else {
261: gen_pwrmodup2(a->body,b,current_mod_gf2n,&t);
262: MKGF2N(t,r);
263: if ( SGN(b) < 0 )
264: invgf2n(r,c);
265: else
266: *c = r;
267: }
268: }
269:
270: int cmpgf2n(a,b)
271: GF2N a,b;
272: {
273: GF2N z;
274:
275: ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
276: if ( !a )
277: if ( !b )
278: return 0;
279: else
280: return -1;
281: else if ( !b )
282: return 1;
283: else
284: return compup2(a->body,b->body);
285: }
286:
287: void randomgf2n(r)
288: GF2N *r;
289: {
290: int i,w,d;
291: unsigned int *tb;
292: UP2 t;
293:
294: if ( !current_mod_gf2n )
295: error("randomgf2n : current_mod_gf2n is not set");
296: w = current_mod_gf2n->dense->w;
297: d = degup2(current_mod_gf2n->dense);
298: NEWUP2(t,w);
299: for ( i = 0, tb = t->b; i < w; i++ )
300: tb[i] = mt_genrand();
301: tb[w-1] &= (1<<(d%BSH))-1;
302: for ( i = w-1; i >= 0 && !tb[i]; i-- );
303: if ( i < 0 )
304: *r = 0;
305: else {
306: t->w = i+1; MKGF2N(t,*r);
307: }
308: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>