Exemple #1
0
        public void SEALContextParamsTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 128,
                PlainModulus      = new SmallModulus(1 << 6),
                CoeffModulus      = CoeffModulus.Create(128, new int[] { 30, 30, 30 })
            };
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: true,
                                                  secLevel: SecLevelType.None);

            SEALContext.ContextData data = context.KeyContextData;
            Assert.IsNotNull(data);

            EncryptionParameters parms2 = data.Parms;

            Assert.AreEqual(parms.PolyModulusDegree, parms2.PolyModulusDegree);

            EncryptionParameterQualifiers qualifiers = data.Qualifiers;

            Assert.IsNotNull(qualifiers);

            Assert.IsTrue(qualifiers.ParametersSet);
            Assert.IsFalse(qualifiers.UsingBatching);
            Assert.IsTrue(qualifiers.UsingFastPlainLift);
            Assert.IsTrue(qualifiers.UsingFFT);
            Assert.IsTrue(qualifiers.UsingNTT);
            Assert.AreEqual(SecLevelType.None, qualifiers.SecLevel);
            Assert.IsFalse(qualifiers.UsingDescendingModulusChain);
            Assert.IsTrue(context.UsingKeyswitching);

            ulong[] cdpm = data.CoeffDivPlainModulus;
            Assert.AreEqual(3, cdpm.Length);

            Assert.AreEqual(32ul, data.PlainUpperHalfThreshold);

            Assert.AreEqual(3, data.PlainUpperHalfIncrement.Length);
            Assert.IsNull(data.UpperHalfThreshold);
            Assert.IsNotNull(data.UpperHalfIncrement);
            Assert.AreEqual(3, data.UpperHalfIncrement.Length);
            Assert.AreEqual(2ul, data.ChainIndex);

            Assert.IsNull(data.PrevContextData);
            SEALContext.ContextData data2 = data.NextContextData;
            Assert.IsNotNull(data2);
            Assert.AreEqual(1ul, data2.ChainIndex);
            Assert.AreEqual(2ul, data2.PrevContextData.ChainIndex);

            SEALContext.ContextData data3 = data2.NextContextData;
            Assert.IsNotNull(data3);
            Assert.AreEqual(0ul, data3.ChainIndex);
            Assert.AreEqual(1ul, data3.PrevContextData.ChainIndex);
            Assert.IsNull(data3.NextContextData);
        }
Exemple #2
0
        public void SaveLoadTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);

            keygen.CreateRelinKeys(out RelinKeys keys);

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

            RelinKeys        other  = new RelinKeys();
            MemoryPoolHandle handle = other.Pool;

            Assert.AreEqual(0ul, other.Size);
            ulong alloced = handle.AllocByteCount;

            using (MemoryStream ms = new MemoryStream())
            {
                keys.Save(ms);
                ms.Seek(offset: 0, loc: SeekOrigin.Begin);
                other.Load(context, ms);
            }

            Assert.AreEqual(1ul, other.Size);
            Assert.IsTrue(ValCheck.IsValidFor(other, context));
            Assert.IsTrue(handle.AllocByteCount > 0ul);

            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.CoeffModulusSize, otherCipher.Data.CoeffModulusSize);

                    ulong coeffCount = keysCipher.Data.Size * keysCipher.Data.PolyModulusDegree * keysCipher.Data.CoeffModulusSize;
                    for (ulong k = 0; k < coeffCount; k++)
                    {
                        Assert.AreEqual(keysCipher.Data[k], otherCipher.Data[k]);
                    }
                }
            }
        }
Exemple #3
0
        private SEALContext GetContext()
        {
            var parms = new EncryptionParameters();

            parms.PolyModulus  = "1x^2048 + 1";
            parms.CoeffModulus = DefaultParams.CoeffModulus128(2048);
            parms.PlainModulus = 1 << 8;
            var context = new SEALContext(parms);

            return(context);
        }
Exemple #4
0
        static GlobalContext()
        {
            EncryptionParameters encParams = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 4096,
                CoeffModulus      = DefaultParams.CoeffModulus128(polyModulusDegree: 4096)
            };

            encParams.SetPlainModulus(0x133Ful);
            Context = SEALContext.Create(encParams);
        }
