本文整理汇总了C#中BigInteger.ShiftRight方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.ShiftRight方法的具体用法?C# BigInteger.ShiftRight怎么用?C# BigInteger.ShiftRight使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.ShiftRight方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WindowNaf
/**
* Computes the Window NAF (non-adjacent Form) of an integer.
* @param width The width <code>w</code> of the Window NAF. The width is
* defined as the minimal number <code>w</code>, such that for any
* <code>w</code> consecutive digits in the resulting representation, at
* most one is non-zero.
* @param k The integer of which the Window NAF is computed.
* @return The Window NAF of the given width, such that the following holds:
* <code>k = −<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
* </code>, where the <code>k<sub>i</sub></code> denote the elements of the
* returned <code>sbyte[]</code>.
*/
public sbyte[] WindowNaf(sbyte width, BigInteger k)
{
// The window NAF is at most 1 element longer than the binary
// representation of the integer k. sbyte can be used instead of short or
// int unless the window width is larger than 8. For larger width use
// short or int. However, a width of more than 8 is not efficient for
// m = log2(q) smaller than 2305 Bits. Note: Values for m larger than
// 1000 Bits are currently not used in practice.
sbyte[] wnaf = new sbyte[k.BitLength + 1];
// 2^width as short and BigInteger
short pow2wB = (short)(1 << width);
BigInteger pow2wBI = BigInteger.ValueOf(pow2wB);
int i = 0;
// The actual length of the WNAF
int length = 0;
// while k >= 1
while (k.SignValue > 0)
{
// if k is odd
if (k.TestBit(0))
{
// k Mod 2^width
BigInteger remainder = k.Mod(pow2wBI);
// if remainder > 2^(width - 1) - 1
if (remainder.TestBit(width - 1))
{
wnaf[i] = (sbyte)(remainder.IntValue - pow2wB);
}
else
{
wnaf[i] = (sbyte)remainder.IntValue;
}
// wnaf[i] is now in [-2^(width-1), 2^(width-1)-1]
k = k.Subtract(BigInteger.ValueOf(wnaf[i]));
length = i;
}
else
{
wnaf[i] = 0;
}
// k = k/2
k = k.ShiftRight(1);
i++;
}
length++;
// Reduce the WNAF array to its actual length
sbyte[] wnafShort = new sbyte[length];
Array.Copy(wnaf, 0, wnafShort, 0, length);
return wnafShort;
}
示例2: GenerateCompactWindowNaf
public static int[] GenerateCompactWindowNaf(int width, BigInteger k)
{
if (width == 2)
{
return GenerateCompactNaf(k);
}
if (width < 2 || width > 16)
throw new ArgumentException("must be in the range [2, 16]", "width");
if ((k.BitLength >> 16) != 0)
throw new ArgumentException("must have bitlength < 2^16", "k");
if (k.SignValue == 0)
return EMPTY_INTS;
int[] wnaf = new int[k.BitLength / width + 1];
// 2^width and a mask and sign bit set accordingly
int pow2 = 1 << width;
int mask = pow2 - 1;
int sign = pow2 >> 1;
bool carry = false;
int length = 0, pos = 0;
while (pos <= k.BitLength)
{
if (k.TestBit(pos) == carry)
{
++pos;
continue;
}
k = k.ShiftRight(pos);
int digit = k.IntValue & mask;
if (carry)
{
++digit;
}
carry = (digit & sign) != 0;
if (carry)
{
digit -= pow2;
}
int zeroes = length > 0 ? pos - 1 : pos;
wnaf[length++] = (digit << 16) | zeroes;
pos = width;
}
// Reduce the WNAF array to its actual length
if (wnaf.Length > length)
{
wnaf = Trim(wnaf, length);
}
return wnaf;
}
示例3: FromBigInteger64
public static ulong[] FromBigInteger64(BigInteger x)
{
if (x.SignValue < 0 || x.BitLength > 448)
throw new ArgumentException();
ulong[] z = Create64();
int i = 0;
while (x.SignValue != 0)
{
z[i++] = (ulong)x.LongValue;
x = x.ShiftRight(64);
}
return z;
}
示例4: FromBigInteger
public static uint[] FromBigInteger(BigInteger x)
{
if (x.SignValue < 0 || x.BitLength > 192)
throw new ArgumentException();
uint[] z = Create();
int i = 0;
while (x.SignValue != 0)
{
z[i++] = (uint)x.IntValue;
x = x.ShiftRight(32);
}
return z;
}
示例5: FromBigInteger
public static uint[] FromBigInteger(int bits, BigInteger x)
{
if (x.SignValue < 0 || x.BitLength > bits)
throw new ArgumentException();
int len = (bits + 31) >> 5;
uint[] z = Create(len);
int i = 0;
while (x.SignValue != 0)
{
z[i++] = (uint)x.IntValue;
x = x.ShiftRight(32);
}
return z;
}
示例6: ModReduce
protected virtual BigInteger ModReduce(BigInteger x)
{
if (r == null)
{
x = x.Mod(q);
}
else
{
bool negative = x.SignValue < 0;
if (negative)
{
x = x.Abs();
}
int qLen = q.BitLength;
if (r.SignValue > 0)
{
BigInteger qMod = BigInteger.One.ShiftLeft(qLen);
bool rIsOne = r.Equals(BigInteger.One);
while (x.BitLength > (qLen + 1))
{
BigInteger u = x.ShiftRight(qLen);
BigInteger v = x.Remainder(qMod);
if (!rIsOne)
{
u = u.Multiply(r);
}
x = u.Add(v);
}
}
else
{
int d = ((qLen - 1) & 31) + 1;
BigInteger mu = r.Negate();
BigInteger u = mu.Multiply(x.ShiftRight(qLen - d));
BigInteger quot = u.ShiftRight(qLen + d);
BigInteger v = quot.Multiply(q);
BigInteger bk1 = BigInteger.One.ShiftLeft(qLen + d);
v = v.Remainder(bk1);
x = x.Remainder(bk1);
x = x.Subtract(v);
if (x.SignValue < 0)
{
x = x.Add(bk1);
}
}
while (x.CompareTo(q) >= 0)
{
x = x.Subtract(q);
}
if (negative && x.SignValue != 0)
{
x = q.Subtract(x);
}
}
return x;
}
示例7: ModHalfAbs
protected virtual BigInteger ModHalfAbs(BigInteger x)
{
if (x.TestBit(0))
{
x = q.Subtract(x);
}
return x.ShiftRight(1);
}
示例8: EvenModPow
/// <summary>
/// Performs modular exponentiation using the Montgomery Reduction.
/// <para>It requires that all parameters be positive and the modulus be even.
/// Based on theThe square and multiply algorithm and the Montgomery Reduction
/// C. K. Koc - Montgomery Reduction with Even Modulus.
/// The square and multiply algorithm and the Montgomery Reduction.
/// ar.org.fitc.ref "C. K. Koc - Montgomery Reduction with Even Modulus"
/// </para>
/// </summary>
///
/// <param name="X">The BigInteger</param>
/// <param name="Y">The Exponent</param>
/// <param name="Modulus">The Modulus</param>
///
/// <returns><c>x1 + q * y</c></returns>
internal static BigInteger EvenModPow(BigInteger X, BigInteger Y, BigInteger Modulus)
{
// PRE: (base > 0), (exponent > 0), (modulus > 0) and (modulus even)
// STEP 1: Obtain the factorization 'modulus'= q * 2^j.
int j = Modulus.LowestSetBit;
BigInteger q = Modulus.ShiftRight(j);
// STEP 2: Compute x1 := base^exponent (mod q).
BigInteger x1 = OddModPow(X, Y, q);
// STEP 3: Compute x2 := base^exponent (mod 2^j).
BigInteger x2 = Pow2ModPow(X, Y, j);
// STEP 4: Compute q^(-1) (mod 2^j) and y := (x2-x1) * q^(-1) (mod 2^j)
BigInteger qInv = ModPow2Inverse(q, j);
BigInteger y = (x2.Subtract(x1)).Multiply(qInv);
InplaceModPow2(y, j);
if (y._sign < 0)
y = y.Add(BigInteger.GetPowerOfTwo(j));
// STEP 5: Compute and return: x1 + q * y
return x1.Add(q.Multiply(y));
}
示例9: TestTestBit
public void TestTestBit()
{
for (int i = 0; i < 10; ++i)
{
var n = new BigInteger(128, Rnd);
Assert.IsFalse(n.TestBit(128));
Assert.IsTrue(n.Negate().TestBit(128));
for (int j = 0; j < 10; ++j)
{
int pos = Rnd.Next(128);
bool test = n.ShiftRight(pos).Remainder(Two).Equals(One);
Assert.AreEqual(test, n.TestBit(pos));
}
}
}
示例10: TestGetLowestSetBit
public void TestGetLowestSetBit()
{
for (int i = 0; i < 10; ++i)
{
BigInteger test = new BigInteger(128, 0, Rnd).Add(One);
int bit1 = test.GetLowestSetBit();
Assert.AreEqual(test, test.ShiftRight(bit1).ShiftLeft(bit1));
int bit2 = test.ShiftLeft(i + 1).GetLowestSetBit();
Assert.AreEqual(i + 1, bit2 - bit1);
int bit3 = test.ShiftLeft(13*i + 1).GetLowestSetBit();
Assert.AreEqual(13*i + 1, bit3 - bit1);
}
}
示例11: TauAdicWNaf
/**
* Computes the <code>[τ]</code>-adic window NAF of an element
* <code>λ</code> of <code><b>Z</b>[τ]</code>.
* @param mu The parameter μ of the elliptic curve.
* @param lambda The element <code>λ</code> of
* <code><b>Z</b>[τ]</code> of which to compute the
* <code>[τ]</code>-adic NAF.
* @param width The window width of the resulting WNAF.
* @param pow2w 2<sup>width</sup>.
* @param tw The auxiliary value <code>t<sub>w</sub></code>.
* @param alpha The <code>α<sub>u</sub></code>'s for the window width.
* @return The <code>[τ]</code>-adic window NAF of
* <code>λ</code>.
*/
public static sbyte[] TauAdicWNaf(sbyte mu, ZTauElement lambda,
sbyte width, BigInteger pow2w, BigInteger tw, ZTauElement[] alpha)
{
if (!((mu == 1) || (mu == -1)))
throw new ArgumentException("mu must be 1 or -1");
BigInteger norm = Norm(mu, lambda);
// Ceiling of log2 of the norm
int log2Norm = norm.BitLength;
// If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52
int maxLength = log2Norm > 30 ? log2Norm + 4 + width : 34 + width;
// The array holding the TNAF
sbyte[] u = new sbyte[maxLength];
// 2^(width - 1)
BigInteger pow2wMin1 = pow2w.ShiftRight(1);
// Split lambda into two BigIntegers to simplify calculations
BigInteger r0 = lambda.u;
BigInteger r1 = lambda.v;
int i = 0;
// while lambda <> (0, 0)
while (!((r0.Equals(BigInteger.Zero))&&(r1.Equals(BigInteger.Zero))))
{
// if r0 is odd
if (r0.TestBit(0))
{
// uUnMod = r0 + r1*tw Mod 2^width
BigInteger uUnMod
= r0.Add(r1.Multiply(tw)).Mod(pow2w);
sbyte uLocal;
// if uUnMod >= 2^(width - 1)
if (uUnMod.CompareTo(pow2wMin1) >= 0)
{
uLocal = (sbyte) uUnMod.Subtract(pow2w).IntValue;
}
else
{
uLocal = (sbyte) uUnMod.IntValue;
}
// uLocal is now in [-2^(width-1), 2^(width-1)-1]
u[i] = uLocal;
bool s = true;
if (uLocal < 0)
{
s = false;
uLocal = (sbyte)-uLocal;
}
// uLocal is now >= 0
if (s)
{
r0 = r0.Subtract(alpha[uLocal].u);
r1 = r1.Subtract(alpha[uLocal].v);
}
else
{
r0 = r0.Add(alpha[uLocal].u);
r1 = r1.Add(alpha[uLocal].v);
}
}
else
{
u[i] = 0;
}
BigInteger t = r0;
if (mu == 1)
{
r0 = r1.Add(r0.ShiftRight(1));
}
else
{
// mu == -1
r0 = r1.Subtract(r0.ShiftRight(1));
}
r1 = t.ShiftRight(1).Negate();
i++;
}
//.........这里部分代码省略.........
示例12: ApproximateDivisionByN
/**
* Approximate division by <code>n</code>. For an integer
* <code>k</code>, the value <code>λ = s k / n</code> is
* computed to <code>c</code> bits of accuracy.
* @param k The parameter <code>k</code>.
* @param s The curve parameter <code>s<sub>0</sub></code> or
* <code>s<sub>1</sub></code>.
* @param vm The Lucas Sequence element <code>V<sub>m</sub></code>.
* @param a The parameter <code>a</code> of the elliptic curve.
* @param m The bit length of the finite field
* <code><b>F</b><sub>m</sub></code>.
* @param c The number of bits of accuracy, i.e. the scale of the returned
* <code>SimpleBigDecimal</code>.
* @return The value <code>λ = s k / n</code> computed to
* <code>c</code> bits of accuracy.
*/
public static SimpleBigDecimal ApproximateDivisionByN(BigInteger k,
BigInteger s, BigInteger vm, sbyte a, int m, int c)
{
int _k = (m + 5)/2 + c;
BigInteger ns = k.ShiftRight(m - _k - 2 + a);
BigInteger gs = s.Multiply(ns);
BigInteger hs = gs.ShiftRight(m);
BigInteger js = vm.Multiply(hs);
BigInteger gsPlusJs = gs.Add(js);
BigInteger ls = gsPlusJs.ShiftRight(_k-c);
if (gsPlusJs.TestBit(_k-c-1))
{
// round up
ls = ls.Add(BigInteger.One);
}
return new SimpleBigDecimal(ls, c);
}
示例13: Karatsuba
/**
* Performs the multiplication with the Karatsuba's algorithm.
* <b>Karatsuba's algorithm:</b>
*<tt>
* u = u<sub>1</sub> * B + u<sub>0</sub><br>
* v = v<sub>1</sub> * B + v<sub>0</sub><br>
*
*
* u*v = (u<sub>1</sub> * v<sub>1</sub>) * B<sub>2</sub> + ((u<sub>1</sub> - u<sub>0</sub>) * (v<sub>0</sub> - v<sub>1</sub>) + u<sub>1</sub> * v<sub>1</sub> +
* u<sub>0</sub> * v<sub>0</sub> ) * B + u<sub>0</sub> * v<sub>0</sub><br>
*</tt>
* @param op1 first factor of the product
* @param op2 second factor of the product
* @return {@code op1 * op2}
* @see #multiply(BigInteger, BigInteger)
*/
private static BigInteger Karatsuba(BigInteger op1, BigInteger op2)
{
BigInteger temp;
if (op2.numberLength > op1.numberLength) {
temp = op1;
op1 = op2;
op2 = temp;
}
if (op2.numberLength < WhenUseKaratsuba) {
return MultiplyPap(op1, op2);
}
/* Karatsuba: u = u1*B + u0
* v = v1*B + v0
* u*v = (u1*v1)*B^2 + ((u1-u0)*(v0-v1) + u1*v1 + u0*v0)*B + u0*v0
*/
// ndiv2 = (op1.numberLength / 2) * 32
int ndiv2 = (int)(op1.numberLength & 0xFFFFFFFE) << 4;
BigInteger upperOp1 = op1.ShiftRight(ndiv2);
BigInteger upperOp2 = op2.ShiftRight(ndiv2);
BigInteger lowerOp1 = op1.Subtract(upperOp1.ShiftLeft(ndiv2));
BigInteger lowerOp2 = op2.Subtract(upperOp2.ShiftLeft(ndiv2));
BigInteger upper = Karatsuba(upperOp1, upperOp2);
BigInteger lower = Karatsuba(lowerOp1, lowerOp2);
BigInteger middle = Karatsuba(upperOp1.Subtract(lowerOp1),
lowerOp2.Subtract(upperOp2));
middle = middle.Add(upper).Add(lower);
middle = middle.ShiftLeft(ndiv2);
upper = upper.ShiftLeft(ndiv2 << 1);
return upper.Add(middle).Add(lower);
}
示例14: TestDivide
public void TestDivide()
{
for (int i = -5; i <= 5; ++i)
{
try
{
Val(i).Divide(Zero);
Assert.Fail("expected ArithmeticException");
}
catch (ArithmeticException)
{
}
}
const int product = 1*2*3*4*5*6*7*8*9;
const int productPlus = product + 1;
BigInteger bigProduct = Val(product);
BigInteger bigProductPlus = Val(productPlus);
for (int divisor = 1; divisor < 10; ++divisor)
{
// Exact division
BigInteger expected = Val(product/divisor);
Assert.AreEqual(expected, bigProduct.Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProduct.Negate().Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProduct.Divide(Val(divisor).Negate()));
Assert.AreEqual(expected, bigProduct.Negate().Divide(Val(divisor).Negate()));
expected = Val((product + 1)/divisor);
Assert.AreEqual(expected, bigProductPlus.Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProductPlus.Negate().Divide(Val(divisor)));
Assert.AreEqual(expected.Negate(), bigProductPlus.Divide(Val(divisor).Negate()));
Assert.AreEqual(expected, bigProductPlus.Negate().Divide(Val(divisor).Negate()));
}
for (int rep = 0; rep < 10; ++rep)
{
var a = new BigInteger(100 - rep, 0, Rnd);
var b = new BigInteger(100 + rep, 0, Rnd);
var c = new BigInteger(10 + rep, 0, Rnd);
BigInteger d = a.Multiply(b).Add(c);
BigInteger e = d.Divide(a);
Assert.AreEqual(b, e);
}
// Special tests for power of two since uses different code path internally
for (int i = 0; i < 100; ++i)
{
int shift = Rnd.Next(64);
BigInteger a = One.ShiftLeft(shift);
var b = new BigInteger(64 + Rnd.Next(64), Rnd);
BigInteger bShift = b.ShiftRight(shift);
string data = "shift=" + shift + ", b=" + b.ToString(16);
Assert.AreEqual(bShift, b.Divide(a), data);
Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
}
// Regression
{
int shift = 63;
BigInteger a = One.ShiftLeft(shift);
var b = new BigInteger(1, "2504b470dc188499".HexToBytes());
BigInteger bShift = b.ShiftRight(shift);
string data = "shift=" + shift + ", b=" + b.ToString(16);
Assert.AreEqual(bShift, b.Divide(a), data);
Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
// Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
}
}
示例15: GenerateWindowNaf
/**
* Computes the Window NAF (non-adjacent Form) of an integer.
* @param width The width <code>w</code> of the Window NAF. The width is
* defined as the minimal number <code>w</code>, such that for any
* <code>w</code> consecutive digits in the resulting representation, at
* most one is non-zero.
* @param k The integer of which the Window NAF is computed.
* @return The Window NAF of the given width, such that the following holds:
* <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
* </code>, where the <code>k<sub>i</sub></code> denote the elements of the
* returned <code>byte[]</code>.
*/
public static byte[] GenerateWindowNaf(int width, BigInteger k)
{
if (width == 2)
{
return GenerateNaf(k);
}
if (width < 2 || width > 8)
throw new ArgumentException("must be in the range [2, 8]", "width");
if (k.SignValue == 0)
return EMPTY_BYTES;
byte[] wnaf = new byte[k.BitLength + 1];
// 2^width and a mask and sign bit set accordingly
int pow2 = 1 << width;
int mask = pow2 - 1;
int sign = pow2 >> 1;
bool carry = false;
int length = 0, pos = 0;
while (pos <= k.BitLength)
{
if (k.TestBit(pos) == carry)
{
++pos;
continue;
}
k = k.ShiftRight(pos);
int digit = k.IntValue & mask;
if (carry)
{
++digit;
}
carry = (digit & sign) != 0;
if (carry)
{
digit -= pow2;
}
length += (length > 0) ? pos - 1 : pos;
wnaf[length++] = (byte)digit;
pos = width;
}
// Reduce the WNAF array to its actual length
if (wnaf.Length > length)
{
wnaf = Trim(wnaf, length);
}
return wnaf;
}