Exemplo n.º 1
0
        public void SetFail2Test()
        {
            // Should fail if set to bigger than 62 bits
            SmallModulus sm = new SmallModulus();

            sm.Set(0x7FFFFFFFFFFFFFFFul);
        }
Exemplo n.º 2
0
        public void SaveLoadTest()
        {
            SmallModulus sm1 = new SmallModulus(65537ul);
            SmallModulus sm2 = new SmallModulus();

            Assert.AreNotSame(sm1, sm2);
            Assert.AreNotEqual(sm1, sm2);
            Assert.AreNotEqual(sm1.IsPrime, sm2.IsPrime);


            using (MemoryStream stream = new MemoryStream())
            {
                sm1.Save(stream);

                stream.Seek(offset: 0, loc: SeekOrigin.Begin);

                sm2.Load(stream);
            }

            Assert.AreNotSame(sm1, sm2);
            Assert.AreEqual(sm1, sm2);
            Assert.AreEqual(sm1.BitCount, sm2.BitCount);
            Assert.AreEqual(sm1.UInt64Count, sm2.UInt64Count);
            Assert.AreEqual(sm1.ConstRatio.Item1, sm2.ConstRatio.Item1);
            Assert.AreEqual(sm1.ConstRatio.Item2, sm2.ConstRatio.Item2);
            Assert.AreEqual(sm1.ConstRatio.Item3, sm2.ConstRatio.Item3);
            Assert.AreEqual(sm1.IsPrime, sm2.IsPrime);
        }
Exemplo n.º 3
0
        public void SetFail2Test()
        {
            // Should fail if set to bigger than 62 bits
            SmallModulus sm = new SmallModulus();

            Utilities.AssertThrows <ArgumentException>(() => sm.Set(0x7FFFFFFFFFFFFFFFul));
        }
Exemplo n.º 4
0
        public void BinaryFractionalEncodeDecodeNET()
        {
            var polyModulus = new BigPoly("1x^1024 + 1");
            var modulus     = new SmallModulus(0x10000);
            var encoder     = new BinaryFractionalEncoder(modulus, polyModulus, 500, 50, MemoryPoolHandle.New());

            var poly = encoder.Encode(0.0);

            Assert.IsTrue(poly.IsZero);
            Assert.AreEqual(0.0, encoder.Decode(poly));

            var poly1 = encoder.Encode(-1.0);

            Assert.AreEqual(-1.0, encoder.Decode(poly1));

            var poly2 = encoder.Encode(0.1);

            Assert.IsTrue(Math.Abs(encoder.Decode(poly2) - 0.1) / 0.1 < 0.000001);

            var poly3 = encoder.Encode(3.123);

            Assert.IsTrue(Math.Abs(encoder.Decode(poly3) - 3.123) / 3.123 < 0.000001);

            var poly4 = encoder.Encode(-123.456);

            Assert.IsTrue(Math.Abs(encoder.Decode(poly4) + 123.456) / (-123.456) < 0.000001);

            var poly5 = encoder.Encode(12345.98765);

            Assert.IsTrue(Math.Abs(encoder.Decode(poly5) - 12345.98765) / 12345.98765 < 0.000001);
        }
Exemplo n.º 5
0
        public void ValueConstructorTest()
        {
            SmallModulus sm = new SmallModulus(5);

            Assert.IsNotNull(sm);
            Assert.IsFalse(sm.IsZero);
            Assert.AreEqual(5ul, sm.Value);
            Assert.AreEqual(3, sm.BitCount);
            Assert.IsTrue(sm.IsPrime);

            // Value is exactly 62 bits
            SmallModulus sm2 = new SmallModulus(0x3FFFFFFFFFFFFFFFul);

            Assert.IsNotNull(sm2);
            Assert.IsFalse(sm2.IsZero);
            Assert.AreEqual(0x3FFFFFFFFFFFFFFFul, sm2.Value);
            Assert.AreEqual(62, sm2.BitCount);
            Assert.AreEqual(1ul, sm2.UInt64Count);
            Assert.IsFalse(sm2.IsPrime);

            SmallModulus sm3 = new SmallModulus(0xF00000F000079ul);

            Assert.IsNotNull(sm3);
            Assert.IsFalse(sm3.IsZero);
            Assert.AreEqual(0xF00000F000079ul, sm3.Value);
            Assert.AreEqual(52, sm3.BitCount);
            Assert.AreEqual(1ul, sm3.UInt64Count);
            Assert.IsTrue(sm3.IsPrime);
        }
