Exemple #1
0
        /// <summary>
        /// Convert the given value string to Int128 using the given radix
        /// </summary>
        internal override object FromString(string value, int radix)
        {
            Debug.Assert(radix == 16);
            Debug.Assert(value is not null);

            return(Int128.Parse(value, NumberStyles.HexNumber));
        }
Exemple #2
0
        public void Int128ParseFailure()
        {
            Assert.IsFalse(Int128.TryParse(null, out _));
            Assert.ThrowsException <ArgumentNullException>(() => Int128.Parse(null));

            Assert.IsFalse(Int128.TryParse(String.Empty, out _));
            Assert.ThrowsException <FormatException>(() => Int128.Parse(String.Empty));

            Assert.IsFalse(Int128.TryParse("1x2", out _));
            Assert.ThrowsException <FormatException>(() => Int128.Parse("-1 2"));

            // Max is parsable, but one more isn't.
            Assert.IsTrue(Int128.TryParse(Int128.MaxValue.ToString(), out Int128 max));
            Assert.IsTrue(max == Int128.MaxValue);
            string oneOverMax = UInt128Test.MakeNumberOneBigger(Int128.MaxValue.ToString());

            Assert.IsFalse(Int128.TryParse(oneOverMax, out _));
            Assert.ThrowsException <OverflowException>(() => Int128.Parse(oneOverMax));

            // Min is parsable, but one less isn't.
            Assert.IsTrue(Int128.TryParse(Int128.MinValue.ToString(), out Int128 min));
            Assert.IsTrue(min == Int128.MinValue);
            string oneUnderMin = UInt128Test.MakeNumberOneBigger(Int128.MinValue.ToString());

            Assert.IsFalse(Int128.TryParse(oneUnderMin, out _));
            Assert.ThrowsException <OverflowException>(() => Int128.Parse(oneUnderMin));
        }
Exemple #3
0
        public static void Parse_Valid(string value, NumberStyles style, IFormatProvider provider, Int128 expected)
        {
            Int128 result;

            // Default style and provider
            if ((style == NumberStyles.Integer) && (provider is null))
            {
                Assert.True(Int128.TryParse(value, out result));
                Assert.Equal(expected, result);
                Assert.Equal(expected, Int128.Parse(value));
            }

            // Default provider
            if (provider is null)
            {
                Assert.Equal(expected, Int128.Parse(value, style));

                // Substitute default NumberFormatInfo
                Assert.True(Int128.TryParse(value, style, new NumberFormatInfo(), out result));
                Assert.Equal(expected, result);
                Assert.Equal(expected, Int128.Parse(value, style, new NumberFormatInfo()));
            }

            // Default style
            if (style == NumberStyles.Integer)
            {
                Assert.Equal(expected, Int128.Parse(value, provider));
            }

            // Full overloads
            Assert.True(Int128.TryParse(value, style, provider, out result));
            Assert.Equal(expected, result);
            Assert.Equal(expected, Int128.Parse(value, style, provider));
        }
Exemple #4
0
        public void Should_sum_big_numbers_correctly(string x, string y, string z)
        {
            Int128 i1 = Int128.Parse(x);
            Int128 i2 = Int128.Parse(y);

            Int128 i3 = i1 + i2;

            ("0x" + i3.ToString("X32")).Should().Be(z);
        }
Exemple #5
0
        public void Should_substruct_big_numbers_correctly(string x, string y, string z)
        {
            var i1 = Int128.Parse(x);
            var i2 = Int128.Parse(y);

            var i3 = i1 - i2;

            ("0x" + i3.ToString("X32")).Should().Be(z);
        }
        public void Should_convert_bytes_to_int128()
        {
            var expectedBigEndian    = Int128.Parse(Int128Value);
            var expectedLittleEndian = Int128.Parse(Int128ValueLittleEndian);

            var bytes = Int128Value.HexToBytes();

            bytes.ToInt128(0, false).Should().Be(expectedBigEndian);
            bytes.ToInt128(0, true).Should().Be(expectedLittleEndian);
        }
