// Find the modulo when dividing two BigInts. *this/divisor. BigInt BigInt::operator%(BigInt divisor){ BigInt response; BigInt quotient; BigInt dividend = *this; BigInt origDivisor = divisor; int idx = BIGINT_SIZE -1; if(divisor.isZero()){ response = 0; } else if(dividend<divisor){ response = dividend; } else{ //Find the leftmost bit in dividend. while(!dividend[idx] ){ idx--; } //Left align the divisor with the dividend. while(!divisor[idx]){ divisor <<= 1; } //Perform binary division. do{ if(dividend >= divisor){ dividend = dividend-divisor; quotient <<= 1; quotient |= 0x01; } else{ quotient <<= 1; } divisor >>= 1; }while(dividend >= origDivisor); response = dividend; } return response; }
//чтение из файла bool getFromFiles(char* fileA, char* fileB, char* fileMod, char operation, bool bin, BigInt& a, BigInt& b, BigInt& mod) { if (bin) { if (!a.getFrom_bin(fileA)) { cout << "Can't get number from " << fileA << endl; return false; } if (!b.getFrom_bin(fileB)) { cout << "Can't get number from " << fileB << endl; return false; } if (fileMod) { if (!mod.getFrom_bin(fileMod)) { cout << "Can't get number from " << fileMod << endl; return false; } } } else { if (!a.getFrom_txt(fileA)) { cout << "Can't get number from " << fileA << endl; return false; } if (!b.getFrom_txt(fileB)) { cout << "Can't get number from " << fileB << endl; return false; } if (fileMod) { if (!mod.getFrom_txt(fileMod)) { cout << "Can't get number from " << fileMod << endl; return false; } } } return true; }
BigInt BnetSRP3::hashSecret(BigInt& secret) const { int i; unsigned char* raw_secret; unsigned char odd[16], even[16], hashedSecret[40]; unsigned char* secretPointer; unsigned char* oddPointer; unsigned char* evenPointer; t_hash odd_hash, even_hash; raw_secret = secret.getData(32, 4, false); secretPointer = raw_secret; oddPointer = odd; evenPointer = even; for (i = 0; i < 16; i++) { *(oddPointer++) = *(secretPointer++); *(evenPointer++) = *(secretPointer++); } xfree(raw_secret); little_endian_sha1_hash(&odd_hash, 16, odd); little_endian_sha1_hash(&even_hash, 16, even); secretPointer = hashedSecret; oddPointer = (unsigned char*)odd_hash; evenPointer = (unsigned char*)even_hash; for (i = 0; i < 20; i++) { *(secretPointer++) = *(oddPointer++); *(secretPointer++) = *(evenPointer++); } return BigInt(hashedSecret, 40, 1, false); }
DivModData BigInt::divMod(BigInt& value) { if (cmp(value) < 0) { return DivModData(new BigInt(0), new BigInt(*this)); } else if (cmp(value) == 0) { return DivModData(new BigInt(1), new BigInt(0)); } int resultSign = this->sign * value.sign; BigInt* base = new BigInt(*this); base->sign *= base->sign; // make sign == 1; BigIntData* divDataAggregator = new BigIntData(0); int shiftLength = base->innerData->size() - value.innerData->size(); BigInt* dividor = new BigInt(value); for (int i = 0; i < shiftLength + 1; ++i) { dividor->copy(value); dividor->shift(shiftLength - i); dividor->sign *= dividor->sign; // make sign == 1; int resDiv = divSimple(*base, *dividor); divDataAggregator->push_back(resDiv); dividor->mult(resDiv); base->sub(*dividor); } base->sign *= resultSign; BigInt* mod = new BigInt(*base); BigInt* div = new BigInt(*base); div->innerData->clear(); for (BigIntData::iterator it = divDataAggregator->end() - 1; it != divDataAggregator->begin() - 1; --it) { div->innerData->push_back(*it); } delete divDataAggregator; delete base; delete dividor; return DivModData(div, mod); }
/* * Set the base */ void Montgomery_Exponentiator::set_base(const BigInt& base) { m_window_bits = Power_Mod::window_bits(m_exp.bits(), base.bits(), m_hints); m_g.resize((1 << m_window_bits)); BigInt z(BigInt::Positive, 2 * (m_mod_words + 1)); secure_vector<word> workspace(z.size()); m_g[0] = 1; bigint_monty_mul(z, m_g[0], m_R2_mod, m_modulus.data(), m_mod_words, m_mod_prime, workspace.data()); m_g[0] = z; m_g[1] = (base >= m_modulus) ? (base % m_modulus) : base; bigint_monty_mul(z, m_g[1], m_R2_mod, m_modulus.data(), m_mod_words, m_mod_prime, workspace.data()); m_g[1] = z; const BigInt& x = m_g[1]; for(size_t i = 2; i != m_g.size(); ++i) { const BigInt& y = m_g[i-1]; bigint_monty_mul(z, x, y, m_modulus.data(), m_mod_words, m_mod_prime, workspace.data()); m_g[i] = z; } }
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; }
void BigInt::operator /=(BigInt num) { if (num == (*this)){ (*this) = 1; return; } if (2 * num.abs() > abs()){ (*this) = 0; return; } BigInt result = 0; BigInt currentDivident; BigInt partialResult; for (auto digit = _number.cbegin(); digit != _number.cend(); ++digit){ currentDivident._number.push_back(*digit); currentDivident._lTrim(); if (currentDivident < num){ continue; } for (int i = 1; i < 10; i++){ if (num * i >= currentDivident){ result._lTrim(); result._number.push_back(--i); currentDivident -= (i * num); break; } } std::cout << (std::string)currentDivident<<std::endl; } result._lTrim(); (*this) = result; }
BigInt& BigInt::operator*=(const BigInt& rhs) // Implements the basic "long multiplication" { if (sign != rhs.sign) // If signs are different than result is negative sign = Sign::negative; else // If they are the same, than the result is positive sign = Sign::positive; BigInt result; for (int i = 0; i < rhs.size(); ++i) { // when multiplying you have multiplying number by each digit of the other, and then summing the results BigInt midSum; for (int j = 0; j < (i - 1); ++j) { // when multiplying every next sum starts at 10x bigger than previous // BigInt() initializes to digits to vector<int>(1, 0) so no need to change the value untill you need two zeros midSum.addSigDigits(0); } int carryOver = 0; for (int j = 0; j < this->size(); ++j) { int result = (rhs.digits[i] * digits[j]) + carryOver; if ((i == 0) && (j == 0)) // BigInt() initializes digits to vector<int>(1, 0) midSum.digits[0] = result % 10; else midSum.addSigDigits((result % 10)); // last digit, in the proper power carryOver = result / 10; } // take care of leftover carryOver if (carryOver != 0) midSum.addSigDigits(carryOver); // add the created midSum to the overall result of the multiplication result += midSum; } result.normalize(); digits = result.digits; // not assigning fully (*this = result) so as to preserve sign information return *this; }
PointGFp operator*(const BigInt& scalar, const PointGFp& point) { const CurveGFp& curve = point.get_curve(); if(scalar.is_zero()) return PointGFp(curve); // zero point std::vector<BigInt> ws(9); if(scalar.abs() <= 2) // special cases for small values { byte value = scalar.abs().byte_at(0); PointGFp result = point; if(value == 2) result.mult2(ws); if(scalar.is_negative()) result.negate(); return result; } const size_t scalar_bits = scalar.bits(); #if 0 PointGFp x1 = PointGFp(curve); PointGFp x2 = point; size_t bits_left = scalar_bits; // Montgomery Ladder while(bits_left) { const bool bit_set = scalar.get_bit(bits_left - 1); if(bit_set) { x1.add(x2, ws); x2.mult2(ws); } else { x2.add(x1, ws); x1.mult2(ws); } --bits_left; } if(scalar.is_negative()) x1.negate(); return x1; #else const size_t window_size = 4; std::vector<PointGFp> Ps(1 << window_size); Ps[0] = PointGFp(curve); Ps[1] = point; for(size_t i = 2; i != Ps.size(); ++i) { Ps[i] = Ps[i-1]; Ps[i].add(point, ws); } PointGFp H(curve); // create as zero size_t bits_left = scalar_bits; while(bits_left >= window_size) { for(size_t i = 0; i != window_size; ++i) H.mult2(ws); const u32bit nibble = scalar.get_substring(bits_left - window_size, window_size); H.add(Ps[nibble], ws); bits_left -= window_size; } while(bits_left) { H.mult2(ws); if(scalar.get_bit(bits_left-1)) H.add(point, ws); --bits_left; } if(scalar.is_negative()) H.negate(); return H; #endif }
BigInt operator / (BigInt &in){ return div(*this, in, (sign() != in.sign())); }
BigInt div(int number) { BigInt result = *this; result.divThis(number); return result; }
bool operator==(BigInt obj) { BigInt temp; temp = obj - *this; return temp.isAbsZero(); }
void Print(BigInt a) { Set(a); printf("%d", (a.size() == 0) ? 0 : a.back()); FORD(i,a.size()-2,0) printf("%09d", a[i]); EL; }
/* * Multiply-Add Operation */ BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c) { if(c.is_negative() || c.is_zero()) throw Invalid_Argument("mul_add: Third argument must be > 0"); BigInt::Sign sign = BigInt::Positive; if(a.sign() != b.sign()) sign = BigInt::Negative; const size_t a_sw = a.sig_words(); const size_t b_sw = b.sig_words(); const size_t c_sw = c.sig_words(); BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1); SecureVector<word> workspace(r.size()); bigint_mul(r.get_reg(), r.size(), workspace, a.data(), a.size(), a_sw, b.data(), b.size(), b_sw); const size_t r_size = std::max(r.sig_words(), c_sw); bigint_add2(r.get_reg(), r_size, c.data(), c_sw); return r; }
/* * Encode a BigInt */ void BigInt::encode(uint8_t output[], const BigInt& n, Base base) { secure_vector<uint8_t> enc = n.encode_locked(base); copy_mem(output, enc.data(), enc.size()); }
int main() { /* test cases */ /* * case 1: addition a>b and b>a and a=b */ BigInt a1("99"); BigInt b1("9131"); std::cout<<"\na = "<<a1.toString(); std::cout<<"\nb = "<<b1.toString(); std::cout<<"\nsum = "<<(a1+b1).toString(); std::cout<<"\na-b = "<<(a1-b1).toString(); BigInt a2("99"); BigInt b2("91"); std::cout<<"\na = "<<a2.toString(); std::cout<<"\nb = "<<b2.toString(); std::cout<<"\nsum = "<<(a2+b2).toString(); std::cout<<"\nproduct = "<<(a2*b2).toString(); std::cout<<"\na-b = "<<(a2-b2).toString(); /* * case 2: a=b and one is negative */ BigInt a3("-99"); BigInt b3("91"); std::cout<<"\na = "<<a3.toString(); std::cout<<"\nb = "<<b3.toString(); std::cout<<"\nsum = "<<(a3+b3).toString(); std::cout<<"\nsum = "<<(b3+a3).toString(); std::cout<<"\nproduct = "<<(b3*a3).toString(); std::cout<<"\na-b = "<<(a3-b3).toString(); /* * case 2: abs(a)>abs(b) and a is negative */ BigInt a4("-999"); BigInt b4("91"); std::cout<<"\na = "<<a4.toString(); std::cout<<"\nb = "<<b4.toString(); std::cout<<"\nsum = "<<(a4+b4).toString(); std::cout<<"\nsum = "<<(b4+a4).toString(); std::cout<<"\nproduct = "<<(b4*a4).toString(); std::cout<<"\na-b = "<<(a4-b4).toString(); /* * case 3: abs(b)>abs(a) and b is negative */ BigInt a5("99"); BigInt b5("-991"); std::cout<<"\na = "<<a5.toString(); std::cout<<"\nb = "<<b5.toString(); std::cout<<"\nsum = "<<(a5+b5).toString(); std::cout<<"\nsum = "<<(b5+a5).toString(); std::cout<<"\nproduct = "<<(b5*a5).toString(); std::cout<<"\na-b = "<<(a5-b5).toString(); /* * case 4: abs(b)>abs(a) and a,b is negative */ BigInt a6("-99"); BigInt b6("-991"); std::cout<<"\na = "<<a6.toString(); std::cout<<"\nb = "<<b6.toString(); std::cout<<"\nsum = "<<(a6+b6).toString(); std::cout<<"\nsum = "<<(b6+a6).toString(); std::cout<<"\na-b = "<<(a6-b6).toString(); /* * case 4: abs(b)=abs(a) and a,b is negative */ BigInt a7("-999"); BigInt b7("-991"); std::cout<<"\na = "<<a7.toString(); std::cout<<"\nb = "<<b7.toString(); std::cout<<"\nsum = "<<(a7+b7).toString(); std::cout<<"\nsum = "<<(b7+a7).toString(); std::cout<<"\nproduct = "<<(b7*a7).toString(); std::cout<<"\na-b = "<<(a7-b7).toString(); /* * case 5: a-b */ BigInt a8("-999"); BigInt b8("-991"); std::cout<<"\na = "<<a8.toString(); std::cout<<"\nb = "<<b8.toString(); std::cout<<"\na-b = "<<(a8-b8).toString(); std::cout<<"\na-b = "<<(a8-b8).toString(); BigInt fact("1"); clock_t time = clock(); fact = fact.factorial(1000); time = clock() - time; std::cout<<"\n\n10000 Factorial:\n"<<fact.toString()<<std::endl; std::cout<<"\n\ntime taken to find 1000! = "<<time/CLOCKS_PER_SEC<<" seconds"; BigInt rd; rd = rd.random(); std::cout<<"\n\n\nRandom number = "<<rd.toString()<<"\n\n"; rd = rd.random(); std::cout<<"\n\n\nRandom number = "<<rd.toString()<<"\n\n"; rd = rd.random(); std::cout<<"\n\n\nRandom number = "<<rd.toString()<<"\n\n"; return 0; }
int main(int argc, char*argv[]){ /* TASK 1: Perform encryption and Decryption each using the RSA routines provided here. They don’t necessarily have to be part of the same code */ int count = 10; int i=0; std::string result; BigInt randMessage[10]; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; std::cout<<"TASK 1a: Create 10 instances of the RSA class without giving arguments, generate random message or assign messages, and perform encryption through each of the 10 classes. "<<std::endl; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; //initializing at once as everytime RSA is called srand is called internally and reseting the random function for(i=0;i<count;i++) { randMessage[i] = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32); } for(i=0;i<count;i++) { RSA myRSA; BigInt message, cipher, deciphered; message = randMessage[i]; //encrypting cipher = myRSA.encrypt(message); //decrypting deciphered = myRSA.decrypt(cipher); //Checking if the decrypted value is equal to original value if(message == deciphered) { result = "success"; } else { result = "failed"; } std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl; } std::cout<<"********************************************************************************************************************************************************************"<<std::endl; std::cout<<"TASK 1b: Create 5 instances of the RSA class by passing a large prime number [p](> 30,000), and perform encryption decryption. "<<std::endl; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; count = 5; //Initializing some primes and non-primes to be used by following tasks. unsigned long int setOfLargePrimes[] = {103919, 103951, 103963, 103967,103969, 104677 ,104681 ,104683 ,104693 ,104701 ,104707 ,104711 ,104717 ,104723 ,104729}; unsigned long int setOfNonPrimes[] = {40000, 31000, 42000, 45000, 50000, 48000,60000,80000,70000,90000,99000}; //iterating 5 time for question 1b for(i=0;i<count;i++) { //giving one primenumber as input to the RSA RSA rsaInstance(setOfLargePrimes[i]); BigInt message, cipher, deciphered; message = randMessage[i]; //decrypting and encrypting cipher = rsaInstance.encrypt(message); deciphered = rsaInstance.decrypt(cipher); //Checking if the decrypted value is equal to original value if(message == deciphered) { result = "success"; } else { result = "failed"; } std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl; } std::cout<<"********************************************************************************************************************************************************************"<<std::endl; std::cout<<"TASK 1c: Create 5 instances of the RSA class by passing 2 large prime numbers [p,q] (> 30,000) and perform encryption decryption "<<std::endl; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; for(i=0;i<count;i++) { //Intializing RSA with two primenumbers RSA rsaInstance(setOfLargePrimes[2*i], setOfLargePrimes[2*i+1]); BigInt message, cipher, deciphered; message = randMessage[i]; //encrypting and decrypting cipher = rsaInstance.encrypt(message); deciphered = rsaInstance.decrypt(cipher); //Checking if the decrypted value is equal to original value if(message == deciphered) { result = "success"; } else { result = "failed"; } std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl; } std::cout<<"********************************************************************************************************************************************************************"<<std::endl; std::cout<<"TASK 1d: Create 10 instances of the RSA class by passing 2 large non prime numbers (> 30,000) and perform encryption decryption. In most of the cases the message should not get decrypted correctly. "<<std::endl; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; for(i=0;i<10;i++) { //Creating RSA with non primes. RSA rsaInstance(setOfNonPrimes[i], setOfNonPrimes[i+1]); BigInt message, cipher, deciphered; message = randMessage[i]; //encrypting and decrypting cipher = rsaInstance.encrypt(message); deciphered = rsaInstance.decrypt(cipher); //Checking if the decrypted value is equal to original value if(message == deciphered) { result = "success"; } else { result = "failed"; } std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl; } /* TASK 2: Challenge Response: Scheme 0 */ std::cout<<"********************************************************************************************************************************************************************"<<std::endl; std::cout<<" TASK 2: Challenge Response Scheme 0 "<<std::endl; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; { RSA RSA1, RSA2; //Assigning RSA1 public key and Modulus to RSA2 BigInt publicKey = RSA1.getPublicKey(); BigInt modulus = RSA1.getModulus(); RSA2.setPublicKey(publicKey); RSA2.setN(modulus); //generating random message BigInt message, cipher, deciphered; message = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32); //encrypting with RSA2 i.e RSA1's public key cipher = RSA2.encrypt(message); //decrypting with RSA1 i.e RSA1's private key deciphered = RSA1.decrypt(cipher); //Checking if the decrypted value is equal to original value if(message == deciphered) { result = "success"; } else { result = "failed"; } std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl; } /* TASK 3: Blind Signature */ std::cout<<"********************************************************************************************************************************************************************"<<std::endl; std::cout<<" TASK 3: Blind Signature "<<std::endl; std::cout<<"********************************************************************************************************************************************************************"<<std::endl; { //Random message requester want to send with signers signature BigInt message = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32); //Passing signers public key to the requester so that he can generate the blind factor BigInt signedMessage = Requester(message, signersRSA.getPublicKey(), signersRSA.getModulus()); BigInt deciphered = signersRSA.encrypt(signedMessage); std::cout<<"Signed Message without blindfactor:" << signedMessage.toHexString() <<std::endl; std::cout<<" Message Decrypted:" << deciphered.toHexString() <<std::endl; //Checking if the decrypted value is equal to original value if(message == deciphered) { std::cout<<"Blind Signature successfully implemented"<<std::endl; } else { std::cout<<"Blind Signature implementation failed"<<std::endl; } } }
BigInt mod(int number) { BigInt result = *this; result.modThis(number); return result; }
// TODO: Custom implementation? inline int JacobiSymbol( const BigInt& m, const BigInt& n ) { return mpz_jacobi( m.LockedPointer(), n.LockedPointer() ); }
KTL_INLINE sprig::krkr::tjs::object_type NativeBigInt::min(tTJSVariant const& v1, tTJSVariant const& v2) { sprig::krkr::tjs::object_type result = createNew(0, 0); BigInt* ptr = reinterpret_cast<BigInt*>(getInstance(sprig::get_pointer(result))); sprig::min(ptr->ref(), convertValue(v1), convertValue(v2)); return result; }
void TestApp::test_bigint(void) { Console::write_line(" Header: bigint.h"); Console::write_line(" Class: BigInt"); Console::write_line(" Function: BigInt constructors"); { BigInt value(1234); BigInt value2(value); BigInt value3; value3 = value; value.set(33); ubyte32 result; value.get(result); if (result != 33) fail(); value2.get(result); if (result != 1234) fail(); value3.get(result); if (result != 1234) fail(); value.set(111); value2.set(222); value3.set(333); value.get(result); if (result != 111) fail(); value2.get(result); if (result != 222) fail(); value3.get(result); if (result != 333) fail(); } Console::write_line(" Function: BigInt set(ubyte32) and get(ubyte32)"); { BigInt value(1234); ubyte32 result; value.get(result); if (result != 1234) fail(); value.set(0xffffffff); value.get(result); if (result != 0xffffffff) fail(); value.set(0x12345678); value.get(result); if (result != 0x12345678) fail(); } Console::write_line(" Function: BigInt set(ubyte64) and get(ubyte64)"); { BigInt value(1234); ubyte64 result; value.get(result); if (result != 1234ULL) fail(); value.set(0xffffffffffffffffULL); value.get(result); if (result != 0xffffffffffffffffULL) fail(); value.set(0x12345678ABCD6543ULL); value.get(result); if (result != 0x12345678ABCD6543ULL) fail(); } Console::write_line(" Function: BigInt set(byte32) and get(byte32)"); { BigInt value(1234); byte32 result; value.get(result); if (result != 1234) fail(); value.set(-1234); value.get(result); if (result != -1234) fail(); value.set(0x7fffffff); value.get(result); if (result != 0x7fffffff) fail(); value.set(-0x7fffffff); value.get(result); if (result != -0x7fffffff) fail(); } Console::write_line(" Function: BigInt set(byte64) and get(byte64)"); { BigInt value(1234LL); byte64 result; value.get(result); if (result != 1234LL) fail(); value.set(-1234LL); value.get(result); if (result != -1234LL) fail(); value.set(0x7fffffffffffffffLL); value.get(result); if (result != 0x7fffffffffffffffLL) fail(); value.set(-0x7fffffffffffffffLL); value.get(result); if (result != -0x7fffffffffffffffLL) fail(); } Console::write_line(" Function: operator + "); { BigInt value1(5); BigInt value2(3); BigInt value3; value3 = value1 + value2; byte32 result; value3.get(result); if (result != 8) fail(); } Console::write_line(" Function: operator += "); { BigInt value1(5); BigInt value2(3); value1 += value2; byte32 result; value1.get(result); if (result != 8) fail(); value1 += 1; value1.get(result); if (result != 9) fail(); } Console::write_line(" Function: operator - "); { BigInt value1(5); BigInt value2(3); BigInt value3; value3 = value1 - value2; byte32 result; value3.get(result); if (result != 2) fail(); } Console::write_line(" Function: operator -= "); { BigInt value1(5); BigInt value2(3); value1 -= value2; byte32 result; value1.get(result); if (result != 2) fail(); } Console::write_line(" Function: operator * "); { BigInt value1(5); BigInt value2(3); BigInt value3; value3 = value1 * value2; byte32 result; value3.get(result); if (result != 15) fail(); } Console::write_line(" Function: operator *= "); { BigInt value1(5); BigInt value2(3); value1 *= value2; byte32 result; value1.get(result); if (result != 15) fail(); value1.set(-5); value2.set(3); value1 *= value2; value1.get(result); if (result != -15) fail(); value1.set(5); value2.set(-3); value1 *= value2; value1.get(result); if (result != -15) fail(); value1.set(-5); value2.set(-3); value1 *= value2; value1.get(result); if (result != 15) fail(); value1.set(-5); value1 *= 3; value1.get(result); if (result != -15) fail(); } Console::write_line(" Function: operator / "); { BigInt value1(9); BigInt value2(4); BigInt value3; value3 = value1 / value2; byte32 result; value3.get(result); if (result != 2) fail(); } Console::write_line(" Function: operator /= "); { BigInt value1(9); BigInt value2(4); value1 /= value2; byte32 result; value1.get(result); if (result != 2) fail(); } Console::write_line(" Function: operator % "); { BigInt value1(9); BigInt value2(4); BigInt value3; value3 = value1 % value2; byte32 result; value3.get(result); if (result != 1) fail(); } Console::write_line(" Function: operator %= "); { BigInt value1(9); BigInt value2(4); value1 %= value2; byte32 result; value1.get(result); if (result != 1) fail(); } Console::write_line(" Function: is_odd() "); { BigInt value; value.set(130); if (value.is_odd()) fail(); value.set(131); if (!value.is_odd()) fail(); } Console::write_line(" Function: is_even() "); { BigInt value; value.set(131); if (value.is_even()) fail(); value.set(130); if (!value.is_even()) fail(); } }
BigRational::BigRational(const BigInt &numerator, const BigInt &denominator, DigitPool *digitPool) : m_numerator( digitPool?digitPool:denominator.getDigitPool()) , m_denominator(digitPool?digitPool:denominator.getDigitPool()) { init(numerator, denominator); }
inline Int NumMantissaBits<BigInt>( const BigInt& alpha ) { return alpha.NumBits(); }
BigRational::BigRational(const BigInt &n, DigitPool *digitPool) : m_numerator( n, digitPool) , m_denominator(BIGREAL_1, digitPool?digitPool:n.getDigitPool()) { }
void Set(BigInt &a) { while (a.size() > 1 && a.back() == 0) a.pop_back(); }
int main(int argc, char*argv[]) { RSA* _RSA_obj[10]; BigInt _message, _encrypt, _decrypt; int _primeNumber[] = { 40343,40351,40357,40361,40387,40423,40427,40429,40433,40459,40471,40483,40487,40493,40499,40507,40519,40529,40531}; int _nonPrimeNumber[] = {36782,36792,36792,36802,36822,36832,36842,36852,36872,36872,37692,37692,37692,37722,37742,37712,37782,37792,37812,37814}; cout << "1)------------------Encryption and Decryption using RSA----------------------"<<"\n"; //------------No arguments RSA -------------------------------------------- cout << "a) 10 instances of RSA routine(argument-less) encryption-decryption"<<"\n"; for (int i = 0; i < 10; i++) { _RSA_obj[i] = new RSA(); usleep(21000); _message = int(((double) rand() / RAND_MAX)*RAND_GEN32); _encrypt = _RSA_obj[i]->encrypt(_message); _decrypt = _RSA_obj[i]->decrypt(_encrypt); cout << "Message: " << _message.toHexString() << "\t\tEncrypted: " << _encrypt.toHexString() << "\t\tDecrypted: " << _decrypt.toHexString()<<"\n"; } cout<<"\n"; //------------1 prime number RSA -------------------------------------------- cout << "b) 5 RSA instances(1 prime number(>30,000) as argument) encryption-decryption "<<"\n"; for (int i = 0; i < 5; i++) { _RSA_obj[i] = new RSA(_primeNumber[i]); usleep(450000); _message = int(((double) rand() / RAND_MAX)*RAND_GEN32); _encrypt = _RSA_obj[i]->encrypt(_message); _decrypt = _RSA_obj[i]->decrypt(_encrypt); cout << "Message: " << _message.toHexString() << "\t\tEncrypted: " << _encrypt.toHexString() << "\t\tDecrypted: " << _decrypt.toHexString()<<"\n"; } cout<<"\n"; //------------2 prime numbers RSA -------------------------------------------- cout << "c) 5 RSA instances(2 prime numbers(>30,000) as arguments) encryption-decryption "<<"\n"; for (int i = 0; i < 5; i++) { _RSA_obj[i] = new RSA(_primeNumber[i], _primeNumber[10-i]); usleep(450000); _message = int(((double) rand() / RAND_MAX)*RAND_GEN32); _encrypt = _RSA_obj[i]->encrypt(_message); _decrypt = _RSA_obj[i]->decrypt(_encrypt); cout << "Message: " << _message.toHexString() << "\tEncrypted: " << _encrypt.toHexString() << "\tDecrypted: " << _decrypt.toHexString()<<"\n"; } cout<<"\n"; //--------------2 non prime numbers RSA------------------------------------- cout << "d) 5 RSA instances(2 non prime numbers(>30,000) as arguments) encryption-decryption "<<"\n"; for (int i = 0; i < 10; i++) { _RSA_obj[i] = new RSA(_nonPrimeNumber[i], _nonPrimeNumber[10-i]); usleep(50000); _message = int(((double) rand() / RAND_MAX)*RAND_GEN32); _encrypt = _RSA_obj[i]->encrypt(_message); _decrypt = _RSA_obj[i]->decrypt(_encrypt); cout << "Message: " << _message.toHexString() << "\tEncrypted: " << _encrypt.toHexString() << "\tDecrypted: " << _decrypt.toHexString()<<"\n"; } cout<<"\n"; //--------------challenge response scheme--------------------------------------- cout << "2)--------------------Challenge response scheme--------------------" << "\n"; RSA obj1, obj2; BigInt rsaPK = obj1.getPublicKey(); BigInt rsaN = obj1.getModulus(); obj2.setPublicKey(rsaPK); obj2.setN(rsaN); //random message _message = int(((double) rand() / RAND_MAX)*RAND_GEN32); BigInt encrypt = obj2.encrypt(_message); BigInt decrypt = obj1.decrypt(encrypt); cout << "Plain Text:" << _message.toHexString() << "\tDecrypted Text:" << decrypt.toHexString() << "\n"; if (_message.operator==(decrypt)) cout << "Challenge response scheme is Successful" << "\n"; else cout << "Challenge response scheme is Unsuccessful" << "\n"; cout << "\n"; //--------------Blind signature--------------------------------------- cout << "3)------------------Blind signature---------------------" << "\n"; RSA obj; BigInt bobN = obj.getModulus(); BigInt bobPK = obj.getPublicKey(); // Generate random number and its inverse double AliceRandomNo = double(((double) rand() / RAND_MAX)*RAND_GEN32); BigInt rnd(AliceRandomNo); BigInt AliceRandomNoInverse = RSAUtil::modInverse(rnd, bobN); //random message _message = int(((double) rand() / RAND_MAX)*RAND_GEN32); BigInt encryptRandom = RSAUtil::modPow(rnd, bobPK, bobN); BigInt messageToRSAobj = encryptRandom.operator*(_message).operator%(bobN); BigInt messageFromRSAobj = getDecryptedMessageFromRSA_obj(messageToRSAobj, obj); BigInt sign = messageFromRSAobj.operator*(AliceRandomNoInverse).operator%(bobN); BigInt flag = RSAUtil::modPow(sign, bobPK, bobN); cout << "message: " << _message.toHexString() << "\tsignature: " << sign.toHexString() << "\tdecrypted: " << flag.toHexString() << "\n"; cout << "\nBlind signature: "; if(_message.operator==(flag)) cout << "Blind signature Successful" <<"\n"; else cout << "Blind signature Unsuccessful" << "\n"; //--------------------------------------end of main-------------------- cout<<"\n"; return 0; }
BigInt operator *(const BigInt& x) const { BigInt r; for (int i = 1; i <= numDigits; i++) r.multiAndAcumWithShift(x, num[i], i-1); return r; }
int main(int argc, char const *argv[]) { string a = "789456123654987456321456987456321", b = "7896541236547896541236457489"; BigInt left(a), right(b); BigInt r = left + b; string sr = r.getStr(); string c = "789464020196224004217998223913810"; if (sr == c) std::cout <<"1 result is right" << std::endl; else std::cout <<"1 result is erro" << std::endl; a = "123456987789654123987456321852741369", b = "1234566987741258963214785963258741369874521456987"; c = "1234566987741382420202575617382728826196374198356"; left.set(a), right.set(b); r = left + right; if (r.getStr() == c) std::cout <<"2 result is right" << std::endl; else { std::cout << r.getStr() << std::endl; std::cout << c << std::endl; std::cout <<"2 result is erro" << std::endl; } a = "9999", b = "1111", c = "11110"; left.set(a), right.set(b); r = left + right; if (r.getStr() == c) std::cout <<"3 result is right" << std::endl; else std::cout <<"3 result is erro" << std::endl; r = left -right; c = "8888"; if (r.getStr() == c) std::cout <<"4 result is right" << std::endl; else std::cout <<"4 result is erro" << std::endl; r = right - left; c = "-8888"; if (r.getStr() == c) std::cout <<"5 result is right" << std::endl; else std::cout <<"5 result is erro" << std::endl; left.set("-98"), right.set("99"); r = left + right; if (r.getStr() == "1") std::cout << "7 right" << std::endl; r = left * right; std::cout << r.getStr()<<std::endl; a = "123456987789654123987456321852741369", b = "1234566987741258963214785963258741369874521456987"; left.set(a), right.set(b); r = left * right; std::cout << r.getStr() << std::endl; r = right / left; std::cout << r.getStr() << std::endl; r = right % left; std::cout << r.getStr() << std::endl; return 0; }
void converttoint(BigInt result,unsigned long * result_long,int size_result_array){ result.toULong(result_long,size_result_array); }
BigInt operator * (BigInt &in){ return mul(*this, in, sign() != in.sign()); }