Exemplo n.º 1
0
        public void KeyEltTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);

            GaloisKeys keys = keygen.GaloisKeys(galoisElts: new ulong[] { 1, 3 });

            Assert.IsNotNull(keys);

            Assert.AreEqual(2ul, keys.Size);

            Assert.IsTrue(keys.HasKey(1));
            Assert.IsTrue(keys.HasKey(3));
            Assert.IsFalse(keys.HasKey(5));
        }
Exemplo n.º 2
0
        public void KeyTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);

            GaloisKeys       keys   = keygen.GaloisKeys();
            MemoryPoolHandle handle = keys.Pool;

            Assert.IsNotNull(keys);
            Assert.AreEqual(24ul, keys.Size);

            Assert.IsFalse(keys.HasKey(galoisElt: 1));
            Assert.IsTrue(keys.HasKey(galoisElt: 3));
            Assert.IsFalse(keys.HasKey(galoisElt: 5));
            Assert.IsFalse(keys.HasKey(galoisElt: 7));
            Assert.IsTrue(keys.HasKey(galoisElt: 9));
            Assert.IsFalse(keys.HasKey(galoisElt: 11));

            IEnumerable <PublicKey> key = keys.Key(3);

            Assert.AreEqual(4, key.Count());

            IEnumerable <PublicKey> key2 = keys.Key(9);

            Assert.AreEqual(4, key2.Count());

            Assert.IsTrue(handle.AllocByteCount > 0ul);
        }
Exemplo n.º 3
0
        public void KeyTest()
        {
            SEALContext  context = GlobalContext.Context;
            KeyGenerator keygen  = new KeyGenerator(context);

            GaloisKeys       keys   = keygen.GaloisKeys(decompositionBitCount: 30);
            MemoryPoolHandle handle = keys.Pool;

            Assert.IsNotNull(keys);
            Assert.AreEqual(30, keys.DecompositionBitCount);
            Assert.AreEqual(22ul, keys.Size);

            Assert.IsFalse(keys.HasKey(galoisElt: 1));
            Assert.IsTrue(keys.HasKey(galoisElt: 3));
            Assert.IsFalse(keys.HasKey(galoisElt: 5));
            Assert.IsFalse(keys.HasKey(galoisElt: 7));
            Assert.IsTrue(keys.HasKey(galoisElt: 9));
            Assert.IsFalse(keys.HasKey(galoisElt: 11));

            IEnumerable <Ciphertext> key = keys.Key(3);

            Assert.AreEqual(2, key.Count());

            IEnumerable <Ciphertext> key2 = keys.Key(9);

            Assert.AreEqual(2, key2.Count());

            Assert.IsTrue(handle.AllocByteCount > 0ul);
        }
Exemplo n.º 4
0
        public void KeyStepTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = CoeffModulus.Create(64, new int[] { 60, 60 })
            };
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            KeyGenerator keygen = new KeyGenerator(context);

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

            Assert.IsNotNull(keys);

            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.º 5
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.º 6
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));
        }