Exemple #7
0
        public void Int128ParseRoundtrip()
        {
            // Random values are round-tripped.
            Random rng = new Random(128);

            foreach (Int128 x in GetRandomInt128(8, rng))
            {
                Assert.IsTrue(Int128.Parse(x.ToString()) == x);
            }
        }
Exemple #8
0
        public void Should_rightShift_correctly(string x, string y, string z)
        {
            //
            // X >> Y should = z
            //
            var i1       = Int128.Parse(x);
            var shifthBy = int.Parse(y);
            var i3       = i1 >> shifthBy;

            ("0x" + i3.ToString("X32")).Should().Be(z);
        }
Exemple #9
0
        public void Should_leftShift_correctly(string x, string y, string z)
        {
            //
            // X >> Y should = z
            //
            Int128 i1       = Int128.Parse(x);
            int    shifthBy = int.Parse(y);
            Int128 i3       = i1 << shifthBy;

            ("0x" + i3.ToString("X32")).Should().Be(z);
        }
        public void Should_convert_int128_to_bytes()
        {
            var i             = Int128.Parse(Int128Value);
            var expectedBytes = Int128Value.HexToBytes();

            var actualBytes = i.ToBytes(false);

            actualBytes.ShouldAllBeEquivalentTo(expectedBytes);

            actualBytes = i.ToBytes(true);
            actualBytes.ShouldAllBeEquivalentTo(Enumerable.Reverse(expectedBytes));
        }
Exemple #11
0
        public void Should_convert_int128_to_bytes()
        {
            Int128 i = Int128.Parse(Int128Value);

            byte[] expectedBytes = Int128Value.HexToBytes();

            byte[] actualBytes = i.ToBytes(false);
            actualBytes.ShouldAllBeEquivalentTo(expectedBytes);

            actualBytes = i.ToBytes(true);
            actualBytes.ShouldAllBeEquivalentTo(expectedBytes.Reverse());
        }
Exemple #12
0
        //[TestCase("2852213850458175921094913949697", "51539607551", "55340232221128654847")]
        public void Should_get_prime_multipliers_for_Int128(string pqS, string expectedPs, string expectedQs)
        {
            Int128 pq = Int128.Parse(pqS);
            Int128 expectedP = Int128.Parse(expectedPs);
            Int128 expectedQ = Int128.Parse(expectedQs);
            Int128 p, q;

            pq.GetPrimeMultipliers(out p, out q);

            p.Should().Be(expectedP);
            q.Should().Be(expectedQ);
        }
Exemple #13
0
        public void Should_compare_big_numbers_correctly(string x, string y, bool?z)
        {
            // z == null means that numbers are equal.

            Int128 i1 = Int128.Parse(x);
            Int128 i2 = Int128.Parse(y);

            bool value = z.HasValue && z.Value;

            (i1 > i2).Should().Be(value);
            (i1 < i2).Should().Be(z.HasValue && !value);
            (i1 == i2).Should().Be(!z.HasValue);
            (i1 != i2).Should().Be(z.HasValue);
        }
        public void Should_convert_bytes_to_int128_less_than_size_of_int128()
        {
            var expectedBigEndian    = Int128.Parse(Int128LessValue);
            var expectedLittleEndian = Int128.Parse(Int128LessValueLittleEndian);

            var bytes = Int128LessValue.HexToBytes();

            var actualBigEndian = bytes.ToInt128(0, false);

            actualBigEndian.Should().Be(expectedBigEndian);

            var actualLittleEndian = bytes.ToInt128(0, true);

            actualLittleEndian.Should().Be(expectedLittleEndian);
        }