Exemple #5
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]);
                    }
                }
            }
        }
Exemple #6
0
        public static SecretKey BuildSecretKeyFromBase64String(string base64, SEALContext context)
        {
            var payload = Convert.FromBase64String(base64);

            using (var ms = new MemoryStream(payload))
            {
                var secretKey = new SecretKey();
                secretKey.Load(context, ms);

                return(secretKey);
            }
        }
Exemple #7
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);
        }
Exemple #8
0
        /// <summary>
        /// Convert a Base64 string to a Ciphertext
        /// </summary>
        /// <param name="b64">Base 64 string</param>
        /// <param name="context">SEALContext to verify resulting Ciphertext is valid for the SEALContext</param>
        /// <returns>Decoded Ciphertext</returns>
        public static Ciphertext Base64ToCiphertext(string b64, SEALContext context)
        {
            Ciphertext result = new Ciphertext();

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

            return(result);
        }
Exemple #9
0
        public void generateKeys(int x, int y, string publicFile, string secretFile, string encryptedXFile, string encryptedYFile)
        {
            //Console.WriteLine("Rider at ({0},{1})",x,y);

            using EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV);
            ulong polyModulusDegree = 4096;

            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            parms.PlainModulus      = new Modulus(1024);


            using SEALContext context = new SEALContext(parms);
            //Console.WriteLine("Set encryption parameters and print");
            //Console.WriteLine(context);
            //Console.WriteLine("Parameter validation (success): {0}", context.ParameterErrorMessage());

            using KeyGenerator keygen = new KeyGenerator(context);

            using PublicKey riderPub = keygen.PublicKey;
            using SecretKey riderSec = keygen.SecretKey;


            using Evaluator evaluator    = new Evaluator(context);
            using IntegerEncoder encoder = new IntegerEncoder(context);

            using Encryptor riderEncryptor = new Encryptor(context, riderPub);
            using Decryptor riderDecryptor = new Decryptor(context, riderSec);

            using Plaintext riderxPlain      = encoder.Encode(x);
            using Plaintext rideryPlain      = encoder.Encode(y);
            using Ciphertext riderxEncrypted = new Ciphertext();
            using Ciphertext rideryEncrypted = new Ciphertext();
            riderEncryptor.Encrypt(riderxPlain, riderxEncrypted);
            riderEncryptor.Encrypt(rideryPlain, rideryEncrypted);

            var fileStream = File.Create(publicFile);

            riderPub.Save(fileStream);
            fileStream.Close();

            fileStream = File.Create(secretFile);
            riderSec.Save(fileStream);
            fileStream.Close();

            fileStream = File.Create(encryptedXFile);
            riderxEncrypted.Save(fileStream);
            fileStream.Close();

            fileStream = File.Create(encryptedYFile);
            rideryEncrypted.Save(fileStream);
            fileStream.Close();
        }
Exemple #10
0
        public static Ciphertext BuildCiphertextFromBase64String(string base64, SEALContext context)
        {
            var payload = Convert.FromBase64String(base64);

            using (var ms = new MemoryStream(payload))
            {
                var ciphertext = new Ciphertext();
                ciphertext.Load(context, ms);

                return(ciphertext);
            }
        }
