Exemplo n.º 1
0
        public void BatchUnbatchPlaintextNET()
        {
            var parms = new EncryptionParameters();

            parms.PolyModulus  = "1x^64 + 1";
            parms.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods60Bit(0)
            };
            parms.PlainModulus = 257;

            var context = new SEALContext(parms);

            Assert.IsTrue(context.Qualifiers.EnableBatching);

            var crtbuilder = new PolyCRTBuilder(context);

            Assert.AreEqual(64, crtbuilder.SlotCount);
            var plain = new Plaintext(crtbuilder.SlotCount);

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain[i] = (UInt64)i;
            }

            crtbuilder.Compose(plain);
            crtbuilder.Decompose(plain);
            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(plain[i] == (UInt64)i);
            }

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain[i] = (UInt64)5;
            }
            crtbuilder.Compose(plain);
            Assert.IsTrue(plain.ToString().Equals("5"));
            crtbuilder.Decompose(plain);
            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(plain[i] == (UInt64)5);
            }

            var short_plain = new Plaintext(20);

            for (int i = 0; i < 20; i++)
            {
                short_plain[i] = (UInt64)i;
            }
            crtbuilder.Compose(short_plain);
            crtbuilder.Decompose(short_plain);
            for (int i = 0; i < 20; i++)
            {
                Assert.IsTrue(short_plain[i] == (UInt64)i);
            }
            for (int i = 20; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(short_plain[i] == 0UL);
            }
        }
Exemplo n.º 2
0
        public void KeyStepTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = new List <SmallModulus>()
                {
                    DefaultParams.SmallMods60Bit(0)
                }
            };
            SEALContext  context = SEALContext.Create(parms);
            KeyGenerator keygen  = new KeyGenerator(context);

            GaloisKeys keys = keygen.GaloisKeys(decompositionBitCount: 15, steps: new int[] { 1, 2, 3 });

            Assert.IsNotNull(keys);

            Assert.AreEqual(15, keys.DecompositionBitCount);
            Assert.AreEqual(3ul, keys.Size);

            Assert.IsFalse(keys.HasKey(1));
            Assert.IsTrue(keys.HasKey(3));
            Assert.IsFalse(keys.HasKey(5));
            Assert.IsFalse(keys.HasKey(7));
            Assert.IsTrue(keys.HasKey(9));
            Assert.IsFalse(keys.HasKey(11));
            Assert.IsFalse(keys.HasKey(13));
            Assert.IsFalse(keys.HasKey(15));
            Assert.IsFalse(keys.HasKey(17));
            Assert.IsFalse(keys.HasKey(19));
            Assert.IsFalse(keys.HasKey(21));
            Assert.IsFalse(keys.HasKey(23));
            Assert.IsFalse(keys.HasKey(25));
            Assert.IsTrue(keys.HasKey(27));
        }
