Esempio n. 1
0
        public void EncodeDecodeDoubleTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);

            parms.PolyModulusDegree = 64;
            parms.CoeffModulus      = CoeffModulus.Create(64, new int[] { 40, 40, 40, 40 });
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);

            int            slots  = 16;
            Plaintext      plain  = new Plaintext();
            double         delta  = 1 << 16;
            List <Complex> result = new List <Complex>();

            CKKSEncoder encoder = new CKKSEncoder(context);

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

            double value = 10d;

            encoder.Encode(value, delta, plain);
            encoder.Decode(plain, result);

            for (int i = 0; i < slots; i++)
            {
                double tmp = Math.Abs(value - result[i].Real);
                Assert.IsTrue(tmp < 0.5);
            }
        }
Esempio n. 2
0
        public void ScaleTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 8,
                CoeffModulus      = CoeffModulus.Create(8, new int[] { 40, 40, 40, 40 })
            };
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: true,
                                                  secLevel: SecLevelType.None);
            KeyGenerator keygen = new KeyGenerator(context);

            keygen.CreatePublicKey(out PublicKey publicKey);
            keygen.CreateGaloisKeys(out GaloisKeys galoisKeys);

            Encryptor   encryptor = new Encryptor(context, publicKey);
            Evaluator   evaluator = new Evaluator(context);
            CKKSEncoder encoder   = new CKKSEncoder(context);

            MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceNew);

            Assert.AreEqual(0ul, pool.AllocByteCount);

            Ciphertext encrypted = new Ciphertext(pool);
            Plaintext  plain     = new Plaintext();

            MemoryPoolHandle cipherPool = encrypted.Pool;

            Assert.IsNotNull(cipherPool);
            Assert.AreEqual(0ul, cipherPool.AllocByteCount);

            List <Complex> input = new List <Complex>()
            {
                new Complex(1, 1),
                new Complex(2, 2),
                new Complex(3, 3),
                new Complex(4, 4)
            };
            double delta = Math.Pow(2, 70);

            encoder.Encode(input, context.FirstParmsId, delta, plain);
            encryptor.Encrypt(plain, encrypted);

            Assert.AreEqual(delta, encrypted.Scale, delta: Math.Pow(2, 60));

            Ciphertext encrypted2 = new Ciphertext();

            encrypted2.Set(encrypted);
            Assert.AreEqual(delta, encrypted2.Scale, delta: Math.Pow(2, 60));

            evaluator.RescaleToNextInplace(encrypted);

            Assert.AreEqual(Math.Pow(2, 30), encrypted.Scale, delta: 10000);
            Assert.AreNotEqual(0ul, cipherPool.AllocByteCount);

            double newScale = Math.Pow(2, 10);

            encrypted.Scale = newScale;
            Assert.AreEqual(newScale, encrypted.Scale, delta: 100);
        }
Esempio n. 3
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);

            SecretKey secret = keygen.SecretKey;
            SecretKey copy   = new SecretKey(secret);

            Assert.AreEqual(64ul, copy.Data.CoeffCount);
            Assert.IsTrue(copy.Data.IsNTTForm);

            SecretKey copy2 = new SecretKey();

            copy2.Set(copy);

            Assert.AreEqual(64ul, copy2.Data.CoeffCount);
            Assert.IsTrue(copy2.Data.IsNTTForm);
        }
Esempio n. 4
0
        public void EncodeDecodeUlongTest()
        {
            int slots = 32;
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);

            parms.PolyModulusDegree = (ulong)slots * 2;
            parms.CoeffModulus      = CoeffModulus.Create(64, new int[] { 40, 40, 40, 40 });
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            CKKSEncoder encoder = new CKKSEncoder(context);

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

            long value = 15;

            encoder.Encode(value, plain);
            encoder.Decode(plain, result);

            for (int i = 0; i < slots; i++)
            {
                double tmp = Math.Abs(value - result[i].Real);
                Assert.IsTrue(tmp < 0.5);
            }
        }
Esempio n. 5
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));
        }
Esempio n. 6
0
        public void EncodeDecodeComplexTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = CoeffModulus.Create(64, new int[] { 40, 40, 40, 40 })
            };

            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            CKKSEncoder encoder = new CKKSEncoder(context);

            Plaintext plain = new Plaintext();
            Complex   value = new Complex(3.1415, 2.71828);

            encoder.Encode(value, scale: Math.Pow(2, 20), destination: plain);

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

            encoder.Decode(plain, result);

            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(3.1415, result[0].Real, delta: 0.0001);
            Assert.AreEqual(2.71828, result[0].Imaginary, delta: 0.0001);
        }
