Пример #1
0
        public void EmptyBigUIntNET()
        {
            var biguint = new BigUInt();

            Assert.AreEqual(0, biguint.BitCount);
            Assert.AreEqual(0ul, biguint.ByteCount);
            Assert.AreEqual(0ul, biguint.UInt64Count);
            Assert.IsTrue(biguint.UInt64Count == 0ul);
            Assert.AreEqual(0, biguint.GetSignificantBitCount());
            Assert.AreEqual("0", biguint.ToString());
            Assert.IsTrue(biguint.IsZero);
            Assert.IsFalse(biguint.IsAlias);
            biguint.SetZero();

            var biguint2 = new BigUInt();

            Assert.IsTrue(biguint.Equals(biguint2));

            biguint.Resize(1);
            Assert.AreEqual(1, biguint.BitCount);
            Assert.IsTrue(biguint.UInt64Count != 0ul);
            Assert.IsFalse(biguint.IsAlias);

            biguint.Resize(0);
            Assert.AreEqual(0, biguint.BitCount);
            Assert.IsTrue(biguint.UInt64Count == 0ul);
            Assert.IsFalse(biguint.IsAlias);
        }
Пример #2
0
        public void CmpTest()
        {
            const int length = 4;
            Random    random = new Random(1234);

            for (int i = 0; i <= 2500; i++)
            {
                UInt32[] mantissa1 = UIntUtil.Random(random, length, random.Next(length * UIntUtil.UInt32Bits + 1));
                UInt32[] mantissa2 = UIntUtil.Random(random, length, random.Next(length * UIntUtil.UInt32Bits + 1));

                BigUInt <Pow2.N4> n1 = new BigUInt <Pow2.N4>(mantissa1, enable_clone: false);
                BigUInt <Pow2.N4> n2 = random.Next(8) < 7 ? new BigUInt <Pow2.N4>(mantissa2, enable_clone: false) : n1.Copy();
                BigInteger        bi1 = n1, bi2 = n2;

                Console.WriteLine(n1);
                Console.WriteLine(n2);

                Assert.AreEqual(n1.Equals(n2), bi1.Equals(bi2));
                Assert.AreEqual(n1 == n2, bi1 == bi2);
                Assert.AreEqual(n1 != n2, bi1 != bi2);
                Assert.AreEqual(n1 < n2, bi1 < bi2);
                Assert.AreEqual(n1 > n2, bi1 > bi2);
                Assert.AreEqual(n1 <= n2, bi1 <= bi2);
                Assert.AreEqual(n1 >= n2, bi1 >= bi2);

                Console.Write("\n");
            }
        }
Пример #3
0
        public void DuplicateFromNET()
        {
            var original = new BigUInt(123);

            original.Set(56789);

            var target = new BigUInt();

            target.DuplicateFrom(original);
            Assert.AreEqual(target.BitCount, original.BitCount);
            Assert.IsTrue(target.Equals(original));
        }
Пример #4
0
        public void CopyConstructorTest()
        {
            BigUInt bui1 = new BigUInt("DEADBEEF");
            BigUInt bui2 = new BigUInt("1234567890ABCDEF1234567890ABCDEF");

            BigUInt bui3 = new BigUInt(bui1);

            Assert.AreEqual(1ul, bui3.UInt64Count);
            Assert.AreEqual(32, bui3.GetSignificantBitCount());
            Assert.AreEqual(0xDEADBEEFul, bui3.Data(0));
            Assert.IsTrue(bui1.Equals(bui3));

            bui3 = new BigUInt(bui2);

            Assert.AreEqual(2ul, bui3.UInt64Count);
            Assert.AreEqual(125, bui3.BitCount);
            Assert.AreEqual(125, bui3.GetSignificantBitCount());
            Assert.AreEqual(0x1234567890ABCDEFul, bui3.Data(0));
            Assert.AreEqual(0x1234567890ABCDEFul, bui3.Data(1));
            Assert.IsTrue(bui2.Equals(bui3));
        }
