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