Exemplo n.º 6
0
        public void SetFail1Test()
        {
            // Should faile if set to 1
            SmallModulus sm = new SmallModulus();

            Utilities.AssertThrows <ArgumentException>(() => sm.Set(1));
        }
Exemplo n.º 7
0
        public void SetFail1Test()
        {
            // Should faile if set to 1
            SmallModulus sm = new SmallModulus();

            sm.Set(1);
        }
Exemplo n.º 8
0
        public void ExplicitCreateTest()
        {
            SmallModulus sm = (SmallModulus)34ul;

            Assert.IsNotNull(sm);
            Assert.AreEqual(34ul, sm.Value);
        }
Exemplo n.º 9
0
        public void SmallMods60BitTest()
        {
            SmallModulus sm = DefaultParams.SmallMods60Bit(45);

            Assert.IsNotNull(sm);
            Assert.AreEqual(60, sm.BitCount);
            Assert.AreEqual(0x0ffffffff1740001ul, sm.Value);
        }
Exemplo n.º 10
0
        public void CopyConstructorTest()
        {
            SmallModulus sm   = new SmallModulus(10);
            SmallModulus copy = new SmallModulus(sm);

            Assert.IsNotNull(copy);
            Assert.IsFalse(copy.IsZero);
            Assert.AreEqual(10ul, copy.Value);
        }
Exemplo n.º 11
0
        public void EmptyConstructorTest()
        {
            SmallModulus sm = new SmallModulus();

            Assert.IsNotNull(sm);
            Assert.IsTrue(sm.IsZero);
            Assert.AreEqual(0ul, sm.Value);
            Assert.AreEqual(0, sm.BitCount);
            Assert.AreEqual(1ul, sm.UInt64Count);
        }
Exemplo n.º 12
0
        public void ExceptionsTest()
        {
            SmallModulus sm = new SmallModulus(0x12345ul);

            Utilities.AssertThrows <ArgumentNullException>(() => sm = new SmallModulus(null));
            Utilities.AssertThrows <ArgumentNullException>(() => sm.Set(null));
            Utilities.AssertThrows <ArgumentNullException>(() => sm.Save(null));
            Utilities.AssertThrows <ArgumentNullException>(() => sm.Load(null));
            Utilities.AssertThrows <EndOfStreamException>(() => sm.Load(new MemoryStream()));
        }
Exemplo n.º 13
0
        public void SmallMods30BitTest()
        {
            SmallModulus sm = DefaultParams.SmallMods30Bit(20);

            Assert.IsNotNull(sm);
            Assert.AreEqual(30, sm.BitCount);
            Assert.AreEqual(0x3BE80001ul, sm.Value);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => DefaultParams.SmallMods30Bit(64));
        }
Exemplo n.º 14
0
        public void EqualsTest()
        {
            SmallModulus sm1 = new SmallModulus(0x12345ul);
            SmallModulus sm2 = new SmallModulus(0x12345ul);

            Assert.AreEqual(sm1, sm2);
            Assert.AreEqual(sm1.GetHashCode(), sm2.GetHashCode());
            Assert.IsTrue(sm1.Equals(0x12345ul));
            Assert.IsFalse(sm1.Equals(0x1234ul));

            Assert.IsFalse(sm1.Equals(null));
        }
Exemplo n.º 15
0
        public void ExceptionsTest()
        {
            SmallModulus sm       = new SmallModulus(0x12345ul);
            MemoryStream ms_empty = new MemoryStream();

            Assert.ThrowsException <ArgumentNullException>(() => sm = new SmallModulus(null));

            Assert.ThrowsException <ArgumentNullException>(() => sm.Set(null));

            Assert.ThrowsException <ArgumentNullException>(() => sm.Save(null));
            Assert.ThrowsException <ArgumentNullException>(() => sm.Load(null));
            Assert.ThrowsException <ArgumentException>(() => sm.Load(ms_empty));
        }
Exemplo n.º 16
0
        public void ConstRatioTest()
        {
            SmallModulus sm = new SmallModulus();

            sm.Set(0x1234567890ABCDEFul);

            Tuple <ulong, ulong, ulong> ratio = sm.ConstRatio;

            Assert.IsNotNull(ratio);
            Assert.AreNotEqual(0ul, ratio.Item1);
            Assert.AreNotEqual(0ul, ratio.Item2);
            Assert.AreNotEqual(0ul, ratio.Item3);
        }
