当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.ShiftRight方法代码示例

本文整理汇总了C#中Org.BouncyCastle.Math.BigInteger.ShiftRight方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.ShiftRight方法的具体用法?C# BigInteger.ShiftRight怎么用?C# BigInteger.ShiftRight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Org.BouncyCastle.Math.BigInteger的用法示例。


在下文中一共展示了BigInteger.ShiftRight方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ECDSA_SIG_recover_key_GFp

        public static ECPoint ECDSA_SIG_recover_key_GFp(BigInteger[] sig, byte[] hash, int recid, bool check)
        {
            X9ECParameters ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
            int i = recid / 2;

            Console.WriteLine("r: "+ToHex(sig[0].ToByteArrayUnsigned()));
            Console.WriteLine("s: "+ToHex(sig[1].ToByteArrayUnsigned()));

            BigInteger order = ecParams.N;
            BigInteger field = (ecParams.Curve as FpCurve).Q;
            BigInteger x = order.Multiply(new BigInteger(i.ToString())).Add(sig[0]);
            if (x.CompareTo(field) >= 0) throw new Exception("X too large");

            Console.WriteLine("Order: "+ToHex(order.ToByteArrayUnsigned()));
            Console.WriteLine("Field: "+ToHex(field.ToByteArrayUnsigned()));

            byte[] compressedPoint = new Byte[x.ToByteArrayUnsigned().Length+1];
            compressedPoint[0] = (byte) (0x02+(recid%2));
            Buffer.BlockCopy(x.ToByteArrayUnsigned(), 0, compressedPoint, 1, compressedPoint.Length-1);
            ECPoint R = ecParams.Curve.DecodePoint(compressedPoint);

            Console.WriteLine("R: "+ToHex(R.GetEncoded()));

            if (check)
            {
                ECPoint O = R.Multiply(order);
                if (!O.IsInfinity) throw new Exception("Check failed");
            }

            int n = (ecParams.Curve as FpCurve).Q.ToByteArrayUnsigned().Length*8;
            BigInteger e = new BigInteger(1, hash);
            if (8*hash.Length > n)
            {
                e = e.ShiftRight(8-(n & 7));
            }
            e = BigInteger.Zero.Subtract(e).Mod(order);
            BigInteger rr = sig[0].ModInverse(order);
            BigInteger sor = sig[1].Multiply(rr).Mod(order);
            BigInteger eor = e.Multiply(rr).Mod(order);
            ECPoint Q = ecParams.G.Multiply(eor).Add(R.Multiply(sor));

            Console.WriteLine("n: "+n);
            Console.WriteLine("e: "+ToHex(e.ToByteArrayUnsigned()));
            Console.WriteLine("rr: "+ToHex(rr.ToByteArrayUnsigned()));
            Console.WriteLine("sor: "+ToHex(sor.ToByteArrayUnsigned()));
            Console.WriteLine("eor: "+ToHex(eor.ToByteArrayUnsigned()));
            Console.WriteLine("Q: "+ToHex(Q.GetEncoded()));

            return Q;
        }
开发者ID:kfitzgerald,项目名称:genesis-generator,代码行数:50,代码来源:Ecdsa.cs

示例2: BitsToInt

        private BigInteger BitsToInt(byte[] t)
        {
            BigInteger v = new BigInteger(1, t);

            if (t.Length * 8 > n.BitLength)
            {
                v = v.ShiftRight(t.Length * 8 - n.BitLength);
            }

            return v;
        }
开发者ID:ubberkid,项目名称:PeerATT,代码行数:11,代码来源:HMacDsaKCalculator.cs

示例3: calculateE

        private BigInteger calculateE(
            BigInteger	n,
            byte[]		message)
        {
            int messageBitLength = message.Length * 8;
            BigInteger trunc = new BigInteger(1, message);

            if (n.BitLength < messageBitLength)
            {
                trunc = trunc.ShiftRight(messageBitLength - n.BitLength);
            }

            return trunc;
        }
开发者ID:NelsonSantos,项目名称:fyiReporting-Android,代码行数:14,代码来源:ECDsaSigner.cs

