void BigInt_PowerModulus(BigInt n, BigInt exp, BigInt modulus, BigInt answer) { DIGIT *eptr, *eend, emask; BigInt p, temp, r; r=BigInt_Zero(); /* If n is negative and the exponent is odd, the answer will be negative. */ int neg=(n->negative && (exp->digits[0]&0x01)); p=BigInt_Create(); temp=BigInt_Create(); if(modulus) r=BigInt_Create(); BigInt_Copy(p, n); p->negative=0; BigInt_Set(answer, 1); /* Continue this loop while the exponent is not zero */ eptr=exp->digits; eend=eptr+exp->length; emask=0x01; while(eptr<eend) { /* If e is odd, multiply the answer by p */ if((*eptr) & emask) { BigInt_Copy(temp, answer); BigInt_Multiply(temp, p, answer); if(modulus) { BigInt_Modulus(answer, modulus, r); BigInt_Copy(answer, r); } } /* Now square p (mod modulus, if supplied) */ BigInt_Multiply(p, p, temp); if(modulus) BigInt_Modulus(temp, modulus, p); else BigInt_Copy(p, temp); /* Shift e right by one bit */ emask<<=1; if(emask==0) { ++eptr; emask=0x01; } } answer->negative=neg; if(modulus) BigInt_Destroy(r); BigInt_Destroy(temp); BigInt_Destroy(p); }
char* EncodeShortV3(unsigned char* keybytes, int keylength, bool level10) { char* cc; char* shortv3digits=(char*)"0123456789ABCDEFGHJKMNPQRTUVWXYZ"; static char retval[512]=""; int level=0; int dcount; int nn; if(level10) level=29; strcpy(retval, ""); BigInt n=BigInt_Create(); BigInt t1=BigInt_Create(); BigInt t2=BigInt_Create(); if(level==29) BigInt_Set(n, 1); for(int x=0; x<keylength; ++x) { BigInt_Shift(n, 8, t1); BigInt_SetU(t2, keybytes[x]); BigInt_Add(t1, t2, n); } cc=retval; dcount=6; while(BigInt_Compare(n, BigInt_Zero())!=0) { BigInt_SetU(t2, 32); BigInt_Modulus(n, t2, t1); nn=BigInt_Get(t1); BigInt_Shift(n, -5, t2); BigInt_Copy(n, t2); if(level==29) { *cc++=shortv3digits[nn]; if(--dcount==0) { dcount=6; *cc++='-'; } } else { if(BigInt_Compare(n, BigInt_Zero())==0) { if(nn<16) { *cc++=shortv3digits[nn+16]; --dcount; } else { *cc++=shortv3digits[nn]; if(--dcount==0) { dcount=6; *cc++='-'; } *cc++=shortv3digits[16]; --dcount; } } else { *cc++=shortv3digits[nn]; if(--dcount==0) { dcount=6; *cc++='-'; } } } } if(level==29) { *cc++='1'; --dcount; } while(dcount-->0) *cc++='0'; *cc=0; mystrrev(retval); BigInt_Destroy(t2); BigInt_Destroy(t1); BigInt_Destroy(n); return retval; }