本文整理汇总了C++中BigInt函数的典型用法代码示例。如果您正苦于以下问题:C++ BigInt函数的具体用法?C++ BigInt怎么用?C++ BigInt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BigInt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ToIntegerVec
VecT<BigInt,2> ToIntegerVec(Vec2i a)
{
VecT<BigInt,2> ret;
ret[0] = BigInt(a[0]);
ret[1] = BigInt(a[1]);
return ret;
}
示例2: divOperator
static void divOperator(const BigInt& left, const BigInt& right, BigInt& qNum, BigInt& remain)
{
if (left.size == right.size) {
if (left >= right) {
qNum = BigInt(1);
remain = left - right;
while(remain >= right) {
remain = remain - right;
++qNum.v[0];
}
}
else {
qNum = BigInt(1);
remain = left;
}
}
else {
BigInt t10("10");
remain = BigInt(1);
vector<short> num;
for(int i = 0; i < left.size; ++i) {
char t = left.v[i] + '0';
remain = remain * t10 + BigInt(t);
short s = 0;
while(remain >= right) {
remain = remain - right;
++s;
}
num.push_back(s);
}
qNum.v = num;
qNum.size = num.size();
rmBeginZero(qNum);
}
}
示例3: BigInt
const BigInt operator *(const BigInt& amount1, const BigInt& amount2)
{
if(amount1==0 || amount2==0) return BigInt();
int i,j,carry;
int *sum = new int[amount1.length + amount2.length];
int *digit1 = amount1.digit, *digit2 = amount2.digit;
for(i = 0 ;i < amount1.length+amount2.length; i++) // initialize
sum[i] = 0;
for(i = amount1.length-1; i >= 0; i--)
{
carry = 0;
for(j = amount2.length-1; j >= 0; j--){
sum[i+j+1] += carry + digit1[i]*digit2[j];
carry = sum[i+j+1] / 10;
sum[i+j+1] %= 10;
}
sum[i+j+1] += carry;
}
if(amount1.sign == amount2.sign)
return BigInt(sum, amount1.length + amount2.length, 0);
else
return BigInt(sum, amount1.length + amount2.length, 1);
}
示例4: BigInt
void SendToAddressTest::testSend()
{
GethSimulator & geth = _context.getGeth();
_context.getKeyStore().registerAccount("0x0000000000000000000000000000000000000000", "asdasd123");
geth.setBalance("0x0000000000000000000000000000000000000000", BigInt("1000000000000000000"));
QString from = "0x0000000000000000000000000000000000000000";
QString to = "0x0000000000000000000000000000000000000001";
QVariantMap request;
request.insert("from", from);
request.insert("to", to);
request.insert("amount", "100000000000000000");
request.insert("password", "asdasd123");
QVariant result = _command(request);
QString txid = result.toString();
QVERIFY(geth.getBalance("0x0000000000000000000000000000000000000001") == BigInt("100000000000000000"));
QVERIFY(geth.getBalance("0x0000000000000000000000000000000000000000") == BigInt("900000000000000000"));
QJsonObject transaction = _context.getDataBase().getTransactions().get(txid.toStdString().c_str());
QCOMPARE(transaction["hash"].toString(), txid);
QCOMPARE(transaction["from"].toString(), from);
QCOMPARE(transaction["to"].toString(), to);
QCOMPARE(transaction["amount"].toString(), QString("100000000000000000"));
}
示例5: mk_smt_bool
smt_astt
smt_convt::overflow_cast(const expr2tc &expr)
{
// If in integer mode, this is completely pointless. Return false.
if (int_encoding)
return mk_smt_bool(false);
const overflow_cast2t &ocast = to_overflow_cast2t(expr);
unsigned int width = ocast.operand->type->get_width();
unsigned int bits = ocast.bits;
smt_sortt boolsort = boolean_sort;
if (ocast.bits >= width || ocast.bits == 0) {
std::cerr << "SMT conversion: overflow-typecast got wrong number of bits"
<< std::endl;
abort();
}
// Basically: if it's positive in the first place, ensure all the top bits
// are zero. If neg, then all the top are 1's /and/ the next bit, so that
// it's considered negative in the next interpretation.
constant_int2tc zero(ocast.operand->type, BigInt(0));
lessthan2tc isnegexpr(ocast.operand, zero);
smt_astt isneg = convert_ast(isnegexpr);
smt_astt orig_val = convert_ast(ocast.operand);
// Difference bits
unsigned int pos_zero_bits = width - bits;
unsigned int neg_one_bits = (width - bits) + 1;
smt_sortt pos_zero_bits_sort =
mk_sort(SMT_SORT_BV, pos_zero_bits, false);
smt_sortt neg_one_bits_sort =
mk_sort(SMT_SORT_BV, neg_one_bits, false);
smt_astt pos_bits = mk_smt_bvint(BigInt(0), false, pos_zero_bits);
smt_astt neg_bits = mk_smt_bvint(BigInt((1 << neg_one_bits) - 1),
false, neg_one_bits);
smt_astt pos_sel = mk_extract(orig_val, width - 1,
width - pos_zero_bits,
pos_zero_bits_sort);
smt_astt neg_sel = mk_extract(orig_val, width - 1,
width - neg_one_bits,
neg_one_bits_sort);
smt_astt pos_eq = mk_func_app(boolsort, SMT_FUNC_EQ, pos_bits, pos_sel);
smt_astt neg_eq = mk_func_app(boolsort, SMT_FUNC_EQ, neg_bits, neg_sel);
// isneg -> neg_eq, !isneg -> pos_eq
smt_astt notisneg = mk_func_app(boolsort, SMT_FUNC_NOT, &isneg, 1);
smt_astt c1 = mk_func_app(boolsort, SMT_FUNC_IMPLIES, isneg, neg_eq);
smt_astt c2 = mk_func_app(boolsort, SMT_FUNC_IMPLIES, notisneg, pos_eq);
smt_astt nooverflow = mk_func_app(boolsort, SMT_FUNC_AND, c1, c2);
return mk_func_app(boolsort, SMT_FUNC_NOT, &nooverflow, 1);
}
示例6: BigInt
BigInt BigInt::PowN(BigInt a, BigInt N)
{
if (N == BigInt("0"))
return BigInt("1");
if (N % 2 == 1)
return PowN(a, N - 1)* a;
else {
BigInt b = PowN(a, N / 2);
return b*b;
}
}
示例7: main
// ------------------------------------------------------------
int main(int ac, char *av[]) {
BigInt i1("9999999998");
BigInt i2("0000000001");
BigInt i3("0000000001");
BigInt i4;
cout << "i1: " << i1 << endl
<< "i2: " << i2 << endl
<< "i3: " << i3 << endl
<< "i4: " << i4 << endl
<< endl;
try {
cout << "Copy c'tor test (i5 = i3):" << endl;
BigInt i5 = i3;
cout << "i5=" << i5 << endl;
cout << "Copy assignment test (i4 = i3):" << endl;
i4 = i3;
cout << "i4=" << i4 << endl << endl;
cout << "Move c'tor test (i6 = std::move(BigInt(\"0000000015\"))):" << endl;
BigInt i6 = std::move(BigInt("0000000015"));
cout << "i6=" << i6 << endl;
cout << "Move assignment test (i5 = BigInt(\"0000000010\"):" << endl;
i5 = BigInt("0000000010");
cout << "i5=" << i5 << endl << endl;
cout << "Equality operators:" << endl
<< "i1 == i2: expected=0: " << (i1 == i2) << endl
<< "i1 != i3: expected=1: " << (i1 != i3) << endl
<< "i2 == i3: expected=1: " << (i2 == i3) << endl
<< endl;
cout << "Less-than operator:" << endl
<< "i2 < i1: expected=1: " << (i2 < i1) << endl
<< "i1 < i3: expected=0: " << (i1 < i3) << endl
<< "i2 < i3: expected=0: " << (i2 < i3) << endl
<< endl;
cout << "Addition operator:" << endl
<< "i1 + i2: expected=9999999999: " << (i1 + i2) << endl
<< "i2 + i3 + i4: expected=0000000003: " << (i2 + i3 + i4) << endl;
cout << "i1 + i2 + i3: expected=overflow: " << (i1 + i3 + i3) << endl
<< endl;
}
catch (const exception &e) {
cout << e.what() << endl;
}
return 0;
}
示例8: RSASetData
/*****************************************************************************
Function:
void RSASetData(BYTE* data, WORD len, RSA_DATA_FORMAT format)
Summary:
Indicates the data to be encrypted or decrypted.
Description:
Call this function to indicate what data is to be encrypted or decrypted.
This function ensures that the data is PKCS #1 padded for encryption
operations, and also normalizes the data to little-endian format
so that it will be compatible with the BigInt libraries.
Precondition:
RSA has already been initialized and RSABeginUsage has returned TRUE.
Parameters:
data - The data to be encrypted or decrypted
len - The length of data
format - One of the RSA_DATA_FORMAT constants indicating the endian-ness
of the input data.
Return Values:
None
Remarks:
For decryption operations, the calculation is done in place. Thererore,
the endian-ness of the input data may be modified by this function.
Encryption operations may expand the input, so separate memory is
allocated for the operation in that case.
***************************************************************************/
void RSASetData(BYTE* data, WORD len, RSA_DATA_FORMAT format)
{
#if defined(STACK_USE_RSA_ENCRYPT)
if(smRSA == SM_RSA_ENCRYPT_START)
{
// Initialize the BigInt wrappers
BigInt(&X, (BIGINT_DATA_TYPE*)rsaData, len/sizeof(BIGINT_DATA_TYPE));
// Copy in the data
memcpy((void*)rsaData, (void*)data, len);
// For big-endian, swap the data
if(format == RSA_BIG_ENDIAN)
BigIntSwapEndianness(&X);
// Resize the big int to full size
BigInt(&X, (BIGINT_DATA_TYPE*)rsaData, keyLength/sizeof(BIGINT_DATA_TYPE));
// Pad the input data according to PKCS #1 Block 2
if(len < keyLength-4)
{
rsaData[len++] = 0x00;
while(len < keyLength-2)
{
do
{
rsaData[len] = RandomGet();
} while(rsaData[len] == 0x00u);
len++;
}
rsaData[len++] = 0x02;
rsaData[len++] = 0x00;
}
}
#endif
#if defined(STACK_USE_RSA_DECRYPT)
if(smRSA == SM_RSA_DECRYPT_START)
{
BigInt(&X, (BIGINT_DATA_TYPE*)data, len/sizeof(BIGINT_DATA_TYPE));
// Correct and save endianness
outputFormat = format;
if(outputFormat == RSA_BIG_ENDIAN)
BigIntSwapEndianness(&X);
}
#endif
}
示例9: generate_number
BigInt generate_number(Seed &s, GeneratorParams ¶ms) {
unsigned int v = params.first;
unsigned int w = params.second;
std::vector<unsigned char> h;
// step 2
BigInt tmp = s.sha1();
// step 3
std::vector<unsigned char> h0 = (std::vector<unsigned char>) tmp.right_bits(w);
h.insert(h.end(), h0.begin(), h0.end()); //step 6
// step 4
BigInt z = s;
BigInt _2_e_160 = BigInt(2).pow(160);
for(unsigned int i = 1; i < v + 1; i++) { // step 5
BigInt zi = z;
zi += i;
Seed si = (zi) % _2_e_160; // step 5.1
std::vector<unsigned char> hi = si.sha1();// step 5.2
h.insert(h.end(), hi.begin(), hi.end()); // step 6
}
BigInt c = h;
return c;
}
示例10: RSASetN
void RSASetN(BYTE* data, RSA_DATA_FORMAT format)
{
BigInt(&N, (BIGINT_DATA_TYPE*)data, keyLength/sizeof(BIGINT_DATA_TYPE));
if(format == RSA_BIG_ENDIAN)
BigIntSwapEndianness(&N);
}
示例11: BigInt
BigInt operator*(const BigInt& left, const BigInt& right)
{
if (left.v[0] == 0 || right.v[0] == 0)
return BigInt(1);
int len = left.size + right.size;
BigInt r(len);
r.positive = left.positive == right.positive ? true : false;
const BigInt *lptr = &left, *rptr = &right;
if (right.size > left.size) {
lptr = &right;
rptr = &left;
}
int i = rptr->size - 1, l = lptr->size;
for(; i >= 0; --i) {
short t = rptr->v[i];
if (t) {
int k = len - rptr->size + i, j = lptr->size - 1;
int tmp, carry = 0;
while(j >= 0) {
tmp = t * lptr->v[j--] + carry + r.v[k];
carry = tmp / 10;
r.v[k--] = tmp % 10;
}
if (carry)
r.v[k] = carry;
}
}
rmBeginZero(r);
return r;
}
示例12: BigInt
/*
* POST decrement operator
*/
TIDorb::types::BigInt TIDorb::types::BigInt::operator-- (int)
{
TIDorb::types::BigInt result = (*this);
(*this) = (*this) - BigInt(1);
_modified = true;
return result;
}
示例13: overflow_error
// ------------------------------------------------------------
BigInt BigInt::operator+(const BigInt &bi) const {
// Perform the addition in columns from LSB to MSB
int result[mySize];
int carry = 0;
for (int i = mySize - 1; i >= 0; --i) {
int add = myVec[i] + bi.myVec[i] + carry;
result[i] = add % 10;
carry = add / 10;
}
// At the end of the computation, if 'carry' is non-zero,
// it means we need another digit to store the result
// (aka, overflow)
if (carry > 0) {
overflow_error("overflow");
}
// Convert vector back to a string
ostringstream buf;
for (auto i: result) {
buf << i;
}
// And return a new BigInt based on the string
return BigInt(buf.str());
}
示例14: fractions
EPtr FunctionNumeric::apply_evaled(EPtr arguments, State &state) {
Number *value = Element::as_number(Pair::car(arguments));
if (! value) return state.error("first argument must be numeric");
Fractional v = value->value();
BigInt fractions(5);
Element *next = Pair::cdr(arguments);
if (next) {
Number *digits = Element::as_number(Pair::car(next));
if (! digits) return state.error("second argument must be numeric");
if (Pair::cdr(next)) return state.error("too many arguments");
if (digits->value().denominator() != BigInt(1)) return state.error("digits must be integer");
fractions = digits->value().numerator();
}
Fractional rounding(1, 2);
Fractional ten(10);
Fractional one(1);
Fractional zero(0);
for (Fractional i = fractions; zero < i; i = i - one) {
rounding = rounding / ten;
}
v = v + rounding;
std::ostringstream buffer;
Fractional fraction(0);
if (v.denominator() > BigInt(1)) {
BigInt rest = v.numerator() % v.denominator();
Fractional full = v - Fractional(rest, v.denominator(), v.isNegative());
buffer << full;
fraction = v - full;
} else {
buffer << v;
fraction = Fractional(0);
}
buffer << ".";
BigInt bi_one(1);
for (BigInt cur(0); cur < fractions; cur = cur + bi_one) {
fraction = fraction * ten;
BigInt rest = fraction.numerator() % fraction.denominator();
Fractional full = fraction - Fractional(rest, fraction.denominator(), fraction.isNegative());
buffer << full;
fraction = fraction - full;
}
return state.creator()->new_string(buffer.str());
}
示例15: BigInt
void BigInt::operator *=(BigInt num)
{
if ((*this) == 1){
(*this) = num;
return;
}
if (num == 1){
return;
}
if ((*this) == 0 || num == 0){
(*this) = 0;
return;
}
BigInt result = BigInt(0);
std::deque<BigInt> rows;
int carry = 0;
int partResult = 0;
int index = 0;
for (auto multiplicant_digit = num._number.rbegin(); multiplicant_digit != num._number.rend(); multiplicant_digit++){
rows.push_back(BigInt(0));
rows.back()._number.pop_back();
for (int i = 0; i < index; i++){
rows.back()._number.push_back(0);
}
index++;
carry = 0;
for (auto multiplier_digit = _number.rbegin(); multiplier_digit != _number.rend(); multiplier_digit++){
partResult = (*multiplicant_digit) * (*multiplier_digit) + carry;
//std::cout <<(*multiplicant_digit) <<"*"<< (*multiplier_digit) <<"+"<<carry<<"="<<partResult<<std::endl;
if (partResult >= 10){
carry = (int)partResult / 10;
partResult %= 10;
} else {
carry = 0;
}
rows.back()._number.push_front(partResult);
}
if (carry > 0){
rows.back()._number.push_front(carry);
}
}
for (std::deque<BigInt>::iterator row = rows.begin(); row != rows.end(); row++){
result += (*row);
}
result._sign = _sign ^ num._sign ? SIGN_NEGATIVE : SIGN_POSITIVE;
(*this) = result;
}