示例4: CalculateE

        private static IBigInteger CalculateE(IBigInteger n, byte[] message)
        {
            var messageBitLength = message.Length * 8;
            IBigInteger trunc = new BigInteger(1, message);

            if (n.BitLength < messageBitLength)
            {
                trunc = trunc.ShiftRight(messageBitLength - n.BitLength);
            }

            return trunc;
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:12,代码来源:ECDsaSigner.cs

示例5: TestTestBit

		public void TestTestBit()
		{
			for (int i = 0; i < 10; ++i)
			{
				BigInteger n = new BigInteger(128, random);

				Assert.IsFalse(n.TestBit(128));
				Assert.IsTrue(n.Negate().TestBit(128));

				for (int j = 0; j < 10; ++j)
				{
					int pos = random.Next(128);
					bool test = n.ShiftRight(pos).Remainder(two).Equals(one);

					Assert.AreEqual(test, n.TestBit(pos));
				}
			}
		}
开发者ID:randombit,项目名称:hacrypto,代码行数:18,代码来源:BigIntegerTest.cs

示例6: TestShiftRight

		public void TestShiftRight()
		{
			for (int i = 0; i < 10; ++i)
			{
				int shift = random.Next(128);
				BigInteger a = new BigInteger(256 + i, random).SetBit(256 + i);
				BigInteger b = a.ShiftRight(shift);

				Assert.AreEqual(a.BitLength - shift, b.BitLength);

				for (int j = 0; j < b.BitLength; ++j)
				{
					Assert.AreEqual(a.TestBit(j + shift), b.TestBit(j));
				}
			}
		}
开发者ID:randombit,项目名称:hacrypto,代码行数:16,代码来源:BigIntegerTest.cs

示例7: TestGetLowestSetBit

		public void TestGetLowestSetBit()
		{
			for (int i = 0; i < 10; ++i)
			{
				BigInteger test = new BigInteger(128, 0, random).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);
			}
		}
开发者ID:randombit,项目名称:hacrypto,代码行数:13,代码来源:BigIntegerTest.cs

示例8: TestDivideAndRemainder

		public void TestDivideAndRemainder()
		{
			// TODO More basic tests

			BigInteger n = new BigInteger(48, random);
			BigInteger[] qr = n.DivideAndRemainder(one);
			Assert.AreEqual(n, qr[0]);
			Assert.AreEqual(zero, qr[1]);

			for (int rep = 0; rep < 10; ++rep)
			{
				BigInteger a = new BigInteger(100 - rep, 0, random);
				BigInteger b = new BigInteger(100 + rep, 0, random);
				BigInteger c = new BigInteger(10 + rep, 0, random);
				BigInteger d = a.Multiply(b).Add(c);
				BigInteger[] es = d.DivideAndRemainder(a);

				Assert.AreEqual(b, es[0]);
				Assert.AreEqual(c, es[1]);
			}

			// Special tests for power of two since uses different code path internally
			for (int i = 0; i < 100; ++i)
			{
				int shift = random.Next(64);
				BigInteger a = one.ShiftLeft(shift);
				BigInteger b = new BigInteger(64 + random.Next(64), random);
				BigInteger bShift = b.ShiftRight(shift);
				BigInteger bMod = b.And(a.Subtract(one));

				string data = "shift=" + shift +", b=" + b.ToString(16);

				qr = b.DivideAndRemainder(a);
				Assert.AreEqual(bShift, qr[0], data);
				Assert.AreEqual(bMod, qr[1], data);

				qr = b.DivideAndRemainder(a.Negate());
				Assert.AreEqual(bShift.Negate(), qr[0], data);
				Assert.AreEqual(bMod, qr[1], data);

				qr = b.Negate().DivideAndRemainder(a);
				Assert.AreEqual(bShift.Negate(), qr[0], data);
				Assert.AreEqual(bMod.Negate(), qr[1], data);

				qr = b.Negate().DivideAndRemainder(a.Negate());
				Assert.AreEqual(bShift, qr[0], data);
				Assert.AreEqual(bMod.Negate(), qr[1], data);
			}
		}
开发者ID:randombit,项目名称:hacrypto,代码行数:49,代码来源:BigIntegerTest.cs

示例9: TestDivide

		public void TestDivide()
		{
			for (int i = -5; i <= 5; ++i)
			{
				try
				{
					val(i).Divide(zero);
					Assert.Fail("expected ArithmeticException");
				}
				catch (ArithmeticException) {}
			}

			int product = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9;
			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)
			{
				BigInteger a = new BigInteger(100 - rep, 0, random);
				BigInteger b = new BigInteger(100 + rep, 0, random);
				BigInteger c = new BigInteger(10 + rep, 0, random);
				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 = random.Next(64);
				BigInteger a = one.ShiftLeft(shift);
				BigInteger b = new BigInteger(64 + random.Next(64), random);
				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);
				BigInteger b = new BigInteger(1, Hex.Decode("2504b470dc188499"));
				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);
			}
		}
开发者ID:randombit,项目名称:hacrypto,代码行数:78,代码来源:BigIntegerTest.cs

示例10: bits2int

 private BigInteger bits2int(byte[] @in)
 {
     BigInteger v = new BigInteger(1, @in);
     int vlen = @in.Length * 8;
     if(vlen > qlen)
     {
         v = v.ShiftRight(vlen - qlen);
     }
     return v;
 }
开发者ID:nikropht,项目名称:NBitcoin,代码行数:10,代码来源:DeterministicECDSA.cs


注:本文中的Org.BouncyCastle.Math.BigInteger.ShiftRight方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。