Exemplo n.º 17
0
        public void CreateSmallModulusNET()
        {
            var mod = new SmallModulus();

            Assert.IsTrue(mod.IsZero);
            Assert.AreEqual(0UL, mod.Value);
            Assert.AreEqual(0, mod.BitCount);
            Assert.AreEqual(1, mod.UInt64Count);
            Assert.AreEqual(0UL, mod.ConstRatio.Item1);
            Assert.AreEqual(0UL, mod.ConstRatio.Item2);
            Assert.AreEqual(0UL, mod.ConstRatio.Item3);

            mod.Set(3);
            Assert.IsFalse(mod.IsZero);
            Assert.AreEqual(3UL, mod.Value);
            Assert.AreEqual(2, mod.BitCount);
            Assert.AreEqual(1, mod.UInt64Count);
            Assert.AreEqual(6148914691236517205UL, mod.ConstRatio.Item1);
            Assert.AreEqual(6148914691236517205UL, mod.ConstRatio.Item2);
            Assert.AreEqual(1UL, mod.ConstRatio.Item3);

            var mod2 = new SmallModulus(2);
            var mod3 = new SmallModulus(3);

            Assert.IsFalse(mod.Equals(mod2));
            Assert.IsTrue(mod.Equals(mod3));

            mod.Set(0);
            Assert.IsTrue(mod.IsZero);
            Assert.AreEqual(0UL, mod.Value);
            Assert.AreEqual(0, mod.BitCount);
            Assert.AreEqual(1, mod.UInt64Count);
            Assert.AreEqual(0UL, mod.ConstRatio.Item1);
            Assert.AreEqual(0UL, mod.ConstRatio.Item2);
            Assert.AreEqual(0UL, mod.ConstRatio.Item3);

            mod.Set(0xF00000F00000F);
            Assert.IsFalse(mod.IsZero);
            Assert.AreEqual(0xF00000F00000FUL, mod.Value);
            Assert.AreEqual(52, mod.BitCount);
            Assert.AreEqual(1, mod.UInt64Count);
            Assert.AreEqual(1224979098644774929UL, mod.ConstRatio.Item1);
            Assert.AreEqual(4369UL, mod.ConstRatio.Item2);
            Assert.AreEqual(281470698520321UL, mod.ConstRatio.Item3);
        }
Exemplo n.º 18
0
        public void SaveLoadSmallModulusNET()
        {
            var stream = new MemoryStream();

            var mod = new SmallModulus();

            stream.Seek(0, SeekOrigin.Begin);
            mod.Save(stream);

            var mod2 = new SmallModulus();

            stream.Seek(0, SeekOrigin.Begin);
            mod2.Load(stream);
            Assert.AreEqual(mod2.Value, mod.Value);
            Assert.AreEqual(mod2.BitCount, mod.BitCount);
            Assert.AreEqual(mod2.UInt64Count, mod.UInt64Count);
            Assert.AreEqual(mod2.ConstRatio.Item1, mod.ConstRatio.Item1);
            Assert.AreEqual(mod2.ConstRatio.Item2, mod.ConstRatio.Item2);
            Assert.AreEqual(mod2.ConstRatio.Item3, mod.ConstRatio.Item3);

            mod.Set(3);
            stream.Seek(0, SeekOrigin.Begin);
            mod.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            mod2.Load(stream);
            Assert.AreEqual(mod2.Value, mod.Value);
            Assert.AreEqual(mod2.BitCount, mod.BitCount);
            Assert.AreEqual(mod2.UInt64Count, mod.UInt64Count);
            Assert.AreEqual(mod2.ConstRatio.Item1, mod.ConstRatio.Item1);
            Assert.AreEqual(mod2.ConstRatio.Item2, mod.ConstRatio.Item2);
            Assert.AreEqual(mod2.ConstRatio.Item3, mod.ConstRatio.Item3);

            mod.Set(0xF00000F00000F);
            stream.Seek(0, SeekOrigin.Begin);
            mod.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            mod2.Load(stream);
            Assert.AreEqual(mod2.Value, mod.Value);
            Assert.AreEqual(mod2.BitCount, mod.BitCount);
            Assert.AreEqual(mod2.UInt64Count, mod.UInt64Count);
            Assert.AreEqual(mod2.ConstRatio.Item1, mod.ConstRatio.Item1);
            Assert.AreEqual(mod2.ConstRatio.Item2, mod.ConstRatio.Item2);
            Assert.AreEqual(mod2.ConstRatio.Item3, mod.ConstRatio.Item3);
        }
