RSAKeySet::RSAKeySet(int length) { BigInt* p; BigInt* q; BigInt n; BigInt fi; while (true) { p = createBigint(length / 2)->makePrime(); q = createBigint(length / 2 + 1)->makePrime(); if(*q == *p) { q->add(*ONE)->makePrime(); } n = *p * *q; fi = (*p - *ONE) * (*q - *ONE); delete p; delete q; BigInt *check = new BigInt(n); if (*check->gcd(fi) == *ONE) { delete check; break; } delete check; } BigInt *e = createBigint(fi.getData()->size() - 1); { BigInt *temp = new BigInt(*e); while (*temp->gcd(fi) != *ONE) { e->add(*ONE); temp->copy(*e); } delete temp; } BigInt *d = new BigInt(*e); d->inverseMod(fi); assert(((*d * *e) % fi) == *ONE); BigInt *test = createBigint(length - 1); BigInt *testCopy = new BigInt(*test); test->powMod(*d,n)->powMod(*e,n); assert (*test == *testCopy); privateKey = new RSAKey(&n, d); publicKey = new RSAKey(&n, e); }
/************************************************* * Solve x = q * y + r * *************************************************/ void modifying_divide(BigInt& x, BigInt& y, BigInt& q) { if(y.is_zero()) throw BigInt::DivideByZero(); if(x.is_negative() || y.is_negative()) throw Invalid_Argument("Arguments to modifying_divide must be positive"); s32bit compare = x.cmp(y); if(compare == -1) { q = 0; return; } if(compare == 0) { q = 1; x = 0; return; } u32bit shifts = 0; while(y[y.sig_words()-1] < MP_WORD_TOP_BIT) { x <<= 1; y <<= 1; shifts++; } u32bit n = x.sig_words() - 1, t = y.sig_words() - 1; q.reg.create(n - t + 1); if(n <= t) { while(x > y) { x -= y; q.add(1); } x >>= shifts; return; } BigInt temp = y << (MP_WORD_BITS * (n-t)); while(x >= temp) { x -= temp; q[n-t]++; } for(u32bit j = n; j != t; j--) { const word x_j0 = x.word_at(j); const word x_j1 = x.word_at(j-1); const word y_t = y.word_at(t); if(x_j0 == y_t) q[j-t-1] = MP_WORD_MAX; else q[j-t-1] = bigint_divop(x_j0, x_j1, y_t); while(bigint_divcore(q[j-t-1], y_t, y.word_at(t-1), x_j0, x_j1, x.word_at(j-2))) q[j-t-1]--; x -= (q[j-t-1] * y) << (MP_WORD_BITS * (j-t-1)); if(x.is_negative()) { x += y << (MP_WORD_BITS * (j-t-1)); q[j-t-1]--; } } x >>= shifts; }
int main() { BigInt f[1505]; f[1]=BigInt("1"); // f[2]=BigInt("1"); for (int i=2;i<1505;i++){ f[i].add(f[i-1]); f[i].add(f[i-2]); } int n; int c=0; while (cin>>n&&n){ printf("Set %d:\n",++c); BigInt result; result.add(f[n+3]); // result.print(); //substract 3 int carry=3; int i=0; while (carry&&i<N){ if (result.val[i]<carry){ result.val[i]+=base-carry; carry=1; }else{ result.val[i]-=carry; carry=0; } i++; } // divide 2 carry=0; i=N; while(i--){ if (carry){ result.val[i]+=base; } carry=result.val[i]%2; result.val[i]/=2; } result.print(); } }
bool BigInt::isPrime() { BigInt* curr = new BigInt(2); BigInt* end = new BigInt(200); BigInt* base = new BigInt(); BigInt* deg = (new BigInt(*this))->sub(*ONE); bool ret = true; while (*curr < *end) { base->copy(*this); if (*base->mod(*curr) == *ZERO) { ret = false; break; } curr->add(*ONE); } delete curr; delete end; delete base; vector<BigInt*> *data = new vector<BigInt*> (); for (int i = 0; i < 200; ++i) { data->push_back(new BigInt(rand())); } powMod(data, *deg, *this); for(int i = 0; i < data->size(); ++i) { if(ret && *data->at(i) != *ONE) { ret = false; } delete data->at(i); } delete data; delete deg; return ret; }
BigInt* BigInt::mult(BigInt& value) { BigInt* sumOfMult = new BigInt(0); BigInt* tempVal = new BigInt(0); int resSign = this->sign * value.sign; this->sign = 1; value.sign = 1; int shift = 0; for (BigIntData::iterator it_val = value.innerData->begin(); it_val != value.innerData->end(); ++it_val) { tempVal->copy(*this); tempVal->mult(*it_val); tempVal->shift(shift++); sumOfMult->add(*tempVal); } this->copy(*sumOfMult); this->sign = resSign; while (*(this->innerData->end() - 1) == 0 && this->innerData->size() > 1) { this->innerData->pop_back(); } delete sumOfMult; delete tempVal; return this; }
int main() { unsigned char intA[] = { 100, 200, 99, 23, 89, 100, 233 }; unsigned char intB[] = { 10, 78, 243, 9, 0 }; BigInt *bigIntA = new BigInt( 1, 7, intA ); BigInt *bigIntB = new BigInt( -1, 5, intB ); char *hexA = bigIntA->convertToHexString(); char *hexB = bigIntB->convertToHexString(); printf( "A = %s\n", hexA ); printf( "B = %s\n", hexB ); BigInt *sum = bigIntA->subtract( bigIntB ); char *hexSum = sum->convertToHexString(); printf( "Sum = %s\n", hexSum ); delete [] hexA; delete [] hexB; delete [] hexSum; delete bigIntA; delete bigIntB; delete sum; int c = 0x58B11F; BigInt *intC = new BigInt( c ); char *hexC = intC->convertToHexString(); printf( "C = %s\n", hexC ); int extractedC = intC->convertToInt(); if( extractedC == c ) { printf( "equal\n" ); } else { printf( "not equal\n" ); } delete [] hexC; delete intC; int limit = 300; printf( "Testing pair operations for all pairs in -%d..%d ...\n", limit, limit ); char failed = false; for( int i=-limit; i<limit && !failed; i++ ) { BigInt *intI = new BigInt( i ); for( int j=-limit; j<limit && !failed; j++ ) { BigInt *intJ = new BigInt( j ); BigInt *intSum = intI->add( intJ ); BigInt *intDiff = intI->subtract( intJ ); int sum = i + j; int diff = i - j; if( sum != intSum->convertToInt() ) { printf( "sum test failed for %d, %d\n", i, j ); printf( " real sum = %d, computed sum = %d\n", sum, intSum->convertToInt() ); failed = true; } if( diff != intDiff->convertToInt() ) { printf( "diff test failed for %d, %d\n", i, j ); printf( " real diff = %d, computed diff = %d\n", diff, intDiff->convertToInt() ); failed = true; } if( intI->isLessThan( intJ ) && ( i >= j ) ) { printf( "first less than test failed for %d, %d\n", i, j ); failed = true; } if( intJ->isLessThan( intI ) && ( j >= i ) ) { printf( "second less than test failed for %d, %d\n", i, j ); failed = true; } if( intI->isEqualTo( intJ ) && ( i != j ) ) { printf( "equality test failed for %d, %d\n", i, j ); failed = true; } delete intSum; delete intDiff; delete intJ; } delete intI; } if( !failed ) { printf( "test passed\n" ); } return 0; }