int likelyPrime(int64_t input) { int64_t random; int64_t exponent; int64_t expmod; int isPrime = 1; int i; seed(); /*********************************** Each iteration halves the likelyhood that 'input' is not prime. ***********************************/ for(i = 0; i < T; i++) { random = (rand()%input)+1; exponent = (input-1)/2; expmod = modExp(random, exponent, input); if(!(expmod == 1 || expmod == (input-1))) { isPrime = 0; break; } } return isPrime; }
binary RSA(const binary& base, const binary& exponent, const binary& mod) { long long int result = 1; long long int baseInt = convertBinToDec(base); long long int exponentInt = convertBinToDec(exponent); long long int modInt = convertBinToDec(mod); result = modExp(baseInt, exponentInt, modInt); return convertDecToBin(result); }
void invokeTestSuite(int option, char *streamFile) { fprintf(freqfp, "________________________________________________________________________________\n\n"); fprintf(freqfp, "\t\tFILE = %s\t\tALPHA = %6.4f\n", streamFile, ALPHA); fprintf(freqfp, "________________________________________________________________________________\n\n"); printf("________________________________________________________________________________\n\n"); printf("FILE = %s BITS = %i BITSTREAMS = %i ALPHA = %6.4f\n", streamFile, tp.n, tp.numOfBitStreams, ALPHA); printf("________________________________________________________________________________\n\n"); if ( option != 0 ) printf("Statistical Testing In Progress.........\n\n"); switch( option ) { case 0: fileBasedBitStreams(streamFile); break; case 1: lcg(); break; case 2: quadRes1(); break; case 3: quadRes2(); break; case 4: cubicRes(); break; case 5: exclusiveOR(); break; case 6: modExp(); break; case 7: bbs(); break; case 8: micali_schnorr(); break; case 9: SHA1(); break; /* INTRODUCE NEW PSEUDO RANDOM NUMBER GENERATORS HERE */ default: printf("Error in invokeTestSuite!\n"); break; } printf("Statistical Testing Complete!!!!!!!!!!!!\n\n"); }
bool isPrime(int x) // Miller-Rabin test { if(x < 3 || x%2 == 0) return false; int temp = x-1; int r = 2; while(temp % r == 0) { r *= 2; } r /= 2; int d = temp / r; for(int i=0; i<ACCURACY; ++i) { int a = generateRandom(); int y = modExp(a, d, x); if(y == 1 || y == x-1) { continue; } for(int j=0; j<r-1; ++j) { y = modExp(y, 2, x); if(y == 1) { return false; } if(y == x-1) { break; } } return false; } return true; // probably }
int main() { int n,i; long long A[50]; long long fx=1,gx=1,c=1000000007; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%lld",&A[i]); fx=fx*A[i]; fx%=c; } gx=A[0]; i=1; while(gx!=1 && i<n){ gx=gcd(gx,A[i]); i++; } printf("%lld\n",modExp(fx,gx,c)); }
long RsaHacker::hackPrivateKey(long e, long n) { //find the prime factors of n long primeFactors[2]; if (n % 2 == 0) { primeFactors[0] = 2; primeFactors[1] = n / 2; } else { long limit = floor(sqrt(n)); for (long i = 3; i <= limit; i+=2) { if (n % i == 0) { primeFactors[0] = i; primeFactors[1] = n / i; } } } //find the mod used for computing the decryption key long m = (primeFactors[0] - 1) * (primeFactors[1] - 1); //find the totient of m using euler's product formula long totient = m; long limit = m / 2; for (long i = 2; i < limit; i++) { if (isPrime(i)) { if (m % i == 0) { totient *= (1 - 1.0 / i); } } } //find the decryption key using the totient int d = modExp(e, totient - 1, m); return d; }
int main(void) { int i; int j; int data; int data2=0; char in = 0; i = 0x0000000004000500; int numBad = 1; int count; long long cpi; volatile void *wptr; short leds = 0; char capInit = 0; //mv1kC1(0x9800000040000000, 0x9800000000001000); //setInterrupts(); //__writeStringLoopback("Stack TLB entry setup.\n"); __writeString("Hello World! Have a BERI nice day!\n"); //debugTlb(); //cp0Regs(); // causeTrap(); // __writeString("Came back from trap and still alive :)\n"); // sysCtrlTest(); // data = rdtscl(5); //int coreid = getCoreID(); /* if (coreid == 0) { delay(); coreCount++; } else { coreCount++; } */ while(in != 'Q') { if (in != '\n') { //if (coreid == 0) { globalReset = 0; coreFlag = 0; __writeString("\n Number of Cores in Use : "); __writeHex(coreCount); __writeString("\n Menu:\n"); __writeString(" \"F\" for floating point co-processor test.\n"); __writeString(" \"L\" for load-linked and store-conditional test.\n"); __writeString(" \"A\" for arithmetic test result.\n"); __writeString(" \"B\" array bounds checking benchmark.\n"); __writeString(" \"D\" for multiply and divide test.\n"); __writeString(" \"C\" for Count register test.\n"); __writeString(" \"M\" for eternal memory test.\n"); //__writeString(" \"N\" for networking test.\n"); __writeString(" \"V\" for framebuffer test.\n"); __writeString(" \"K\" for Capability initialization.\n"); __writeString(" \"l\" to invert LEDs.\n"); __writeString(" \"T\" for touchscreen test.\n"); __writeString(" \"q\" for quicksort boundschecking test.\n"); __writeString(" \"d\" for domain crossing benchmark.\n"); __writeString(" \"G\" for compositor test.\n"); __writeString(" \"Q\" to quit.\n"); } } in = __readUARTChar(); __writeUARTChar(in); __writeString("\n"); //__writeHex(in); //__writeString("\n"); if (in == 'F') { __writeString("Floating Point co-processor test\n"); CoProFPTest(); } if (in == 'L') { __writeString("Load-linked and store-conditional test:\n"); data = 13; data = ll(&data); data = sc(&data, 14); //__writeHex(data); __writeString(" < load-linked and store-conditional result (0)\n"); data = testNset(&data, 14); //__writeHex(data); __writeString(" < test and set result (1)\n"); } if (in == 'A') { __writeString("Arithmetic test:\n"); data = 0; data = arithmaticTest(); __writeHex(data); __writeString(" < arithmetic test result (0)\n"); } if (in == 'T') { int * tX= (int *)0x9000000005000000; int * tY= (int *)0x9000000005000004; int * tDown= (int *)0x9000000005000008; __writeString("X:"); data = *tX; __writeHex(data); __writeString(" Y:"); data = *tY; __writeHex(data); __writeString(" Down:"); data = *tDown; __writeHex(data); __writeString("\n"); } if (in == 'G') { __writeString("Compositor test:\n"); } if (in == 'D') { numBad = 1; __writeString("Multiply and divide test.\n"); for (i = -10; i < 10; i++) { data = numBad * i; __writeHex(numBad); __writeString(" * "); __writeHex(i); __writeString(" = "); __writeHex(data); __writeString("\n"); if (i!=0) data2 = data / i; __writeHex(data); __writeString(" / "); __writeHex(i); __writeString(" = "); __writeHex(data2); __writeString("\n"); __writeString("\n"); if (data == 0) data = 1; numBad = data; } } if (in == 'M') { __writeString("Memory test:\n"); i = 0; while(1) { count = getCount(); //__writeString("count:"); //__writeHex(count); //__writeString("\n"); int idx = 0; for (j=0; j<0x4000; j++) { idx = i+j; ((volatile int *)DRAM_BASE)[idx] = DRAM_BASE + (idx<<2); } for (j=0; j<0x4000; j++) { idx = i+j; data = ((volatile int *)DRAM_BASE)[idx]; if (data != (int)(DRAM_BASE + (idx<<2))) { //__writeHex((int)(DRAM_BASE + (idx<<2))); //__writeString(" = "); //__writeHex(data); //__writeString("?\n"); numBad++; } } cpi = getCount() - count; //__writeString("newCount - count:"); //__writeHex(cpi); //__writeString("\n"); __writeHex((int)(DRAM_BASE + (idx<<2))); __writeString(" = "); __writeHex(data); __writeString("?\n"); if (numBad == 0) { __writeString("All good! \n"); } else { __writeHex(numBad); __writeString(" were bad :(\n"); numBad = 0; } cpi = (cpi*1000); //__writeString("diff*1000:"); //__writeHex(cpi); //__writeString("\n"); // 8 instructions in the first loop, 12 in the second. cpi = cpi/((8+12)*0x4000); __writeString("CPI of "); __writeDigit(cpi); __writeString("\n"); i+=0x4000; if (i > 0x07000000) i = 0; } } if (in == 'C') { __writeString("Count Register Test:\n"); for(i=0;i<10;i++) { data = ((volatile int *)MIPS_PHYS_TO_UNCACHED(CHERI_COUNT))[0]; __writeHex(data); __writeString(", "); } __writeString("\n"); } if (in == 'K') { if (capInit==0) { FBIncBase(0x9000000004000000); long length = FBGetLeng(); length = length - 800*600*2; FBDecLeng(length); capInit = 1; } __writeString("C4.base= ");__writeHex(FBGetBase());__writeString("\n"); __writeString("C4.length= ");__writeHex(FBGetLeng());__writeString("\n"); CapRegDump(); } if (in == 'V') { int color = 0x8888; int x = 50; int y = 50; int length = 75; int height = 50; long frameBuff = 0x9000000004000000; for (x=200; x<500; x+=100) { for (y=300; y<500; y+=75) { draw3DRect(color, x, y, length, height); } } for (i=0; i<(800*600/4); i++) { FBSDR(0x0C63F80007E0001F,i<<3); } int offset = y*800 + x; int addOff; int totOff; for (i=0; i<(800*600); i++) { ((volatile short*)frameBuff)[i] = i; } for (i=0; i<height; i++) { for (j=0; j<length; j++) { addOff = (800*i) + j; totOff = (offset+addOff); ((volatile short*)frameBuff)[totOff] = color; } } } if (in == 'l') { leds = ~leds; IO_WR(CHERI_LEDS,leds); } if (in == 'N') { wptr = (void *)CHERI_NET_RX; i = *(volatile int *)wptr; __writeString("After accessing CHERI_NET_RX, read:\n"); __writeDigit(i); i = 0xabcd; wptr = (void *)CHERI_NET_TX; __writeString("Before writing 123 to CHERI_NET_TX\n"); *((volatile int *)CHERI_NET_TX) = i; __writeString("After writing 123 to CHERI_NET_TX\n"); } if (in == 'B') { arrayBench(); } if (in == 'q') { doQuicksort(); } if (in == 'd') { armArray(); } // ADDED >> if (in == 'X') { int A[SORT_SIZE]; writeString("Branch Exercise:\n"); fillArray(A, SORT_SIZE/2, 1000); bubbleSort(A, SORT_SIZE/2); writeString("Finished Bubble Sort!\n"); for (i = 0; i<SORT_SIZE/2; i+= SORT_SIZE/2/32) { writeHex(i); writeString(" = "); writeHex(A[i]); writeString("\n"); } fillArray(A, SORT_SIZE, 1234); quickSort(A, 0, SORT_SIZE); writeString("Finished Quick Sort!\n"); for (i = 0; i<SORT_SIZE; i+= SORT_SIZE/32) { writeHex(i); writeString(" = "); writeHex(A[i]); writeString("\n"); } writeString("Searching for each element...\n"); for (j = 0; j<4; j++) { for (i = 0; i<SORT_SIZE; i++) { binarySearch(A, A[i], 0, SORT_SIZE); } } writeString("Searching Done.\n"); writeString("Starting Modular Eponentiation\n"); for (i = 0; i<SORT_SIZE/4; i++) { writeHex(modExp(i,0xAAAAAAAAAAAAAAAA)); writeString("\n"); } } // ADDED << //debugRegs(); } return 0; }