[BACK]Return to gf2n.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / engine

Annotation of OpenXM_contrib2/asir2000/engine/gf2n.c, Revision 1.1.1.1

1.1       noro        1: /* $OpenXM: OpenXM/src/asir99/engine/gf2n.c,v 1.1.1.1 1999/11/10 08:12:26 noro Exp $ */
                      2: #include "ca.h"
                      3: #include "base.h"
                      4:
                      5: extern int lm_lazy;
                      6:
                      7: GEN_UP2 current_mod_gf2n;
                      8:
                      9: void setmod_gf2n(p)
                     10: P p;
                     11: {
                     12:        if ( !current_mod_gf2n ) {
                     13:                current_mod_gf2n = (GEN_UP2)MALLOC(sizeof(struct oGEN_UP2));
                     14:                current_mod_gf2n->id = UP2_SPARSE; /* use sparse rep. by default */
                     15:        }
                     16:        ptoup2(p,&current_mod_gf2n->dense);
                     17:        ptoup2_sparse(p,&current_mod_gf2n->sparse);
                     18: }
                     19:
                     20: void getmod_gf2n(p)
                     21: UP2 *p;
                     22: {
                     23:        if ( current_mod_gf2n )
                     24:                *p = current_mod_gf2n->dense;
                     25:        else
                     26:                *p = 0;
                     27: }
                     28:
                     29: void simpgf2n(n,r)
                     30: GF2N n;
                     31: GF2N *r;
                     32: {
                     33:        UP2 rem;
                     34:
                     35:        if ( !n )
                     36:                *r = 0;
                     37:        else if ( NID(n) != N_GF2N )
                     38:                *r = n;
                     39:        else {
                     40:                gen_simpup2(n->body,current_mod_gf2n,&rem);
                     41:                MKGF2N(rem,*r);
                     42:        }
                     43: }
                     44:
                     45: void ptogf2n(q,l)
                     46: Obj q;
                     47: GF2N *l;
                     48: {
                     49:        UP2 q1;
                     50:
                     51:        if ( !q || (OID(q)==O_N && NID(q)==N_GF2N) ) {
                     52:                *l = (GF2N)q;
                     53:        } else if ( (OID(q)==O_N && NID(q)==N_Q) || OID(q)==O_P ) {
                     54:                ptoup2((P)q,&q1);
                     55:                MKGF2N(q1,*l);
                     56:        } else
                     57:                error("ptogf2n : invalid argument");
                     58: }
                     59:
                     60: void gf2ntop(q,l)
                     61: GF2N q;
                     62: P *l;
                     63: {
                     64:        if ( !q )
                     65:                *l = 0;
                     66:        else
                     67:                up2top(q->body,l);
                     68: }
                     69:
                     70: void gf2ntovect(q,l)
                     71: GF2N q;
                     72: VECT *l;
                     73: {
                     74:        if ( !q )
                     75:                *l = 0;
                     76:        else
                     77:                up2tovect(q->body,l);
                     78: }
                     79:
                     80: #define NZGF2N(a) ((a)&&(OID(a)==O_N)&&(NID(a)==N_GF2N))
                     81:
                     82: void addgf2n(a,b,c)
                     83: GF2N a,b;
                     84: GF2N *c;
                     85: {
                     86:        UP2 t,t1;
                     87:        GF2N z;
                     88:
                     89:        ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
                     90:        if ( !a )
                     91:                *c = b;
                     92:        else if ( !b )
                     93:                *c = a;
                     94:        else {
                     95:                addup2(a->body,b->body,&t);
                     96:                gen_simpup2(t,current_mod_gf2n,&t1);
                     97:                MKGF2N(t1,*c);
                     98:        }
                     99: }
                    100:
                    101: void subgf2n(a,b,c)
                    102: GF2N a,b;
                    103: GF2N *c;
                    104: {
                    105:        addgf2n(a,b,c);
                    106: }
                    107:
                    108: void mulgf2n(a,b,c)
                    109: GF2N a,b;
                    110: GF2N *c;
                    111: {
                    112:        UP2 t;
                    113:        GF2N z;
                    114:
                    115:        ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
                    116:        if ( !a || !b )
                    117:                *c = 0;
                    118:        else {
                    119:                mulup2(a->body,b->body,&t);
                    120: #if 0
                    121:                gen_simpup2(t,current_mod_gf2n,&t1);
                    122:                MKGF2N(t1,*c);
                    123: #else
                    124:                gen_simpup2_destructive(t,current_mod_gf2n);
                    125:                if ( !t || !t->w )
                    126:                        *c = 0;
                    127:                else
                    128:                        MKGF2N(t,*c);
                    129: #endif
                    130:        }
                    131: }
                    132:
                    133: void squaregf2n(a,c)
                    134: GF2N a;
                    135: GF2N *c;
                    136: {
                    137:        UP2 t;
                    138:        GF2N z;
                    139:
                    140:        ptogf2n((Obj)a,&z); a = z;
                    141:        if ( !a )
                    142:                *c = 0;
                    143:        else {
                    144:                squareup2(a->body,&t);
                    145: #if 0
                    146:                gen_simpup2(t,current_mod_gf2n,&t1);
                    147:                MKGF2N(t1,*c);
                    148: #else
                    149:                gen_simpup2_destructive(t,current_mod_gf2n);
                    150:                if ( !t || !t->w )
                    151:                        *c = 0;
                    152:                else
                    153:                        MKGF2N(t,*c);
                    154: #endif
                    155:        }
                    156: }
                    157:
                    158: void divgf2n(a,b,c)
                    159: GF2N a,b;
                    160: GF2N *c;
                    161: {
                    162:        UP2 t,i,s;
                    163:        GF2N z;
                    164:
                    165:        ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
                    166:        if ( !b )
                    167:                error("divgf2n: division by 0");
                    168:        else if ( !a )
                    169:                *c = 0;
                    170:        else {
                    171:                gen_invup2(b->body,current_mod_gf2n,&i);
                    172:                mulup2(a->body,i,&t);
                    173:                gen_simpup2(t,current_mod_gf2n,&s);
                    174:                MKGF2N(s,*c);
                    175:        }
                    176: }
                    177:
                    178: void invgf2n(b,c)
                    179: GF2N b;
                    180: GF2N *c;
                    181: {
                    182:        UP2 i;
                    183:        GF2N z;
                    184:
                    185:        ptogf2n((Obj)b,&z); b = z;
                    186:        if ( !b )
                    187:                error("divgf2n: division by 0");
                    188:        else {
                    189:                gen_invup2(b->body,current_mod_gf2n,&i);
                    190:                MKGF2N(i,*c);
                    191:        }
                    192: }
                    193:
                    194: void chsgngf2n(a,c)
                    195: GF2N a,*c;
                    196: {
                    197:        *c = a;
                    198: }
                    199:
                    200: void pwrgf2n(a,b,c)
                    201: GF2N a;
                    202: Q b;
                    203: GF2N *c;
                    204: {
                    205:        UP2 t;
                    206:        GF2N r;
                    207:
                    208:        if ( !b ) {
                    209:                MKGF2N(ONEUP2,*c);
                    210:        } else if ( !a )
                    211:                *c = 0;
                    212:        else {
                    213:                gen_pwrmodup2(a->body,b,current_mod_gf2n,&t);
                    214:                MKGF2N(t,r);
                    215:                if ( SGN(b) < 0 )
                    216:                        invgf2n(r,c);
                    217:                else
                    218:                        *c = r;
                    219:        }
                    220: }
                    221:
                    222: int cmpgf2n(a,b)
                    223: GF2N a,b;
                    224: {
                    225:        GF2N z;
                    226:
                    227:        ptogf2n((Obj)a,&z); a = z; ptogf2n((Obj)b,&z); b = z;
                    228:        if ( !a )
                    229:                if ( !b )
                    230:                        return 0;
                    231:                else
                    232:                        return -1;
                    233:        else if ( !b )
                    234:                        return 1;
                    235:        else
                    236:                return compup2(a->body,b->body);
                    237: }
                    238:
                    239: void randomgf2n(r)
                    240: GF2N *r;
                    241: {
                    242:        int i,w,d;
                    243:        unsigned int *tb;
                    244:        UP2 t;
                    245:
                    246:        if ( !current_mod_gf2n )
                    247:                error("randomgf2n : current_mod_gf2n is not set");
                    248:        w = current_mod_gf2n->dense->w;
                    249:        d = degup2(current_mod_gf2n->dense);
                    250:        NEWUP2(t,w);
                    251:        for ( i = 0, tb = t->b; i < w; i++ )
                    252:                tb[i] = mt_genrand();
                    253:        tb[w-1] &= (1<<(d%BSH))-1;
                    254:        for ( i = w-1; i >= 0 && !tb[i]; i-- );
                    255:        if ( i < 0 )
                    256:                *r = 0;
                    257:        else {
                    258:                t->w = i+1; MKGF2N(t,*r);
                    259:        }
                    260: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>