Exemplo n.º 19
0
        public void ExceptionsTest()
        {
            SmallModulus sm       = new SmallModulus(0x12345ul);
            MemoryStream ms_empty = new MemoryStream();

            Assert.ThrowsException <ArgumentNullException>(() => sm = new SmallModulus(null));

            Assert.ThrowsException <ArgumentNullException>(() => sm.Set(null));

            Assert.ThrowsException <ArgumentNullException>(() => sm.Save(null));
            Assert.ThrowsException <ArgumentNullException>(() => sm.Load(null));
            Assert.ThrowsException <ArgumentException>(() => sm.Load(ms_empty));

            MemoryStream ms = new MemoryStream();

            using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8, leaveOpen: true))
            {
                // Bit count
                writer.Write(16);
                // Uint64 count
                writer.Write(1ul);
                // Value that does not match bit count
                writer.Write(0x123456789ABCDul);
            }

            ms.Seek(offset: 0, loc: SeekOrigin.Begin);
            Assert.ThrowsException <InvalidOperationException>(() => sm.Load(ms));

            ms = new MemoryStream();
            using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8, leaveOpen: true))
            {
                // Bit count
                writer.Write(16);
                // UInt64 count that does not match value
                writer.Write(2ul);
                // Value
                writer.Write(0xFABCul);
            }

            ms.Seek(offset: 0, loc: SeekOrigin.Begin);
            Assert.ThrowsException <InvalidOperationException>(() => sm.Load(ms));
        }
Exemplo n.º 20
0
        public void SetTest()
        {
            SmallModulus sm1 = new SmallModulus(456);
            SmallModulus sm2 = new SmallModulus();

            Assert.IsFalse(sm1.IsZero);
            Assert.AreEqual(456ul, sm1.Value);
            Assert.IsTrue(sm2.IsZero);
            Assert.AreEqual(0ul, sm2.Value);

            sm2.Set(sm1);

            Assert.IsFalse(sm2.IsZero);
            Assert.AreEqual(456ul, sm2.Value);

            sm2.Set(value: 123);

            Assert.IsFalse(sm2.IsZero);
            Assert.AreEqual(123ul, sm2.Value);
        }
Exemplo n.º 21
0
        public void ConstRatioTest()
        {
            SmallModulus sm = new SmallModulus();

            sm.Set(0x1234567890ABCDEFul);

            Tuple <ulong, ulong, ulong> ratio = sm.ConstRatio;

            Assert.IsNotNull(ratio);
            Assert.AreNotEqual(0ul, ratio.Item1);
            Assert.AreNotEqual(0ul, ratio.Item2);
            Assert.AreNotEqual(0ul, ratio.Item3);

            sm.Set(0xF00000F000079ul);
            ratio = sm.ConstRatio;

            Assert.IsNotNull(ratio);
            Assert.AreEqual(1224979096621368355ul, ratio.Item1);
            Assert.AreEqual(4369ul, ratio.Item2);
            Assert.AreEqual(1144844808538997ul, ratio.Item3);
        }
Exemplo n.º 22
0
        public void CompareToTest()
        {
            SmallModulus sminv = null;
            SmallModulus sm0   = new SmallModulus();
            SmallModulus sm2   = new SmallModulus(2);
            SmallModulus sm5   = new SmallModulus(5);
            SmallModulus smbig = new SmallModulus(0xFFFFFFF);

            Assert.AreEqual(1, sm0.CompareTo(sminv));
            Assert.AreEqual(0, sm0.CompareTo(sm0));
            Assert.AreEqual(-1, sm2.CompareTo(sm5));
            Assert.AreEqual(-1, sm2.CompareTo(smbig));
            Assert.AreEqual(1, sm2.CompareTo(sminv));
            Assert.AreEqual(0, sm5.CompareTo(sm5));
            Assert.AreEqual(0, smbig.CompareTo(smbig));
            Assert.AreEqual(1, smbig.CompareTo(sm0));
            Assert.AreEqual(1, smbig.CompareTo(sm5));
            Assert.AreEqual(1, smbig.CompareTo(sminv));
            Assert.AreEqual(-1, sm5.CompareTo(6));
            Assert.AreEqual(0, sm5.CompareTo(5));
            Assert.AreEqual(1, sm5.CompareTo(4));
            Assert.AreEqual(1, sm5.CompareTo(0));
        }
