本文整理汇总了C#中BigInteger.Negate方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.Negate方法的具体用法?C# BigInteger.Negate怎么用?C# BigInteger.Negate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.Negate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VerifySmartCard
public static void VerifySmartCard(SmartCardDevice smartCardDevice, byte[] com, byte[] response, string hashFunctionName, byte[] proofSession, byte[] challangeMgs)
{
BigInteger resp = new BigInteger(1, response);
//BigInteger comBig = new BigInteger(1, com);
HashFunction hash = new HashFunction(hashFunctionName);
//hash.Hash(new byte[1] {0x1});
//hash.Hash(1);
byte[] proofSessionFoo = new byte[1 + proofSession.Length];
proofSessionFoo[0] = 1;
Buffer.BlockCopy(proofSession, 0, proofSessionFoo, 1, proofSession.Length);
hash.Hash(proofSessionFoo);
hash.Hash(challangeMgs);
byte[] cByte = hash.Digest;
BigInteger c = new BigInteger(1, cByte);
byte[] devicePubKeyByte = smartCardDevice.Device.GetDevicePublicKey(true);
//BigInteger devicePubKey = new BigInteger(1, devicePubKeyByte);
SubgroupGroupDescription subGq = (SubgroupGroupDescription)smartCardDevice.getGroupDescription();
SubgroupGroupElement leftSide = (SubgroupGroupElement)smartCardDevice.getGroupElement().Exponentiate(resp);
SubgroupGroupElement pk = (SubgroupGroupElement)subGq.CreateGroupElement(devicePubKeyByte);
SubgroupGroupElement pkc = (SubgroupGroupElement)pk.Exponentiate(c.Negate());
SubgroupGroupElement rightSide = (SubgroupGroupElement)pkc.Multiply(smartCardDevice.getGroupDescription().CreateGroupElement(com));
Console.Out.WriteLine("Printing left and right side");
Utils.PrintByteArrayToConsole(leftSide.GetEncoded());
Utils.PrintByteArrayToConsole(rightSide.GetEncoded());
}
示例2: Calculate
public virtual Number Calculate(BigInteger num)
{
if (num == null)
{
return -0;
}
return num.Negate();
}
示例3: TestBitLength
public void TestBitLength()
{
Assert.AreEqual(0, Zero.BitLength);
Assert.AreEqual(1, One.BitLength);
Assert.AreEqual(0, MinusOne.BitLength);
Assert.AreEqual(2, Two.BitLength);
Assert.AreEqual(1, MinusTwo.BitLength);
for (int i = 0; i < 100; ++i)
{
int bit = i + Rnd.Next(64);
BigInteger odd = new BigInteger(bit, Rnd).SetBit(bit + 1).SetBit(0);
BigInteger pow2 = One.ShiftLeft(bit);
Assert.AreEqual(bit + 2, odd.BitLength);
Assert.AreEqual(bit + 2, odd.Negate().BitLength);
Assert.AreEqual(bit + 1, pow2.BitLength);
Assert.AreEqual(bit, pow2.Negate().BitLength);
}
}
示例4: Jacobi
/// <summary>
/// Computes the value of the Jacobi symbol (A|B).
/// </summary>
///
/// <param name="A">The integer value</param>
/// <param name="B">The integer value</param>
///
/// <returns>Returns value of the jacobi symbol (A|B)</returns>
public static int Jacobi(BigInteger A, BigInteger B)
{
BigInteger a, b, v;
long k = 1;
// test trivial cases
if (B.Equals(ZERO))
{
a = A.Abs();
return a.Equals(ONE) ? 1 : 0;
}
if (!A.TestBit(0) && !B.TestBit(0))
return 0;
a = A;
b = B;
if (b.Signum() == -1)
{ // b < 0
b = b.Negate();
if (a.Signum() == -1)
k = -1;
}
v = ZERO;
while (!b.TestBit(0))
{
v = v.Add(ONE);
b = b.Divide(TWO);
}
if (v.TestBit(0))
k = k * _jacobiTable[a.ToInt32() & 7];
if (a.Signum() < 0)
{
if (b.TestBit(1))
k = -k;
a = a.Negate();
}
// main loop
while (a.Signum() != 0)
{
v = ZERO;
while (!a.TestBit(0))
{ // a is even
v = v.Add(ONE);
a = a.Divide(TWO);
}
if (v.TestBit(0))
k = k * _jacobiTable[b.ToInt32() & 7];
if (a.CompareTo(b) < 0)
{
// swap and correct intermediate result
BigInteger x = a;
a = b;
b = x;
if (a.TestBit(1) && b.TestBit(1))
k = -k;
}
a = a.Subtract(b);
}
return b.Equals(ONE) ? (int)k : 0;
}
示例5: DivideAndRound
/// <summary>
/// Divide two BigIntegers and return the rounded result
/// </summary>
///
/// <param name="A">The first BigInteger</param>
/// <param name="B">The second BigInteger</param>
///
/// <returns>The rounded result</returns>
public static BigInteger DivideAndRound(BigInteger A, BigInteger B)
{
if (A.Signum() < 0)
return DivideAndRound(A.Negate(), B).Negate();
if (B.Signum() < 0)
return DivideAndRound(A, B.Negate()).Negate();
return A.ShiftLeft(1).Add(B).Divide(B.ShiftLeft(1));
}
示例6: Negate_zero_is_zero
public void Negate_zero_is_zero()
{
BigInteger x = new BigInteger(0, new uint[0]);
BigInteger xn = x.Negate();
Expect(SameValue(xn, 0, new uint[0]));
}
示例7: Calculate
public override Number Calculate(BigInteger bigint1, BigInteger bigint2)
{
if (bigint1 == null || bigint2 == null)
{
return 0;
}
var comp = bigint2.CompareTo(BigInteger.Zero);
if (comp == 0)
{
return 0;
}
if (comp < 0)
{
return bigint1.Negate().Mod(bigint2).Negate();
}
return bigint1.Mod(bigint2);
}
示例8: 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));
}
}
}
示例9: TestMultiply
public void TestMultiply()
{
BigInteger one = BigInteger.One;
Assert.AreEqual(one, one.Negate().Multiply(one.Negate()));
for (int i = 0; i < 100; ++i)
{
int aLen = 64 + Rnd.Next(64);
int bLen = 64 + Rnd.Next(64);
BigInteger a = new BigInteger(aLen, Rnd).SetBit(aLen);
BigInteger b = new BigInteger(bLen, Rnd).SetBit(bLen);
var c = new BigInteger(32, Rnd);
BigInteger ab = a.Multiply(b);
BigInteger bc = b.Multiply(c);
Assert.AreEqual(ab.Add(bc), a.Add(c).Multiply(b));
Assert.AreEqual(ab.Subtract(bc), a.Subtract(c).Multiply(b));
}
// 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.ShiftLeft(shift);
Assert.AreEqual(bShift, a.Multiply(b));
Assert.AreEqual(bShift.Negate(), a.Multiply(b.Negate()));
Assert.AreEqual(bShift.Negate(), a.Negate().Multiply(b));
Assert.AreEqual(bShift, a.Negate().Multiply(b.Negate()));
Assert.AreEqual(bShift, b.Multiply(a));
Assert.AreEqual(bShift.Negate(), b.Multiply(a.Negate()));
Assert.AreEqual(bShift.Negate(), b.Negate().Multiply(a));
Assert.AreEqual(bShift, b.Negate().Multiply(a.Negate()));
}
}
示例10: Multiply
/// <summary>
/// Multiplies two BigIntegers using the Schönhage-Strassen algorithm.
/// </summary>
///
/// <param name="A">Factor A</param>
/// <param name="B">Factor B</param>
///
/// <returns>BigInteger equal to <c>A.Multiply(B)</c></returns>
public static BigInteger Multiply(BigInteger A, BigInteger B)
{
// remove any minus signs, multiply, then fix sign
int signum = A.Signum() * B.Signum();
if (A.Signum() < 0)
A = A.Negate();
if (B.Signum() < 0)
B = B.Negate();
int[] aIntArr = ToIntArray(A);
int[] bIntArr = ToIntArray(B);
int[] cIntArr = Multiply(aIntArr, A.BitLength, bIntArr, B.BitLength);
BigInteger c = ToBigInteger(cIntArr);
if (signum < 0)
c = c.Negate();
return c;
}
示例11: TestAllDivs
private void TestAllDivs(BigInteger i1, BigInteger i2)
{
TestDiv(i1, i2);
TestDiv(i1.Negate(), i2);
TestDiv(i1, i2.Negate());
TestDiv(i1.Negate(), i2.Negate());
}
示例12: testAllMults
private static void testAllMults(BigInteger i1, BigInteger i2, BigInteger ans)
{
Assert.IsTrue(i1.Multiply(i2).Equals(ans), "i1*i2=ans");
Assert.IsTrue(i2.Multiply(i1).Equals(ans), "i2*i1=ans");
Assert.IsTrue(i1.Negate().Multiply(i2).Equals(ans.Negate()), "-i1*i2=-ans");
Assert.IsTrue(i2.Negate().Multiply(i1).Equals(ans.Negate()), "-i2*i1=-ans");
Assert.IsTrue(i1.Multiply(i2.Negate()).Equals(ans.Negate()), "i1*-i2=-ans");
Assert.IsTrue(i2.Multiply(i1.Negate()).Equals(ans.Negate()), "i2*-i1=-ans");
Assert.IsTrue(i1.Negate().Multiply(i2.Negate()).Equals(ans), "-i1*-i2=ans");
Assert.IsTrue(i2.Negate().Multiply(i1.Negate()).Equals(ans), "-i2*-i1=ans");
}
示例13: subtract
/** @see BigInteger#subtract(BigInteger) */
internal static BigInteger subtract(BigInteger op1, BigInteger op2)
{
int resSign;
int[] resDigits;
int op1Sign = op1.Sign;
int op2Sign = op2.Sign;
if (op2Sign == 0) {
return op1;
}
if (op1Sign == 0) {
return op2.Negate();
}
int op1Len = op1.numberLength;
int op2Len = op2.numberLength;
if (op1Len + op2Len == 2) {
long a = (op1.Digits[0] & 0xFFFFFFFFL);
long b = (op2.Digits[0] & 0xFFFFFFFFL);
if (op1Sign < 0) {
a = -a;
}
if (op2Sign < 0) {
b = -b;
}
return BigInteger.ValueOf(a - b);
}
int cmp = ((op1Len != op2Len) ? ((op1Len > op2Len) ? 1 : -1)
: Elementary.compareArrays(op1.Digits, op2.Digits, op1Len));
if (cmp == BigInteger.LESS) {
resSign = -op2Sign;
resDigits = (op1Sign == op2Sign) ? subtract(op2.Digits, op2Len,
op1.Digits, op1Len) : add(op2.Digits, op2Len, op1.Digits,
op1Len);
} else {
resSign = op1Sign;
if (op1Sign == op2Sign) {
if (cmp == BigInteger.EQUALS) {
return BigInteger.Zero;
}
resDigits = subtract(op1.Digits, op1Len, op2.Digits, op2Len);
} else {
resDigits = add(op1.Digits, op1Len, op2.Digits, op2Len);
}
}
BigInteger res = new BigInteger(resSign, resDigits.Length, resDigits);
res.CutOffLeadingZeroes();
return res;
}
示例14: TestBitLength
public void TestBitLength()
{
Assert.AreEqual(0, zero.BitLength);
Assert.AreEqual(1, one.BitLength);
Assert.AreEqual(0, minusOne.BitLength);
Assert.AreEqual(2, two.BitLength);
Assert.AreEqual(1, minusTwo.BitLength);
for (int i = 0; i < 100; ++i)
{
int bit = i + _random.Next(64);
IBigInteger odd = new BigInteger(bit, _random).SetBit(bit + 1).SetBit(0);
IBigInteger pow2 = one.ShiftLeft(bit);
Assert.AreEqual(bit + 2, odd.BitLength);
Assert.AreEqual(bit + 2, odd.Negate().BitLength);
Assert.AreEqual(bit + 1, pow2.BitLength);
Assert.AreEqual(bit, pow2.Negate().BitLength);
}
}
示例15: ModInverseLorencz
private static BigInteger ModInverseLorencz(BigInteger X, BigInteger Modulo)
{
// Based on "New Algorithm for Classical Modular Inverse" Róbert Lórencz. LNCS 2523 (2002)
// PRE: a is coprime with modulo, a < modulo
int max = System.Math.Max(X._numberLength, Modulo._numberLength);
int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation
int[] vDigits = new int[max + 1];
Array.Copy(Modulo._digits, 0, uDigits, 0, Modulo._numberLength);
Array.Copy(X._digits, 0, vDigits, 0, X._numberLength);
BigInteger u = new BigInteger(Modulo._sign, Modulo._numberLength, uDigits);
BigInteger v = new BigInteger(X._sign, X._numberLength, vDigits);
BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO;
BigInteger s = new BigInteger(1, 1, new int[max + 1]);
s._digits[0] = 1;
// r == 0 && s == 1, but with enough place
int coefU = 0, coefV = 0;
int n = Modulo.BitLength;
int k;
while (!IsPowerOfTwo(u, coefU) && !IsPowerOfTwo(v, coefV))
{
// modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if
k = HowManyIterations(u, n);
if (k != 0)
{
BitLevel.InplaceShiftLeft(u, k);
if (coefU >= coefV)
{
BitLevel.InplaceShiftLeft(r, k);
}
else
{
BitLevel.InplaceShiftRight(s, System.Math.Min(coefV - coefU, k));
if (k - (coefV - coefU) > 0)
BitLevel.InplaceShiftLeft(r, k - coefV + coefU);
}
coefU += k;
}
k = HowManyIterations(v, n);
if (k != 0)
{
BitLevel.InplaceShiftLeft(v, k);
if (coefV >= coefU)
{
BitLevel.InplaceShiftLeft(s, k);
}
else
{
BitLevel.InplaceShiftRight(r, System.Math.Min(coefU - coefV, k));
if (k - (coefU - coefV) > 0)
BitLevel.InplaceShiftLeft(s, k - coefU + coefV);
}
coefV += k;
}
if (u.Signum() == v.Signum())
{
if (coefU <= coefV)
{
Elementary.CompleteInPlaceSubtract(u, v);
Elementary.CompleteInPlaceSubtract(r, s);
}
else
{
Elementary.CompleteInPlaceSubtract(v, u);
Elementary.CompleteInPlaceSubtract(s, r);
}
}
else
{
if (coefU <= coefV)
{
Elementary.CompleteInPlaceAdd(u, v);
Elementary.CompleteInPlaceAdd(r, s);
}
else
{
Elementary.CompleteInPlaceAdd(v, u);
Elementary.CompleteInPlaceAdd(s, r);
}
}
if (v.Signum() == 0 || u.Signum() == 0)
throw new ArithmeticException("BigInteger not invertible");
}
if (IsPowerOfTwo(v, coefV))
{
r = s;
if (v.Signum() != u.Signum())
u = u.Negate();
}
if (u.TestBit(n))
{
//.........这里部分代码省略.........