예제 #1
0
        public static decimal ToDecimal(this BigInteger bi, bool noThrow = false)
        {
            var thirtyTwoBits = BigInteger.ValueOf(0xFFFFFFFFL);

            if (bi.SignValue == 0)
            {
                return(decimal.Zero);
            }

            var signed = bi.SignValue < 0;

            if (signed)
            {
                bi = bi.Negate();
            }

            var low = unchecked ((int)(uint)bi.And(thirtyTwoBits).LongValue);

            bi = bi.ShiftRight(32);
            var med = unchecked ((int)(uint)bi.And(thirtyTwoBits).LongValue);

            bi = bi.ShiftRight(32);
            var high = unchecked ((int)(uint)bi.And(thirtyTwoBits).LongValue);

            bi = bi.ShiftRight(32);
            if (!noThrow && bi.SignValue != 0)
            {
                throw new OverflowException("BigInteger too big to conform to decimal.");
            }

            return(new decimal(low, med, high, signed, 0));
        }
예제 #2
0
        public static string ToCheckSumAddress(string address)
        {
            if (!Validation.IsAddress(address))
            {
                throw new Exception("not a valid base 16 address");
            }

            address = address.ToLower().Replace("0x", "");
            string        hash = ByteUtil.ByteArrayToHexString(HashUtil.Sha256(ByteUtil.HexStringToByteArray(address)));
            StringBuilder ret  = new StringBuilder("0x");

            byte[]     x = ByteUtil.HexStringToByteArray(hash);
            BigInteger v = new BigInteger(ByteUtil.HexStringToByteArray(hash));

            for (int i = 0; i < address.Length; i++)
            {
                if ("1234567890".IndexOf(address.ToCharArray()[i]) != -1)
                {
                    ret.Append(address.ToCharArray()[i]);
                }
                else
                {
                    BigInteger checker = v.And(BigInteger.ValueOf(2).Pow(255 - 6 * i));//(BigInteger.valueOf(2l).pow(255 - 6 * i))
                    ret.Append(checker.CompareTo(BigInteger.ValueOf(1)) < 0 ? address.ToCharArray()[i].ToString().ToLower() : address.ToCharArray()[i].ToString().ToUpper());
                }
            }
            return(ret.ToString());
        }
예제 #3
0
 private void SetKey(byte[] key, byte[] nonce)
 {
     byte[] z = new byte[key.Length];
     Array.Copy(key, z, key.Length);
     Array.Reverse(z);
     // Extract r portion of key
     r = new BigInteger(1, z, 16, 16);
     r = r.And(clamp);
     s = new BigInteger(1, z, 0, 16);
 }
예제 #4
0
        public DataWord ShiftRight(DataWord value)
        {
            if (value.ToBigInteger().CompareTo(BigInteger.ValueOf(MAX_POW)) >= 0)
            {
                return(DataWord.ZERO);
            }

            BigInteger result = ToBigInteger().ShiftRight(value.ToIntSafety());

            return(new DataWord(CopyTo(result.And(MAX_VALUE))));
        }
예제 #5
0
        public void MultipyMod(DataWord word1, DataWord word2)
        {
            if (this.IsZero || word1.IsZero || word2.IsZero)
            {
                this.data = new byte[32];
                return;
            }

            BigInteger result = ToBigInteger().Multiply(word1.ToBigInteger().Mod(word2.ToBigInteger()));

            this.data = CopyTo(result.And(MAX_VALUE));
        }