Exemplo n.º 23
0
        public void BalancedFractionalEncodeDecodeNET()
        {
            var polyModulus = new BigPoly("1x^1024 + 1");
            var modulus     = new SmallModulus(0x10000);

            var poly  = new Plaintext();
            var poly1 = new Plaintext();
            var poly2 = new Plaintext();
            var poly3 = new Plaintext();
            var poly4 = new Plaintext();
            var poly5 = new Plaintext();

            for (ulong b = 3; b < 20; b += 2)
            {
                var encoder = new BalancedFractionalEncoder(modulus, polyModulus, 500, 50, b, MemoryPoolHandle.New());

                poly = encoder.Encode(0.0);
                Assert.AreEqual(1, poly.CoeffCount);
                Assert.IsTrue(poly.IsZero);
                Assert.AreEqual(0.0, encoder.Decode(poly));

                poly1 = encoder.Encode(-1.0);
                Assert.AreEqual(-1.0, encoder.Decode(poly1));

                poly2 = encoder.Encode(0.1);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly2) - 0.1) / 0.1 < 0.000001);

                poly3 = encoder.Encode(3.123);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly3) - 3.123) / 3.123 < 0.000001);

                poly4 = encoder.Encode(-123.456);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly4) + 123.456) / (-123.456) < 0.000001);

                poly5 = encoder.Encode(12345.98765);
                Assert.IsTrue(Math.Abs(encoder.Decode(poly5) - 12345.98765) / 12345.98765 < 0.000001);
            }
        }
Exemplo n.º 24
0
        public void BinaryEncodeDecodeInt32NET()
        {
            var modulus = new SmallModulus(0xFFFFFFFFFFFFFFF);
            var encoder = new BinaryEncoder(modulus, MemoryPoolHandle.New());

            var poly = encoder.Encode(0);

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

            var poly1 = encoder.Encode(1);

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

            var poly2 = encoder.Encode(2);

            Assert.AreEqual(2, poly2.SignificantCoeffCount());
            Assert.AreEqual("1x^1", poly2.ToString());
            Assert.AreEqual(2, encoder.DecodeInt32(poly2));

            var poly3 = encoder.Encode(3);

            Assert.AreEqual(2, poly3.SignificantCoeffCount());
            Assert.AreEqual("1x^1 + 1", poly3.ToString());
            Assert.AreEqual(3, encoder.DecodeInt32(poly3));

            var poly4 = encoder.Encode(-1);

            Assert.AreEqual(1, poly4.SignificantCoeffCount());
            Assert.AreEqual("FFFFFFFFFFFFFFE", poly4.ToString());
            Assert.AreEqual(-1, encoder.DecodeInt32(poly4));

            var poly5 = encoder.Encode(-2);

            Assert.AreEqual(2, poly5.SignificantCoeffCount());
            Assert.AreEqual("FFFFFFFFFFFFFFEx^1", poly5.ToString());
            Assert.AreEqual(-2, encoder.DecodeInt32(poly5));

            var poly6 = encoder.Encode(-3);

            Assert.AreEqual(2, poly6.SignificantCoeffCount());
            Assert.AreEqual("FFFFFFFFFFFFFFEx^1 + FFFFFFFFFFFFFFE", poly6.ToString());
            Assert.AreEqual(-3, encoder.DecodeInt32(poly6));

            var poly7 = encoder.Encode(0x7FFFFFFF);

            Assert.AreEqual(31, poly7.SignificantCoeffCount());
            for (int i = 0; i < 31; ++i)
            {
                Assert.AreEqual("1", poly7[i].ToString());
            }
            Assert.AreEqual(0x7FFFFFFF, encoder.DecodeInt32(poly7));

            var poly8 = encoder.Encode(unchecked ((int)0x80000000));

            Assert.AreEqual(32, poly8.SignificantCoeffCount());
            Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly8[31]);
            for (int i = 0; i < 31; ++i)
            {
                Assert.IsTrue(poly8[i] == 0);
            }
            Assert.AreEqual(unchecked ((int)0x80000000), encoder.DecodeInt32(poly8));

            var poly9 = encoder.Encode(0x80F02);

            Assert.AreEqual(20, poly9.SignificantCoeffCount());
            for (int i = 0; i < 20; ++i)
            {
                if (i == 19 || (i >= 8 && i <= 11) || i == 1)
                {
                    Assert.AreEqual("1", poly9[i].ToString());
                }
                else
                {
                    Assert.IsTrue(poly9[i] == 0);
                }
            }
            Assert.AreEqual(0x80F02, encoder.DecodeInt32(poly9));

            var poly10 = encoder.Encode(-1073);

            Assert.AreEqual(11, poly10.SignificantCoeffCount());
            Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[10]);
            Assert.IsTrue(poly10[9] == 0);
            Assert.IsTrue(poly10[8] == 0);
            Assert.IsTrue(poly10[7] == 0);
            Assert.IsTrue(poly10[6] == 0);
            Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[5]);
            Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[4]);
            Assert.IsTrue(poly10[3] == 0);
            Assert.IsTrue(poly10[2] == 0);
            Assert.IsTrue(poly10[1] == 0);
            Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[0]);
            Assert.AreEqual(-1073, encoder.DecodeInt32(poly10));

            modulus.Set(0xFFFF);
            var encoder2 = new BinaryEncoder(modulus, MemoryPoolHandle.New());
            var poly11   = new Plaintext(6);

            poly11[0] = 1;
            poly11[1] = 0xFFFE; // -1
            poly11[2] = 0xFFFD; // -2
            poly11[3] = 0x8000; // -32767
            poly11[4] = 0x7FFF; // 32767
            poly11[5] = 0x7FFE; // 32766
            Assert.AreEqual(1 + -1 * 2 + -2 * 4 + -32767 * 8 + 32767 * 16 + 32766 * 32, encoder2.DecodeInt32(poly11));
        }