Пример #5
0
        public void BalancedEncodeDecodeBigUIntNET()
        {
            var modulus = new SmallModulus(0x10000);
            var encoder = new BalancedEncoder(modulus, 3, MemoryPoolHandle.New());

            var value = new BigUInt(64);

            value.Set("0");
            var poly = encoder.Encode(value);

            Assert.AreEqual(0, poly.SignificantCoeffCount());
            Assert.IsTrue(poly.IsZero);
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly));

            value.Set("1");
            var poly1 = encoder.Encode(value);

            Assert.AreEqual(1, poly1.SignificantCoeffCount());
            Assert.AreEqual("1", poly1.ToString());
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly1));

            value.Set("2");
            var poly2 = encoder.Encode(value);

            Assert.AreEqual(2, poly2.SignificantCoeffCount());
            Assert.AreEqual("1x^1 + FFFF", poly2.ToString());
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly2));

            value.Set("3");
            var poly3 = encoder.Encode(value);

            Assert.AreEqual(2, poly3.SignificantCoeffCount());
            Assert.AreEqual("1x^1", poly3.ToString());
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly3));

            value.Set("2671");
            var poly4 = encoder.Encode(value);

            Assert.AreEqual(9, poly4.SignificantCoeffCount());
            for (int i = 0; i < 9; ++i)
            {
                Assert.AreEqual("1", poly4[i].ToString());
            }
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly4));

            value.Set("D4EB");
            var poly5 = encoder.Encode(value);

            Assert.AreEqual(11, poly5.SignificantCoeffCount());
            for (int i = 0; i < 11; ++i)
            {
                if (i % 3 == 1)
                {
                    Assert.AreEqual(0x1UL, poly5[i]);
                }
                else if (i % 3 == 0)
                {
                    Assert.IsTrue(poly5[i] == 0);
                }
                else
                {
                    Assert.AreEqual(0xFFFFUL, poly5[i]);
                }
            }
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly5));

            var poly6 = new Plaintext(3);

            poly6[0] = 1;
            poly6[1] = 500;
            poly6[2] = 1023;
            value.Set(1 + 500 * 3 + 1023 * 9);
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly6));

            var encoder2 = new BalancedEncoder(modulus, 7, MemoryPoolHandle.New());
            var poly7    = new Plaintext(4);

            poly7[0] = 123;    // 123   (*1)
            poly7[1] = 0xFFFF; // -1 (*7)
            poly7[2] = 511;    // 511  (*49)
            poly7[3] = 1;      // 1    (*343)
            value.Set(123 + -1 * 7 + 511 * 49 + 1 * 343);
            Assert.AreEqual(value, encoder2.DecodeBigUInt(poly7));

            var encoder3 = new BalancedEncoder(modulus, 6, MemoryPoolHandle.New());
            var poly8    = new Plaintext(4);

            poly8[0] = 5;
            poly8[1] = 4;
            poly8[2] = 3;
            poly8[3] = 2;
            value.Set(5 + 4 * 6 + 3 * 36 + 2 * 216);
            Assert.AreEqual(value, encoder3.DecodeBigUInt(poly8));

            var encoder4 = new BalancedEncoder(modulus, 10, MemoryPoolHandle.New());
            var poly9    = new Plaintext(4);

            poly9[0] = 1;
            poly9[1] = 2;
            poly9[2] = 3;
            poly9[3] = 4;
            value.Set(4321);
            Assert.AreEqual(value, encoder4.DecodeBigUInt(poly9));

            value.Set("4D2");
            var poly10 = encoder2.Encode(value);

            Assert.AreEqual(5, poly10.SignificantCoeffCount());
            Assert.IsTrue(value.Equals(encoder2.DecodeBigUInt(poly10)));

            value.Set("4D2");
            var poly11 = encoder3.Encode(value);

            Assert.AreEqual(5, poly11.SignificantCoeffCount());
            Assert.IsTrue(value.Equals(encoder3.DecodeBigUInt(poly11)));

            value.Set("4D2");
            var poly12 = encoder4.Encode(value);

            Assert.AreEqual(4, poly12.SignificantCoeffCount());
            Assert.IsTrue(value.Equals(encoder4.DecodeBigUInt(poly12)));
        }