Esempio n. 7
0
        public void CreateTest()
        {
            List <Modulus> cm = (List <Modulus>)CoeffModulus.Create(2, new int[] { });

            Assert.AreEqual(0, cm.Count);

            cm = (List <Modulus>)CoeffModulus.Create(2, new int[] { 3 });
            Assert.AreEqual(1, cm.Count);
            Assert.AreEqual(5ul, cm[0].Value);

            cm = (List <Modulus>)CoeffModulus.Create(2, new int[] { 3, 4 });
            Assert.AreEqual(2, cm.Count);
            Assert.AreEqual(5ul, cm[0].Value);
            Assert.AreEqual(13ul, cm[1].Value);

            cm = (List <Modulus>)CoeffModulus.Create(2, new int[] { 3, 5, 4, 5 });
            Assert.AreEqual(4, cm.Count);
            Assert.AreEqual(5ul, cm[0].Value);
            Assert.AreEqual(17ul, cm[1].Value);
            Assert.AreEqual(13ul, cm[2].Value);
            Assert.AreEqual(29ul, cm[3].Value);

            cm = (List <Modulus>)CoeffModulus.Create(32, new int[] { 30, 40, 30, 30, 40 });
            Assert.AreEqual(5, cm.Count);
            Assert.AreEqual(30, (int)(Math.Log(cm[0].Value, 2)) + 1);
            Assert.AreEqual(40, (int)(Math.Log(cm[1].Value, 2)) + 1);
            Assert.AreEqual(30, (int)(Math.Log(cm[2].Value, 2)) + 1);
            Assert.AreEqual(30, (int)(Math.Log(cm[3].Value, 2)) + 1);
            Assert.AreEqual(40, (int)(Math.Log(cm[4].Value, 2)) + 1);
            Assert.AreEqual(1ul, cm[0].Value % 64);
            Assert.AreEqual(1ul, cm[1].Value % 64);
            Assert.AreEqual(1ul, cm[2].Value % 64);
            Assert.AreEqual(1ul, cm[3].Value % 64);
            Assert.AreEqual(1ul, cm[4].Value % 64);
        }
Esempio n. 8
0
        private static void ExampleCKKSPerformanceDefault()
        {
            Utilities.PrintExampleBanner("CKKS Performance Test with Degrees: 4096, 8192, and 16384");

            // It is not recommended to use BFVDefault primes in CKKS. However, for performance
            // test, BFVDefault primes are good enough.
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);
            ulong polyModulusDegree    = 4096;

            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            CKKSPerformanceTest(new SEALContext(parms));

            Console.WriteLine();
            polyModulusDegree       = 8192;
            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            CKKSPerformanceTest(new SEALContext(parms));

            Console.WriteLine();
            polyModulusDegree       = 16384;
            parms.PolyModulusDegree = polyModulusDegree;
            parms.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            CKKSPerformanceTest(new SEALContext(parms));

            /*
             * Comment out the following to run the biggest example.
             */
            //Console.WriteLine();
            //polyModulusDegree = 32768;
            //parms.PolyModulusDegree = polyModulusDegree;
            //parms.CoeffModulus = CoeffModulus.BFVDefault(polyModulusDegree);
            //CKKSPerformanceTest(new SEALContext(parms));
        }
Esempio n. 9
0
        public void ExceptionsTest()
        {
            Modulus sm = new Modulus(0x12345ul);

            Utilities.AssertThrows <ArgumentNullException>(() => sm = new Modulus(null));
            Utilities.AssertThrows <ArgumentNullException>(() => sm.Set(null));
            Utilities.AssertThrows <ArgumentNullException>(() => sm.Save(null));
            Utilities.AssertThrows <ArgumentNullException>(() => sm.Load(null));
            Utilities.AssertThrows <EndOfStreamException>(() => sm.Load(new MemoryStream()));

            // Too small polyModulusDegree
            Utilities.AssertThrows <ArgumentException>(() => CoeffModulus.Create(1, new int[] { 2 }));

            // Too large polyModulusDegree
            Utilities.AssertThrows <ArgumentException>(() => CoeffModulus.Create(262144, new int[] { 30 }));

            // Invalid polyModulusDegree
            Utilities.AssertThrows <ArgumentException>(() => CoeffModulus.Create(1023, new int[] { 20 }));

            // Invalid bitSize
            Utilities.AssertThrows <ArgumentException>(() => CoeffModulus.Create(2048, new int[] { 0 }));
            Utilities.AssertThrows <ArgumentException>(() => CoeffModulus.Create(2048, new int[] { -30 }));
            Utilities.AssertThrows <ArgumentException>(() => CoeffModulus.Create(2048, new int[] { 30, -30 }));

            // Too small primes requested
            Utilities.AssertThrows <InvalidOperationException>(() => CoeffModulus.Create(2, new int[] { 2 }));
            Utilities.AssertThrows <InvalidOperationException>(() => CoeffModulus.Create(2, new int[] { 3, 3, 3 }));
            Utilities.AssertThrows <InvalidOperationException>(() => CoeffModulus.Create(1024, new int[] { 8 }));
        }
