void test16(void) { int n1, n2, n3, m; n1 = 0x12345678; n2 = 0x66554433; n3 = 12; bigFromInt(n1); bip.op1 = bip.res; bigFromInt(n2); bip.op2 = bip.res; bigMul(); bip.op1 = bip.res; bigFromInt(n3); bip.op2 = bip.res; bigMul(); bip.op1 = bip.res; m = 0x5764; bigFromInt(m); bip.op2 = bip.res; dump("", bip.op1, " / "); dump("", bip.op2, " =\n"); bigDiv(); dump("", bip.res, " R. "); dump("", bip.rem, "\n"); bip.op1 = bip.res; bigMul(); bip.op1 = bip.res; bip.op2 = bip.rem; bigAdd(); dump("quotient * divisor + remainder =\n", bip.res, "\n"); }
void test17(void) { /* prepare two big integers with 8 and 4 digits, respectively */ bigFromInt(0x11111111); bip.op1 = bip.res; bip.op2 = bip.res; bigMul(); bip.op1 = bip.res; /* set all 8 digits of dividend */ bip.op1->data[5 + 0] = 0xF2; bip.op1->data[5 + 1] = 0xFB; bip.op1->data[5 + 2] = 0xE3; bip.op1->data[5 + 3] = 0x46; bip.op1->data[5 + 4] = 0x7C; bip.op1->data[5 + 5] = 0xC2; bip.op1->data[5 + 6] = 0x54; bip.op1->data[5 + 7] = 0xF8; /* set all 4 digits of divisor */ bip.op2->data[5 + 0] = 0x1B; bip.op2->data[5 + 1] = 0xE8; bip.op2->data[5 + 2] = 0xE7; bip.op2->data[5 + 3] = 0x8D; /* divide */ dump("", bip.op1, " / "); dump("", bip.op2, " =\n"); bigDiv(); dump("", bip.res, " R. "); dump("", bip.rem, "\n"); /* verify */ bip.op1 = bip.res; bigMul(); bip.op1 = bip.res; bip.op2 = bip.rem; bigAdd(); dump("quotient * divisor + remainder =\n", bip.res, "\n"); }
void test18(void) { /* prepare two big integers with 8 and 4 digits, respectively */ bigFromInt(0x11111111); bip.op1 = bip.res; bip.op2 = bip.res; bigMul(); bip.op1 = bip.res; /* set all 8 digits of dividend */ bip.op1->data[5 + 0] = 0x4D; bip.op1->data[5 + 1] = 0xCC; bip.op1->data[5 + 2] = 0x8C; bip.op1->data[5 + 3] = 0x18; bip.op1->data[5 + 4] = 0x34; bip.op1->data[5 + 5] = 0xDF; bip.op1->data[5 + 6] = 0x1D; bip.op1->data[5 + 7] = 0xFD; /* set all 4 digits of divisor */ bip.op2->data[5 + 0] = 0x69; bip.op2->data[5 + 1] = 0xF4; bip.op2->data[5 + 2] = 0x94; bip.op2->data[5 + 3] = 0x37; /* divide */ dump("", bip.op1, " / "); dump("", bip.op2, " =\n"); bigDiv(); dump("", bip.res, " R. "); dump("", bip.rem, "\n"); /* verify */ bip.op1 = bip.res; bigMul(); bip.op1 = bip.res; bip.op2 = bip.rem; bigAdd(); dump("quotient * divisor + remainder =\n", bip.res, "\n"); }
void factorial(int n) { bigFromInt(1); while (n > 0) { bip.op1 = bip.res; bigFromInt(n); bip.op2 = bip.res; bigMul(); n--; } }
void getRSAkeys(uint8_t* e,uint8_t* d,uint8_t* n){ uint32_t expected=0.693147*NUMLEN; uint8_t q[NUMLEN],p[NUMLEN]; *e=0; while(!*e){ #if RSADEBUG0==1 USART0_SendStr("Generating "); USART0_SendNum(PRIMELEN); USART0_SendStr("-byte primes.\n"); USART0_SendStr("Expected number of attempts: "); USART0_SendNum(expected); USART0_SendStr("\n\nGenerating prime (1/2):\n"); #endif getLargePrime(q,PRIMELEN); unsigned temp=bigBitLen(q); USART0_SendStr("==========="); USART0_SendNum(temp); USART0_SendStr("==========="); #if RSADEBUG0==1 USART0_SendStr("Generating prime (2/2):\n"); #endif getLargePrime(p,PRIMELEN); q[0]&=~1;//q-=1 p[0]&=~1;//p-=1 bigCopy(n,q); bigMul(n,p,NULL);//n=(q-1)(p-1)=phi(q*p) if(bigBitLen(n)<=MSGBITLEN) continue; q[0]|=1; p[0]|=1; *e=getOpenExp(n); } bigReverseBS(n,*e,d); bigCopy(n,q); bigMul(n,p,NULL);//n=q*p }
void test09(void) { int i, j; bigFromInt(2); bip.op2 = bip.res; for (i = 1; i < 10; i++) { bip.res = bip.op2; for (j = 1; j < i * i; j++) { bip.op1 = bip.res; bigMul(); } printf("2 ^ (%d ^ 2) = ", i); dump("", bip.res, "\n"); } }
void test14(void) { int n, m; ObjRef dividend; ObjRef divisor; int ok; printf("divisor"); for (m = 0; m < 256; m++) { if (m % 8 == 0) { printf("\n%3d to %3d: ", m, m + 7); } if (m == 0) { printf(" "); fflush(stdout); continue; } bigFromInt(m); divisor = bip.res; ok = 1; for (n = 0; n < (1 << 18); n++) { bigFromInt(n); dividend = bip.res; bip.op1 = dividend; bip.op2 = divisor; bigDiv(); bip.op1 = bip.res; bigMul(); free(bip.op1); bip.op1 = bip.res; bip.op2 = bip.rem; bigAdd(); free(bip.op1); free(bip.op2); bip.op1 = bip.res; bip.op2 = dividend; if (bigCmp() != 0) { ok = 0; } free(bip.op1); free(dividend); } free(divisor); printf("%c", ok ? '.' : '?'); fflush(stdout); } printf("\n"); }
int32_t millerRabin(uint8_t* n){ //Most memory requirement part. mem=8*NUMLEN #if RSADEBUG==1 USART0_SendStr("Start MR test:\n"); #endif if(n[0]&1==0) return 0; uint32_t t=0; int32_t i=0,k; uint8_t temp; uint8_t s[NUMLEN]; uint8_t a[NUMLEN]; int32_t next=0; bigCopy(s,n); s[0]^=1;//s=s-1 while(!s[i]){ ++i; t+=8; } temp=s[i]; while((temp&1)==0){ ++t; temp>>=1; } bigRShift(s,t); k=0; while(k<MILLER_RABIN_K){ #if RSADEBUG==1 USART0_SendByte('.'); #endif n[0]|=1; bigRandom(a,n); bigExp(a,s,n);//a=a**s mod n n[0]^=1; if(!(bigIsOne(a)||bigEqual(a,n))){ next=0; for(i=0;(i<t-1)&&!next;++i){ n[0]|=1; bigMul(a,a,n); if(bigIsOne(a)){ #if RSADEBUG==1 USART0_SendByte(' '); #endif return 0; } n[0]^=1; if(bigEqual(a,n)) next=1; } if(!next){ #if RSADEBUG==1 USART0_SendByte(' '); #endif return 0; } } k+=2; } #if RSADEBUG==1 USART0_SendStr("\n"); #endif n[0]|=1; return 1; }
void test21(void) { bigFromInt(0); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(1); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(-1); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(0x1234); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(-0x1234); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(0x186A0); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(-0x186A0); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(0x12345678); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(-0x12345678); dump("", bip.res, " = "); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); bigFromInt(987654321); bip.op1 = bip.res; bip.op2 = bip.res; bigMul(); bip.op1 = bip.res; bip.op2 = bip.res; bigMul(); dump("", bip.res, " =\n"); bip.op1 = bip.res; bigPrint(stdout); printf("\n"); }