ShiftRight() public method

public ShiftRight ( int n ) : BigInteger
n int
return BigInteger
Ejemplo n.º 1
0
        public String decipher(List <Org.BouncyCastle.Math.BigInteger> cipher, MHPrivateKey key)
        {
            String decrypted = "";

            Org.BouncyCastle.Math.BigInteger temp = Org.BouncyCastle.Math.BigInteger.ValueOf(0);
            int tmp = 0;

            Org.BouncyCastle.Math.BigInteger bits = Org.BouncyCastle.Math.BigInteger.ValueOf(0);

            for (int i = 0; i < cipher.Count; i++)
            {
                temp = cipher.ElementAt(i);
                int bitlen = temp.BitLength;
                int ff     = 0;
                while (bitlen < (int)Math.Pow(2, ff))
                {
                    ff++;
                }
                if (ff > bitlen)
                {
                    bitlen = ff;
                }

                for (int j = 0; j < bitlen; j++)
                {
                    if (temp.Mod(Org.BouncyCastle.Math.BigInteger.ValueOf(2)).CompareTo(Org.BouncyCastle.Math.BigInteger.ValueOf(1)) == 0)
                    {
                        bits = bits.Add(key.w1.Multiply(Org.BouncyCastle.Math.BigInteger.ValueOf((long)Math.Pow(2, j))));
                    }
                    temp = temp.ShiftRight(1);
                }
                bits = bits.Mod(key.n);
                List <Org.BouncyCastle.Math.BigInteger> list = key.a;
                Org.BouncyCastle.Math.BigInteger        temper;

                int k = key.a.Count - 1;
                while (k >= 0)
                {
                    temper = list.ElementAt(k);
                    if (bits.CompareTo(temper) > -1)
                    {
                        tmp += (int)Math.Pow(2, k);
                        bits = bits.Subtract(temper);
                    }
                    k--;
                }
                decrypted += (binaryToChar(Convert.ToString(tmp, 2))).ToString();

                bits = Org.BouncyCastle.Math.BigInteger.ValueOf(0);
                tmp  = 0;
            }
            return(decrypted);
        }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        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;
        }
        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;
        }
        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;
        }
Ejemplo n.º 6
0
		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));
				}
			}
		}
Ejemplo n.º 7
0
		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));
				}
			}
		}
Ejemplo n.º 8
0
		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);
			}
		}
Ejemplo n.º 9
0
		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);
			}
		}
Ejemplo n.º 10
0
		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);
			}
		}
Ejemplo n.º 11
0
 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;
 }