VecT<BigInt,2> ToIntegerVec(Vec2i a) { VecT<BigInt,2> ret; ret[0] = BigInt(a[0]); ret[1] = BigInt(a[1]); return ret; }
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); } }
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); }
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")); }
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); }
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; } }
// ------------------------------------------------------------ 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; }
/***************************************************************************** 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 }
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; }
const BigInt operator +(const BigInt& amount1, const BigInt& amount2) { int *sum,carry(0),i; int *digit1 = amount1.digit, *digit2 = amount2.digit; if(amount1.sign == amount2.sign) { if(amount1.length >= amount2.length) { sum = new int[amount1.length+1]; for(i = amount1.length ; i > amount1.length - amount2.length; i--) { sum[i] = digit1[i-1] + digit2[i-amount1.length+amount2.length-1] + carry; carry = sum[i]/10; sum[i] %= 10; } for(; i>0; i--) { sum[i] = digit1[i-1] + carry; carry = sum[i] / 10; sum[i] %= 10; } sum[0] = carry; return BigInt(sum, amount1.length+1, amount1.sign); } else return amount2 + amount1; } else return amount1 - (-amount2); }
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); }
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; }
/* * POST decrement operator */ TIDorb::types::BigInt TIDorb::types::BigInt::operator-- (int) { TIDorb::types::BigInt result = (*this); (*this) = (*this) - BigInt(1); _modified = true; return result; }
// ------------------------------------------------------------ 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()); }
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()); }
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; }
// Internal function used to get the .sgi header from the current file. ILboolean iGetSgiHead(iSgiHeader *Header) { if (iread(Header, sizeof(iSgiHeader), 1) != 1) return IL_FALSE; BigUShort(&Header->MagicNum); BigUShort(&Header->Dim); BigUShort(&Header->XSize); BigUShort(&Header->YSize); BigUShort(&Header->ZSize); BigInt(&Header->PixMin); BigInt(&Header->PixMax); BigInt(&Header->Dummy1); BigInt(&Header->ColMap); return IL_TRUE; }
BigInt& BigInt::operator/=(const BigInt& that) { if (that.isZero()) throw std::invalid_argument("division by zero"); else if (positive && *this < that) return *this = zero; bool signsDiffer = positive != that.positive; BigInt dividend(*this); BigInt divisor(that); size_t shift = dividend.size() - divisor.size(); std::vector<bool> binaryDigits; dividend.positive = divisor.positive = true; divisor <<= shift; while (divisor > dividend) { divisor >>= 1; shift--; } while (shift) { if (divisor <= dividend) { dividend -= divisor; binaryDigits.push_back(true); } else binaryDigits.push_back(false); --shift; divisor >>= 1; } if (dividend >= divisor) { binaryDigits.push_back(true); dividend -= divisor; } else binaryDigits.push_back(false); std::reverse(binaryDigits.begin(), binaryDigits.end()); *this = BigInt(BigInt::binaryToWords(binaryDigits), positive == that.positive); if (signsDiffer && !dividend.isZero()) *this -= 1; trim(); return *this; }
QVariant EstimateProfileOperationCommand::estimateEditDetails(const QVariantMap &request) { if(!request.contains("uri")) { return makeFeeObject(BigInt(136000), request); } return makeFeeObject(estimateEdit(request["uri"].toString(), "details", "ipfs://QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"), request); }
/***************************************************************************** Function: void RSAInit(void) Summary: Initializes the RSA engine Description: Call this function once at boot to configure the memories for RSA key storage and temporary processing space. Precondition: None Parameters: None Returns: None Remarks: This function is called only one during lifetime of the application. ***************************************************************************/ void RSAInit(void) { #if defined(STACK_USE_RSA_DECRYPT) BigIntROM(&P, (ROM BIGINT_DATA_TYPE*)SSL_P, RSA_PRIME_WORDS); BigIntROM(&Q, (ROM BIGINT_DATA_TYPE*)SSL_Q, RSA_PRIME_WORDS); BigIntROM(&dP, (ROM BIGINT_DATA_TYPE*)SSL_dP, RSA_PRIME_WORDS); BigIntROM(&dQ, (ROM BIGINT_DATA_TYPE*)SSL_dQ, RSA_PRIME_WORDS); BigIntROM(&qInv, (ROM BIGINT_DATA_TYPE*)SSL_qInv, RSA_PRIME_WORDS); #endif #if defined(STACK_USE_RSA_ENCRYPT) BigInt(&E, (BIGINT_DATA_TYPE*)eData, sizeof(eData)/sizeof(BIGINT_DATA_TYPE)); #endif BigInt(&tmp, (BIGINT_DATA_TYPE*)rsaTemp, sizeof(rsaTemp)/sizeof(BIGINT_DATA_TYPE)); smRSA = SM_RSA_IDLE; }
Rational::Rational(BigInt newNumerator, BigInt newDenominator){ if(newDenominator == BigInt(0)){ cout << "Illegal denominator\n"; exit(1); } normalize(newNumerator, newDenominator); numerator = newNumerator; denominator = newDenominator; };
/* * Generate a random integer within given range */ BigInt BigInt::random_integer(RandomNumberGenerator& rng, const BigInt& min, const BigInt& max) { BigInt range = max - min; if(range <= 0) throw Invalid_Argument("random_integer: invalid min/max values"); return (min + (BigInt(rng, range.bits() + 2) % range)); }
int main() { BigInt fib0 = BigInt(1); BigInt fib1 = BigInt(1); BigInt fibTmp; int numIteration = 2; while (fib1.getNumDigits() < 1000) { fibTmp = fib1; fib1 += fib0; fib0 = fibTmp; ++numIteration; } std::cout << numIteration << ". term" << std::endl; return 0; }
const BigInt BigInt::leftShift(int times) const // this *= 10^times { int i, *arr = new int[length + times]; for(i = 0 ; i < length ; i++) arr[i] = digit[i]; for(; i < length+times ; i++) arr[i] = 0; return BigInt(arr, length + times, sign); }
void BigRational::init(const BigInt &numerator, const BigInt &denominator) { DEFINEMETHODNAME; if(denominator.isZero()) { throwInvalidArgumentException(method, _T("Denominator is zero")); } DigitPool *pool = getDigitPool(); if(numerator.isZero()) { // zero always 0/1 m_numerator = pool->get0(); m_denominator = pool->get1(); } else { const BigInt gcd = findGCD(BigInt(fabs(numerator)),BigInt(fabs(denominator))); m_numerator = numerator / gcd; m_denominator = denominator / gcd; if(denominator.isNegative()) { // Negative numbers are represented with negative numerator and positive denominator m_numerator = -m_numerator; m_denominator = -m_denominator; } } }
QVariant EstimateProfileOperationCommand::estimateStealthLink(const QVariantMap &request) { if(!request.contains("uri")) { return makeFeeObject(BigInt(136000), request); } else { return makeFeeObject(estimateEdit(request["uri"].toString(), "payments", request["stealth"].toString()), request); } }
Point ECDSA::Double(Point P) { Point r; BigInt numerator = BigInt(3) * (P.x ^ 2) + a; numerator = ModP(numerator); BigInt denominator = BigInt(2) * P.y; denominator = ModP(denominator); BigInt lambda = numerator * Inverse(denominator); lambda = ModP(lambda); r.x = (lambda ^ BigInt(2)) - (BigInt(2) * P.x); r.x = ModP(r.x); r.y = (lambda * (P.x - r.x)) - P.y; r.y = ModP(r.y); return r; }
const BigInt operator /(const BigInt& amount1, const BigInt& amount2) { // magn = magnificent; BigInt quotient, magn(amount2), dividend(amount1), divisor(amount2); dividend.sign = divisor.sign = 0; if(dividend < divisor) return BigInt(0); if(dividend > divisor) magn = magn.leftShift(dividend.length - divisor.length); //if(dividend < magn) // magn = magn.rightShift(); // magn = magn/10; if(dividend >= magn) quotient.length = amount1.length - amount2.length + 1; else{ quotient.length = amount1.length - amount2.length; magn = magn.rightShift(); // magn = magn/10; } quotient.digit = new int[quotient.length]; for(int i = 0 ; i < quotient.length ; i++) quotient.digit[i] = 0; int index(0); while(dividend >= divisor){ while(dividend >= magn) { quotient.digit[index]++; dividend = dividend - magn; } magn = magn.rightShift(); index++; } if(amount1.getSign() == amount2.getSign()) return BigInt( quotient.digit , quotient.length, quotient.sign); else return BigInt( quotient.digit , quotient.length, !quotient.sign); }
//left shift base Decimal const BigInt BigInt::leftShift(size_t n)const { if( n == 0 ) return *this; std::string str = toString(); if( str.size() == 1 && str[0] == '0') return *this; str.insert(str.end(), n, '0'); return BigInt(str); }
BigInt EstimateProfileOperationCommand::estimateEdit(const QString &uri, const QString &key, const QString &value) { BitProfileStore::Iterator it = _store.find(uri); if(it==_store.end()) { return BigInt(0); } BitProfile::ProfileAdministrator admin = BitProfile::ProfileAdministrator::FromDescriptor(_provider, *it); return _estimator.estimateEdit(admin, key.toStdString(), value.toStdString()); }