Exemple #11
0
        public void PropertiesTest()
        {
            SEALContext context = GlobalContext.BFVContext;

            Assert.IsTrue(context.FirstContextData.Qualifiers.ParametersSet);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingBatching);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFastPlainLift);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFFT);
            Assert.AreEqual(SecLevelType.TC128, context.FirstContextData.Qualifiers.SecLevel);
            Assert.IsFalse(context.FirstContextData.Qualifiers.UsingDescendingModulusChain);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingNTT);
            Assert.IsTrue(context.UsingKeyswitching);

            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 4096,
                CoeffModulus      = CoeffModulus.BFVDefault(4096)
            };

            SEALContext context2 = new SEALContext(parms);

            Assert.IsTrue(context2.FirstContextData.Qualifiers.ParametersSet);
            Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingBatching);
            Assert.IsFalse(context2.FirstContextData.Qualifiers.UsingFastPlainLift);
            Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingFFT);
            Assert.AreEqual(SecLevelType.TC128, context2.FirstContextData.Qualifiers.SecLevel);
            Assert.IsFalse(context.FirstContextData.Qualifiers.UsingDescendingModulusChain);
            Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingNTT);
            Assert.IsTrue(context.UsingKeyswitching);

            EncryptionParameterQualifiers qualifiers = new EncryptionParameterQualifiers(context2.FirstContextData.Qualifiers);

            Assert.IsNotNull(qualifiers);
            Assert.IsTrue(qualifiers.ParametersSet);
            Assert.IsTrue(qualifiers.UsingBatching);
            Assert.IsFalse(qualifiers.UsingFastPlainLift);
            Assert.IsTrue(qualifiers.UsingFFT);
            Assert.AreEqual(SecLevelType.TC128, qualifiers.SecLevel);
            Assert.IsTrue(qualifiers.UsingDescendingModulusChain);
            Assert.IsTrue(qualifiers.UsingNTT);

            SEALContext context3 = GlobalContext.BGVContext;

            Assert.IsTrue(context.FirstContextData.Qualifiers.ParametersSet);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingBatching);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFastPlainLift);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFFT);
            Assert.AreEqual(SecLevelType.TC128, context.FirstContextData.Qualifiers.SecLevel);
            Assert.IsFalse(context.FirstContextData.Qualifiers.UsingDescendingModulusChain);
            Assert.IsTrue(context.FirstContextData.Qualifiers.UsingNTT);
            Assert.IsTrue(context.UsingKeyswitching);
        }
Exemple #12
0
 public SalaryComputation()
 {
     //Constructor.
     parms = new EncryptionParameters(SchemeType.CKKS);
     parms.PolyModulusDegree = polyModulusDegree;
     parms.CoeffModulus      = CoeffModulus.Create(
         polyModulusDegree, new int[] { 60, 40, 40, 60 });
     //parms.PlainModulus = PlainModulus.Batching(polyModulusDegree, 20);
     context   = new SEALContext(parms);
     evaluator = new Evaluator(context);
     encoder   = new CKKSEncoder(context);
     SetConstants();
 }
Exemple #13
0
 public EncounterContext()
 {
     @params = new EncryptionParameters(SchemeType.BFV)
     {
         PolyModulusDegree = polyModDeg,           // Must be a positive power of 2
         CoeffModulus      = CoeffModulus.BFVDefault(polyModDeg),
         PlainModulus      = new SmallModulus(256) // Try to keep this as small as possible
     };
     SealContext = new SEALContext(@params);
     KeyGen      = new KeyGenerator(SealContext);
     Decryptor   = new Decryptor(SealContext, KeyGen.SecretKey);
     Encryptor   = new Encryptor(SealContext, KeyGen.PublicKey);
 }
Exemple #14
0
        public void KeyTest()
        {
            {
                SEALContext  context = GlobalContext.BFVContext;
                KeyGenerator keygen  = new KeyGenerator(context);
                keygen.CreateGaloisKeys(out GaloisKeys keys);
                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);
            }
            {
                SEALContext  context = GlobalContext.BGVContext;
                KeyGenerator keygen  = new KeyGenerator(context);
                keygen.CreateGaloisKeys(out GaloisKeys keys);
                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);
            }
        }