예제 #6
0
        public void Mod(DataWord value)
        {
            if (value.IsZero)
            {
                this.AND(value);
                return;
            }

            BigInteger result = ToBigInteger().Mod(value.ToBigInteger());

            this.data = CopyTo(result.And(MAX_VALUE));
        }
        /// <summary>
        /// Gene bytemask function extracts a gene bit from <paramref name="input"/>.
        /// </summary>
        /// <param name="input">The <see cref="BigInteger"/> containing gene data.</param>
        /// <param name="start">The offset bit</param>
        /// <param name="numberOfBytes">Number of bits.</param>
        /// <returns>The masked byte.</returns>
        private static byte GeneByteMask(BigInteger input, int start, int numberOfBytes)
        {
            // mask = 2**numbytes - 1
            var mask = BigInteger.Two.Pow(numberOfBytes).Subtract(BigInteger.One);

            // mask = mask << start
            mask = mask.ShiftLeft(start);
            var ret = input.And(mask);

            ret = ret.ShiftRight(start);
            return((byte)ret.IntValue);
        }
예제 #8
0
        public void Div(DataWord word)
        {
            if (word.IsZero())
            {
                this.And(ZERO);
                return;
            }

            BigInteger result = GetValue().Divide(word.GetValue());

            _data = ByteUtil.CopyToArray(result.And(MAX_VALUE));
        }
예제 #9
0
        public void TestDivideAndRemainder()
        {
            // TODO More basic tests

            BigInteger n = new BigInteger(48, random);

            BigInteger[] qr = n.DivideAndRemainder(n);
            Assert.AreEqual(one, qr[0]);
            Assert.AreEqual(zero, qr[1]);
            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);
            }
        }
예제 #10
0
        public void AndLBigInteger()
        {
            foreach (BigInteger[] element in booleanPairs)
            {
                BigInteger i1 = element[0], i2 = element[1];
                BigInteger res = i1.And(i2);
                IsTrue(res.Equals(i2.And(i1)), "symmetry of and");
                int len = System.Math.Max(i1.BitLength, i2.BitLength) + 66;

                for (int i = 0; i < len; i++)
                {
                    IsTrue((i1.TestBit(i) && i2.TestBit(i)) == res.TestBit(i), "and");
                }
            }
        }
예제 #11
0
        public SqlNumber And(SqlNumber value)
        {
            if (State == NumericState.NotANumber)
            {
                return(this);
            }

            if (Scale == 0 && value.Scale == 0)
            {
                BigInteger bi1 = innerValue.ToBigInteger();
                BigInteger bi2 = value.innerValue.ToBigInteger();
                return(new SqlNumber(NumericState.None, new BigDecimal(bi1.And(bi2))));
            }

            return(Null);
        }
예제 #12
0
        public void TestMod()
        {
            // TODO Basic tests

            for (int rep = 0; rep < 100; ++rep)
            {
                int        diff = random.Next(25);
                BigInteger a    = new BigInteger(100 - diff, 0, random);
                BigInteger b    = new BigInteger(100 + diff, 0, random);
                BigInteger c    = new BigInteger(10 + diff, 0, random);

                BigInteger d = a.Multiply(b).Add(c);
                BigInteger e = d.Mod(a);
                Assert.AreEqual(c, e);

                BigInteger pow2 = one.ShiftLeft(random.Next(128));
                Assert.AreEqual(b.And(pow2.Subtract(one)), b.Mod(pow2));
            }
        }
예제 #13
0
        public void Multiply(DataWord value)
        {
            BigInteger result = ToBigInteger().Multiply(value.ToBigInteger());

            this.data = CopyTo(result.And(MAX_VALUE));
        }
예제 #14
0
        public void Sub(DataWord value)
        {
            BigInteger result = ToBigInteger().Subtract(value.ToBigInteger());

            this.data = CopyTo(result.And(MAX_VALUE));
        }
예제 #15
0
        public void Sub(DataWord word)
        {
            BigInteger result = GetValue().Subtract(word.GetValue());

            _data = ByteUtil.CopyToArray(result.And(MAX_VALUE));
        }
예제 #16
0
 //x1~=2^w+(x1 AND (2^w-1))
 private BigInteger Reduce(BigInteger x)
 {
     return(x.And(BigInteger.One.ShiftLeft(mW).Subtract(BigInteger.One)).SetBit(mW));
 }
