public void BalancedEncodeDecodeBigUIntNET()
        {
            var modulus = new BigUInt("10000");
            var encoder = new BalancedEncoder(modulus, 3);

            var value = new BigUInt(64);

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

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

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

            Assert.AreEqual(1, poly1.GetSignificantCoeffCount());
            Assert.AreEqual(modulus.BitCount, poly1.CoeffBitCount);
            Assert.AreEqual("1", poly1.ToString());
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly1));

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

            Assert.AreEqual(2, poly2.GetSignificantCoeffCount());
            Assert.AreEqual(modulus.BitCount, poly2.CoeffBitCount);
            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.GetSignificantCoeffCount());
            Assert.AreEqual(modulus.BitCount, poly3.CoeffBitCount);
            Assert.AreEqual("1x^1", poly3.ToString());
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly3));

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

            Assert.AreEqual(9, poly4.GetSignificantCoeffCount());
            Assert.AreEqual(modulus.BitCount, poly4.CoeffBitCount);
            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.GetSignificantCoeffCount());
            Assert.AreEqual(modulus.BitCount, poly5.CoeffBitCount);
            for (int i = 0; i < 11; ++i)
            {
                if (i % 3 == 1)
                {
                    Assert.AreEqual("1", poly5[i].ToString());
                }
                else if (i % 3 == 0)
                {
                    Assert.IsTrue(poly5[i].IsZero);
                }
                else
                {
                    Assert.AreEqual("FFFF", poly5[i].ToString());
                }
            }
            Assert.AreEqual(value, encoder.DecodeBigUInt(poly5));

            var poly6 = new BigPoly(3, 10);

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

            var encoder2 = new BalancedEncoder(modulus, 7);
            var poly7    = new BigPoly(4, 16);

            poly7[0].Set(123);    // 123   (*1)
            poly7[1].Set("FFFF"); // -1 (*7)
            poly7[2].Set(511);    // 511  (*49)
            poly7[3].Set(1);      // 1    (*343)
            value.Set(123 + -1 * 7 + 511 * 49 + 1 * 343);
            Assert.AreEqual(value, encoder2.DecodeBigUInt(poly7));
        }
Beispiel #2
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)));
        }