Пример #6
0
        public void ExceptionsTest()
        {
            BigUInt bui  = new BigUInt("DEADBEEF");
            BigUInt bui1 = null;

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = new BigUInt((BigUInt)null));
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = new BigUInt((string)null));
            Utilities.AssertThrows <ArgumentException>(() => bui1     = new BigUInt(bitCount: -1));
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = new BigUInt(bitCount: 64, hexString: null));
            Utilities.AssertThrows <ArgumentException>(() => bui1     = new BigUInt(bitCount: -1, hexString: "ABCDEF"));
            Utilities.AssertThrows <ArgumentException>(() => bui1     = new BigUInt(bitCount: -1, value: 10ul));

            bui1 = new BigUInt();
            BigUInt bui2 = new BigUInt();

            Utilities.AssertThrows <ArgumentOutOfRangeException>(() => bui.Data(1));

            Utilities.AssertThrows <ArgumentOutOfRangeException>(() => bui[5]);
            Utilities.AssertThrows <ArgumentOutOfRangeException>(() => bui[5] = 2);

            Utilities.AssertThrows <ArgumentNullException>(() => bui.DivideRemainder(bui1, null));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.DivideRemainder(null, bui2));
            Utilities.AssertThrows <ArgumentException>(() => bui.DivideRemainder(bui1, bui2));

            Utilities.AssertThrows <ArgumentNullException>(() => bui.DivideRemainder(1ul, null));
            Utilities.AssertThrows <ArgumentException>(() => bui.DivideRemainder(0ul, bui2));

            Utilities.AssertThrows <ArgumentNullException>(() => bui.DuplicateFrom(null));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.DuplicateTo(null));

            Assert.IsFalse(bui.Equals(null));

            Utilities.AssertThrows <ArgumentNullException>(() => bui.Load(null));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.ModuloInvert(null));
            Utilities.AssertThrows <ArgumentException>(() => bui.Resize(bitCount: -1));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.Save(null));

            Utilities.AssertThrows <ArgumentNullException>(() => bui.Set((BigUInt)null));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.Set((string)null));

            Utilities.AssertThrows <ArgumentNullException>(() => bui.TryModuloInvert(bui1, null));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.TryModuloInvert(null, bui2));
            Utilities.AssertThrows <ArgumentException>(() => bui.TryModuloInvert(bui1, bui2));
            Utilities.AssertThrows <ArgumentNullException>(() => bui.TryModuloInvert(1ul, null));

            bui2 = null;

            Utilities.AssertThrows <ArgumentNullException>(() => bui2 = +bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui2 = -bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui2 = ~bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui2++);
            Utilities.AssertThrows <ArgumentNullException>(() => bui2--);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui + bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 + bui);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 + 1ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui - bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 - bui);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 - 1ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui * bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 * bui);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 * 1ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui / bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 / bui);
            Utilities.AssertThrows <ArgumentException>(() => bui1     = bui / bui1);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 / 1ul);
            Utilities.AssertThrows <ArgumentException>(() => bui1     = bui / 0ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui ^ bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 ^ bui);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 ^ 1ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui & bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 & bui);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 & 1ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui | bui2);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 | bui);
            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 | 1ul);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 << 5);
            Utilities.AssertThrows <ArgumentException>(() => bui1     = bui1 << -1);

            Utilities.AssertThrows <ArgumentNullException>(() => bui1 = bui2 >> 5);
            Utilities.AssertThrows <ArgumentException>(() => bui1     = bui1 >> -1);

            Utilities.AssertThrows <ArgumentNullException>(() => { double d = (double)bui2; });
            Utilities.AssertThrows <ArgumentNullException>(() => { float f = (float)bui2; });
            Utilities.AssertThrows <ArgumentNullException>(() => { ulong u = (ulong)bui2; });
            Utilities.AssertThrows <ArgumentNullException>(() => { long l = (long)bui2; });
            Utilities.AssertThrows <ArgumentNullException>(() => { uint u = (uint)bui2; });
            Utilities.AssertThrows <ArgumentNullException>(() => { int i = (int)bui2; });
        }