예제 #17
0
        private void MultiplyOverField(int blockSizeBits, byte[] x, byte[] y, byte[] x_mult_y)
        {
            //Multiplication over GF(2^m), GF(2^256), GF(2^512) with correspondent extension polynomials:
            ///
            /// GF(2^128) | x^128 + x^7 + x^2 + x
            /// GF(2^256) | x^256 + x^10 + x^5 + x^2 + 1
            /// GF(2^512) | x^512 + x^8 + x^5 + x^2 + 1
            ///
            //Thanks to João H de A Franco script. https://jhafranco.com/2012/02/17/multiplication-over-the-binary-finite-field-gf2m/

            byte[] copy1 = new byte[cipher.GetBlockSize()];
            byte[] copy2 = new byte[cipher.GetBlockSize()];

            Array.Copy(x, 0, copy1, 0, cipher.GetBlockSize());
            Array.Copy(y, 0, copy2, 0, cipher.GetBlockSize());


            Array.Reverse(copy1);
            Array.Reverse(copy2);


            BigInteger mask1;
            BigInteger mask2;
            BigInteger polyred;

            switch (blockSizeBits)
            {
            case 128:
                mask1   = mask1_128;
                mask2   = mask2_128;
                polyred = polyred_128;
                break;

            case 256:
                mask1   = mask1_256;
                mask2   = mask2_256;
                polyred = polyred_256;
                break;

            case 512:
                mask1   = mask1_512;
                mask2   = mask2_512;
                polyred = polyred_512;
                break;

            default:
                mask1   = mask1_128;
                mask2   = mask2_128;
                polyred = polyred_128;
                break;
            }

            BigInteger p  = BigInteger.Zero;
            BigInteger p1 = new BigInteger(1, copy1);
            BigInteger p2 = new BigInteger(1, copy2);

            while (!p2.Equals(BigInteger.Zero))
            {
                if (p2.And(BigInteger.One).Equals(BigInteger.One))
                {
                    p = p.Xor(p1);
                }

                p1 = p1.ShiftLeft(1);

                if (!p1.And(mask1).Equals(BigInteger.Zero))
                {
                    p1 = p1.Xor(polyred);
                }

                p2 = p2.ShiftRight(1);
            }

            byte[] got = p.And(mask2).ToByteArrayUnsigned();

            Array.Clear(x_mult_y, 0, cipher.GetBlockSize());

            Array.Copy(got, 0, x_mult_y, 0, got.Length);
        }
예제 #18
0
        public void TestDivideAndRemainder()
        {
            // TODO More basic tests

            var n = new BigInteger(48, Rnd);
            BigInteger[] qr = n.DivideAndRemainder(One);
            Assert.AreEqual(n, qr[0]);
            Assert.AreEqual(Zero, qr[1]);

            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[] 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 = Rnd.Next(64);
                BigInteger a = One.ShiftLeft(shift);
                var b = new BigInteger(64 + Rnd.Next(64), Rnd);
                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);
            }
        }
예제 #19
0
        public void TestMod()
        {
            // TODO Basic tests

            for (int rep = 0; rep < 100; ++rep)
            {
                int diff = Rnd.Next(25);
                var a = new BigInteger(100 - diff, 0, Rnd);
                var b = new BigInteger(100 + diff, 0, Rnd);
                var c = new BigInteger(10 + diff, 0, Rnd);

                BigInteger d = a.Multiply(b).Add(c);
                BigInteger e = d.Mod(a);
                Assert.AreEqual(c, e);

                BigInteger pow2 = One.ShiftLeft(Rnd.Next(128));
                Assert.AreEqual(b.And(pow2.Subtract(One)), b.Mod(pow2));
            }
        }
예제 #20
0
        public void Mul(DataWord word)
        {
            BigInteger result = GetValue().Multiply(word.GetValue());

            _data = ByteUtil.CopyToArray(result.And(MAX_VALUE));
        }