Exemplo n.º 3
0
        public void SmallMods60BitTest()
        {
            SmallModulus sm = DefaultParams.SmallMods60Bit(45);

            Assert.IsNotNull(sm);
            Assert.AreEqual(60, sm.BitCount);
            Assert.AreEqual(0x0ffffffff1740001ul, sm.Value);
        }
        public void FVKeyGenerationNET()
        {
            var parms = new EncryptionParameters();
            {
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^64 + 1";
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0)
                };
                parms.PlainModulus = 1 << 6;

                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                Assert.IsTrue(keygen.PublicKey.HashBlock.Equals(parms.HashBlock));
                Assert.IsTrue(keygen.SecretKey.HashBlock.Equals(parms.HashBlock));

                var evk = new EvaluationKeys();
                keygen.GenerateEvaluationKeys(60, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(2, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(30, 1, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(4, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(2, 2, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(60, evk.Key(2)[0].Size);
            }
            {
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^256 + 1";
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0), DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods30Bit(1)
                };
                parms.PlainModulus = 1 << 6;

                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                Assert.AreEqual(keygen.PublicKey.HashBlock, parms.HashBlock);
                Assert.AreEqual(keygen.SecretKey.HashBlock, parms.HashBlock);

                var evk = new EvaluationKeys();
                keygen.GenerateEvaluationKeys(60, 2, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(2, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(30, 2, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(4, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(4, 1, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(30, evk.Key(2)[0].Size);
            }
        }
        public void EncryptionParametersSaveLoadNET()
        {
            var stream = new MemoryStream();
            var parms  = new EncryptionParameters();
            var parms2 = new EncryptionParameters();

            parms.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            parms2.Load(stream);
            Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation);
            Assert.AreEqual(parms.NoiseMaxDeviation, parms2.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus.SequenceEqual(parms2.CoeffModulus));
            Assert.IsTrue(parms.PlainModulus.Equals(parms2.PlainModulus));
            Assert.IsTrue(parms.PolyModulus.Equals(parms2.PolyModulus));
            Assert.IsTrue(parms.Equals(parms2));

            parms.NoiseStandardDeviation = 3.19;
            parms.CoeffModulus           = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(0)
            };
            parms.PlainModulus = 1 << 6;
            parms.PolyModulus  = "1x^64 + 1";
            stream.Seek(0, SeekOrigin.Begin);
            parms.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            parms2.Load(stream);
            Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation);
            Assert.AreEqual(parms.NoiseMaxDeviation, parms2.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus.SequenceEqual(parms2.CoeffModulus));
            Assert.IsTrue(parms.PlainModulus.Equals(parms2.PlainModulus));
            Assert.IsTrue(parms.PolyModulus.Equals(parms2.PolyModulus));
            Assert.IsTrue(parms.Equals(parms2));

            parms.NoiseStandardDeviation = 3.19;
            parms.CoeffModulus           = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(0),
                DefaultParams.SmallMods60Bit(0),
                DefaultParams.SmallMods60Bit(1)
            };
            parms.PlainModulus = 1 << 30;
            parms.PolyModulus  = "1x^256 + 1";
            stream.Seek(0, SeekOrigin.Begin);
            parms.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            parms2.Load(stream);
            Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation);
            Assert.AreEqual(parms.NoiseMaxDeviation, parms2.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus.SequenceEqual(parms2.CoeffModulus));
            Assert.IsTrue(parms.PlainModulus.Equals(parms2.PlainModulus));
            Assert.IsTrue(parms.PolyModulus.Equals(parms2.PolyModulus));
            Assert.IsTrue(parms.Equals(parms2));
        }
Exemplo n.º 6
0
        public void SaveLoadPublicKeyNET()
        {
            var stream = new MemoryStream();
            {
                var parms = new EncryptionParameters();
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^64 + 1";
                parms.PlainModulus           = 1 << 6;
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0)
                };

                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                var pk = keygen.PublicKey;
                Assert.IsTrue(pk.HashBlock.Equals(parms.HashBlock));
                stream.Seek(0, SeekOrigin.Begin);
                pk.Save(stream);

                var pk2 = new PublicKey();
                stream.Seek(0, SeekOrigin.Begin);
                pk2.Load(stream);
                Assert.AreEqual(pk.Data, pk2.Data);
                Assert.AreEqual(pk.HashBlock, pk2.HashBlock);
            }
            {
                var parms = new EncryptionParameters();
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^256 + 1";
                parms.PlainModulus           = 1 << 20;
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods40Bit(0)
                };

                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                var pk = keygen.PublicKey;
                Assert.IsTrue(pk.HashBlock.Equals(parms.HashBlock));
                stream.Seek(0, SeekOrigin.Begin);
                pk.Save(stream);

                var pk2 = new PublicKey();
                stream.Seek(0, SeekOrigin.Begin);
                pk2.Load(stream);
                Assert.AreEqual(pk.Data, pk2.Data);
                Assert.AreEqual(pk.HashBlock, pk2.HashBlock);
            }
        }
