本文整理汇总了C++中BigInt类的典型用法代码示例。如果您正苦于以下问题:C++ BigInt类的具体用法?C++ BigInt怎么用?C++ BigInt使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BigInt类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
// 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;
}
示例2: getFromFiles
//чтение из файла
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;
}
示例3: xfree
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);
}
示例4: DivModData
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);
}
示例5: z
/*
* 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;
}
}
示例6: BigInt
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;
}
示例7:
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;
}
示例8: fully
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;
}
示例9: PointGFp
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
//.........这里部分代码省略.........
示例10: div
BigInt operator / (BigInt &in){
return div(*this, in, (sign() != in.sign()));
}
示例11: div
BigInt div(int number) {
BigInt result = *this;
result.divThis(number);
return result;
}
示例12:
bool operator==(BigInt obj) {
BigInt temp;
temp = obj - *this;
return temp.isAbsZero();
}
示例13: Print
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;
}
示例14: mul_add
/*
* 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;
}
示例15: encode
/*
* 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());
}