Ejemplo n.º 1
0
        public void EncodeDecodeUlongTest()
        {
            int slots = 32;
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);

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

            coeffModulus.Add(DefaultParams.SmallMods40Bit(0));
            coeffModulus.Add(DefaultParams.SmallMods40Bit(1));
            coeffModulus.Add(DefaultParams.SmallMods40Bit(2));
            coeffModulus.Add(DefaultParams.SmallMods40Bit(3));
            parms.CoeffModulus = coeffModulus;
            SEALContext context = SEALContext.Create(parms);
            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);
            }
        }
Ejemplo n.º 2
0
        public void EncodeDecodeDoubleTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS);

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

            coeffModulus.Add(DefaultParams.SmallMods40Bit(0));
            coeffModulus.Add(DefaultParams.SmallMods40Bit(1));
            coeffModulus.Add(DefaultParams.SmallMods40Bit(2));
            coeffModulus.Add(DefaultParams.SmallMods40Bit(3));
            parms.CoeffModulus = coeffModulus;
            SEALContext context = SEALContext.Create(parms);

            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);
            }
        }
Ejemplo n.º 3
0
        public void EncodeDecodeVectorDoubleTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = new List <SmallModulus>()
                {
                    DefaultParams.SmallMods30Bit(0),
                    DefaultParams.SmallMods30Bit(1)
                }
            };

            SEALContext context = SEALContext.Create(parms);
            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);
        }
Ejemplo n.º 4
0
        public void CreateTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>
            {
                DefaultParams.SmallMods40Bit(0)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 64,
                PlainModulus      = new SmallModulus(1 << 6),
                CoeffModulus      = coeffModulus
            };
            SEALContext  context = SEALContext.Create(parms);
            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);
        }
Ejemplo n.º 5
0
        public ActionResult <string> CreateDefault([FromBody] DefaultParams p)
        {
            string   playerName = _signInManager.UserManager.GetUserName(this.User);
            GameTask game       = GameManager.Instance.CreateDefaultGame(playerName);

            return(Ok(game.Id));
        }
Ejemplo n.º 6
0
        public void BatchUnbatchPlaintextNET()
        {
            var parms = new EncryptionParameters();

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

            var context = new SEALContext(parms);

            Assert.IsTrue(context.Qualifiers.EnableBatching);

            var crtbuilder = new PolyCRTBuilder(context);

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

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

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

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

            var short_plain = new Plaintext(20);

            for (int i = 0; i < 20; i++)
            {
                short_plain[i] = (UInt64)i;
            }
            crtbuilder.Compose(short_plain);
            crtbuilder.Decompose(short_plain);
            for (int i = 0; i < 20; i++)
            {
                Assert.IsTrue(short_plain[i] == (UInt64)i);
            }
            for (int i = 20; i < crtbuilder.SlotCount; i++)
            {
                Assert.IsTrue(short_plain[i] == 0UL);
            }
        }
Ejemplo n.º 7
0
        public void EncodeDecodeComplexTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = new List <SmallModulus>()
                {
                    DefaultParams.SmallMods40Bit(0),
                    DefaultParams.SmallMods40Bit(1),
                    DefaultParams.SmallMods40Bit(2),
                    DefaultParams.SmallMods40Bit(3)
                }
            };

            SEALContext context = SEALContext.Create(parms);
            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);
        }