Exemplo n.º 25
0
        public void BinaryEncodeDecodeBigUIntNET()
        {
            var modulus = new SmallModulus(0xFFFFFFFFFFFFFFF);
            var encoder = new BinaryEncoder(modulus);

            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", 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 + 1", poly3.ToString());
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly3));

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

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

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

            Assert.AreEqual(20, poly5.SignificantCoeffCount());
            for (int i = 0; i < 20; ++i)
            {
                if (i == 19 || (i >= 8 && i <= 11) || i == 1)
                {
                    Assert.AreEqual("1", poly5[i].ToString());
                }
                else
                {
                    Assert.IsTrue(poly5[i] == 0);
                }
            }
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly5));

            var poly6 = new Plaintext(3);

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

            modulus.Set(1024);
            var encoder2 = new BinaryEncoder(modulus);
            var poly7    = new Plaintext(4);

            poly7[0] = 1023; // -1   (*1)
            poly7[1] = 512;  // -512 (*2)
            poly7[2] = 511;  // 511  (*4)
            poly7[3] = 1;    // 1    (*8)
            value.Set(-1 + -512 * 2 + 511 * 4 + 1 * 8);
            Assert.AreEqual(value, encoder2.DecodeBigUInt(poly7));
        }
Exemplo n.º 26
0
 public void ConstructorFail1Test()
 {
     // Should fail if value is 1
     SmallModulus sm = new SmallModulus(1);
 }
Exemplo n.º 27
0
        public void BinaryEncodeDecodeUInt64NET()
        {
            var modulus = new SmallModulus(0xFFFFFFFFFFFFFFF);
            var encoder = new BinaryEncoder(modulus, MemoryPoolHandle.New());

            var poly = encoder.Encode(0UL);

            Assert.AreEqual(0, poly.SignificantCoeffCount());
            Assert.IsTrue(poly.IsZero);
            Assert.AreEqual(0UL, encoder.DecodeUInt64(poly));

            var poly1 = encoder.Encode(1UL);

            Assert.AreEqual(1, poly1.SignificantCoeffCount());
            Assert.AreEqual("1", poly1.ToString());
            Assert.AreEqual(1UL, encoder.DecodeUInt64(poly1));

            var poly2 = encoder.Encode(2UL);

            Assert.AreEqual(2, poly2.SignificantCoeffCount());
            Assert.AreEqual("1x^1", poly2.ToString());
            Assert.AreEqual(2UL, encoder.DecodeUInt64(poly2));

            var poly3 = encoder.Encode(3UL);

            Assert.AreEqual(2, poly3.SignificantCoeffCount());
            Assert.AreEqual("1x^1 + 1", poly3.ToString());
            Assert.AreEqual(3UL, encoder.DecodeUInt64(poly3));

            var poly4 = encoder.Encode(0xFFFFFFFFFFFFFFFFUL);

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

            var poly5 = encoder.Encode(0x80F02UL);

            Assert.AreEqual(20, poly5.SignificantCoeffCount());
            for (int i = 0; i < 20; ++i)
            {
                if (i == 19 || (i >= 8 && i <= 11) || i == 1)
                {
                    Assert.AreEqual("1", poly5[i].ToString());
                }
                else
                {
                    Assert.IsTrue(poly5[i] == 0);
                }
            }
            Assert.AreEqual(0x80F02UL, encoder.DecodeUInt64(poly5));

            var poly6 = new Plaintext(3);

            poly6[0] = 1;
            poly6[1] = 500;
            poly6[2] = 1023;
            Assert.AreEqual((1UL + 500 * 2 + 1023 * 4), encoder.DecodeUInt64(poly6));

            modulus.Set(1024);
            var encoder2 = new BinaryEncoder(modulus, MemoryPoolHandle.New());
            var poly7    = new Plaintext(4);

            poly7[0] = 1023; // -1   (*1)
            poly7[1] = 512;  // -512 (*2)
            poly7[2] = 511;  // 511  (*4)
            poly7[3] = 1;    // 1    (*8)
            Assert.AreEqual((ulong)(-1 + -512 * 2 + 511 * 4 + 1 * 8), encoder2.DecodeUInt64(poly7));
        }
