예제 #1
0
        public void PlaintextBasicsNET()
        {
            var plain = new Plaintext(2);

            Assert.AreEqual(2, plain.Capacity);
            Assert.AreEqual(2, plain.CoeffCount);
            Assert.AreEqual(0, plain.SignificantCoeffCount());
            plain[0] = 1;
            plain[1] = 2;

            plain.Reserve(10);
            Assert.AreEqual(10, plain.Capacity);
            Assert.AreEqual(2, plain.CoeffCount);
            Assert.AreEqual(2, plain.SignificantCoeffCount());
            Assert.AreEqual(1UL, plain[0]);
            Assert.AreEqual(2UL, plain[1]);

            plain.Resize(5);
            Assert.AreEqual(10, plain.Capacity);
            Assert.AreEqual(5, plain.CoeffCount);
            Assert.AreEqual(2, plain.SignificantCoeffCount());
            Assert.AreEqual(1UL, plain[0]);
            Assert.AreEqual(2UL, plain[1]);
            Assert.AreEqual(0UL, plain[2]);
            Assert.AreEqual(0UL, plain[3]);
            Assert.AreEqual(0UL, plain[4]);
        }
예제 #2
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)));
        }