Exemplo n.º 7
0
        public void ExceptionsTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>()
            {
                DefaultParams.SmallMods60Bit(0)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = coeffModulus,
                PlainModulus      = new SmallModulus(257)
            };

            SEALContext      context     = SEALContext.Create(parms);
            BatchEncoder     enc         = new BatchEncoder(context);
            List <ulong>     valu        = new List <ulong>();
            List <ulong>     valu_null   = null;
            List <long>      vall        = new List <long>();
            List <long>      vall_null   = null;
            Plaintext        plain       = new Plaintext();
            Plaintext        plain_null  = null;
            MemoryPoolHandle pool_uninit = new MemoryPoolHandle();

            Assert.ThrowsException <ArgumentNullException>(() => enc = new BatchEncoder(null));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(valu, plain_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(valu_null, plain));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(vall, plain_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(vall_null, plain));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(plain_null));
            Assert.ThrowsException <ArgumentException>(() => enc.Encode(plain, pool_uninit));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain, valu_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null, valu));
            Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, valu, pool_uninit));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain, vall_null));
            Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null, vall));
            Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, vall, pool_uninit));

            Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null));
            Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, pool_uninit));
        }
Exemplo n.º 8
0
        public void EncodeInPlaceTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>()
            {
                DefaultParams.SmallMods60Bit(0)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = coeffModulus,
                PlainModulus      = new SmallModulus(257)
            };

            SEALContext context = SEALContext.Create(parms);

            BatchEncoder encoder = new BatchEncoder(context);

            Assert.AreEqual(64ul, encoder.SlotCount);

            Plaintext plain = new Plaintext("6x^5 + 5x^4 + 4x^3 + 3x^2 + 2x^1 + 1");

            Assert.AreEqual(6ul, plain.CoeffCount);

            encoder.Encode(plain);

            Assert.AreEqual(64ul, plain.CoeffCount);

            encoder.Decode(plain);
            Assert.AreEqual(64ul, plain.CoeffCount);

            for (ulong i = 0; i < 6; i++)
            {
                Assert.AreEqual((i + 1), plain[i]);
            }

            for (ulong i = 6; i < plain.CoeffCount; i++)
            {
                Assert.AreEqual(0ul, plain[i]);
            }
        }
Exemplo n.º 9
0
        public void EncodeDecodeVectorTest()
        {
            int slots = 32;
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);

            parms.PolyModulusDegree = (ulong)slots * 2;
            List <SmallModulus> coeffModulus = new List <SmallModulus>(4);

            coeffModulus.Add(DefaultParams.SmallMods60Bit(0));
            coeffModulus.Add(DefaultParams.SmallMods60Bit(1));
            coeffModulus.Add(DefaultParams.SmallMods60Bit(2));
            coeffModulus.Add(DefaultParams.SmallMods60Bit(3));
            parms.CoeffModulus = coeffModulus;
            SEALContext context = SEALContext.Create(parms);
            CKKSEncoder encoder = new CKKSEncoder(context);

            List <Complex> values    = new List <Complex>(slots);
            Random         rnd       = new Random();
            int            dataBound = 1 << 30;
            double         delta     = 1ul << 40;

            for (int i = 0; i < slots; i++)
            {
                values.Add(new Complex(rnd.Next() % dataBound, 0));
            }

            Plaintext plain = new Plaintext();

            encoder.Encode(values, delta, plain);

            List <Complex> result = new List <Complex>();

            encoder.Decode(plain, result);

            for (int i = 0; i < slots; i++)
            {
                double tmp = Math.Abs(values[i].Real - result[i].Real);
                Assert.IsTrue(tmp < 0.5);
            }
        }
