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

Annotation of OpenXM_contrib2/asir2018/engine/lmi.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/lmi.c,v 1.1 2018/09/19 05:45:07 noro Exp $
1.1       noro       49: */
                     50: #include "ca.h"
                     51: #include "base.h"
                     52: #include "inline.h"
                     53:
                     54: int lm_lazy;
                     55:
                     56: static LM current_mod_lm;
                     57:
                     58: void random_lm(LM *r)
                     59: {
                     60:   Z n;
                     61:   LM s;
                     62:
                     63:   if ( !current_mod_lm )
                     64:     error("random_lm : current_mod_lm is not set");
                     65:   randomz(z_bits((Q)current_mod_lm)+1,&n);
                     66:   MKLM(BDY(n),s);
                     67:   simplm_force(s,r);
                     68: }
                     69:
                     70: void setmod_lm(Z p)
                     71: {
                     72:   MKLM(BDY(p),current_mod_lm);
                     73:   setmod_lf(p);
                     74: }
                     75:
                     76: void getmod_lm(Z *p)
                     77: {
                     78:   if ( !current_mod_lm )
                     79:     *p = 0;
                     80:   else
                     81:     MPZTOZ(BDY(current_mod_lm),*p);
                     82: }
                     83:
                     84: void simplm(LM n,LM *r)
                     85: {
                     86:   mpz_t rem;
                     87:
1.2     ! noro       88:   if ( !n || NID(n) != N_LM || lm_lazy )
1.1       noro       89:     *r = n;
                     90:   else {
                     91:     mpz_init(rem);
                     92:     mpz_mod(rem,BDY(n),BDY(current_mod_lm));
1.2     ! noro       93:     if ( mpz_sgn(rem) )
        !            94:       MKLM(rem,*r);
        !            95:     else
        !            96:       *r = 0;
1.1       noro       97:   }
                     98: }
                     99:
                    100: void simplm_force(LM n,LM *r)
                    101: {
                    102:   mpz_t rem;
                    103:
                    104:   if ( !n || NID(n) != N_LM )
                    105:     *r = n;
                    106:   else {
                    107:     mpz_init(rem);
                    108:     mpz_mod(rem,BDY(n),BDY(current_mod_lm));
1.2     ! noro      109:     if ( mpz_sgn(rem) )
        !           110:       MKLM(rem,*r);
        !           111:     else
        !           112:       *r = 0;
1.1       noro      113:   }
                    114: }
                    115:
                    116: void qtolm(Q q,LM *l)
                    117: {
1.2     ! noro      118:   LM nm0,nm,nm1,dn,dn1;
        !           119:   mpz_t t;
1.1       noro      120:
                    121:   if ( !q || (OID(q)==O_N && ((NID(q) == N_LM) || (NID(q) == N_GFPN))) ) { /* XXX */
                    122:     *l = (LM)q;
                    123:   } else if ( OID(q) == O_N && NID(q) == N_Q ) {
                    124:     if ( q->z ) {
1.2     ! noro      125:       if ( mpz_sgn(BDY((Z)q)) > 0 ) {
        !           126:         MKLM(BDY((Z)q),nm);
        !           127:         simplm(nm,l);
        !           128:       } else {
        !           129:         mpz_init(t); mpz_neg(t,BDY((Z)q));
        !           130:         MKLM(t,nm); simplm(nm,&nm1); chsgnlm(nm1,l);
        !           131:       }
1.1       noro      132:     } else {
1.2     ! noro      133:       if ( mpq_sgn(BDY(q)) > 0 ) {
        !           134:         MKLM(mpq_numref(BDY(q)),nm);
        !           135:         simplm_force(nm,&nm1);
        !           136:       } else {
        !           137:         mpz_init(t); mpz_neg(t,mpq_numref(BDY(q)));
        !           138:         MKLM(t,nm0); simplm(nm0,&nm); chsgnlm(nm,&nm1);
        !           139:       }
1.1       noro      140:       MKLM(mpq_denref(BDY(q)),dn);
1.2     ! noro      141:       simplm_force(dn,&dn1);
1.1       noro      142:       divlm(nm1,dn1,l);
                    143:     }
                    144:   } else
                    145:     error("qtolm : invalid argument");
                    146: }
                    147:
                    148: #define NZLM(a) ((a)&&(NID(a)==N_LM))
                    149:
                    150: void addlm(LM a,LM b,LM *c)
                    151: {
                    152:   mpz_t t;
                    153:   LM s,z;
                    154:
                    155:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    156:   if ( !a )
                    157:     *c = b;
                    158:   else if ( !b )
                    159:     *c = a;
                    160:   else {
                    161:     mpz_init(t); mpz_add(t,BDY(a),BDY(b));
                    162:     MKLM(t,s); simplm(s,c);
                    163:   }
                    164: }
                    165:
                    166: void sublm(LM a,LM b,LM *c)
                    167: {
                    168:   mpz_t t;
                    169:   LM s,z;
1.2     ! noro      170:   int sgn;
1.1       noro      171:
                    172:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    173:   if ( !b )
                    174:     *c = a;
                    175:   else if ( !a )
                    176:     chsgnlm(b,c);
                    177:   else {
1.2     ! noro      178:     sgn = mpz_cmp(BDY(a),BDY(b));
        !           179:     if ( sgn > 0 ) {
        !           180:       mpz_init(t); mpz_sub(t,BDY(a),BDY(b));
        !           181:       MKLM(t,*c);
        !           182:     } else if ( sgn < 0 ) {
        !           183:       mpz_init(t); mpz_sub(t,BDY(b),BDY(a));
        !           184:       MKLM(t,s); chsgnlm(s,c);
        !           185:     } else
        !           186:       *c = 0;
1.1       noro      187:   }
                    188: }
                    189:
                    190: void mullm(LM a,LM b,LM *c)
                    191: {
                    192:   mpz_t t;
                    193:   LM s,z;
                    194:
                    195:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    196:   if ( !a || !b )
                    197:     *c = 0;
                    198:   else {
                    199:     mpz_init(t); mpz_mul(t,BDY(a),BDY(b));
                    200:     MKLM(t,s); simplm(s,c);
                    201:   }
                    202: }
                    203:
                    204: void divlm(LM a,LM b,LM *c)
                    205: {
                    206:   mpz_t t,u;
                    207:   LM s,z;
                    208:
                    209:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    210:   if ( !b )
                    211:     error("divlm: division by 0");
                    212:   else if ( !a )
                    213:     *c = 0;
                    214:   else {
                    215:     mpz_init(t); mpz_invert(t,BDY(b),BDY(current_mod_lm));
                    216:     mpz_init(u); mpz_mul(u,BDY(a),t);
                    217:     MKLM(u,s); simplm(s,c);
                    218:   }
                    219: }
                    220:
                    221: void chsgnlm(LM a,LM *c)
                    222: {
                    223:   LM t;
                    224:
                    225:   if ( !a )
                    226:     *c = a;
                    227:   else {
                    228:     qtolm((Q)a,&t); a = t;
                    229:     simplm_force(a,&t);
                    230:     sublm(current_mod_lm,t,c);
                    231:   }
                    232: }
                    233:
                    234: void pwrlm(LM a,Z b,LM *c)
                    235: {
                    236:   mpz_t t;
                    237:
                    238:   if ( !b )
                    239:     MKLM(BDY(ONE),*c);
                    240:   else if ( !a )
                    241:     *c = 0;
                    242:   else {
                    243:     mpz_init(t); mpz_powm(t,BDY(a),BDY(b),BDY(current_mod_lm));
                    244:     MKLM(t,*c);
                    245:   }
                    246: }
                    247:
                    248: int cmplm(LM a,LM b)
                    249: {
                    250:   LM z;
                    251:   int sgn;
                    252:
                    253:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
                    254:   if ( !a )
                    255:     if ( !b )
                    256:       return 0;
                    257:     else
                    258:       return -1;
                    259:   else if ( !b )
                    260:       return 1;
                    261:   else {
                    262:     sgn = mpz_cmp(BDY(a),BDY(b));
                    263:     if ( sgn > 0 ) return 1;
                    264:     else if ( sgn < 0 ) return -1;
                    265:     else return 0;
                    266:   }
                    267: }

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