Ejemplo n.º 8
0
        public void KeyStepTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = new List <SmallModulus>()
                {
                    DefaultParams.SmallMods60Bit(0)
                }
            };
            SEALContext  context = SEALContext.Create(parms);
            KeyGenerator keygen  = new KeyGenerator(context);

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

            Assert.IsNotNull(keys);

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

            Assert.IsFalse(keys.HasKey(1));
            Assert.IsTrue(keys.HasKey(3));
            Assert.IsFalse(keys.HasKey(5));
            Assert.IsFalse(keys.HasKey(7));
            Assert.IsTrue(keys.HasKey(9));
            Assert.IsFalse(keys.HasKey(11));
            Assert.IsFalse(keys.HasKey(13));
            Assert.IsFalse(keys.HasKey(15));
            Assert.IsFalse(keys.HasKey(17));
            Assert.IsFalse(keys.HasKey(19));
            Assert.IsFalse(keys.HasKey(21));
            Assert.IsFalse(keys.HasKey(23));
            Assert.IsFalse(keys.HasKey(25));
            Assert.IsTrue(keys.HasKey(27));
        }
        public void EncryptionParametersSetNET()
        {
            var parms = new EncryptionParameters();

            parms.NoiseStandardDeviation = 0;
            parms.CoeffModulus           = new List <SmallModulus> {
            };
            parms.PlainModulus           = 0;
            parms.PolyModulus            = "0";

            Assert.AreEqual(0.0, parms.NoiseStandardDeviation);
            Assert.AreEqual(0.0, parms.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus.Count == 0);
            Assert.IsTrue(parms.PlainModulus.Value == 0);
            Assert.IsTrue(parms.PolyModulus.ToString() == "0");

            parms.NoiseStandardDeviation = 0;
            parms.CoeffModulus           = new List <SmallModulus> {
                2
            };
            parms.PlainModulus = 2;
            parms.PolyModulus  = "1";

            Assert.AreEqual(0.0, parms.NoiseStandardDeviation);
            Assert.AreEqual(0.0, parms.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus[0].Value == 2);
            Assert.IsTrue(parms.PlainModulus.Value == 2);
            Assert.IsTrue(parms.PolyModulus.ToString() == "1");

            parms.NoiseStandardDeviation = 3.19;
            parms.CoeffModulus           = new List <SmallModulus> {
                2, 3
            };
            parms.PlainModulus = 2;
            parms.PolyModulus  = "1x^2 + 1";

            Assert.AreEqual(3.19, parms.NoiseStandardDeviation);
            Assert.AreEqual(3.19 * 6, parms.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus[0].Value == 2);
            Assert.IsTrue(parms.CoeffModulus[1].Value == 3);
            Assert.IsTrue(parms.PlainModulus.Value == 2);
            Assert.IsTrue(parms.PolyModulus.ToString() == "1x^2 + 1");

            parms.NoiseStandardDeviation = 3.19;
            parms.CoeffModulus           = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(0),
                DefaultParams.SmallMods40Bit(0),
                DefaultParams.SmallMods50Bit(0)
            };
            parms.PlainModulus = 2;
            parms.PolyModulus  = "1x^128 + 1";

            Assert.AreEqual(3.19, parms.NoiseStandardDeviation);
            Assert.AreEqual(3.19 * 6, parms.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus[0].Equals(DefaultParams.SmallMods30Bit(0)));
            Assert.IsTrue(parms.CoeffModulus[1].Equals(DefaultParams.SmallMods40Bit(0)));
            Assert.IsTrue(parms.CoeffModulus[2].Equals(DefaultParams.SmallMods50Bit(0)));
            Assert.IsTrue(parms.PolyModulus.ToString() == "1x^128 + 1");
        }
        public void EncryptionParametersCompareNET()
        {
            var parms1 = new EncryptionParameters();

            parms1.NoiseStandardDeviation = 3.19;
            parms1.CoeffModulus           = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(0)
            };
            parms1.PlainModulus = 1 << 6;
            parms1.PolyModulus  = "1x^64 + 1";

            var parms2 = new EncryptionParameters(parms1);

            Assert.IsTrue(parms1.Equals(parms2));

            var parms3 = new EncryptionParameters();

            parms3.Set(parms2);
            Assert.IsTrue(parms3.Equals(parms2));
            parms3.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(1)
            };
            Assert.IsFalse(parms3.Equals(parms2));

            parms3.Set(parms2);
            Assert.IsTrue(parms3.Equals(parms2));
            parms3.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods30Bit(1)
            };
            Assert.IsFalse(parms3.Equals(parms2));

            parms3.Set(parms2);
            parms3.PolyModulus = "1x^128 + 1";
            Assert.IsFalse(parms3.Equals(parms1));

            parms3.Set(parms2);
            parms3.PlainModulus = (1 << 6) + 1;
            Assert.IsFalse(parms3.Equals(parms2));

            parms3.Set(parms2);
            parms3.NoiseStandardDeviation = 3.18;
            Assert.IsFalse(parms3.Equals(parms2));

            parms3.Set(parms2);
            parms3.PolyModulus = "1";
            parms3.PolyModulus = "1x^128 + 1";
            parms3.PolyModulus = "1x^64 + 1";
            Assert.IsTrue(parms3.Equals(parms1));

            parms3.Set(parms2);
            parms3.CoeffModulus = new List <SmallModulus> {
                2
            };
            parms3.CoeffModulus = new List <SmallModulus> {
                DefaultParams.SmallMods50Bit(0)
            };
            parms3.CoeffModulus = parms2.CoeffModulus;
            Assert.IsTrue(parms3.Equals(parms2));
        }