Exemplo n.º 10
0
        public void BFVKeyGenerationNET()
        {
            var parms = new EncryptionParameters();
            {
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^64 + 1";
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0)
                };
                parms.PlainModulus = 1 << 6;

                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                Assert.IsTrue(keygen.PublicKey.HashBlock.Equals(parms.HashBlock));
                Assert.IsTrue(keygen.SecretKey.HashBlock.Equals(parms.HashBlock));

                var evk = new EvaluationKeys();
                keygen.GenerateEvaluationKeys(60, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(2, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(30, 1, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(4, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(2, 2, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(60, evk.Key(2)[0].Size);

                var galks = new GaloisKeys();
                keygen.GenerateGaloisKeys(60, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.AreEqual(2, galks.Key(3)[0].Size);
                Assert.AreEqual(10, galks.Size);

                keygen.GenerateGaloisKeys(30, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.AreEqual(4, galks.Key(3)[0].Size);
                Assert.AreEqual(10, galks.Size);

                keygen.GenerateGaloisKeys(2, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.AreEqual(60, galks.Key(3)[0].Size);
                Assert.AreEqual(10, galks.Size);

                keygen.GenerateGaloisKeys(60, new List <UInt64> {
                    1, 3, 5, 7
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(3));
                Assert.IsTrue(galks.HasKey(5));
                Assert.IsTrue(galks.HasKey(7));
                Assert.IsFalse(galks.HasKey(9));
                Assert.IsFalse(galks.HasKey(127));
                Assert.AreEqual(2, galks.Key(1)[0].Size);
                Assert.AreEqual(2, galks.Key(3)[0].Size);
                Assert.AreEqual(2, galks.Key(5)[0].Size);
                Assert.AreEqual(2, galks.Key(7)[0].Size);
                Assert.AreEqual(4, galks.Size);

                keygen.GenerateGaloisKeys(30, new List <UInt64> {
                    1, 3, 5, 7
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(3));
                Assert.IsTrue(galks.HasKey(5));
                Assert.IsTrue(galks.HasKey(7));
                Assert.IsFalse(galks.HasKey(9));
                Assert.IsFalse(galks.HasKey(127));
                Assert.AreEqual(4, galks.Key(1)[0].Size);
                Assert.AreEqual(4, galks.Key(3)[0].Size);
                Assert.AreEqual(4, galks.Key(5)[0].Size);
                Assert.AreEqual(4, galks.Key(7)[0].Size);
                Assert.AreEqual(4, galks.Size);

                keygen.GenerateGaloisKeys(2, new List <UInt64> {
                    1, 3, 5, 7
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(3));
                Assert.IsTrue(galks.HasKey(5));
                Assert.IsTrue(galks.HasKey(7));
                Assert.IsFalse(galks.HasKey(9));
                Assert.IsFalse(galks.HasKey(127));
                Assert.AreEqual(60, galks.Key(1)[0].Size);
                Assert.AreEqual(60, galks.Key(3)[0].Size);
                Assert.AreEqual(60, galks.Key(5)[0].Size);
                Assert.AreEqual(60, galks.Key(7)[0].Size);
                Assert.AreEqual(4, galks.Size);

                keygen.GenerateGaloisKeys(30, new List <UInt64> {
                    1
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsFalse(galks.HasKey(3));
                Assert.IsFalse(galks.HasKey(127));
                Assert.AreEqual(4, galks.Key(1)[0].Size);
                Assert.AreEqual(1, galks.Size);

                keygen.GenerateGaloisKeys(30, new List <UInt64> {
                    127
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsFalse(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(127));
                Assert.AreEqual(4, galks.Key(127)[0].Size);
                Assert.AreEqual(1, galks.Size);
            }
            {
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^256 + 1";
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0), DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods30Bit(1)
                };
                parms.PlainModulus = 1 << 6;

                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                Assert.AreEqual(keygen.PublicKey.HashBlock, parms.HashBlock);
                Assert.AreEqual(keygen.SecretKey.HashBlock, parms.HashBlock);

                var evk = new EvaluationKeys();
                keygen.GenerateEvaluationKeys(60, 2, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(2, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(30, 2, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(4, evk.Key(2)[0].Size);

                keygen.GenerateEvaluationKeys(4, 1, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(30, evk.Key(2)[0].Size);

                var galks = new GaloisKeys();
                keygen.GenerateGaloisKeys(60, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.AreEqual(2, galks.Key(3)[0].Size);
                Assert.AreEqual(14, galks.Size);

                keygen.GenerateGaloisKeys(30, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.AreEqual(4, galks.Key(3)[0].Size);
                Assert.AreEqual(14, galks.Size);

                keygen.GenerateGaloisKeys(2, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.AreEqual(60, galks.Key(3)[0].Size);
                Assert.AreEqual(14, galks.Size);

                keygen.GenerateGaloisKeys(60, new List <UInt64> {
                    1, 3, 5, 7
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(3));
                Assert.IsTrue(galks.HasKey(5));
                Assert.IsTrue(galks.HasKey(7));
                Assert.IsFalse(galks.HasKey(9));
                Assert.IsFalse(galks.HasKey(511));
                Assert.AreEqual(2, galks.Key(1)[0].Size);
                Assert.AreEqual(2, galks.Key(3)[0].Size);
                Assert.AreEqual(2, galks.Key(5)[0].Size);
                Assert.AreEqual(2, galks.Key(7)[0].Size);
                Assert.AreEqual(4, galks.Size);

                keygen.GenerateGaloisKeys(30, new List <UInt64> {
                    1, 3, 5, 7
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(3));
                Assert.IsTrue(galks.HasKey(5));
                Assert.IsTrue(galks.HasKey(7));
                Assert.IsFalse(galks.HasKey(9));
                Assert.IsFalse(galks.HasKey(511));
                Assert.AreEqual(4, galks.Key(1)[0].Size);
                Assert.AreEqual(4, galks.Key(3)[0].Size);
                Assert.AreEqual(4, galks.Key(5)[0].Size);
                Assert.AreEqual(4, galks.Key(7)[0].Size);
                Assert.AreEqual(4, galks.Size);

                keygen.GenerateGaloisKeys(2, new List <UInt64> {
                    1, 3, 5, 7
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(3));
                Assert.IsTrue(galks.HasKey(5));
                Assert.IsTrue(galks.HasKey(7));
                Assert.IsFalse(galks.HasKey(9));
                Assert.IsFalse(galks.HasKey(511));
                Assert.AreEqual(60, galks.Key(1)[0].Size);
                Assert.AreEqual(60, galks.Key(3)[0].Size);
                Assert.AreEqual(60, galks.Key(5)[0].Size);
                Assert.AreEqual(60, galks.Key(7)[0].Size);
                Assert.AreEqual(4, galks.Size);

                keygen.GenerateGaloisKeys(30, new List <UInt64> {
                    1
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsTrue(galks.HasKey(1));
                Assert.IsFalse(galks.HasKey(3));
                Assert.IsFalse(galks.HasKey(511));
                Assert.AreEqual(4, galks.Key(1)[0].Size);
                Assert.AreEqual(1, galks.Size);

                keygen.GenerateGaloisKeys(30, new List <UInt64> {
                    511
                }, galks);
                Assert.AreEqual(galks.HashBlock, parms.HashBlock);
                Assert.IsFalse(galks.HasKey(1));
                Assert.IsTrue(galks.HasKey(511));
                Assert.AreEqual(4, galks.Key(511)[0].Size);
                Assert.AreEqual(1, galks.Size);
            }
        }
Exemplo n.º 11
0
        public void GaloisKeysSaveLoadNET()
        {
            var stream = new MemoryStream();
            {
                var parms = new EncryptionParameters();
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^64 + 1";
                parms.PlainModulus           = 65537;
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0)
                };
                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                var keys = new GaloisKeys();
                Assert.AreEqual(keys.DecompositionBitCount, 0);
                var test_keys = new GaloisKeys();
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                Assert.AreEqual(0, keys.Size);

                keygen.GenerateGaloisKeys(1, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 1);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Data[j].Count; i++)
                    {
                        Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size);
                        Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count);
                    }
                }
                Assert.AreEqual(10, keys.Size);

                keygen.GenerateGaloisKeys(8, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 8);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Data[j].Count; i++)
                    {
                        Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size);
                        Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count);
                    }
                }
                Assert.AreEqual(10, keys.Size);

                keygen.GenerateGaloisKeys(60, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 60);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Data[j].Count; i++)
                    {
                        Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size);
                        Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count);
                    }
                }
                Assert.AreEqual(10, keys.Size);
            }
            {
                var parms = new EncryptionParameters();
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus            = "1x^256 + 1";
                parms.PlainModulus           = 65537;
                parms.CoeffModulus           = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0), DefaultParams.SmallMods50Bit(0)
                };
                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                var keys = new GaloisKeys();
                Assert.AreEqual(keys.DecompositionBitCount, 0);
                var test_keys = new GaloisKeys();
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                Assert.AreEqual(0, keys.Size);

                keygen.GenerateGaloisKeys(8, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 8);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Data[j].Count; i++)
                    {
                        Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size);
                        Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count);
                    }
                }
                Assert.AreEqual(14, keys.Size);

                keygen.GenerateGaloisKeys(60, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 60);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.AreEqual(keys.HashBlock, test_keys.HashBlock);
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Data[j].Count; i++)
                    {
                        Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size);
                        Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count);
                    }
                }
                Assert.AreEqual(14, keys.Size);
            }
        }
