本文整理汇总了C#中BigInteger.bitLength方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.bitLength方法的具体用法?C# BigInteger.bitLength怎么用?C# BigInteger.bitLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.bitLength方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: squareAndMultiply
internal static BigInteger squareAndMultiply(BigInteger x2, BigInteger a2, BigInteger exponent,BigInteger modulus, int n2 )
{
BigInteger res = x2;
for (int i = exponent.bitLength() - 1; i >= 0; i--) {
res = monPro(res,res,modulus, n2);
if (BitLevel.testBit(exponent, i)) {
res = monPro(res, a2, modulus, n2);
}
}
return res;
}
示例2: slidingWindow
/*Implements the Montgomery modular exponentiation based in <i>The sliding windows algorithm and the Mongomery
*Reduction</i>.
*@ar.org.fitc.ref "A. Menezes,P. van Oorschot, S. Vanstone - Handbook of Applied Cryptography";
*@see #oddModPow(BigInteger, BigInteger,
* BigInteger)
*/
internal static BigInteger slidingWindow(BigInteger x2, BigInteger a2, BigInteger exponent,BigInteger modulus, int n2)
{
// fill odd low pows of a2
BigInteger []pows = new BigInteger[8];
BigInteger res = x2;
int lowexp;
BigInteger x3;
int acc3;
pows[0] = a2;
x3 = monPro(a2,a2,modulus,n2);
for (int i = 1; i <= 7; i++){
pows[i] = monPro(pows[i-1],x3,modulus,n2) ;
}
for (int i = exponent.bitLength()-1; i>=0;i--){
if( BitLevel.testBit(exponent,i) ) {
lowexp = 1;
acc3 = i;
for(int j = java.lang.Math.max(i-3,0);j <= i-1 ;j++) {
if (BitLevel.testBit(exponent,j)) {
if (j<acc3) {
acc3 = j;
lowexp = (lowexp << (i-j))^1;
} else {
lowexp = lowexp^(1<<(j-acc3));
}
}
}
for(int j = acc3; j <= i; j++) {
res = monPro(res,res,modulus,n2);
}
res = monPro(pows[(lowexp-1)>>1], res, modulus,n2);
i = acc3 ;
}else{
res = monPro(res, res, modulus, n2) ;
}
}
return res;
}
示例3: nextProbablePrime
/**
* It uses the sieve of Eratosthenes to discard several composite numbers in
* some appropriate range (at the moment {@code [this, this + 1024]}). After
* this process it applies the Miller-Rabin test to the numbers that were
* not discarded in the sieve.
*
* @see BigInteger#nextProbablePrime()
* @see #millerRabin(BigInteger, int)
*/
internal static BigInteger nextProbablePrime(BigInteger n)
{
// PRE: n >= 0
int i, j;
int certainty;
int gapSize = 1024; // for searching of the next probable prime number
int []modules = new int[primes.Length];
bool[] isDivisible = new bool[gapSize];
BigInteger startPoint;
BigInteger probPrime;
// If n < "last prime of table" searches next prime in the table
if ((n.numberLength == 1) && (n.digits[0] >= 0)
&& (n.digits[0] < primes[primes.Length - 1])) {
for (i = 0; n.digits[0] >= primes[i]; i++) {
;
}
return BIprimes[i];
}
/*
* Creates a "N" enough big to hold the next probable prime Note that: N <
* "next prime" < 2*N
*/
startPoint = new BigInteger(1, n.numberLength,
new int[n.numberLength + 1]);
java.lang.SystemJ.arraycopy(n.digits, 0, startPoint.digits, 0, n.numberLength);
// To fix N to the "next odd number"
if (n.testBit(0)) {
Elementary.inplaceAdd(startPoint, 2);
} else {
startPoint.digits[0] |= 1;
}
// To set the improved certainly of Miller-Rabin
j = startPoint.bitLength();
for (certainty = 2; j < BITS[certainty]; certainty++) {
;
}
// To calculate modules: N mod p1, N mod p2, ... for first primes.
for (i = 0; i < primes.Length; i++) {
modules[i] = Division.remainder(startPoint, primes[i]) - gapSize;
}
while (true) {
// At this point, all numbers in the gap are initialized as
// probably primes
java.util.Arrays<Object>.fill(isDivisible, false);
// To discard multiples of first primes
for (i = 0; i < primes.Length; i++) {
modules[i] = (modules[i] + gapSize) % primes[i];
j = (modules[i] == 0) ? 0 : (primes[i] - modules[i]);
for (; j < gapSize; j += primes[i]) {
isDivisible[j] = true;
}
}
// To execute Miller-Rabin for non-divisible numbers by all first
// primes
for (j = 0; j < gapSize; j++) {
if (!isDivisible[j]) {
probPrime = startPoint.copy();
Elementary.inplaceAdd(probPrime, j);
if (millerRabin(probPrime, certainty)) {
return probPrime;
}
}
}
Elementary.inplaceAdd(startPoint, gapSize);
}
}
示例4: inplaceModPow2
/**
* Performs {@code x = x mod (2<sup>n</sup>)}.
*
* @param x a positive number, it will store the result.
* @param n a positive exponent of {@code 2}.
*/
internal static void inplaceModPow2(BigInteger x, int n)
{
// PRE: (x > 0) and (n >= 0)
int fd = n >> 5;
int leadingZeros;
if ((x.numberLength < fd) || (x.bitLength() <= n)) {
return;
}
leadingZeros = 32 - (n & 31);
x.numberLength = fd + 1;
x.digits[fd] &= (leadingZeros < 32) ? (java.dotnet.lang.Operator.shiftRightUnsignet(-1, leadingZeros)) : 0;
x.cutOffLeadingZeroes();
}
示例5: isProbablePrime
/**
* @see BigInteger#isProbablePrime(int)
* @see #millerRabin(BigInteger, int)
* @ar.org.fitc.ref Optimizations: "A. Menezes - Handbook of applied
* Cryptography, Chapter 4".
*/
internal static bool isProbablePrime(BigInteger n, int certainty)
{
// PRE: n >= 0;
if ((certainty <= 0) || ((n.numberLength == 1) && (n.digits[0] == 2))) {
return true;
}
// To discard all even numbers
if (!n.testBit(0)) {
return false;
}
// To check if 'n' exists in the table (it fit in 10 bits)
if ((n.numberLength == 1) && ((n.digits[0] & 0XFFFFFC00) == 0)) {
return (java.util.Arrays<Object>.binarySearch(primes, n.digits[0]) >= 0);
}
// To check if 'n' is divisible by some prime of the table
for (int i = 1; i < primes.Length; i++) {
if (Division.remainderArrayByInt(n.digits, n.numberLength,
primes[i]) == 0) {
return false;
}
}
// To set the number of iterations necessary for Miller-Rabin test
int iJ;
int bitLength = n.bitLength();
for (iJ = 2; bitLength < BITS[iJ]; iJ++) {
;
}
certainty = java.lang.Math.min(iJ, 1 + ((certainty - 1) >> 1));
return millerRabin(n, certainty);
}