Ejemplo n.º 11
0
        public void SmallMods60BitTest()
        {
            SmallModulus sm = DefaultParams.SmallMods60Bit(45);

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

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

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

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

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

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

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

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

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

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

                keygen.GenerateEvaluationKeys(4, 1, evk);
                Assert.AreEqual(evk.HashBlock, parms.HashBlock);
                Assert.AreEqual(30, evk.Key(2)[0].Size);
            }
        }
Ejemplo n.º 13
0
        public HomomorphicKeyManager()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV);

            parms.PolyModulusDegree = 2048;
            parms.CoeffModulus      = DefaultParams.CoeffModulus128(polyModulusDegree: 2048);
            parms.PlainModulus      = new SmallModulus(1 << 8);
            Context = SEALContext.Create(parms);
        }
Ejemplo n.º 14
0
        static SEALContext createContext(ulong keysize)
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV);

            parms.PolyModulusDegree = keysize;
            parms.CoeffModulus      = DefaultParams.CoeffModulus128(polyModulusDegree: keysize);
            parms.PlainModulus      = new SmallModulus(1 << 8);
            return(SEALContext.Create(parms));
        }
Ejemplo n.º 15
0
        public void SEALContextParamsTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>
            {
                DefaultParams.SmallMods30Bit(0),
                DefaultParams.SmallMods30Bit(1),
                DefaultParams.SmallMods30Bit(2)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 128,
                PlainModulus      = new SmallModulus(1 << 6),
                CoeffModulus      = coeffModulus
            };
            SEALContext context = SEALContext.Create(parms);

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

            EncryptionParameters parms2 = data.Parms;

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

            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.IsTrue(qualifiers.UsingHEStdSecurity);

            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);

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

            SEALContext.ContextData data3 = data2.NextContextData;
            Assert.IsNotNull(data3);
            Assert.AreEqual(0ul, data3.ChainIndex);

            Assert.IsNull(data3.NextContextData);
        }
Ejemplo n.º 16
0
        public void SmallMods30BitTest()
        {
            SmallModulus sm = DefaultParams.SmallMods30Bit(20);

            Assert.IsNotNull(sm);
            Assert.AreEqual(30, sm.BitCount);
            Assert.AreEqual(0x3BE80001ul, sm.Value);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => DefaultParams.SmallMods30Bit(64));
        }
Ejemplo n.º 17
0
        public void SaveLoadTest()
        {
            TestDelegate save_load_test = delegate(SchemeType scheme)
            {
                List <SmallModulus> coeffModulus = new List <SmallModulus>
                {
                    DefaultParams.SmallMods40Bit(0),
                    DefaultParams.SmallMods40Bit(1)
                };
                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]);
                Assert.AreEqual(parms.NoiseMaxDeviation, loaded.NoiseMaxDeviation, delta: 0.001);
                Assert.AreEqual(parms.NoiseStandardDeviation, loaded.NoiseStandardDeviation, delta: 0.001);
            };

            save_load_test(SchemeType.BFV);
            save_load_test(SchemeType.CKKS);
        }
Ejemplo n.º 18
0
        static GlobalContext()
        {
            EncryptionParameters encParams = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 4096,
                CoeffModulus      = DefaultParams.CoeffModulus128(polyModulusDegree: 4096)
            };

            encParams.SetPlainModulus(0x133Ful);
            Context = SEALContext.Create(encParams);
        }