Exemplo n.º 12
0
        public void FVEncryptDecryptNET()
        {
            var parms         = new EncryptionParameters();
            var plain_modulus = new SmallModulus(1 << 6);

            parms.NoiseStandardDeviation = 3.19;
            parms.PlainModulus           = plain_modulus;
            {
                parms.PolyModulus  = "1x^64 + 1";
                parms.CoeffModulus = new List <SmallModulus> {
                    DefaultParams.SmallMods60Bit(0)
                };
                var context = new SEALContext(parms);

                var keygen  = new KeyGenerator(context);
                var encoder = new BalancedEncoder(plain_modulus);

                var encryptor = new Encryptor(context, keygen.PublicKey);
                var decryptor = new Decryptor(context, keygen.SecretKey);

                var encrypted = new Ciphertext();
                var plain     = new Plaintext();
                encryptor.Encrypt(encoder.Encode(0x12345678), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x12345678UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(1), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(1UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(2), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(2UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFD), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFDUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFE), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFEUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFF), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(314159265), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(314159265UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);
            }
            {
                parms.PolyModulus  = "1x^128 + 1";
                parms.CoeffModulus = new List <SmallModulus> {
                    DefaultParams.SmallMods40Bit(0), DefaultParams.SmallMods40Bit(1)
                };
                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                var encoder = new BalancedEncoder(plain_modulus);

                var encryptor = new Encryptor(context, keygen.PublicKey);
                var decryptor = new Decryptor(context, keygen.SecretKey);

                var encrypted = new Ciphertext();
                var plain     = new Plaintext();
                encryptor.Encrypt(encoder.Encode(0x12345678), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x12345678UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(1), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(1UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(2), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(2UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFD), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFDUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFE), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFEUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFF), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(314159265), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(314159265UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);
            }

            {
                parms.PolyModulus  = "1x^256 + 1";
                parms.CoeffModulus = new List <SmallModulus> {
                    DefaultParams.SmallMods40Bit(0), DefaultParams.SmallMods40Bit(1), DefaultParams.SmallMods40Bit(2)
                };
                var context = new SEALContext(parms);
                var keygen  = new KeyGenerator(context);

                var encoder = new BalancedEncoder(plain_modulus);

                var encryptor = new Encryptor(context, keygen.PublicKey);
                var decryptor = new Decryptor(context, keygen.SecretKey);

                var encrypted = new Ciphertext();
                var plain     = new Plaintext();
                encryptor.Encrypt(encoder.Encode(0x12345678), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x12345678UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(1), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(1UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(2), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(2UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFD), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFDUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFE), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFEUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(0x7FFFFFFFFFFFFFFF), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);

                encryptor.Encrypt(encoder.Encode(314159265), encrypted);
                decryptor.Decrypt(encrypted, plain);
                Assert.AreEqual(314159265UL, encoder.DecodeUInt64(plain));
                Assert.AreEqual(encrypted.HashBlock, parms.HashBlock);
            }
        }