Exemple #15
0
        public void CreateTest()
        {
            {
                EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
                {
                    PolyModulusDegree = 64,
                    PlainModulus      = new Modulus(1 << 6),
                    CoeffModulus      = CoeffModulus.Create(64, new int[] { 40 })
                };
                SEALContext context = new SEALContext(parms,
                                                      expandModChain: false,
                                                      secLevel: SecLevelType.None);
                KeyGenerator keygen = new KeyGenerator(context);
                keygen.CreatePublicKey(out PublicKey pub);
                PublicKey copy = new PublicKey(pub);

                Assert.IsNotNull(copy);
                Assert.AreEqual(2ul, copy.Data.Size);
                Assert.IsTrue(copy.Data.IsNTTForm);

                PublicKey copy2 = new PublicKey();
                copy2.Set(copy);

                Assert.AreEqual(2ul, copy2.Data.Size);
                Assert.IsTrue(copy2.Data.IsNTTForm);
            }
            {
                EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
                {
                    PolyModulusDegree = 64,
                    PlainModulus      = new Modulus(1 << 6),
                    CoeffModulus      = CoeffModulus.Create(64, new int[] { 40 })
                };
                SEALContext context = new SEALContext(parms,
                                                      expandModChain: false,
                                                      secLevel: SecLevelType.None);
                KeyGenerator keygen = new KeyGenerator(context);
                keygen.CreatePublicKey(out PublicKey pub);
                PublicKey copy = new PublicKey(pub);

                Assert.IsNotNull(copy);
                Assert.AreEqual(2ul, copy.Data.Size);
                Assert.IsTrue(copy.Data.IsNTTForm);

                PublicKey copy2 = new PublicKey();
                copy2.Set(copy);

                Assert.AreEqual(2ul, copy2.Data.Size);
                Assert.IsTrue(copy2.Data.IsNTTForm);
            }
        }
        public void Create3Test()
        {
            SEALContext context = GlobalContext.BFVContext;
            ParmsId     parms   = context.FirstParmsId;

            Assert.AreNotEqual(0ul, parms.Block[0]);
            Assert.AreNotEqual(0ul, parms.Block[1]);
            Assert.AreNotEqual(0ul, parms.Block[2]);
            Assert.AreNotEqual(0ul, parms.Block[3]);

            Ciphertext cipher = new Ciphertext(context, parms, sizeCapacity: 5);

            Assert.AreEqual(5ul, cipher.SizeCapacity);
        }
        public void IndexRangeFail3Test()
        {
            SEALContext  context   = GlobalContext.BFVContext;
            KeyGenerator keygen    = new KeyGenerator(context);
            Encryptor    encryptor = new Encryptor(context, keygen.PublicKey);
            Plaintext    plain     = new Plaintext("1");
            Ciphertext   cipher    = new Ciphertext();

            encryptor.Encrypt(plain, cipher);
            ulong data = 0;

            Utilities.AssertThrows <IndexOutOfRangeException>(() => data          = cipher[65536]);
            Utilities.AssertThrows <IndexOutOfRangeException>(() => cipher[65536] = 10ul);
        }
Exemple #18
0
			//private static bool _firstTime = true;

            //private static Decryptor _decryptor;

            public SecureSvc(int nRows, double[][] vectors, double[][] coefficients, double[] intercepts,
				 String kernel, double gamma, double coef0, ulong degree , int power)
			{


				//this._nRows			= nRows;

				this._vectors		= vectors;
				this._coefficients	= coefficients;
				this._intercepts		= intercepts;

				this._kernel			= Enum.Parse<Kernel>(kernel, true);
				this._gamma			= gamma;
				this._coef0			= coef0;
				this._degree			= degree;
				this._power			= power;
				EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);

				ulong polyModulusDegree = 16384;
				
				if (power >= 20 && power < 40 )
				{
					parms.CoeffModulus = CoeffModulus.Create(polyModulusDegree,
						new int[] { 60, 20, 21, 22, 23, 24, 25, 26, 27, 60 });
				}
				else if (power >= 40 && power < 60)
				{
					parms.CoeffModulus = CoeffModulus.Create(polyModulusDegree,
						new int[] { 60, 40, 40, 40, 40, 40, 40, 40 , 60 });
				}
				else if (power == 60)
				{
					polyModulusDegree = 32768;
                    parms.CoeffModulus = CoeffModulus.Create(polyModulusDegree,
						new int[] { 60, 60, 60, 60, 60, 60, 60, 60, 60 });
				}
				parms.PolyModulusDegree = polyModulusDegree;
                _context = new SEALContext(parms);

				KeyGenerator keygen = new KeyGenerator(_context);
				_publicKey = keygen.PublicKey;
				_secretKey = keygen.SecretKey;
				_relinKeys = keygen.RelinKeys();
				_galoisKeys = keygen.GaloisKeys();

				_encryptor = new Encryptor(_context, _publicKey);
				_evaluator = new Evaluator(_context);
				_decryptor = new Decryptor(_context, _secretKey);
				_encoder = new CKKSEncoder(_context);
			}
 public BMIComputation(RelinKeys Keys)
 {
     parms = new EncryptionParameters(SchemeType.CKKS);
     parms.PolyModulusDegree = polyModulusDegree;
     parms.CoeffModulus      = CoeffModulus.Create(
         polyModulusDegree, new int[] { 60, 40, 40, 60 });
     //parms.PlainModulus = PlainModulus.Batching(polyModulusDegree, 20);
     context   = new SEALContext(parms);
     evaluator = new Evaluator(context);
     encoder   = new CKKSEncoder(context);
     scale     = Math.Pow(2.0, 40);
     SetConstants();
     KeysRelin = Keys;
 }