Ejemplo n.º 19
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);
        }
        public void EncryptionParametersSaveLoadNET()
        {
            var stream = new MemoryStream();
            var parms  = new EncryptionParameters();
            var parms2 = new EncryptionParameters();

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

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

            parms.NoiseStandardDeviation = 3.19;
            parms.CoeffModulus           = new List <SmallModulus> {
                DefaultParams.SmallMods30Bit(0),
                DefaultParams.SmallMods60Bit(0),
                DefaultParams.SmallMods60Bit(1)
            };
            parms.PlainModulus = 1 << 30;
            parms.PolyModulus  = "1x^256 + 1";
            stream.Seek(0, SeekOrigin.Begin);
            parms.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            parms2.Load(stream);
            Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation);
            Assert.AreEqual(parms.NoiseMaxDeviation, parms2.NoiseMaxDeviation);
            Assert.IsTrue(parms.CoeffModulus.SequenceEqual(parms2.CoeffModulus));
            Assert.IsTrue(parms.PlainModulus.Equals(parms2.PlainModulus));
            Assert.IsTrue(parms.PolyModulus.Equals(parms2.PolyModulus));
            Assert.IsTrue(parms.Equals(parms2));
        }
Ejemplo n.º 21
0
 void Start()
 {
     animator               = GetComponent <Animator>();
     combatCanvas           = GetComponentInChildren <Canvas>();
     _controller            = GetComponent <EnemyController>();
     _boxCollider           = GetComponent <BoxCollider2D>();
     _targetPosition        = GameManager.instance._treeRef.transform.position;
     _moveDirection         = (_targetPosition.x - transform.position.x > 0 ? Vector2.right : Vector2.left);
     _isDead                = false;
     kinematicMotionEnabled = false;
     _velocity              = new Vector2(moveVelocity * _moveDirection.x, 0f);
     _defaultParams         = new DefaultParams(moveVelocity, hitPoints, damage);
 }
Ejemplo n.º 22
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));
        }
Ejemplo n.º 23
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);
            }
        }
Ejemplo n.º 24
0
        public void Coeffs256Test()
        {
            List <SmallModulus> coeffs = new List <SmallModulus>(DefaultParams.CoeffModulus256(4096));

            Assert.IsNotNull(coeffs);
            Assert.AreEqual(1, coeffs.Count);
            Assert.AreEqual(0x03ffffffff040001ul, coeffs[0].Value);

            coeffs = new List <SmallModulus>(DefaultParams.CoeffModulus256(8192));

            Assert.IsNotNull(coeffs);
            Assert.AreEqual(2, coeffs.Count);
            Assert.AreEqual(0x07ffffffffcc0001ul, coeffs[0].Value);
            Assert.AreEqual(0x07ffffffffb00001ul, coeffs[1].Value);
        }
Ejemplo n.º 25
0
        public void Coeffs192Test()
        {
            List <SmallModulus> coeffs = new List <SmallModulus>(DefaultParams.CoeffModulus192(4096));

            Assert.IsNotNull(coeffs);
            Assert.AreEqual(2, coeffs.Count);
            Assert.AreEqual(0x0000003fffe80001ul, coeffs[0].Value);
            Assert.AreEqual(0x0000001ffffc0001ul, coeffs[1].Value);

            coeffs = new List <SmallModulus>(DefaultParams.CoeffModulus192(8192));

            Assert.IsNotNull(coeffs);
            Assert.AreEqual(3, coeffs.Count);
            Assert.AreEqual(0x0007ffffff9c0001ul, coeffs[0].Value);
            Assert.AreEqual(0x0007ffffff900001ul, coeffs[1].Value);
            Assert.AreEqual(0x0003ffffffb80001ul, coeffs[2].Value);
        }
