private unsafe BigInteger EvenModTwoPow (BigInteger exp)
{
exp.Normalize ();
uint [] wkspace = new uint [mod.length << 1 + 1];
BigInteger resultNum = new BigInteger (2, mod.length << 1 +1);
uint value = exp.data [exp.length - 1];
uint mask = 0x80000000;
// Find the first bit of the exponent
while ((value & mask) == 0)
mask >>= 1;
//
// We know that the first itr will make the val 2,
// so eat one bit of the exponent
//
mask >>= 1;
uint wPos = exp.length - 1;
do {
value = exp.data [wPos];
do {
Kernel.SquarePositive (resultNum, ref wkspace);
if (resultNum.length >= mod.length)
BarrettReduction (resultNum);
if ((value & mask) != 0) {
//
// resultNum = (resultNum * 2) % mod
//
fixed (uint* u = resultNum.data) {
//
// Double
//
uint* uu = u;
uint* uuE = u + resultNum.length;
uint x, carry = 0;
while (uu < uuE) {
x = *uu;
*uu = (x << 1) | carry;
carry = x >> (32 - 1);
uu++;
}
// subtraction inlined because we know it is square
if (carry != 0 || resultNum >= mod) {
uu = u;
uint c = 0;
uint [] s = mod.data;
uint i = 0;
do {
uint a = s [i];
if (((a += c) < c) | ((* (uu++) -= a) > ~a))
c = 1;
else
c = 0;
i++;
} while (uu < uuE);
}
}
}
} while ((mask >>= 1) > 0);
mask = 0x80000000;
} while (wPos-- > 0);
return resultNum;
}