Exemple #15
0
        public void Should_create(Sender sender)
        {
            byte[] authKey =
                "752BC8FC163832CB2606F7F3DC444D39A6D725761CA2FC984958E20EB7FDCE2AA1A65EB92D224CEC47EE8339AA44DF3906D79A01148CB6AACF70D53F98767EBD7EADA5A63C4229117EFBDB50DA4399C9E1A5D8B2550F263F3D43B936EF9259289647E7AAC8737C4E007C0C9108631E2B53C8900C372AD3CCA25E314FBD99AFFD1B5BCB29C5E40BB8366F1DFD07B053F1FBBBE0AA302EEEE5CF69C5A6EA7DEECDD965E0411E3F00FE112428330EBD432F228149FD2EC9B5775050F079C69CED280FE7E13B968783E3582B9C58CEAC2149039B3EF5A4265905D661879A41AF81098FBCA6D0B91D5B595E1E27E166867C155A3496CACA9FD6CF5D16DB2ADEBB2D3E"
                .HexToBytes();

            const ulong expectedAuthKeyId = 0x1a0a7a922fcdae14;
            Int128      expectedMsgKey    = Int128.Parse("0x3B7400E9316554B14686D405F0EAE4A8");

            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterType <IHashServices, HashServices>();
            serviceLocator.RegisterType <IEncryptionServices, EncryptionServices>();

            var hashServices       = serviceLocator.ResolveType <IHashServices>();
            var encryptionServices = serviceLocator.ResolveType <IEncryptionServices>();

            byte[]    messageData           = Enumerable.Range(1, 100).ToArray().ConvertAll(i => (byte)i);
            const int expectedMessageLength = 168;

            var message = new EncryptedMessage(authKey, 1, 2, 3, 4, messageData, sender, hashServices, encryptionServices);

            message.AuthKeyId.Should().Be(expectedAuthKeyId);
            message.Length.Should().Be(expectedMessageLength);
            message.MessageData.ShouldAllBeEquivalentTo(messageData);
            message.MessageDataLength.Should().Be(messageData.Length);
            message.Salt.Should().Be(1);
            message.SessionId.Should().Be(2);
            message.MessageId.Should().Be(3);
            message.SeqNumber.Should().Be(4);
            message.MsgKey.Should().Be(expectedMsgKey);

            // Restoring an encrypted message.
            var decryptedMessage = new EncryptedMessage(authKey, message.MessageBytes, sender, hashServices, encryptionServices);

            decryptedMessage.AuthKeyId.Should().Be(expectedAuthKeyId);
            decryptedMessage.Length.Should().Be(expectedMessageLength);
            decryptedMessage.MessageData.ShouldAllBeEquivalentTo(messageData);
            decryptedMessage.MessageDataLength.Should().Be(messageData.Length);
            decryptedMessage.Salt.Should().Be(1);
            decryptedMessage.SessionId.Should().Be(2);
            decryptedMessage.MessageId.Should().Be(3);
            decryptedMessage.SeqNumber.Should().Be(4);
            decryptedMessage.MsgKey.Should().Be(expectedMsgKey);
        }
Exemple #16
0
 /// <summary>
 /// Convert the given value string to Int128 using given formatInfo
 /// </summary>
 internal override object FromString(string value, NumberFormatInfo?formatInfo) =>
 Int128.Parse(value, formatInfo);