Ejemplo n.º 26
0
        public void SaveLoadTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>
            {
                DefaultParams.SmallMods40Bit(0)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 64,
                PlainModulus      = new SmallModulus(1 << 6),
                CoeffModulus      = coeffModulus
            };
            SEALContext  context = SEALContext.Create(parms);
            KeyGenerator keygen  = new KeyGenerator(context);

            SecretKey secret = keygen.SecretKey;

            Assert.AreEqual(64ul, secret.Data.CoeffCount);
            Assert.IsTrue(secret.Data.IsNTTForm);
            Assert.AreNotEqual(ParmsId.Zero, secret.ParmsId);

            SecretKey        secret2 = new SecretKey();
            MemoryPoolHandle handle  = secret2.Pool;

            Assert.IsNotNull(secret2);
            Assert.AreEqual(0ul, secret2.Data.CoeffCount);
            Assert.IsFalse(secret2.Data.IsNTTForm);
            ulong alloced = handle.AllocByteCount;

            using (MemoryStream stream = new MemoryStream())
            {
                secret.Save(stream);

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

                secret2.Load(context, stream);
            }

            Assert.AreNotSame(secret, secret2);
            Assert.AreEqual(64ul, secret2.Data.CoeffCount);
            Assert.IsTrue(secret2.Data.IsNTTForm);
            Assert.AreNotEqual(ParmsId.Zero, secret2.ParmsId);
            Assert.AreEqual(secret.ParmsId, secret2.ParmsId);
            Assert.IsTrue(handle.AllocByteCount != alloced);
        }
Ejemplo n.º 27
0
        public void ExceptionsTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>()
            {
                DefaultParams.SmallMods60Bit(0)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 64,
                CoeffModulus      = coeffModulus,
                PlainModulus      = new SmallModulus(257)
            };

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

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

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

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

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

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

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

            Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null));
            Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, pool_uninit));
        }
Ejemplo n.º 28
0
    public void SetParams(int level, Vector2 position)
    {
        gameObject.SetActive(true);
        transform.position = position;

        if (_defaultParams == null)
        {
            _defaultParams = new DefaultParams(moveVelocity, hitPoints, damage);
        }

        _defaultParams.Reset(this);

        this.damage              += this.damage * (float)level / 10;
        this.hitPoints           += this.hitPoints * (float)level / 10;
        this.transform.localScale = new Vector3(currentScale + (float)level / 50, currentScale + (float)level / 50, 0);

        //Debug.Log(gameObject.name + "  level: " + level + " hp: " + hitPoints + " damage: " + damage);
    }
Ejemplo n.º 29
0
        public void EqualsTest()
        {
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 8,
                PlainModulus      = new SmallModulus(257),
                CoeffModulus      = new List <SmallModulus>()
                {
                    DefaultParams.SmallMods40Bit(0),
                    DefaultParams.SmallMods40Bit(1)
                }
            };

            EncryptionParameters parms2 = new EncryptionParameters(SchemeType.CKKS);

            Assert.AreNotEqual(parms, parms2);
            Assert.IsFalse(parms.Equals(null));
        }
Ejemplo n.º 30
0
        public void SaveLoadTest()
        {
            List <SmallModulus> coeffModulus = new List <SmallModulus>
            {
                DefaultParams.SmallMods40Bit(0)
            };
            EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
            {
                PolyModulusDegree = 64,
                PlainModulus      = new SmallModulus(1 << 6),
                CoeffModulus      = coeffModulus
            };
            SEALContext  context = SEALContext.Create(parms);
            KeyGenerator keygen  = new KeyGenerator(context);

            PublicKey pub = keygen.PublicKey;

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

            PublicKey        pub2   = new PublicKey();
            MemoryPoolHandle handle = pub2.Pool;

            Assert.AreEqual(0ul, pub2.Data.Size);
            Assert.IsFalse(pub2.Data.IsNTTForm);
            Assert.AreEqual(ParmsId.Zero, pub2.ParmsId);

            using (MemoryStream stream = new MemoryStream())
            {
                pub.Save(stream);

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

                pub2.Load(context, stream);
            }

            Assert.AreNotSame(pub, pub2);
            Assert.AreEqual(2ul, pub2.Data.Size);
            Assert.IsTrue(pub2.Data.IsNTTForm);
            Assert.AreEqual(pub.ParmsId, pub2.ParmsId);
            Assert.AreNotEqual(ParmsId.Zero, pub2.ParmsId);
            Assert.IsTrue(handle.AllocByteCount != 0ul);
        }