Exemplo n.º 28
0
        public void BalancedEncodeDecodeInt32NET()
        {
            var modulus = new SmallModulus(0x10000);
            var encoder = new BalancedEncoder(modulus, 3, MemoryPoolHandle.New());

            var poly = encoder.Encode(0);

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

            var poly1 = encoder.Encode(1);

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

            var poly2 = encoder.Encode(2);

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

            var poly3 = encoder.Encode(3);

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

            var poly4 = encoder.Encode(-1);

            Assert.AreEqual(1, poly4.SignificantCoeffCount());
            Assert.AreEqual("FFFF", poly4.ToString());
            Assert.AreEqual(-1, encoder.DecodeInt32(poly4));

            var poly5 = encoder.Encode(-2);

            Assert.AreEqual(2, poly5.SignificantCoeffCount());
            Assert.AreEqual("FFFFx^1 + 1", poly5.ToString());
            Assert.AreEqual(-2, encoder.DecodeInt32(poly5));

            var poly6 = encoder.Encode(-3);

            Assert.AreEqual(2, poly6.SignificantCoeffCount());
            Assert.AreEqual("FFFFx^1", poly6.ToString());
            Assert.AreEqual(-3, encoder.DecodeInt32(poly6));

            var poly7 = encoder.Encode(-0x2671);

            Assert.AreEqual(9, poly7.SignificantCoeffCount());
            for (int i = 0; i < 9; ++i)
            {
                Assert.AreEqual(0xFFFFUL, poly7[i]);
            }
            Assert.AreEqual(-0x2671, encoder.DecodeInt32(poly7));

            var poly8 = encoder.Encode(-4374);

            Assert.AreEqual(9, poly8.SignificantCoeffCount());
            Assert.AreEqual(0xFFFFUL, poly8[8]);
            Assert.AreEqual(1UL, poly8[7]);
            for (int i = 0; i < 7; ++i)
            {
                Assert.IsTrue(poly8[i] == 0);
            }
            Assert.AreEqual(-4374, encoder.DecodeInt32(poly8));

            var poly9 = encoder.Encode(-0xD4EB);

            Assert.AreEqual(11, poly9.SignificantCoeffCount());
            for (int i = 0; i < 11; ++i)
            {
                if (i % 3 == 1)
                {
                    Assert.AreEqual(0xFFFFUL, poly9[i]);
                }
                else if (i % 3 == 0)
                {
                    Assert.IsTrue(poly9[i] == 0);
                }
                else
                {
                    Assert.AreEqual(1UL, poly9[i]);
                }
            }
            Assert.AreEqual(-0xD4EB, encoder.DecodeInt32(poly9));

            var poly10 = encoder.Encode(-30724);

            Assert.AreEqual(11, poly10.SignificantCoeffCount());
            Assert.AreEqual(0xFFFFUL, poly10[10]);
            Assert.AreEqual(1UL, poly10[9]);
            Assert.AreEqual(1UL, poly10[8]);
            Assert.AreEqual(1UL, poly10[7]);
            Assert.IsTrue(poly10[6] == 0);
            Assert.IsTrue(poly10[5] == 0);
            Assert.AreEqual(0xFFFFUL, poly10[4]);
            Assert.AreEqual(0xFFFFUL, poly10[3]);
            Assert.IsTrue(poly10[2] == 0);
            Assert.AreEqual(1UL, poly10[1]);
            Assert.AreEqual(0xFFFFUL, poly10[0]);
            Assert.AreEqual(-30724, encoder.DecodeInt32(poly10));

            modulus.Set(0xFFFF);
            var encoder2 = new BalancedEncoder(modulus, 7, MemoryPoolHandle.New());
            var poly12   = new Plaintext(6);

            poly12[0] = 1;
            poly12[1] = 0xFFFE; // -1
            poly12[2] = 0xFFFD; // -2
            poly12[3] = 0x8000; // -32767
            poly12[4] = 0x7FFF; // 32767
            poly12[5] = 0x7FFE; // 32766
            Assert.AreEqual(1 + -1 * 7 + -2 * 49 + -32767 * 343 + 32767 * 2401 + 32766 * 16807, encoder2.DecodeInt32(poly12));

            var encoder4 = new BalancedEncoder(modulus, 6, MemoryPoolHandle.New());

            poly8    = new Plaintext(4);
            poly8[0] = 5;
            poly8[1] = 4;
            poly8[2] = 3;
            poly8[3] = (modulus.Value - 2);
            Int32 value = 5 + 4 * 6 + 3 * 36 - 2 * 216;

            Assert.AreEqual(value, encoder4.DecodeInt32(poly8));

            var encoder5 = new BalancedEncoder(modulus, 10, MemoryPoolHandle.New());

            poly9    = new Plaintext(4);
            poly9[0] = 1;
            poly9[1] = 2;
            poly9[2] = 3;
            poly9[3] = 4;
            value    = 4321;
            Assert.AreEqual(value, encoder5.DecodeInt32(poly9));

            value = -1234;
            poly10.Set(encoder2.Encode(value));
            Assert.AreEqual(5, poly10.SignificantCoeffCount());
            Assert.IsTrue(value.Equals(encoder2.DecodeInt32(poly10)));

            value = -1234;
            var poly11 = encoder4.Encode(value);

            Assert.AreEqual(5, poly11.SignificantCoeffCount());
            Assert.AreEqual(value, encoder4.DecodeInt32(poly11));

            value = -1234;
            poly12.Set(encoder5.Encode(value));
            Assert.AreEqual(4, poly12.SignificantCoeffCount());
            Assert.IsTrue(value.Equals(encoder5.DecodeInt32(poly12)));
        }
