#include “”
BigInteger::BigInteger(void)
: dataLength(0), data(0)
{
data = new unsigned int[maxLength];
memset(data, 0, maxLength * sizeof(unsigned int));
dataLength = 1;
}
BigInteger::BigInteger(__int64 value)
{
data = new unsigned int[maxLength];
memset(data, 0, maxLength * sizeof(unsigned int));
__int64 tempVal = value;
dataLength = 0;
while (value != 0 && dataLength < maxLength)
{
data[dataLength] = (unsigned int)(value & 0xFFFFFFFF);
value = value >> 32;
dataLength++;
}
if (tempVal > 0)
{
if (value != 0 || (data[maxLength - 1] & 0x80000000) != 0)
assert(false);
}
else if (tempVal < 0)
{
if (value != -1 || (data[dataLength - 1] & 0x80000000) == 0)
assert(false);
}
if (dataLength == 0)
dataLength = 1;
}
BigInteger::BigInteger(unsigned __int64 value)
{
data = new unsigned int[maxLength];
memset(data, 0, maxLength * sizeof(unsigned int));
dataLength = 0;
while (value != 0 && dataLength < maxLength)
{
data[dataLength] = (unsigned int)(value & 0xFFFFFFFF);
value >>= 32;
dataLength++;
}
if (value != 0 || (data[maxLength - 1] & 0x80000000) != 0)
assert(false);
if (dataLength == 0)
dataLength = 1;
}
BigInteger::BigInteger(const BigInteger &bi)
{
data = new unsigned int[maxLength];
dataLength = ;
for (int i = 0; i < maxLength; i++)
data[i] = [i];
}
BigInteger::~BigInteger(void)
{
if (data != NULL)
{
delete []data;
}
}
BigInteger::BigInteger(string value, int radix)
{
BigInteger multiplier((__int64)1);
BigInteger result;
transform((), (), (), toupper);
int limit = 0;
if (value[0] == ‘-‘)
limit = 1;
for (int i = () - 1; i >= limit; i–)
{
int posVal = (int)value[i];
if (posVal >= ‘0’ && posVal <= ‘9’)
posVal -= ’0’;
else if (posVal >= ‘A’ && posVal <= ‘Z’)
posVal = (posVal - ’A’) + 10;
else
posVal = 9999999;
if (posVal >= radix)
{
assert(false);
}
else
{
result = result + (multiplier * BigInteger((__int64)posVal));
if ((i - 1) >= limit)
multiplier = multiplier * BigInteger((__int64)radix);
}
}
if (value[0] == ‘-‘)
result = -result;
if (value[0] == ‘-‘)
{
if (([maxLength - 1] & 0x80000000) == 0)
assert(false);
}
else
{
if (([maxLength - 1] & 0x80000000) != 0)
assert(false);
}
data = new unsigned int[maxLength];
for (int i = 0; i < maxLength; i++)
data[i] = [i];
dataLength = ;
}
BigInteger::BigInteger(byte inData[], int inLen)
{
dataLength = inLen >> 2;
int leftOver = inLen & 0x3;
if (leftOver != 0)
dataLength++;
if (dataLength > maxLength)
assert(false);
data = new unsigned int[maxLength];
memset(data, 0, maxLength * sizeof(unsigned int));
for (int i = inLen - 1, j = 0; i >= 3; i -= 4, j++)
{
data[j] = (unsigned int)((inData[i - 3] << 24) + (inData[i - 2] << 16) + (inData[i - 1] << 8) + inData[i]);
}
if (leftOver == 1)
data[dataLength - 1] = (unsigned int)inData[0];
else if (leftOver == 2)
data[dataLength - 1] = (unsigned int)((inData[0] << 8) + inData[1]);
else if (leftOver == 3)
data[dataLength - 1] = (unsigned int)((inData[0] << 16) + (inData[1] << 8) + inData[2]);
while (dataLength > 1 && data[dataLength - 1] == 0)
dataLength–;
}
BigInteger::BigInteger(unsigned int inData[], int inLen)
{
dataLength = inLen;
if (dataLength > maxLength)
assert(false);
data = new unsigned int[maxLength];
memset(data, 0, maxLength * sizeof(maxLength));
for (int i = dataLength - 1, j = 0; i >= 0; i–, j++)
data[j] = inData[i];
while (dataLength > 1 && data[dataLength - 1] == 0)
dataLength–;
}
BigInteger BigInteger::operator *(BigInteger bi2)
{
BigInteger bi1(*this);
int lastPos = maxLength - 1;
bool bi1Neg = false, bi2Neg = false;
try
{
if ((this->data[lastPos] & 0x80000000) != 0)
{
bi1Neg = true;
bi1 = -bi1;
}
if (([lastPos] & 0x80000000) != 0)
{
bi2Neg = true; bi2 = -bi2;
}
}
catch (…) { }
BigInteger result;
try
{
for (int i = 0; i < ; i++)
{
if ([i] == 0) continue;
unsigned __int64 mcarry = 0;
for (int j = 0, k = i; j < ; j++, k++)
{
unsigned __int64 val = ((unsigned __int64)[i] * (unsigned __int64)[j]) + (unsigned __int64)[k] + mcarry;
[k] = (unsigned __int64)(val & 0xFFFFFFFF);
mcarry = (val >> 32);
}
if (mcarry != 0)
[i + ] = (unsigned int)mcarry;
}
}
catch (…)
{
assert(false);
}
= + ;
if ( > maxLength)
= maxLength;
while ( > 1 && [ - 1] == 0)
–;
if (([lastPos] & 0x80000000) != 0)
{
if (bi1Neg != bi2Neg && [lastPos] == 0x80000000)
{
if ( == 1)
return result;
else
{
bool isMaxNeg = true;
for (int i = 0; i < - 1 && isMaxNeg; i++)
{
if ([i] != 0)
isMaxNeg = false;
}
if (isMaxNeg)
return result;
}
}
assert(false);
}
if (bi1Neg != bi2Neg)
return -result;
return result;
}
BigInteger BigInteger::operator =(const BigInteger &bi2)
{
if (&bi2 == this)
{
return *this;
}
if (data != NULL)
{
delete []data;
data = NULL;
}
data = new unsigned int[maxLength];
memset(data, 0, maxLength * sizeof(unsigned int));
dataLength = ;
for (int i = 0; i < maxLength; i++)
data[i] = [i];
return *this;
}
BigInteger BigInteger::operator +(BigInteger &bi2)
{
int lastPos = maxLength - 1;
bool bi1Neg = false, bi2Neg = false;
BigInteger bi1(*this);
BigInteger result;
if ((this->data[lastPos] & 0x80000000) != 0)
bi1Neg = true;
if (([lastPos] & 0x80000000) != 0)
bi2Neg = true;
if(bi1Neg == false && bi2Neg == false)
{
= (this->dataLength > ) ? this->dataLength : ;
__int64 carry = 0;
for (int i = 0; i < ; i++)
{
__int64 sum = (__int64)this->data[i] + (__int64)[i] + carry;
carry = sum >> 32;
[i] = (unsigned int)(sum & 0xFFFFFFFF);
}
if (carry != 0 && < maxLength)
{
[] = (unsigned int)(carry);
++;
}
while ( > 1 && [ - 1] == 0)
–;
if ((this->data[lastPos] & 0x80000000) == ([lastPos] & 0x80000000) &&
([lastPos] & 0x80000000) != (this->data[lastPos] & 0x80000000))
{
assert(false);
}
return result;
}
if(bi1Neg == false && bi2Neg == true)
{
BigInteger bi3 = -bi2;
if(bi1 > bi3)
{
result = bi1 - bi3;
return result;
}
else
{
result = -(bi3 - bi1);
return result;
}
}
if(bi1Neg == true && bi2Neg == false)
{
BigInteger bi3 = -bi1;
if(bi3 > bi2)
{
result = -(bi3 - bi2);
return result;
}
else
{
result = bi2 - bi3;
return result;
}
}
if(bi1Neg == true && bi2Neg == true)
{
result = - ((-bi1) + (-bi2));
return result;
}
}
BigInteger BigInteger::operator -()
{
if (this->dataLength == 1 && this->data[0] == 0)
return *this;
BigInteger result(*this);
for (int i = 0; i < maxLength; i++)
[i] = (unsigned int)(~(this->data[i]));
__int64 val, carry = 1;
int index = 0;
while (carry != 0 && index < maxLength)
{
val = (__int64)([index]);
val++;
[index] = (unsigned int)(val & 0xFFFFFFFF);
carry = val >> 32;
index++;
}
if ((this->data[maxLength - 1] & 0x80000000) == ([maxLength - 1] & 0x80000000))
= maxLength;
while ( > 1 && [ - 1] == 0)
–;
return result;
}
BigInteger BigInteger::modPow(BigInteger exp, BigInteger n)
{
if (([maxLength - 1] & 0x80000000) != 0)
return BigInteger((__int64)0);
BigInteger resultNum((__int64)1);
BigInteger tempNum;
bool thisNegative = false;
if ((this->data[maxLength - 1] & 0x80000000) != 0)
{
tempNum = -(*this) % n;
thisNegative = true;
}
else
tempNum = (*this) % n;
if (([maxLength - 1] & 0x80000000) != 0)
n = -n;
BigInteger constant;
int i = << 1;
[i] = 0x00000001;
= i + 1;
constant = constant / n;
int totalBits = ();
int count = 0;
for (int pos = 0; pos < ; pos++)
{
unsigned int mask = 0x01;
for (int index = 0; index < 32; index++)
{
if (([pos] & mask) != 0)
resultNum = BarrettReduction(resultNum * tempNum, n, constant);
mask <<= 1;
tempNum = BarrettReduction(tempNum * tempNum, n, constant);
if ( == 1 && [0] == 1)
{
if (thisNegative && ([0] & 0x1) != 0)
return -resultNum;
return resultNum;
}
count++;
if (count == totalBits)
break;
}
}
if (thisNegative && ([0] & 0x1) != 0)
return -resultNum;
return resultNum;
}
int BigInteger::bitCount()
{
while (dataLength > 1 && data[dataLength - 1] == 0)
dataLength–;
unsigned int value = data[dataLength - 1];
unsigned int mask = 0x80000000;
int bits = 32;
while (bits > 0 && (value & mask) == 0)
{
bits–;
mask >>= 1;
}
bits += ((dataLength - 1) << 5);
return bits;
}
BigInteger BigInteger::BarrettReduction(BigInteger x, BigInteger n, BigInteger constant)
{
int k = ,
kPlusOne = k + 1,
kMinusOne = k - 1;
BigInteger q1;
for (int i = kMinusOne, j = 0; i < ; i++, j++)
[j] = [i];
= - kMinusOne;
if ( <= 0)
= 1;
BigInteger q2 = q1 * constant;
BigInteger q3;
for (int i = kPlusOne, j = 0; i < ; i++, j++)
[j] = [i];
= - kPlusOne;
if ( <= 0)
= 1;
BigInteger r1;
int lengthToCopy = ( > kPlusOne) ? kPlusOne : ;
for (int i = 0; i < lengthToCopy; i++)
[i] = [i];
= lengthToCopy;
BigInteger r2;
for (int i = 0; i < ; i++)
{
if ([i] == 0) continue;
unsigned __int64 mcarry = 0;
int t = i;
for (int j = 0; j < && t < kPlusOne; j++, t++)
{
unsigned __int64 val = ((unsigned __int64)[i] * (unsigned __int64)[j]) +
(unsigned __int64)[t] + mcarry;
[t] = (unsigned int)(val & 0xFFFFFFFF);
mcarry = (val >> 32);
}
if (t < kPlusOne)
[t] = (unsigned int)mcarry;
}
= kPlusOne;
while ( > 1 && [ - 1] == 0)
–;
r1 -= r2;
if (([maxLength - 1] & 0x80000000) != 0)
{
BigInteger val;
[kPlusOne] = 0x00000001;
= kPlusOne + 1;
r1 += val;
}
while (r1 >= n)
r1 -= n;
return r1;
}
bool BigInteger::operator >(BigInteger bi2)
{
int pos = maxLength - 1;
BigInteger bi1(*this);
if (([pos] & 0x80000000) != 0 && ([pos] & 0x80000000) == 0)
return false;
else if (([pos] & 0x80000000) == 0 && ([pos] & 0x80000000) != 0)
return true;
int len = ( > ) ? : ;
for (pos = len - 1; pos >= 0 && [pos] == [pos]; pos–) ;
if (pos >= 0)
{
if ([pos] > [pos])
return true;
return false;
}
return false;
}
bool BigInteger::operator ==(BigInteger bi2)
{
if (this->dataLength != )
return false;
for (int i = 0; i < this->dataLength; i++)
{
if (this->data[i] != [i])
return false;
}
return true;
}
bool BigInteger::operator !=(BigInteger bi2)
{
if(this->dataLength != )
return true;
for(int i = 0; i < this->dataLength; i++)
{
if(this->data[i] != [i])
return true;
}
return false;
}
BigInteger BigInteger::operator %(BigInteger bi2)
{
BigInteger bi1(*this);
BigInteger quotient;
BigInteger remainder(bi1);
int lastPos = maxLength - 1;
bool dividendNeg = false;
if (([lastPos] & 0x80000000) != 0)
{
bi1 = -bi1;
dividendNeg = true;
}
if (([lastPos] & 0x80000000) != 0)
bi2 = -bi2;
if (bi1 < bi2)
{
return remainder;
}
else
{
if ( == 1)
singleByteDivide(bi1, bi2, quotient, remainder);
else
multiByteDivide(bi1, bi2, quotient, remainder);
if (dividendNeg)
return -remainder;
return remainder;
}
}
void BigInteger::singleByteDivide(BigInteger &bi1, BigInteger &bi2,
BigInteger &outQuotient, BigInteger &outRemainder)
{
unsigned int result[maxLength];
memset(result, 0, sizeof(unsigned int) * maxLength);
int resultPos = 0;
for (int i = 0; i < maxLength; i++)
[i] = [i];
= ;
while ( > 1 && [ - 1] == 0)
–;
unsigned __int64 divisor = (unsigned __int64)[0];
int pos = - 1;
unsigned __int64 dividend = (unsigned __int64)[pos];
if (dividend >= divisor)
{
unsigned __int64 quotient = dividend / divisor;
result[resultPos++] = (unsigned __int64)quotient;
[pos] = (unsigned __int64)(dividend % divisor);
}
pos–;
while (pos >= 0)
{
dividend = ((unsigned __int64)[pos + 1] << 32) + (unsigned __int64)[pos];
unsigned __int64 quotient = dividend / divisor;
result[resultPos++] = (unsigned int)quotient;
[pos + 1] = 0;
[pos–] = (unsigned int)(dividend % divisor);
}
= resultPos;
int j = 0;
for (int i = - 1; i >= 0; i–, j++)
[j] = result[i];
for (; j < maxLength; j++)
[j] = 0;
while ( > 1 && [ - 1] == 0)
–;
if ( == 0)
= 1;
while ( > 1 && [ - 1] == 0)
–;
}
void BigInteger::multiByteDivide(BigInteger &bi1, BigInteger &bi2,
BigInteger &outQuotient, BigInteger &outRemainder)
{
unsigned int result[maxLength];
memset(result, 0, sizeof(unsigned int) * maxLength);
int remainderLen = + 1;
unsigned int *remainder = new unsigned int[remainderLen];
memset(remainder, 0, sizeof(unsigned int) * remainderLen);
unsigned int mask = 0x80000000;
unsigned int val = [ - 1];
int shift = 0, resultPos = 0;
while (mask != 0 && (val & mask) == 0)
{
shift++; mask >>= 1;
}
for (int i = 0; i < ; i++)
remainder[i] = [i];
this->shiftLeft(remainder, remainderLen, shift);
bi2 = bi2 << shift;
int j = remainderLen - ;
int pos = remainderLen - 1;
unsigned __int64 firstDivisorByte = [ - 1];
unsigned __int64 secondDivisorByte = [ - 2];
int divisorLen = + 1;
unsigned int *dividendPart = new unsigned int[divisorLen];
memset(dividendPart, 0, sizeof(unsigned int) * divisorLen);
while (j > 0)
{
unsigned __int64 dividend = ((unsigned __int64)remainder[pos] << 32) + (unsigned __int64)remainder[pos - 1];
unsigned __int64 q_hat = dividend / firstDivisorByte;
unsigned __int64 r_hat = dividend % firstDivisorByte;
bool done = false;
while (!done)
{
done = true;
if (q_hat == 0x100000000 || (q_hat * secondDivisorByte) > ((r_hat << 32) + remainder[pos - 2]))
{
q_hat–;
r_hat += firstDivisorByte;
if (r_hat < 0x100000000)
done = false;
}
}
for (int h = 0; h < divisorLen; h++)
dividendPart[h] = remainder[pos - h];
BigInteger kk(dividendPart, divisorLen);
BigInteger ss = bi2 * BigInteger((__int64)q_hat);
while (ss > kk)
{
q_hat–;
ss -= bi2;
}
BigInteger yy = kk - ss;
for (int h = 0; h < divisorLen; h++)
remainder[pos - h] = [ - h];
result[resultPos++] = (unsigned int)q_hat;
pos–;
j–;
}
= resultPos;
int y = 0;
for (int x = - 1; x >= 0; x–, y++)
[y] = result[x];
for (; y < maxLength; y++)
[y] = 0;
while ( > 1 && [ - 1] == 0)
–;
if ( == 0)
= 1;
= this->shiftRight(remainder, remainderLen, shift);
for (y = 0; y < ; y++)
[y] = remainder[y];
for (; y < maxLength; y++)
[y] = 0;
delete []remainder;
delete []dividendPart;
}
int BigInteger::shiftRight(unsigned int buffer[], int bufferLen,int shiftVal)
{
int shiftAmount = 32;
int invShift = 0;
int bufLen = bufferLen;
while (bufLen > 1 && buffer[bufLen - 1] == 0)
bufLen–;
for (int count = shiftVal; count > 0; )
{
if (count < shiftAmount)
{
shiftAmount = count;
invShift = 32 - shiftAmount;
}
unsigned __int64 carry = 0;
for (int i = bufLen - 1; i >= 0; i–)
{
unsigned __int64 val = ((unsigned __int64)buffer[i]) >> shiftAmount;
val |= carry;
carry = ((unsigned __int64)buffer[i]) << invShift;
buffer[i] = (unsigned int)(val);
}
count -= shiftAmount;
}
while (bufLen > 1 && buffer[bufLen - 1] == 0)
bufLen–;
return bufLen;
}
BigInteger BigInteger::operator <<(int shiftVal)
{
BigInteger result(*this);
= shiftLeft(, maxLength, shiftVal);
return result;
}
int BigInteger::shiftLeft(unsigned int buffer[], int bufferLen, int shiftVal)
{
int shiftAmount = 32;
int bufLen = bufferLen;
while (bufLen > 1 && buffer[bufLen - 1] == 0)
bufLen–;
for (int count = shiftVal; count > 0; )
{
if (count < shiftAmount)
shiftAmount = count;
unsigned __int64 carry = 0;
for (int i = 0; i < bufLen; i++)
{
unsigned __int64 val = ((unsigned __int64)buffer[i]) << shiftAmount;
val |= carry;
buffer[i] = (unsigned int)(val & 0xFFFFFFFF);
carry = val >> 32;
}
if (carry != 0)
{
if (bufLen + 1 <= bufferLen)
{
buffer[bufLen] = (unsigned int)carry;
bufLen++;
}
}
count -= shiftAmount;
}
return bufLen;
}
bool BigInteger::operator <(BigInteger bi2)
{
BigInteger bi1(*this);
int pos = maxLength - 1;
if (([pos] & 0x80000000) != 0 && ([pos] & 0x80000000) == 0)
return true;
else if (([pos] & 0x80000000) == 0 && ([pos] & 0x80000000) != 0)
return false;
int len = ( > ) ? : ;
for (pos = len - 1; pos >= 0 && [pos] == [pos]; pos–) ;
if (pos >= 0)
{
if ([pos] < [pos])
return true;
return false;
}
return false;
}
BigInteger BigInteger::operator +=(BigInteger bi2)
{
*this = *this + bi2;
return *this;
}
BigInteger BigInteger::operator /(BigInteger bi2)
{
BigInteger bi1(*this);
BigInteger quotient;
BigInteger remainder;
int lastPos = maxLength - 1;
bool divisorNeg = false, dividendNeg = false;
if (([lastPos] & 0x80000000) != 0)
{
bi1 = -bi1;
dividendNeg = true;
}
if (([lastPos] & 0x80000000) != 0)
{
bi2 = -bi2;
divisorNeg = true;
}
if (bi1 < bi2)
{
return quotient;
}
else
{
if ( == 1)
singleByteDivide(bi1, bi2, quotient, remainder);
else
multiByteDivide(bi1, bi2, quotient, remainder);
if (dividendNeg != divisorNeg)
return -quotient;
return quotient;
}
}
BigInteger BigInteger::operator -=(BigInteger bi2)
{
*this = *this - bi2;
return *this;
}
BigInteger BigInteger::operator -(BigInteger bi2)
{
BigInteger bi1(*this);
BigInteger result;
int lastPos = maxLength - 1;
bool bi1Neg = false, bi2Neg = false;
if ((this->data[lastPos] & 0x80000000) != 0)
bi1Neg = true;
if (([lastPos] & 0x80000000) != 0)
bi2Neg = true;
if(bi1Neg == false && bi2Neg == false)
{
if(bi1 < bi2)
{
result = -(bi2 - bi1);
return result;
}
= ( > ) ? : ;
__int64 carryIn = 0;
for (int i = 0; i < ; i++)
{
__int64 diff;
diff = (__int64)[i] - (__int64)[i] - carryIn;
[i] = (unsigned int)(diff & 0xFFFFFFFF);
if (diff < 0)
carryIn = 1;
else
carryIn = 0;
}
if (carryIn != 0)
{
for (int i = ; i < maxLength; i++)
[i] = 0xFFFFFFFF;
= maxLength;
}
while ( > 1 && [ - 1] == 0)
–;
if (([lastPos] & 0x80000000) != ([lastPos] & 0x80000000) &&
([lastPos] & 0x80000000) != ([lastPos] & 0x80000000))
{
assert(false);
}
return result;
}
if(bi1Neg == true && bi2Neg == false)
{
result = -(-bi1 + bi2);
return result;
}
if(bi1Neg == false && bi2Neg == true)
{
result = bi1 + (-bi2);
return result;
}
if(bi1Neg == true && bi2Neg == true)
{
BigInteger bi3 = -bi1, bi4 = -bi2;
if(bi3 > bi4)
{
result = -(bi3 - bi4);
return result;
}
else
{
result = bi4 - bi3;
return result;
}
}
}
string BigInteger::DecToHex(unsigned int value, string format)
{
string HexStr;
int a[100];
int i = 0;
int m = 0;
int mod = 0;
char hex[16]={‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘A’,‘B’,‘C’,‘D’,‘E’,‘F’};
while(value > 0)
{
mod = value % 16;
a[i++] = mod;
value = value/16;
}
for(i = i - 1; i >= 0; i–)
{
m=a[i];
HexStr.push_back(hex[m]);
}
while (format == string(“X8”) && () < 8)
{
HexStr = ”0” + HexStr;
}
return HexStr;
}
string BigInteger::ToHexString()
{
string result = DecToHex(data[dataLength - 1], string(”X”));
for (int i = dataLength - 2; i >= 0; i–)
{
result += DecToHex(data[i], string(”X8”));
}
return result;
}
ostream& operator<<(ostream& output, BigInteger &obj)
{
for(int i = - 1; i >= 0; i–)
output << hex << [i];
return output;
}
bool Miller_Robin(BigInteger &bi1)
{
BigInteger one((__int64)1), two((__int64)2), sum, a, b, temp;
int k = 0, len = primeLength / 2;
temp = sum = bi1 - one;
while(([0] & 0x00000001) == 0)
{
= (, maxLength, 1);
k++;
}
srand((unsigned)time(0));
for(int i = 0; i < len; i++)
{
[i] =(unsigned int)rand ();
if([i] != 0) = i + 1;
}
b = (sum, bi1);
if (b == one) return true;
for(int i = 0; i < k; i++)
{
if(b == temp) return true;
else b = (two, bi1);
}
return false;
}
bool IsPrime (BigInteger &obj)
{
BigInteger zero;
for(int i = 0; i < 303; i++)
{
BigInteger prime((__int64)primesBelow2000[i]);
if(obj % prime == zero)
return false;
}
cout << ”第一轮素性检验通过… … … …” << endl;
cout << ”正在进行Miller_Robin素性检验… … … …” << endl;
if(Miller_Robin(obj))
return true;
return false;
}
BigInteger GetPrime()
{
BigInteger one((__int64)1), two((__int64)2), result;
srand((unsigned)time(0));
for(int i = 0; i < primeLength; i++)
{
[i] =(unsigned int)rand();
if([i] != 0)
= i + 1;
}
[0] |= 0x00000001;
while(!IsPrime(result))
{
result = result + two;
cout << ”检验没有通过,进行下一个数的检验,运行之中… … … …” << endl;
cout << endl;
}
return result;
}
BigInteger extended_euclidean(BigInteger n, BigInteger m, BigInteger &x, BigInteger &y)
{
BigInteger x1((__int64)1), x2, x3(n);
BigInteger y1, y2((__int64)1), y3(m);
BigInteger zero;
while(x3 % y3 != zero)
{
BigInteger d = x3 / y3;
BigInteger t1, t2, t3;
t1 = x1 - d * y1;
t2 = x2 - d * y2;
t3 = x3 - d * y3;
x1 = y1; x2 = y2; x3 = y3;
y1 = t1; y2 = t2; y3 = t3;
}
x = y1; y = y2;
return y3;
}
BigInteger Gcd(BigInteger &bi1, BigInteger &bi2)
{
BigInteger x, y;
BigInteger g = extended_euclidean(bi1, bi2, x, y);
return g;
}
BigInteger MultipInverse(BigInteger &bi1, BigInteger &n)
{
BigInteger x, y;
extended_euclidean(bi1, n, x, y);
if (([maxLength-1] & 0x80000000) != 0)
x = x + n;
return x;
}