Exemple #17
0
        private static IEnumerable <DataUnit> GetData()
        {
            return(new List <DataUnit>
            {
                new DataUnit(int.MinValue, GetBytes(int.MinValue)),
                new DataUnit(int.MaxValue, GetBytes(int.MaxValue)),
                new DataUnit(uint.MinValue, GetBytes(uint.MinValue)),
                new DataUnit(uint.MaxValue, GetBytes(uint.MaxValue)),
                new DataUnit(long.MinValue, GetBytes(long.MinValue)),
                new DataUnit(long.MaxValue, GetBytes(long.MaxValue)),
                new DataUnit(ulong.MinValue, GetBytes(ulong.MinValue)),
                new DataUnit(ulong.MaxValue, GetBytes(ulong.MaxValue)),
                new DataUnit(double.MinValue, GetBytes(double.MinValue)),
                new DataUnit(double.MaxValue, GetBytes(double.MaxValue)),
                new DataUnit(double.Epsilon, GetBytes(double.Epsilon)),
                new DataUnit(double.NaN, GetBytes(double.NaN)),
                new DataUnit(double.PositiveInfinity, GetBytes(double.PositiveInfinity)),
                new DataUnit(double.NegativeInfinity, GetBytes(double.NegativeInfinity)),
                new DataUnit(true, GetBytes(0x997275b5)),
                new DataUnit(false, GetBytes(0xbc799737)),
                new DataUnit("P", GetBytes(0x5001)),
                new DataUnit("Pa", GetBytes(0x615002)),
                new DataUnit("Pav", GetBytes(0x76615003)),
                new DataUnit("Pave", GetBytes(0x76615004, 0x65)),
                new DataUnit("Pavel", GetBytes(0x76615005, 0x6c65)),
                new DataUnit(new string('P', 500), GetBytes(0x1F4FE, Enumerable.Repeat((byte)0x50, 500))),
                new DataUnit(new string('P', 501), GetBytes(0x1F5FE, Enumerable.Repeat((byte)0x50, 501), new byte[] { 0, 0, 0 })),
                new DataUnit(new string('P', 502), GetBytes(0x1F6FE, Enumerable.Repeat((byte)0x50, 502), new byte[] { 0, 0 })),
                new DataUnit(new string('P', 503), GetBytes(0x1F7FE, Enumerable.Repeat((byte)0x50, 503), new byte[] { 0 })),
                new DataUnit(
                    new TestObject
                {
                    TestBoolean = true,
                    TestDouble = Double.Epsilon,
                    TestInt = Int32.MaxValue,
                    TestIntVector = new List <int> {
                        1, 2, 3, 4, 5
                    },
                    TestLong = Int64.MaxValue,
                    TestString = "PPP",
                    TestInt128 = Int128.Parse("0x0102030405060708090A0B0C0D0E0F10"),
                    TestInt256 = Int256.Parse("0x0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20"),
                    TestUsersVector =
                        new List <IUser>
                    {
                        new User {
                            Id = 2, FirstName = "Pavel", LastName = "Durov", Key = new byte[] { 1, 2, 3, 4, 5 }
                        },
                        new NoUser {
                            Id = 3
                        },
                        new User {
                            Id = 4, FirstName = "Nikolay", LastName = "Durov", Key = new byte[] { 6, 7, 8, 9, 10 }
                        }
                    },
                    TestIntBareVector = new List <int> {
                        9, 99, 999, 9999, 99999, 999999
                    }
                },
                    GetBytes(0xA1B2C3D4, 0x997275b5, Double.Epsilon, Int32.MaxValue, 0x1CB5C415, 5, 1, 2, 3, 4, 5, Int64.MaxValue, 0x50505003, 0x090A0B0C0D0E0F10,
                             0x0102030405060708UL, 0x191A1B1C1D1E1F20, 0x1112131415161718, 0x090A0B0C0D0E0F10, 0x0102030405060708UL, 0x1cb5c415, 0x3, 0xd23c81a3, 0x2, 0x76615005, 0x6c65,
                             0x72754405, 0x766f, 0x03020105, 0x0504, 0xc67599d1, 0x3, 0xd23c81a3, 0x4, 0x6b694e07, 0x79616c6f, 0x72754405, 0x766f, 0x08070605, 0x0A09,
                             6, 9, 99, 999, 9999, 99999, 999999), "TestObject"),

                // getUsers([2,3,4])
                new DataUnit(new GetUsersFunction {
                    Arg1 = new List <int> {
                        2, 3, 4
                    }
                }, GetBytes(0x2d84d5f5, 0x1cb5c415, 0x3, 0x2, 0x3, 0x4), "GetUsersFunction"),

                // Response to getUsers([2,3,4])
                new DataUnit(
                    new List <IUser>
                {
                    new User {
                        Id = 2, FirstName = "Pavel", LastName = "Durov", Key = new byte[] { 1, 2, 3, 4, 5 }
                    },
                    new NoUser {
                        Id = 3
                    },
                    new User {
                        Id = 4, FirstName = "Nikolay", LastName = "Durov", Key = new byte[] { 6, 7, 8, 9, 10 }
                    }
                },
                    GetBytes(0x1cb5c415, 0x3, 0xd23c81a3, 0x2, 0x76615005, 0x6c65, 0x72754405, 0x766f, 0x03020105, 0x0504, 0xc67599d1, 0x3, 0xd23c81a3, 0x4, 0x6b694e07, 0x79616c6f,
                             0x72754405, 0x766f, 0x08070605, 0x0A09), "List<IUser>"),
            });
        }