Exemplo n.º 29
0
 public void ConstructorFail2Test()
 {
     // Should fail if value is larger than 62 bits
     SmallModulus sm = new SmallModulus(0x7FFFFFFFFFFFFFFFul);
 }
Exemplo n.º 30
0
        public void BalancedEncodeDecodeUInt32NET()
        {
            var modulus = new SmallModulus(0x10000);
            var encoder = new BalancedEncoder(modulus, 3, MemoryPoolHandle.New());

            var poly = encoder.Encode(0U);

            Assert.AreEqual(0, poly.SignificantCoeffCount());
            Assert.IsTrue(poly.IsZero);
            Assert.AreEqual(0U, encoder.DecodeUInt32(poly));

            var poly1 = encoder.Encode(1U);

            Assert.AreEqual(1, poly1.SignificantCoeffCount());
            Assert.AreEqual("1", poly1.ToString());
            Assert.AreEqual(1U, encoder.DecodeUInt32(poly1));

            var poly2 = encoder.Encode(2U);

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

            var poly3 = encoder.Encode(3U);

            Assert.AreEqual(2, poly3.SignificantCoeffCount());
            Assert.AreEqual("1x^1", poly3.ToString());
            Assert.AreEqual(3U, encoder.DecodeUInt32(poly3));

            var poly4 = encoder.Encode(0x2671U);

            Assert.AreEqual(9, poly4.SignificantCoeffCount());
            for (int i = 0; i < 9; ++i)
            {
                Assert.AreEqual(1UL, poly4[i]);
            }
            Assert.AreEqual(0x2671U, encoder.DecodeUInt32(poly4));

            var poly5 = encoder.Encode(0xD4EBU);

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

            var poly6 = new Plaintext(3);

            poly6[0] = 1;
            poly6[1] = 500;
            poly6[2] = 1023;
            Assert.AreEqual(1U + 500 * 3 + 1023 * 9, encoder.DecodeUInt32(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)
            Assert.AreEqual((UInt32)(123 + -1 * 7 + 511 * 49 + 1 * 343), encoder2.DecodeUInt32(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;
            UInt64 value = 5 + 4 * 6 + 3 * 36 + 2 * 216;

            Assert.AreEqual(value, encoder3.DecodeUInt32(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    = 4321;
            Assert.AreEqual(value, encoder4.DecodeUInt32(poly9));

            value = 1234;
            var poly10 = encoder2.Encode(value);

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

            value = 1234;
            var poly11 = encoder3.Encode(value);

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

            value = 1234;
            var poly12 = encoder4.Encode(value);

            Assert.AreEqual(4, poly12.SignificantCoeffCount());
            Assert.IsTrue(value.Equals(encoder4.DecodeUInt32(poly12)));
        }