Esempio n. 10
0
        private static void ExampleCKKSPerformanceCustom()
        {
            Console.Write("> Set PolyModulusDegree (1024, 2048, 4096, 8192, 16384, or 32768): ");
            string input = Console.ReadLine();

            if (!ulong.TryParse(input, out ulong polyModulusDegree))
            {
                Console.WriteLine("Invalid option.");
                return;
            }
            if (polyModulusDegree < 1024 || polyModulusDegree > 32768 ||
                (polyModulusDegree & (polyModulusDegree - 1)) != 0)
            {
                Console.WriteLine("Invalid option.");
                return;
            }

            string banner = $"CKKS Performance Test with Degree: {polyModulusDegree}";

            Utilities.PrintExampleBanner(banner);

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

            using (SEALContext context = new SEALContext(parms))
            {
                CKKSPerformanceTest(context);
            }
        }
Esempio n. 11
0
        public void ExpandModChainTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 4096,
                CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree: 4096),
                PlainModulus      = new Modulus(1 << 20)
            };

            SEALContext context1 = new SEALContext(parms,
                                                   expandModChain: true,
                                                   secLevel: SecLevelType.None);

            // By default there is a chain
            SEALContext.ContextData contextData = context1.KeyContextData;
            Assert.IsNotNull(contextData);
            Assert.IsNull(contextData.PrevContextData);
            Assert.IsNotNull(contextData.NextContextData);
            contextData = context1.FirstContextData;
            Assert.IsNotNull(contextData);
            Assert.IsNotNull(contextData.PrevContextData);
            Assert.IsNotNull(contextData.NextContextData);

            // This should not create a chain
            SEALContext context2 = new SEALContext(parms, expandModChain: false);

            contextData = context2.KeyContextData;
            Assert.IsNotNull(contextData);
            Assert.IsNull(contextData.PrevContextData);
            Assert.IsNotNull(contextData.NextContextData);
            contextData = context2.FirstContextData;
            Assert.IsNotNull(contextData);
            Assert.IsNotNull(contextData.PrevContextData);
            Assert.IsNull(contextData.NextContextData);
        }
Esempio n. 12
0
        public void ExceptionsTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);
            SecretKey    secret  = new SecretKey();
            List <uint>  elts    = new List <uint> {
                16385
            };
            List <uint> elts_null = null;
            List <int>  steps     = new List <int> {
                4096
            };
            List <int> steps_null = null;

            Utilities.AssertThrows <ArgumentNullException>(() => keygen = new KeyGenerator(null));

            Utilities.AssertThrows <ArgumentNullException>(() => keygen = new KeyGenerator(context, null));
            Utilities.AssertThrows <ArgumentNullException>(() => keygen = new KeyGenerator(null, keygen.SecretKey));
            Utilities.AssertThrows <ArgumentException>(() => keygen     = new KeyGenerator(context, secret));

            Utilities.AssertThrows <ArgumentNullException>(() => keygen.CreateGaloisKeys(elts_null));
            Utilities.AssertThrows <ArgumentException>(() => keygen.CreateGaloisKeys(elts));
            Utilities.AssertThrows <ArgumentNullException>(() => keygen.CreateGaloisKeys(steps_null));
            Utilities.AssertThrows <ArgumentException>(() => keygen.CreateGaloisKeys(steps));

            EncryptionParameters smallParms = new EncryptionParameters(SchemeType.CKKS);

            smallParms.PolyModulusDegree = 128;
            smallParms.CoeffModulus      = CoeffModulus.Create(smallParms.PolyModulusDegree, new int[] { 60 });
            context = new SEALContext(smallParms, true, SecLevelType.None);
            keygen  = new KeyGenerator(context);
            Utilities.AssertThrows <InvalidOperationException>(() => keygen.CreateRelinKeys());
            Utilities.AssertThrows <InvalidOperationException>(() => keygen.CreateGaloisKeys());
        }
