[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.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: #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:
        !            88:   if ( !n || NID(n) != N_LM || !lm_lazy )
        !            89:     *r = n;
        !            90:   else {
        !            91:     mpz_init(rem);
        !            92:     mpz_mod(rem,BDY(n),BDY(current_mod_lm));
        !            93:     MKLM(rem,*r);
        !            94:   }
        !            95: }
        !            96:
        !            97: void simplm_force(LM n,LM *r)
        !            98: {
        !            99:   mpz_t rem;
        !           100:
        !           101:   if ( !n || NID(n) != N_LM )
        !           102:     *r = n;
        !           103:   else {
        !           104:     mpz_init(rem);
        !           105:     mpz_mod(rem,BDY(n),BDY(current_mod_lm));
        !           106:     MKLM(rem,*r);
        !           107:   }
        !           108: }
        !           109:
        !           110: void qtolm(Q q,LM *l)
        !           111: {
        !           112:   LM nm,nm1,dn,dn1;
        !           113:
        !           114:   if ( !q || (OID(q)==O_N && ((NID(q) == N_LM) || (NID(q) == N_GFPN))) ) { /* XXX */
        !           115:     *l = (LM)q;
        !           116:   } else if ( OID(q) == O_N && NID(q) == N_Q ) {
        !           117:     if ( q->z ) {
        !           118:       MKLM(BDY((Z)q),nm);
        !           119:       simplm(nm,l);
        !           120:     } else {
        !           121:       MKLM(mpq_numref(BDY(q)),nm);
        !           122:       simplm_force(nm,&nm1);
        !           123:       MKLM(mpq_denref(BDY(q)),dn);
        !           124:       simplm_force(nm,&dn1);
        !           125:       divlm(nm1,dn1,l);
        !           126:     }
        !           127:   } else
        !           128:     error("qtolm : invalid argument");
        !           129: }
        !           130:
        !           131: #define NZLM(a) ((a)&&(NID(a)==N_LM))
        !           132:
        !           133: void addlm(LM a,LM b,LM *c)
        !           134: {
        !           135:   mpz_t t;
        !           136:   LM s,z;
        !           137:
        !           138:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
        !           139:   if ( !a )
        !           140:     *c = b;
        !           141:   else if ( !b )
        !           142:     *c = a;
        !           143:   else {
        !           144:     mpz_init(t); mpz_add(t,BDY(a),BDY(b));
        !           145:     MKLM(t,s); simplm(s,c);
        !           146:   }
        !           147: }
        !           148:
        !           149: void sublm(LM a,LM b,LM *c)
        !           150: {
        !           151:   mpz_t t;
        !           152:   LM s,z;
        !           153:
        !           154:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
        !           155:   if ( !b )
        !           156:     *c = a;
        !           157:   else if ( !a )
        !           158:     chsgnlm(b,c);
        !           159:   else {
        !           160:     mpz_init(t); mpz_sub(t,BDY(a),BDY(b));
        !           161:     MKLM(t,s); simplm(s,c);
        !           162:   }
        !           163: }
        !           164:
        !           165: void mullm(LM a,LM b,LM *c)
        !           166: {
        !           167:   mpz_t t;
        !           168:   LM s,z;
        !           169:
        !           170:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
        !           171:   if ( !a || !b )
        !           172:     *c = 0;
        !           173:   else {
        !           174:     mpz_init(t); mpz_mul(t,BDY(a),BDY(b));
        !           175:     MKLM(t,s); simplm(s,c);
        !           176:   }
        !           177: }
        !           178:
        !           179: void divlm(LM a,LM b,LM *c)
        !           180: {
        !           181:   mpz_t t,u;
        !           182:   LM s,z;
        !           183:
        !           184:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
        !           185:   if ( !b )
        !           186:     error("divlm: division by 0");
        !           187:   else if ( !a )
        !           188:     *c = 0;
        !           189:   else {
        !           190:     mpz_init(t); mpz_invert(t,BDY(b),BDY(current_mod_lm));
        !           191:     mpz_init(u); mpz_mul(u,BDY(a),t);
        !           192:     MKLM(u,s); simplm(s,c);
        !           193:   }
        !           194: }
        !           195:
        !           196: void chsgnlm(LM a,LM *c)
        !           197: {
        !           198:   LM t;
        !           199:
        !           200:   if ( !a )
        !           201:     *c = a;
        !           202:   else {
        !           203:     qtolm((Q)a,&t); a = t;
        !           204:     simplm_force(a,&t);
        !           205:     sublm(current_mod_lm,t,c);
        !           206:   }
        !           207: }
        !           208:
        !           209: void pwrlm(LM a,Z b,LM *c)
        !           210: {
        !           211:   mpz_t t;
        !           212:
        !           213:   if ( !b )
        !           214:     MKLM(BDY(ONE),*c);
        !           215:   else if ( !a )
        !           216:     *c = 0;
        !           217:   else {
        !           218:     mpz_init(t); mpz_powm(t,BDY(a),BDY(b),BDY(current_mod_lm));
        !           219:     MKLM(t,*c);
        !           220:   }
        !           221: }
        !           222:
        !           223: int cmplm(LM a,LM b)
        !           224: {
        !           225:   LM z;
        !           226:   int sgn;
        !           227:
        !           228:   qtolm((Q)a,&z); a = z; qtolm((Q)b,&z); b = z;
        !           229:   if ( !a )
        !           230:     if ( !b )
        !           231:       return 0;
        !           232:     else
        !           233:       return -1;
        !           234:   else if ( !b )
        !           235:       return 1;
        !           236:   else {
        !           237:     sgn = mpz_cmp(BDY(a),BDY(b));
        !           238:     if ( sgn > 0 ) return 1;
        !           239:     else if ( sgn < 0 ) return -1;
        !           240:     else return 0;
        !           241:   }
        !           242: }

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