extern "C" ENCODER_DECLARE_EXPORT char * encoder_moves_reg_to_reg(OpndSize size, int reg, bool isPhysical,int reg2, bool isPhysical2, LowOpndRegType type, char * stream) { EncoderBase::Operands args; add_r(args, reg2, OpndSize_32); //destination add_r(args, reg, size); char* stream_start = stream; stream = (char *)EncoderBase::encode(stream, Mnemonic_MOVSX, args); #ifdef PRINT_ENCODER_STREAM printEncoderInst(Mnemonic_MOVSX, args); decodeThenPrint(stream_start); #endif return stream; }
extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg(Mnemonic m, OpndSize size, int reg, bool isPhysical, LowOpndRegType type, char * stream) { EncoderBase::Operands args; if(m == Mnemonic_DIV || m == Mnemonic_IDIV || m == Mnemonic_MUL || m == Mnemonic_IMUL) { add_r(args, 0/*eax*/, size); add_r(args, 3/*edx*/, size); } add_r(args, reg, size); char* stream_start = stream; stream = (char *)EncoderBase::encode(stream, m, args); #ifdef PRINT_ENCODER_STREAM printEncoderInst(m, args); decodeThenPrint(stream_start); #endif return stream; }
static int compile(char *rx) { int r=0,p=0,d_r; d_r=add_r(rx); if((r=ht_get(&ht_r,i_r))==-1) { if(rx_compact&&i_p>=P_AVG_SIZE*LIM_P) {rx_clear(); d_r=add_r(rx);} ht_put(&ht_r,r=i_r); i_r+=d_r; bind(r); p=expression(); if(sym!=SYM_END) error(RX_ER_BADCH); r2p[i_2][0]=r; r2p[i_2][1]=p; ht_put(&ht_2,i_2++); if(i_2==len_2) r2p=(int(*)[2])m_stretch(r2p,len_2=2*i_2,i_2,sizeof(int[2])); } else { r2p[i_2][0]=r; p=r2p[ht_get(&ht_2,i_2)][1]; } return p; }
extern "C" ENCODER_DECLARE_EXPORT char * encoder_imm_reg(Mnemonic m, OpndSize size, int imm, int reg, bool isPhysical, LowOpndRegType type, char * stream) { EncoderBase::Operands args; add_r(args, reg, size); //dst if(m == Mnemonic_IMUL) add_r(args, reg, size); //src CHECK if(m == Mnemonic_SAL || m == Mnemonic_SHR || m == Mnemonic_SHL || m == Mnemonic_SAR || m == Mnemonic_ROR) //fix for shift opcodes add_imm(args, OpndSize_8, imm, true/*is_signed*/); else add_imm(args, size, imm, true/*is_signed*/); char* stream_start = stream; stream = (char *)EncoderBase::encode(stream, m, args); #ifdef PRINT_ENCODER_STREAM printEncoderInst(m, args); decodeThenPrint(stream_start); #endif return stream; }
//! \brief Allows for different operand sizes extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg_reg_diff_sizes(Mnemonic m, OpndSize srcOpndSize, int reg, bool isPhysical, OpndSize destOpndSize, int reg2, bool isPhysical2, LowOpndRegType type, char * stream) { if((m == Mnemonic_MOV || m == Mnemonic_MOVQ) && reg == reg2) return stream; EncoderBase::Operands args; add_r(args, reg2, destOpndSize); //destination if(m == Mnemonic_SAL || m == Mnemonic_SHR || m == Mnemonic_SHL || m == Mnemonic_SAR) add_r(args, reg, OpndSize_8); else add_r(args, reg, srcOpndSize); char* stream_start = stream; stream = (char *)EncoderBase::encode(stream, m, args); #ifdef PRINT_ENCODER_STREAM printEncoderInst(m, args); decodeThenPrint(stream_start); #endif return stream; }
extern "C" ENCODER_DECLARE_EXPORT char * encoder_moves_mem_to_reg(OpndSize size, int disp, int base_reg, bool isBasePhysical, int reg, bool isPhysical, char * stream) { EncoderBase::Operands args; add_r(args, reg, OpndSize_32); add_m(args, base_reg, disp, size); char* stream_start = stream; stream = (char *)EncoderBase::encode(stream, Mnemonic_MOVSX, args); #ifdef PRINT_ENCODER_STREAM printEncoderInst(Mnemonic_MOVSX, args); decodeThenPrint(stream_start); #endif return stream; }
extern "C" ENCODER_DECLARE_EXPORT char * encoder_reg_mem(Mnemonic m, OpndSize size, int reg, bool isPhysical, int disp, int base_reg, bool isBasePhysical, LowOpndRegType type, char * stream) { EncoderBase::Operands args; add_m(args, base_reg, disp, size); add_r(args, reg, size); char* stream_start = stream; stream = (char *)EncoderBase::encode(stream, m, args); #ifdef PRINT_ENCODER_STREAM printEncoderInst(m, args); decodeThenPrint(stream_start); #endif return stream; }
long rho(big q,big r,big m,big n) { /* find q^m = r^n */ long iter,rr,i; big ax,bx,ay,by,x,y; ax=mirvar(0); bx=mirvar(0); ay=mirvar(0); by=mirvar(0); x=mirvar(1); y=mirvar(1); iter=0L; rr=1L; do { /* Brent's Cycle finder */ copy(y,x); copy(ay,ax); copy(by,bx); rr*=2; for (i=1L;i<=rr;i++) { iter++; iterate(y,q,r,ay,by); if (compare(x,y)==0) break; } } while (compare(x,y)!=0); subtract(ax,ay,m); if (size(m)<0) add_r(m,order,m); subtract(by,bx,n); if (size(n)<0) add_r(n,order,n); mirkill(y); mirkill(x); mirkill(by); mirkill(ay); mirkill(bx); mirkill(ax); return iter; }
int main() { int i; FILE *fp; big K,rid,id,w,a,b,n,q1; miracl *mip=mirsys(200,256); for (i=0;i<NPRIMES;i++) { pp[i]=mirvar(0); rem[i]=mirvar(0); } w=mirvar(0); n=mirvar(0); a=mirvar(0); b=mirvar(0); p=mirvar(0); p1=mirvar(0); q1=mirvar(0); K=mirvar(0); lim1=mirvar(0); lim2=mirvar(0); id=mirvar(0); rid=mirvar(0); order=mirvar(0); printf("Enter ID= "); innum(rid,stdin); getprime("trap1.dat"); copy(p,n); getprime("trap2.dat"); multiply(n,p,n); printf("\ncomposite =\n"); cotnum(n,stdout); premult(rid,256,id); while (jack(id,n)!=1) { /* bad identity - id=256*rid+i */ printf("No Discrete Log. for this ID -- incrementing\n"); incr(id,1,id); } getprime("trap1.dat"); copy(p1,q1); pollard(id,b); getprime("trap2.dat"); pollard(id,a); xgcd(p1,q1,K,K,K); subtract(b,a,w); mad(w,K,w,q1,q1,w); if(size(w)<0) add_r(w,q1,w); subdiv(w,2,w); multiply(w,p1,w); add_r(w,a,w); fp=fopen("secret.dat","w"); otnum(rid,fp); cotnum(w,fp); cotnum(n,fp); fclose(fp); printf("\nDiscrete log (secret key) \n"); cotnum(w,stdout); powltr(PROOT,w,n,id); subdiv(id,256,id); otstr(id,mip->IOBUFF); printf("Check Identity= %s\n",mip->IOBUFF); return 0; }
int xgcd(_MIPD_ big x,big y,big xd,big yd,big z) { /* greatest common divisor by Euclids method * * extended to also calculate xd and yd where * * z = x.xd + y.yd = gcd(x,y) * * if xd, yd not distinct, only xd calculated * * z only returned if distinct from xd and yd * * xd will always be positive, yd negative */ int q,r,a,b,c,d,s,n; mr_small m,sr; #ifdef mr_dltype mr_large u,v,lq,lr; #else mr_small u,v,lq,lr; #endif BOOL last; big t; #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return 0; MR_IN(30) copy(x,mr_mip->w1); copy(y,mr_mip->w2); s=exsign(mr_mip->w1); insign(PLUS,mr_mip->w1); insign(PLUS,mr_mip->w2); /* copy(mr_mip->w1,mr_mip->w3); copy(mr_mip->w2,mr_mip->w4); */ convert(_MIPP_ 1,mr_mip->w3); zero(mr_mip->w4); last=FALSE; a=b=c=d=0; while (size(mr_mip->w2)!=0) { if (b==0) { /* update mr_mip->w1 and mr_mip->w2 */ divide(_MIPP_ mr_mip->w1,mr_mip->w2,mr_mip->w5); t=mr_mip->w1,mr_mip->w1=mr_mip->w2,mr_mip->w2=t; /* swap(mr_mip->w1,mr_mip->w2) */ multiply(_MIPP_ mr_mip->w4,mr_mip->w5,mr_mip->w0); subtract(_MIPP_ mr_mip->w3,mr_mip->w0,mr_mip->w3); t=mr_mip->w3,mr_mip->w3=mr_mip->w4,mr_mip->w4=t; /* swap(xd,yd) */ } else { premult(_MIPP_ mr_mip->w1,c,mr_mip->w5); premult(_MIPP_ mr_mip->w1,a,mr_mip->w1); premult(_MIPP_ mr_mip->w2,b,mr_mip->w0); premult(_MIPP_ mr_mip->w2,d,mr_mip->w2); add_r(_MIPP_ mr_mip->w1,mr_mip->w0,mr_mip->w1); add_r(_MIPP_ mr_mip->w2,mr_mip->w5,mr_mip->w2); premult(_MIPP_ mr_mip->w3,c,mr_mip->w5); premult(_MIPP_ mr_mip->w3,a,mr_mip->w3); premult(_MIPP_ mr_mip->w4,b,mr_mip->w0); premult(_MIPP_ mr_mip->w4,d,mr_mip->w4); add_r(_MIPP_ mr_mip->w3,mr_mip->w0,mr_mip->w3); add_r(_MIPP_ mr_mip->w4,mr_mip->w5,mr_mip->w4); } if (mr_mip->ERNUM || size(mr_mip->w2)==0) break; n=(int)mr_mip->w1[0]; a=1; b=0; c=0; d=1; if (n==1) { last=TRUE; u=mr_mip->w1[1]; v=mr_mip->w2[1]; } else { m=mr_mip->w1[n]+1; if (mr_mip->base==0) { #ifdef mr_dltype /* use double length type if available */ if (n>2 && m!=0) { /* squeeze out as much significance as possible */ MR_TOP(u)=muldvm(mr_mip->w1[n],mr_mip->w1[n-1],m,&sr); MR_BOT(u)=muldvm(sr,mr_mip->w1[n-2],m,&sr); MR_TOP(v)=muldvm(mr_mip->w2[n],mr_mip->w2[n-1],m,&sr); MR_BOT(v)=muldvm(sr,mr_mip->w2[n-2],m,&sr); } else { MR_TOP(u)=mr_mip->w1[n]; MR_BOT(u)=mr_mip->w1[n-1]; MR_TOP(v)=mr_mip->w2[n]; MR_BOT(v)=mr_mip->w2[n-1]; if (n==2) last=TRUE; } #else if (m==0) { u=mr_mip->w1[n]; v=mr_mip->w2[n]; } else { u=muldvm(mr_mip->w1[n],mr_mip->w1[n-1],m,&sr); v=muldvm(mr_mip->w2[n],mr_mip->w2[n-1],m,&sr); } #endif } else { #ifdef mr_dltype /* use double length type if available */ if (n>2) { /* squeeze out as much significance as possible */ u=muldiv(mr_mip->w1[n],mr_mip->base,mr_mip->w1[n-1],m,&sr); u=u*mr_mip->base+muldiv(sr,mr_mip->base,mr_mip->w1[n-2],m,&sr); v=muldiv(mr_mip->w2[n],mr_mip->base,mr_mip->w2[n-1],m,&sr); v=v*mr_mip->base+muldiv(sr,mr_mip->base,mr_mip->w2[n-2],m,&sr); } else { u=(mr_large)mr_mip->base*mr_mip->w1[n]+mr_mip->w1[n-1]; v=(mr_large)mr_mip->base*mr_mip->w2[n]+mr_mip->w2[n-1]; last=TRUE; } #else u=muldiv(mr_mip->w1[n],mr_mip->base,mr_mip->w1[n-1],m,&sr); v=muldiv(mr_mip->w2[n],mr_mip->base,mr_mip->w2[n-1],m,&sr); #endif } } forever { /* work only with most significant piece */ if (last) { if (v==0) break; lq=u/v; } else { if (((v+c)==0) || ((v+d)==0)) break; lq=(u+a)/(v+c); if (lq!=(u+b)/(v+d)) break; } #ifdef mr_dltype if (lq>=(mr_large)(MR_TOOBIG/abs(d))) break; #else if (lq>=(mr_small)(MR_TOOBIG/abs(d))) break; #endif q=(int)lq; r=a-q*c; a=c; c=r; r=b-q*d; b=d; d=r; lr=u-lq*v; u=v; v=lr; } } if (s==MINUS) negate(mr_mip->w3,mr_mip->w3); if (size(mr_mip->w3)<=0) add_r(_MIPP_ mr_mip->w3,y,mr_mip->w3); if (xd!=yd) { negate(x,mr_mip->w2); mad(_MIPP_ mr_mip->w2,mr_mip->w3,mr_mip->w1,y,mr_mip->w4,mr_mip->w4); copy(mr_mip->w4,yd); } copy(mr_mip->w3,xd); if (z!=xd && z!=yd) copy(mr_mip->w1,z); MR_OUT return (size(mr_mip->w1)); }