Exemplo n.º 13
0
        public void EvaluationKeysSaveLoadNET()
        {
            var stream = new MemoryStream();
            {
                var parms = new EncryptionParameters();
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus = "1x^64 + 1";
                parms.PlainModulus = 1 << 6;
                parms.CoeffModulus = new List<SmallModulus> { DefaultParams.SmallMods60Bit(0) };
                var context = new SEALContext(parms);
                var keygen = new KeyGenerator(context);

                var keys = new EvaluationKeys();
                Assert.AreEqual(keys.DecompositionBitCount, 0);
                var test_keys = new EvaluationKeys();
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                Assert.AreEqual(0, keys.Size);

                keygen.GenerateEvaluationKeys(1, 1, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 1);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }

                keygen.GenerateEvaluationKeys(2, 1, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 2);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }

                keygen.GenerateEvaluationKeys(59, 2, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 59);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }

                keygen.GenerateEvaluationKeys(60, 5, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 60);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }
            }
            {
                var parms = new EncryptionParameters();
                parms.NoiseStandardDeviation = 3.19;
                parms.PolyModulus = "1x^256 + 1";
                parms.PlainModulus = 1 << 6;
                parms.CoeffModulus = new List<SmallModulus> {
                    DefaultParams.SmallMods60Bit(0), DefaultParams.SmallMods50Bit(0) };
                var context = new SEALContext(parms);
                var keygen = new KeyGenerator(context);

                var keys = new EvaluationKeys();
                Assert.AreEqual(keys.DecompositionBitCount, 0);
                var test_keys = new EvaluationKeys();
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                Assert.AreEqual(0, keys.Size);

                keygen.GenerateEvaluationKeys(8, 1, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 8);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }

                keygen.GenerateEvaluationKeys(8, 2, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 8);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }

                keygen.GenerateEvaluationKeys(59, 2, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 59);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }

                keygen.GenerateEvaluationKeys(60, 5, keys);
                Assert.AreEqual(keys.DecompositionBitCount, 60);
                stream.Seek(0, SeekOrigin.Begin);
                keys.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                test_keys.Load(stream);
                Assert.AreEqual(keys.Size, test_keys.Size);
                Assert.IsTrue(keys.HashBlock.Equals(test_keys.HashBlock));
                Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount);
                for (int j = 0; j < test_keys.Size; j++)
                {
                    for (int i = 0; i < test_keys.Key(j + 2).Count; i++)
                    {
                        Assert.AreEqual(keys.Key(j + 2)[i].Size, test_keys.Key(j + 2)[i].Size);
                        Assert.AreEqual(keys.Key(j + 2)[i].UInt64Count, test_keys.Key(j + 2)[i].UInt64Count);
                    }
                }
            }
        }