Exemple #20
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);
            }
        }
        public void Create2Test()
        {
            SEALContext context = GlobalContext.BFVContext;
            ParmsId     parms   = context.FirstParmsId;

            Assert.AreNotEqual(0ul, parms.Block[0]);
            Assert.AreNotEqual(0ul, parms.Block[1]);
            Assert.AreNotEqual(0ul, parms.Block[2]);
            Assert.AreNotEqual(0ul, parms.Block[3]);

            Ciphertext cipher = new Ciphertext(context, parms);

            Assert.AreEqual(parms, cipher.ParmsId);
        }
Exemple #22
0
        public void KeyEltTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);

            keygen.CreateGaloisKeys(galoisElts: new uint[] { 1, 3 }, out GaloisKeys keys);
            Assert.IsNotNull(keys);

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

            Assert.IsTrue(keys.HasKey(1));
            Assert.IsTrue(keys.HasKey(3));
            Assert.IsFalse(keys.HasKey(5));
        }
Exemple #23
0
        public static SEALContext GetContext()
        {
            var encryptionParameters = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 32768,
                CoeffModulus      = DefaultParams.CoeffModulus128(polyModulusDegree: 32768)
            };

            encryptionParameters.SetPlainModulus(0x133Ful);

            Debug.WriteLine("[COMMON]: Successfully created context");

            return(SEALContext.Create(encryptionParameters));
        }
Exemple #24
0
        public void Create2Test()
        {
            SEALContext  context    = GlobalContext.BFVContext;
            KeyGenerator keygen1    = new KeyGenerator(context);
            Encryptor    encryptor1 = new Encryptor(context, keygen1.PublicKey);
            Decryptor    decryptor1 = new Decryptor(context, keygen1.SecretKey);

            Ciphertext cipher = new Ciphertext();
            Plaintext  plain  = new Plaintext("2x^1 + 5");
            Plaintext  plain2 = new Plaintext();

            encryptor1.Encrypt(plain, cipher);
            decryptor1.Decrypt(cipher, plain2);

            Assert.AreNotSame(plain, plain2);
            Assert.AreEqual(plain, plain2);

            KeyGenerator keygen2    = new KeyGenerator(context, keygen1.SecretKey);
            Encryptor    encryptor2 = new Encryptor(context, keygen2.PublicKey);
            Decryptor    decryptor2 = new Decryptor(context, keygen2.SecretKey);

            Plaintext plain3 = new Plaintext();

            decryptor2.Decrypt(cipher, plain3);

            Assert.AreNotSame(plain, plain3);
            Assert.AreEqual(plain, plain3);

            KeyGenerator keygen3    = new KeyGenerator(context, keygen1.SecretKey, keygen1.PublicKey);
            Encryptor    encryptor3 = new Encryptor(context, keygen3.PublicKey);
            Decryptor    decryptor3 = new Decryptor(context, keygen3.SecretKey);

            Plaintext plain4 = new Plaintext();

            decryptor3.Decrypt(cipher, plain4);

            Assert.AreNotSame(plain, plain4);
            Assert.AreEqual(plain, plain4);

            Ciphertext cipher2 = new Ciphertext();

            plain2.Release();

            encryptor3.Encrypt(plain, cipher2);
            decryptor2.Decrypt(cipher2, plain2);

            Assert.AreNotSame(plain, plain2);
            Assert.AreEqual(plain, plain2);
        }
        /// <summary>
        /// Helper function: Prints the parameters in a SEALContext.
        /// </summary>
        public static void PrintParameters(SEALContext context)
        {
            // Verify parameters
            if (null == context)
            {
                throw new ArgumentNullException("context is not set");
            }

            SEALContext.ContextData contextData = context.FirstContextData;

            /*
             * Which scheme are we using?
             */
            string schemeName = null;

            switch (contextData.Parms.Scheme)
            {
            case SchemeType.BFV:
                schemeName = "BFV";
                break;

            case SchemeType.CKKS:
                schemeName = "CKKS";
                break;

            default:
                throw new ArgumentException("unsupported scheme");
            }

            Console.WriteLine($"/ Encryption parameters:");
            Console.WriteLine($"| Scheme: {schemeName}");
            Console.WriteLine($"| PolyModulusDegree: {contextData.Parms.PolyModulusDegree}");

            /*
             * Print the size of the true (product) coefficient modulus.
             */
            Console.WriteLine($"| CoeffModulus size: {contextData.TotalCoeffModulusBitCount} bits");

            /*
             * For the BFV scheme print the plain_modulus parameter.
             */
            if (contextData.Parms.Scheme == SchemeType.BFV)
            {
                Console.WriteLine($"| PlainModulus: {contextData.Parms.PlainModulus.Value}");
            }

            Console.WriteLine($"\\ NoiseStandardDeviation: {contextData.Parms.NoiseStandardDeviation}");
            Console.WriteLine();
        }
