Exemple #1
0
	bignum golden(const bignum &b)
	{
		bignum temp1 = bignum(fibonacci(b));
		bignum temp2 = bignum(fibonacci(b - 1));

		return (temp2 == 0 ? 0 : temp1 / temp2);
	}
Exemple #2
0
	//returns average of all values passed
	bignum average(vector<bignum> numbers_passed)
	{
		bignum counter((int)numbers_passed.size());
		bignum total;

		for (int i = 0; i < numbers_passed.size(); i++)
			total += numbers_passed.at(i);

		if (total == bignum(0) || counter == bignum(0))
			return bignum(0);

		return divideNumbers(total, counter);
	}
Exemple #3
0
term_t bignum_to_term(bignum_t *a, xpool_t *xp)
{
	if (bn_size(a) == 0)
		return intnum(0);
	if (bn_sign(a) == 0 && bn_size(a) == 1 && a->digits[0] <= MAX_INT_VALUE)
		return intnum(a->digits[0]);
	if (bn_sign(a) == 1 && bn_size(a) == 1 && a->digits[0] <= MAX_INT_VALUE+1)
		return intnum(-(int_value_t)a->digits[0]);
	if (bn_sign(a) == 0 && bn_size(a) == 2)
	{
		apr_uint64_t v = (apr_uint64_t)a->digits[0] << 32;
		v += a->digits[1];	// somehow it doesn't work if combined with previous line
		
		if (v <= MAX_INT_VALUE)
			return intnum(v);
	}
	if (bn_sign(a) == 1 && bn_size(a) == 2)
	{
		apr_uint64_t v = (apr_uint64_t)a->digits[0] << 32;
		v += a->digits[1];
		
		if (v <= MAX_INT_VALUE+1)
			return intnum(-(int_value_t)v);
	}

	return bignum(a);
}
Exemple #4
0
term_t long2term(apr_int64_t value, xpool_t *xp)
{
	if (value < MIN_INT_VALUE || value > MAX_INT_VALUE)
		return bignum(bignum_from64(value, xp));
	else
		return intnum(value);
}
Exemple #5
0
	bignum bignum::mod_inverse_of(const bignum& mod)const
	{
		pair<bignum, bignum> t = make_pair<bignum, bignum>(0x00, 0x01U);
		pair<bits_value, bits_value> r(mod.value_, value_);
		
		while(r.second.any())
		{
			auto d = bits_divide(r.first, r.second);
			r.first = r.second;
			r.second = d.second;
			
			//t.first = t.second, t.second = first - second*d.quotient
			auto transit = t.second;
			t.second *= bignum(move(d.first));
			t.first -= t.second;
			t.second = t.first;
			t.first = move(transit);
		}
		
		if(r.first.test(0) && r.first.count() == 1)
		{
			if(t.first.sign_)
				t.first.value_ = substract(mod.value_, t.first.value_);
			
			t.first.sign_ = sign_;
			
			return move(t.first);
		}
		
		throw_with_nested(coprime_error("given two nums are not co-prime"));
		
	}
Exemple #6
0
	bignum fibonacci(const bignum &b)
	{
		if (b.getDecimalCount() > 0)
			throw error_handler(__FILE__, __LINE__, "fibonacci sequence values may only be derived from integers");

		bignum counter;
		bignum high;
		bignum low;
		bignum temp;
		bignum zero;

		counter.setBase(b.getBase());
		high.setBase(b.getBase());
		low.setBase(b.getBase());

		if (b > bignum(bignum(), b.getBase()))
		{
			high++;

			while (counter < b)
			{
				temp = low;
				low = high;
				high += temp;
				counter++;
			}

			return low;
		}

		else if (b < bignum(bignum(), b.getBase()))
		{
			low++;

			while (counter > b.absolute())
			{
				temp = high;
				high = low;
				low = (temp - low);
				counter++;
			}

			return high;
		}

		return temp;
	}
static void CheckCreateInt(const int64_t& num)
{
    CBigNum bignum(num);
    CScriptNum scriptnum(num);
    BOOST_CHECK(verify(bignum, scriptnum));
    BOOST_CHECK(verify(bignum.getint(), CScriptNum(scriptnum.getint())));
    BOOST_CHECK(verify(scriptnum.getint(), CScriptNum(bignum.getint())));
    BOOST_CHECK(verify(CBigNum(scriptnum.getint()).getint(), CScriptNum(CScriptNum(bignum.getint()).getint())));
}
int main()
{
	char a[]="-99999990798068769895123152432412419812123127951024709862187461298749999";
	char b[]="998464513313213123132165798796412315152321996465465465469999999999999";
	printf("%s\n +\n%s \n= \n",a,b);
	char* result=bignum(a,b);
	printf("%s\n",result);
	return 0;
}//bug
static void CheckCreateVch(const int64_t& num)
{
    CBigNum bignum(num);
    CScriptNum scriptnum(num);
    BOOST_CHECK(verify(bignum, scriptnum));

    CBigNum bignum2(bignum.getvch());
    CScriptNum scriptnum2(scriptnum.getvch(), false);
    BOOST_CHECK(verify(bignum2, scriptnum2));

    CBigNum bignum3(scriptnum2.getvch());
    CScriptNum scriptnum3(bignum2.getvch(), false);
    BOOST_CHECK(verify(bignum3, scriptnum3));
}
Exemple #10
0
	bignum factorial(const bignum &bn)
	{
		if (bn > bignum(1000))
			throw error_handler(__FILE__, __LINE__, "The desired calculation is too large");

		bignum temp(bn);

		for (bignum counter(bn); counter > 1; counter--)
		{
			if (bn > counter)
				temp *= counter;
		}

		temp.updateDigits();
		return temp;
	}