Exemplo n.º 14
0
        public void EncodeLongTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV);

            parms.PolyModulusDegree = 64;
            List <SmallModulus> coeffModulus = new List <SmallModulus>();

            coeffModulus.Add(DefaultParams.SmallMods60Bit(0));
            parms.CoeffModulus = coeffModulus;
            parms.PlainModulus = new SmallModulus(257);

            SEALContext context = SEALContext.Create(parms);

            BatchEncoder encoder = new BatchEncoder(context);

            Assert.AreEqual(64ul, encoder.SlotCount);

            List <long> plainList = new List <long>();

            for (ulong i = 0; i < encoder.SlotCount; i++)
            {
                plainList.Add((long)i);
            }

            Plaintext plain = new Plaintext();

            encoder.Encode(plainList, plain);

            List <long> plainList2 = new List <long>();

            encoder.Decode(plain, plainList2);

            for (ulong i = 0; i < encoder.SlotCount; i++)
            {
                Assert.AreEqual(plainList[(int)i], plainList2[(int)i]);
            }

            for (ulong i = 0; i < encoder.SlotCount; i++)
            {
                plainList[(int)i] = 5;
            }

            encoder.Encode(plainList, plain);
            Assert.AreEqual("5", plain.ToString());

            encoder.Decode(plain, plainList2);

            for (ulong i = 0; i < encoder.SlotCount; i++)
            {
                Assert.AreEqual(plainList[(int)i], plainList2[(int)i]);
            }

            List <long> shortList = new List <long>();

            for (int i = 0; i < 20; i++)
            {
                shortList.Add((long)i);
            }

            encoder.Encode(shortList, plain);

            List <long> shortList2 = new List <long>();

            encoder.Decode(plain, shortList2);

            Assert.AreEqual(20, shortList.Count);
            Assert.AreEqual(64, shortList2.Count);

            for (int i = 0; i < 20; i++)
            {
                Assert.AreEqual(shortList[i], shortList2[i]);
            }

            for (ulong i = 20; i < encoder.SlotCount; i++)
            {
                Assert.AreEqual(0L, shortList2[(int)i]);
            }
        }