Esempio n. 13
0
        static GlobalContext()
        {
            EncryptionParameters encParams = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 8192,
                CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree: 8192)
            };

            encParams.SetPlainModulus(65537ul);
            BFVContext = new SEALContext(encParams);

            encParams = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 8192,
                CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree: 8192)
            };
            CKKSContext = new SEALContext(encParams);

            encParams = new EncryptionParameters(SchemeType.BGV)
            {
                PolyModulusDegree = 8192,
                CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree: 8192)
            };
            encParams.SetPlainModulus(65537ul);
            BGVContext = new SEALContext(encParams);
        }
Esempio n. 14
0
        public void EncodeDecodeVectorDoubleTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = CoeffModulus.Create(64, new int[] { 30, 30 })
            };

            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            CKKSEncoder encoder = new CKKSEncoder(context);
            Plaintext   plain   = new Plaintext();

            double[] values = new double[] { 0.1, 2.3, 34.4 };
            encoder.Encode(values, scale: Math.Pow(2, 20), destination: plain);

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

            encoder.Decode(plain, result);

            Assert.IsNotNull(result);
            Assert.AreEqual(0.1, result[0], delta: 0.001);
            Assert.AreEqual(2.3, result[1], delta: 0.001);
            Assert.AreEqual(34.4, result[2], delta: 0.001);
        }
Esempio n. 15
0
        public void SeededKeyTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 128,
                PlainModulus      = new Modulus(1 << 6),
                CoeffModulus      = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
            };
            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);
            KeyGenerator keygen = new KeyGenerator(context);

            RelinKeys relinKeys = new RelinKeys();

            using (MemoryStream stream = new MemoryStream())
            {
                keygen.CreateRelinKeys().Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                relinKeys.Load(context, stream);
            }

            keygen.CreatePublicKey(out PublicKey publicKey);
            Encryptor encryptor = new Encryptor(context, publicKey);
            Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
            Evaluator evaluator = new Evaluator(context);

            Ciphertext encrypted1 = new Ciphertext(context);
            Ciphertext encrypted2 = new Ciphertext(context);
            Plaintext  plain1     = new Plaintext();
            Plaintext  plain2     = new Plaintext();

            plain1.Set(0);
            encryptor.Encrypt(plain1, encrypted1);
            evaluator.SquareInplace(encrypted1);
            evaluator.RelinearizeInplace(encrypted1, relinKeys);
            decryptor.Decrypt(encrypted1, plain2);

            Assert.AreEqual(1ul, plain2.CoeffCount);
            Assert.AreEqual(0ul, plain2[0]);

            plain1.Set("1x^10 + 2");
            encryptor.Encrypt(plain1, encrypted1);
            evaluator.SquareInplace(encrypted1);
            evaluator.RelinearizeInplace(encrypted1, relinKeys);
            evaluator.SquareInplace(encrypted1);
            evaluator.Relinearize(encrypted1, relinKeys, encrypted2);
            decryptor.Decrypt(encrypted2, plain2);

            // {1x^40 + 8x^30 + 18x^20 + 20x^10 + 10}
            Assert.AreEqual(41ul, plain2.CoeffCount);
            Assert.AreEqual(16ul, plain2[0]);
            Assert.AreEqual(32ul, plain2[10]);
            Assert.AreEqual(24ul, plain2[20]);
            Assert.AreEqual(8ul, plain2[30]);
            Assert.AreEqual(1ul, plain2[40]);
        }
Esempio n. 16
0
        public ContextManager()
        {
            EncryptionParams = new EncryptionParameters(SchemeType.BFV);
            const ulong polyModulusDegree = 2048;

            EncryptionParams.PolyModulusDegree = polyModulusDegree;
            EncryptionParams.CoeffModulus      = CoeffModulus.BFVDefault(polyModulusDegree);
            EncryptionParams.PlainModulus      = new Modulus(1024);
            Context = new SEALContext(EncryptionParams);
        }
Esempio n. 17
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);
        }
Esempio n. 18
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();
        }
Esempio n. 19
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);
        }
Esempio n. 20
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);
 }
Esempio n. 21
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();
 }
Esempio n. 22
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);
            }
        }
Esempio n. 23
0
        public void EqualsTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 8,
                PlainModulus      = new SmallModulus(257),
                CoeffModulus      = CoeffModulus.Create(8, new int[] { 40, 40 })
            };

            EncryptionParameters parms2 = new EncryptionParameters(SchemeType.CKKS);

            Assert.AreNotEqual(parms, parms2);
            Assert.IsFalse(parms.Equals(null));
        }
 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;
 }