static void CheckCreateVch(const int64_t& num)
{
    CScriptNum10 bignum(num);
    CScriptNum scriptnum(num);
    BOOST_CHECK(verify(bignum, scriptnum));

    std::vector<unsigned char> vch = bignum.getvch();

    CScriptNum10 bignum2(bignum.getvch(), false);
    vch = scriptnum.getvch();
    CScriptNum scriptnum2(scriptnum.getvch(), false);
    BOOST_CHECK(verify(bignum2, scriptnum2));

    CScriptNum10 bignum3(scriptnum2.getvch(), false);
    CScriptNum scriptnum3(bignum2.getvch(), false);
    BOOST_CHECK(verify(bignum3, scriptnum3));
}
	void SecureServer::generateRSAKey (const int& bits, const std::string& publicFilename, const std::string &privateFilename, const std::string &password) throw (boost::system::system_error) {
		const unsigned long e = RSA_F4;
		
		//Generate RSA key
		std::unique_ptr<BIGNUM,void (*)(BIGNUM*)> bignum(BN_new(), &BN_free);
		int ret;
		if((ret = BN_set_word(bignum.get(), e)) != 1){
			throw_system_error_ssl("Could not set BIGNUM word");
		}
		
		std::unique_ptr<RSA,void (*)(RSA*)> rsa(RSA_new(), &RSA_free);
		if(RSA_generate_key_ex(rsa.get(), bits, bignum.get(), nullptr) != 1){
			throw_system_error_ssl("Could not generate RSA key");
		}
		
		//Save public key
		std::unique_ptr<BIO,void (*)(BIO*)> publicBIO(BIO_new_file(publicFilename.c_str(), "w"), &BIO_free_all);
		if(!publicBIO){
			throw_system_error_ssl("Could not open "+publicFilename+" for writing");
		}
		if(PEM_write_bio_RSAPublicKey(publicBIO.get(), rsa.get()) != 1){
			throw_system_error_ssl("Could not write RSA public key");
		}
		
		//Save private key
		std::unique_ptr<BIO,void (*)(BIO*)> privateBIO(BIO_new_file(privateFilename.c_str(), "w"), &BIO_free_all);
		if(!privateBIO){
			throw_system_error_ssl("Could not open "+privateFilename+" for writing");
		}
		if(password.empty()){
			if(PEM_write_bio_RSAPrivateKey(privateBIO.get(), rsa.get(), nullptr, nullptr, 0, nullptr, nullptr) != 1){
				throw_system_error_ssl("Could not write RSA private key");
			}
		}else{
			if(PEM_write_bio_RSAPrivateKey(privateBIO.get(), rsa.get(), EVP_des_ede3_cbc(), (unsigned char *)password.data(), password.size(), nullptr, nullptr) != 1){
				throw_system_error_ssl("Could not write RSA private key");
			}
		}
	}
Exemple #13
0
		bignum bignum::take_ownership(pointer _ptr)
		{
			error::throw_error_if_not(_ptr);

			return bignum(_ptr, deleter);
		}
Exemple #14
0
	void bignum::convertBase(int n)
	{
		if (isZero())
		{
			base = n;
			return;
		}

		bool original_negative = negative;

		bignum toAdd;
		bignum temp(0);
		temp.setBase(n);

		bignum original_ten(10);
		original_ten.setBase(base);
		bignum converted_ten(original_ten);
		converted_ten.convertBaseSimple(n);

		bignum original_nth;
		bignum converted_nth;

		if (base != n)
		{
			for (int i = 0; i < digitRange; i++)
			{
				int index(left_most - i);

				if (i == 0)
				{
					original_nth = bignum(index - ONES_PLACE);
					original_nth.convertBaseSimple(base);
					converted_nth = bignum(original_nth);
					converted_nth.convertBaseSimple(n);
				}
				
				else converted_nth--;

				if (index >= MAXDIGITS)
					throw error_handler(__FILE__, __LINE__, "The value being calculated is too large for the settings provided");

				if (index < 0)
					break;

				//each digit of the number is evaluated evaluated X * 10^n format
				bignum original_digit(getDigit(index));
				if (original_digit.isZero())
					continue;

				original_digit.convertBaseSimple(base);

				//each of the above format is converted simply
				bignum converted_digit(original_digit);
				converted_digit.convertBaseSimple(n);
				
				//add X * 10^n to the solution
				bignum multiplier(exponent(converted_ten, converted_nth));
				bignum toAdd = converted_digit * multiplier;

				temp += toAdd;
			}
			*this = temp;
		}
		negative = original_negative;
		updateDigits();
	}