Exemplo n.º 15
0
 public void SmallMods60BitFailTest()
 {
     SmallModulus sm = DefaultParams.SmallMods60Bit(64);
 }
Exemplo n.º 16
0
        public void BatchUnbatchIntVectorNET()
        {
            var parms = new EncryptionParameters();

            parms.PolyModulus  = "1x^64 + 1";
            parms.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods60Bit(0)
            };
            parms.PlainModulus = 257;

            var context = new SEALContext(parms);

            Assert.IsTrue(context.Qualifiers.EnableBatching);

            var crtbuilder = new PolyCRTBuilder(context);

            Assert.AreEqual(64, crtbuilder.SlotCount);
            var plain_vec = new List <Int64>();

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain_vec.Add((Int64)i * (1 - 2 * (i % 2)));
            }

            var plain = new Plaintext();

            crtbuilder.Compose(plain_vec, plain);
            var plain_vec2 = new List <Int64>();

            crtbuilder.Decompose(plain, plain_vec2);
            Assert.IsTrue(plain_vec.SequenceEqual(plain_vec2));

            for (int i = 0; i < crtbuilder.SlotCount; i++)
            {
                plain_vec[i] = -5;
            }
            crtbuilder.Compose(plain_vec, plain);
            Assert.IsTrue(plain.ToString().Equals("FC"));
            crtbuilder.Decompose(plain, plain_vec2);
            Assert.IsTrue(plain_vec.SequenceEqual(plain_vec2));

            var short_plain_vec = new List <Int64>();

            for (int i = 0; i < 20; i++)
            {
                short_plain_vec.Add((Int64)i * (1 - 2 * (i % 2)));
            }
            crtbuilder.Compose(short_plain_vec, plain);
            var short_plain_vec2 = new List <Int64>();

            crtbuilder.Decompose(plain, short_plain_vec2);
            Assert.AreEqual(20, short_plain_vec.Count);
            Assert.AreEqual(64, short_plain_vec2.Count);
            for (int i = 0; i < 20; i++)
            {
                Assert.AreEqual(short_plain_vec[i], short_plain_vec2[i]);
            }
            for (int i = 20; i < crtbuilder.SlotCount; i++)
            {
                Assert.AreEqual(0L, short_plain_vec2[i]);
            }
        }