Пример #7
0
        public void BigUInt99BitsNET()
        {
            var biguint = new BigUInt(99);

            Assert.AreEqual(99, biguint.BitCount);
            Assert.IsTrue(biguint.UInt64Count != 0ul);
            Assert.AreEqual(13ul, biguint.ByteCount);
            Assert.AreEqual(2ul, biguint.UInt64Count);
            Assert.AreEqual(0, biguint.GetSignificantBitCount());
            Assert.AreEqual("0", biguint.ToString());
            Assert.IsTrue(biguint.IsZero);
            Assert.AreEqual(0UL, biguint.Data(0));
            Assert.AreEqual(0UL, biguint.Data(1));
            Assert.AreEqual(0, biguint[0]);
            Assert.AreEqual(0, biguint[1]);
            Assert.AreEqual(0, biguint[2]);
            Assert.AreEqual(0, biguint[3]);
            Assert.AreEqual(0, biguint[4]);
            Assert.AreEqual(0, biguint[5]);
            Assert.AreEqual(0, biguint[6]);
            Assert.AreEqual(0, biguint[7]);
            Assert.AreEqual(0, biguint[8]);
            Assert.AreEqual(0, biguint[9]);
            Assert.AreEqual(0, biguint[10]);
            Assert.AreEqual(0, biguint[11]);
            Assert.AreEqual(0, biguint[12]);

            biguint.Set(1);
            Assert.AreEqual(1, biguint.GetSignificantBitCount());
            Assert.AreEqual("1", biguint.ToString());
            Assert.IsFalse(biguint.IsZero);
            Assert.AreEqual(13ul, biguint.ByteCount);
            Assert.AreEqual(2ul, biguint.UInt64Count);
            Assert.AreEqual(1UL, biguint.Data(0));
            Assert.AreEqual(0UL, biguint.Data(1));
            Assert.AreEqual(1, biguint[0]);
            Assert.AreEqual(0, biguint[1]);
            Assert.AreEqual(0, biguint[2]);
            Assert.AreEqual(0, biguint[3]);
            Assert.AreEqual(0, biguint[4]);
            Assert.AreEqual(0, biguint[5]);
            Assert.AreEqual(0, biguint[6]);
            Assert.AreEqual(0, biguint[7]);
            Assert.AreEqual(0, biguint[8]);
            Assert.AreEqual(0, biguint[9]);
            Assert.AreEqual(0, biguint[10]);
            Assert.AreEqual(0, biguint[11]);
            Assert.AreEqual(0, biguint[12]);
            biguint.SetZero();
            Assert.IsTrue(biguint.IsZero);
            Assert.AreEqual(0UL, biguint.Data(0));
            Assert.AreEqual(0UL, biguint.Data(1));

            biguint.Set("7FFFFFFFFFFFFFFFFFFFFFFFF");
            Assert.AreEqual(99, biguint.GetSignificantBitCount());
            Assert.AreEqual("7FFFFFFFFFFFFFFFFFFFFFFFF", biguint.ToString());
            Assert.IsFalse(biguint.IsZero);
            Assert.AreEqual(0xFFFFFFFFFFFFFFFFUL, biguint.Data(0));
            Assert.AreEqual(0x7FFFFFFFFUL, biguint.Data(1));
            Assert.AreEqual(0xFF, biguint[0]);
            Assert.AreEqual(0xFF, biguint[1]);
            Assert.AreEqual(0xFF, biguint[2]);
            Assert.AreEqual(0xFF, biguint[3]);
            Assert.AreEqual(0xFF, biguint[4]);
            Assert.AreEqual(0xFF, biguint[5]);
            Assert.AreEqual(0xFF, biguint[6]);
            Assert.AreEqual(0xFF, biguint[7]);
            Assert.AreEqual(0xFF, biguint[8]);
            Assert.AreEqual(0xFF, biguint[9]);
            Assert.AreEqual(0xFF, biguint[10]);
            Assert.AreEqual(0xFF, biguint[11]);
            Assert.AreEqual(0x07, biguint[12]);
            biguint.SetZero();
            Assert.IsTrue(biguint.IsZero);
            Assert.AreEqual(0UL, biguint.Data(0));
            Assert.AreEqual(0UL, biguint.Data(1));

            biguint.Set("4000000000000000000000000");
            Assert.AreEqual(99, biguint.GetSignificantBitCount());
            Assert.AreEqual("4000000000000000000000000", biguint.ToString());
            Assert.IsFalse(biguint.IsZero);
            Assert.AreEqual(0UL, biguint.Data(0));
            Assert.AreEqual(0x400000000UL, biguint.Data(1));
            Assert.AreEqual(0, biguint[0]);
            Assert.AreEqual(0, biguint[1]);
            Assert.AreEqual(0, biguint[2]);
            Assert.AreEqual(0, biguint[3]);
            Assert.AreEqual(0, biguint[4]);
            Assert.AreEqual(0, biguint[5]);
            Assert.AreEqual(0, biguint[6]);
            Assert.AreEqual(0, biguint[7]);
            Assert.AreEqual(0, biguint[8]);
            Assert.AreEqual(0, biguint[9]);
            Assert.AreEqual(0, biguint[10]);
            Assert.AreEqual(0, biguint[11]);
            Assert.AreEqual(0x04, biguint[12]);

            biguint.Set(0x8001);
            Assert.AreEqual(16, biguint.GetSignificantBitCount());
            Assert.AreEqual("8001", biguint.ToString());
            Assert.IsFalse(biguint.IsZero);
            Assert.AreEqual(0x8001UL, biguint.Data(0));
            Assert.AreEqual(0UL, biguint.Data(1));
            Assert.AreEqual(0x01, biguint[0]);
            Assert.AreEqual(0x80, biguint[1]);
            Assert.AreEqual(0, biguint[2]);
            Assert.AreEqual(0, biguint[3]);
            Assert.AreEqual(0, biguint[4]);
            Assert.AreEqual(0, biguint[5]);
            Assert.AreEqual(0, biguint[6]);
            Assert.AreEqual(0, biguint[7]);
            Assert.AreEqual(0, biguint[8]);
            Assert.AreEqual(0, biguint[9]);
            Assert.AreEqual(0, biguint[10]);
            Assert.AreEqual(0, biguint[11]);
            Assert.AreEqual(0, biguint[12]);

            var biguint2 = new BigUInt("123");

            Assert.IsFalse(biguint.Equals(biguint2));
            Assert.IsFalse(biguint2.Equals(biguint));
            Assert.AreNotEqual(biguint.GetHashCode(), biguint2.GetHashCode());

            biguint.Set(biguint2);
            Assert.IsTrue(biguint.Equals(biguint2));
            Assert.IsTrue(biguint2.Equals(biguint));
            Assert.AreEqual(biguint.GetHashCode(), biguint2.GetHashCode());
            Assert.AreEqual(9, biguint.GetSignificantBitCount());
            Assert.AreEqual("123", biguint.ToString());
            Assert.AreEqual(0x123UL, biguint.Data(0));
            Assert.AreEqual(0UL, biguint.Data(1));
            Assert.AreEqual(0x23, biguint[0]);
            Assert.AreEqual(0x01, biguint[1]);
            Assert.AreEqual(0, biguint[2]);
            Assert.AreEqual(0, biguint[3]);
            Assert.AreEqual(0, biguint[4]);
            Assert.AreEqual(0, biguint[5]);
            Assert.AreEqual(0, biguint[6]);
            Assert.AreEqual(0, biguint[7]);
            Assert.AreEqual(0, biguint[8]);
            Assert.AreEqual(0, biguint[9]);
            Assert.AreEqual(0, biguint[10]);
            Assert.AreEqual(0, biguint[11]);
            Assert.AreEqual(0, biguint[12]);

            biguint.Resize(8);
            Assert.AreEqual(8, biguint.BitCount);
            Assert.AreEqual(1ul, biguint.UInt64Count);
            Assert.AreEqual("23", biguint.ToString());

            biguint.Resize(100);
            Assert.AreEqual(100, biguint.BitCount);
            Assert.AreEqual(2ul, biguint.UInt64Count);
            Assert.AreEqual("23", biguint.ToString());

            biguint.Resize(0);
            Assert.AreEqual(0, biguint.BitCount);
            Assert.AreEqual(0ul, biguint.UInt64Count);
            Assert.IsTrue(biguint.UInt64Count == 0);
        }