Exemple #26
0
        public void CreateNonEmptyTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);

            GaloisKeys keys = keygen.GaloisKeys();

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

            GaloisKeys copy = new GaloisKeys(keys);

            Assert.IsNotNull(copy);
            Assert.AreEqual(24ul, copy.Size);
        }
Exemple #27
0
        private static void ExampleBFVPerformanceDefault()
        {
            Utilities.PrintExampleBanner("BFV Performance Test with Degrees: 4096, 8192, and 16384");

            using EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV);
            ulong polyModulusDegree = 4096;

            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            parms.PlainModulus      = new Modulus(786433);
            using (SEALContext context = new SEALContext(parms))
            {
                BFVPerformanceTest(context);
            }

            Console.WriteLine();
            polyModulusDegree       = 8192;
            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            parms.PlainModulus      = new Modulus(786433);
            using (SEALContext context = new SEALContext(parms))
            {
                BFVPerformanceTest(context);
            }

            Console.WriteLine();
            polyModulusDegree       = 16384;
            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            parms.PlainModulus      = new Modulus(786433);
            using (SEALContext context = new SEALContext(parms))
            {
                BFVPerformanceTest(context);
            }

            /*
             * Comment out the following to run the biggest example.
             */
            //Console.WriteLine();
            //polyModulusDegree = 32768;
            //parms.PolyModulusDegree = polyModulusDegree;
            //parms.CoeffModulus = CoeffModulus.BFVDefault(polyModulusDegree);
            //parms.PlainModulus = new Modulus(786433);
            //using (SEALContext context = new SEALContext(parms))
            //{
            //    BFVPerformanceTest(context);
            //}
        }
Exemple #28
0
        public void KeyEltTest()
        {
            SEALContext  context = GlobalContext.Context;
            KeyGenerator keygen  = new KeyGenerator(context);

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

            Assert.IsNotNull(keys);

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

            Assert.IsTrue(keys.HasKey(1));
            Assert.IsTrue(keys.HasKey(3));
            Assert.IsFalse(keys.HasKey(5));
        }
        public MetricsController()
        {
            // Initialize context
            // Getting context from Commons project
            _sealContext = SEALUtils.GetContext();


            // Initialize key generator and encryptor
            // Initialize key Generator that will be use to get the Public and Secret keys
            _keyGenerator = new KeyGenerator(_sealContext);
            // Initializing encryptor
            _encryptor = new Encryptor(_sealContext, _keyGenerator.PublicKey);

            // Initialize evaluator
            _evaluator = new Evaluator(_sealContext);
        }
        public void IndexRangeFail1Test()
        {
            SEALContext  context   = GlobalContext.BFVContext;
            KeyGenerator keygen    = new KeyGenerator(context);
            Encryptor    encryptor = new Encryptor(context, keygen.PublicKey);
            Plaintext    plain     = new Plaintext("1");
            Ciphertext   cipher    = new Ciphertext(context);

            encryptor.Encrypt(plain, cipher);

            Utilities.AssertThrows <IndexOutOfRangeException>(() =>
            {
                // We only have 2 polynomials
                ulong data = cipher[2, 0];
            });
        }