Exemplo n.º 1
0
        public void SaveLoadTest()
        {
            SEALContext  context = GlobalContext.Context;
            KeyGenerator keyGen  = new KeyGenerator(context);

            GaloisKeys keys  = keyGen.GaloisKeys(decompositionBitCount: 30);
            GaloisKeys other = new GaloisKeys();

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

            using (MemoryStream ms = new MemoryStream())
            {
                keys.Save(ms);

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

                other.Load(context, ms);
            }

            Assert.AreEqual(30, other.DecompositionBitCount);
            Assert.AreEqual(22ul, other.Size);
            Assert.IsTrue(other.IsMetadataValidFor(context));

            List <IEnumerable <Ciphertext> > keysData  = new List <IEnumerable <Ciphertext> >(keys.Data);
            List <IEnumerable <Ciphertext> > otherData = new List <IEnumerable <Ciphertext> >(other.Data);

            Assert.AreEqual(keysData.Count, otherData.Count);
            for (int i = 0; i < keysData.Count; i++)
            {
                List <Ciphertext> keysCiphers  = new List <Ciphertext>(keysData[i]);
                List <Ciphertext> otherCiphers = new List <Ciphertext>(otherData[i]);

                Assert.AreEqual(keysCiphers.Count, otherCiphers.Count);

                for (int j = 0; j < keysCiphers.Count; j++)
                {
                    Ciphertext keysCipher  = keysCiphers[j];
                    Ciphertext otherCipher = otherCiphers[j];

                    Assert.AreEqual(keysCipher.Size, otherCipher.Size);
                    Assert.AreEqual(keysCipher.PolyModulusDegree, otherCipher.PolyModulusDegree);
                    Assert.AreEqual(keysCipher.CoeffModCount, otherCipher.CoeffModCount);

                    ulong coeffCount = keysCipher.Size * keysCipher.PolyModulusDegree * keysCipher.CoeffModCount;
                    for (ulong k = 0; k < coeffCount; k++)
                    {
                        Assert.AreEqual(keysCipher[k], otherCipher[k]);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Convert a Base64 string to a GaloisKeys object
        /// </summary>
        /// <param name="b64">Base 64 string</param>
        /// <param name="context">SEALContext to verify resulting GaloisKeys is valid for the SEALContext</param>
        /// <returns>Decoded GaloisKeys</returns>
        public static GaloisKeys Base64ToGaloisKeys(string b64, SEALContext context)
        {
            GaloisKeys result = new GaloisKeys();

            byte[] bytes = Convert.FromBase64String(b64);
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                result.Load(context, ms);
            }

            return(result);
        }
Exemplo n.º 3
0
        public void SaveLoadTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keyGen  = new KeyGenerator(context);

            GaloisKeys keys  = keyGen.GaloisKeys();
            GaloisKeys other = new GaloisKeys();

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

            using (MemoryStream ms = new MemoryStream())
            {
                keys.Save(ms);

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

                other.Load(context, ms);
            }

            Assert.AreEqual(24ul, other.Size);
            Assert.IsTrue(ValCheck.IsValidFor(other, context));

            List <IEnumerable <PublicKey> > keysData  = new List <IEnumerable <PublicKey> >(keys.Data);
            List <IEnumerable <PublicKey> > otherData = new List <IEnumerable <PublicKey> >(other.Data);

            Assert.AreEqual(keysData.Count, otherData.Count);
            for (int i = 0; i < keysData.Count; i++)
            {
                List <PublicKey> keysCiphers  = new List <PublicKey>(keysData[i]);
                List <PublicKey> otherCiphers = new List <PublicKey>(otherData[i]);

                Assert.AreEqual(keysCiphers.Count, otherCiphers.Count);

                for (int j = 0; j < keysCiphers.Count; j++)
                {
                    PublicKey keysCipher  = keysCiphers[j];
                    PublicKey otherCipher = otherCiphers[j];

                    Assert.AreEqual(keysCipher.Data.Size, otherCipher.Data.Size);
                    Assert.AreEqual(keysCipher.Data.PolyModulusDegree, otherCipher.Data.PolyModulusDegree);
                    Assert.AreEqual(keysCipher.Data.CoeffModCount, otherCipher.Data.CoeffModCount);

                    ulong coeffCount = keysCipher.Data.Size * keysCipher.Data.PolyModulusDegree * keysCipher.Data.CoeffModCount;
                    for (ulong k = 0; k < coeffCount; k++)
                    {
                        Assert.AreEqual(keysCipher.Data[k], otherCipher.Data[k]);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void SeededKeyTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 8,
                PlainModulus      = new SmallModulus(257),
                CoeffModulus      = CoeffModulus.Create(8, new int[] { 40, 40 })
            };
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            KeyGenerator keygen    = new KeyGenerator(context);
            Encryptor    encryptor = new Encryptor(context, keygen.PublicKey);
            Decryptor    decryptor = new Decryptor(context, keygen.SecretKey);
            Evaluator    evaluator = new Evaluator(context);
            BatchEncoder encoder   = new BatchEncoder(context);

            GaloisKeys galoisKeys = new GaloisKeys();

            using (MemoryStream stream = new MemoryStream())
            {
                keygen.GaloisKeysSave(stream);
                stream.Seek(0, SeekOrigin.Begin);
                galoisKeys.Load(context, stream);
            }

            Plaintext    plain = new Plaintext();
            List <ulong> vec   = new List <ulong>
            {
                1, 2, 3, 4,
                5, 6, 7, 8
            };

            encoder.Encode(vec, plain);

            Ciphertext encrypted = new Ciphertext();
            Ciphertext encdest   = new Ciphertext();
            Plaintext  plaindest = new Plaintext();

            encryptor.Encrypt(plain, encrypted);
            evaluator.RotateColumns(encrypted, galoisKeys, encdest);
            decryptor.Decrypt(encdest, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                5, 6, 7, 8,
                1, 2, 3, 4
            }));

            evaluator.RotateRows(encdest, -1, galoisKeys, encrypted);
            decryptor.Decrypt(encrypted, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                8, 5, 6, 7,
                4, 1, 2, 3
            }));

            evaluator.RotateRowsInplace(encrypted, 2, galoisKeys);
            decryptor.Decrypt(encrypted, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                6, 7, 8, 5,
                2, 3, 4, 1
            }));

            evaluator.RotateColumnsInplace(encrypted, galoisKeys);
            decryptor.Decrypt(encrypted, plaindest);
            encoder.Decode(plaindest, vec);

            Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong>
            {
                2, 3, 4, 1,
                6, 7, 8, 5
            }));
        }
Exemplo n.º 5
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);
            }
        }