Esempio n. 25
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);
			}
Esempio n. 26
0
        public void SaveLoadTest()
        {
            TestDelegate save_load_test = delegate(SchemeType scheme)
            {
                List <SmallModulus>  coeffModulus = (List <SmallModulus>)CoeffModulus.Create(8, new int[] { 40, 40 });
                EncryptionParameters parms        = new EncryptionParameters(scheme)
                {
                    PolyModulusDegree = 8,
                    CoeffModulus      = coeffModulus
                };
                if (scheme == SchemeType.BFV)
                {
                    parms.SetPlainModulus(257);
                }

                EncryptionParameters loaded = null;

                using (MemoryStream stream = new MemoryStream())
                {
                    EncryptionParameters.Save(parms, stream);

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

                    loaded = EncryptionParameters.Load(stream);
                }

                Assert.AreEqual(scheme, loaded.Scheme);
                Assert.AreEqual(8ul, loaded.PolyModulusDegree);
                if (scheme == SchemeType.BFV)
                {
                    Assert.AreEqual(257ul, loaded.PlainModulus.Value);
                }
                else if (scheme == SchemeType.CKKS)
                {
                    Assert.AreEqual(0ul, loaded.PlainModulus.Value);
                }

                List <SmallModulus> loadedCoeffModulus = new List <SmallModulus>(loaded.CoeffModulus);
                Assert.AreEqual(2, loadedCoeffModulus.Count);
                Assert.AreNotSame(coeffModulus[0], loadedCoeffModulus[0]);
                Assert.AreNotSame(coeffModulus[1], loadedCoeffModulus[1]);
                Assert.AreEqual(coeffModulus[0], loadedCoeffModulus[0]);
                Assert.AreEqual(coeffModulus[1], loadedCoeffModulus[1]);
            };

            save_load_test(SchemeType.BFV);
            save_load_test(SchemeType.CKKS);
        }
Esempio n. 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);
            //}
        }
Esempio n. 28
0
        public void SchemeIsCKKSTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 8,
                CoeffModulus      = CoeffModulus.Create(8, new int[] { 40, 40, 40, 40 })
            };

            SEALContext context = new SEALContext(parms,
                                                  expandModChain: false,
                                                  secLevel: SecLevelType.None);

            Utilities.AssertThrows <ArgumentException>(() =>
            {
                BatchEncoder encoder = new BatchEncoder(context);
            });
        }
Esempio n. 29
0
 public CKKSEncryptor()
 {
     //Set scheme Primes and encryption parameters.
     parms = new EncryptionParameters(SchemeType.CKKS);
     parms.PolyModulusDegree = polyModulusDegree;
     parms.CoeffModulus      = CoeffModulus.Create(
         polyModulusDegree, new int[] { 60, 40, 40, 60 });
     scale   = Math.Pow(2.0, 40);
     context = new SEALContext(parms);
     keygen  = new KeyGenerator(context);
     //Generate private and public key.
     publicKey = keygen.PublicKey;
     secretKey = keygen.SecretKey;
     encryptor = new Encryptor(context, publicKey);
     encoder   = new CKKSEncoder(context);
     KeysRelin = keygen.RelinKeys();
 }
Esempio n. 30
0
        public void encryptDriverLocation(int x, int y, string publicFile, string encryptedXFile, string encryptedYFile)
        {
            //Console.WriteLine("Driver 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);

            using KeyGenerator keygen = new KeyGenerator(context);

            using PublicKey riderPub = new PublicKey();


            using Ciphertext xEncrypted = new Ciphertext();
            using (var sr = new StreamReader(publicFile))
            {
                riderPub.Load(context, sr.BaseStream);
            }

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

            using Encryptor riderEncryptor = new Encryptor(context, riderPub);

            using Plaintext driverxPlain      = encoder.Encode(x);
            using Plaintext driveryPlain      = encoder.Encode(y);
            using Ciphertext driverxEncrypted = new Ciphertext();
            using Ciphertext driveryEncrypted = new Ciphertext();
            riderEncryptor.Encrypt(driverxPlain, driverxEncrypted);
            riderEncryptor.Encrypt(driveryPlain, driveryEncrypted);

            var fileStream = File.Create(encryptedXFile);

            driverxEncrypted.Save(fileStream);
            fileStream.Close();

            fileStream = File.Create(encryptedYFile);
            driveryEncrypted.Save(fileStream);
            fileStream.Close();
        }