Exemplo n.º 1
0
        public void ExceptionsTest()
        {
            SEALContext      context     = GlobalContext.BFVContext;
            Plaintext        plain       = new Plaintext();
            MemoryPoolHandle pool        = MemoryManager.GetPool(MMProfOpt.ForceGlobal);
            MemoryPoolHandle pool_uninit = new MemoryPoolHandle();

            Assert.ThrowsException <ArgumentException>(() => plain     = new Plaintext(pool_uninit));
            Assert.ThrowsException <ArgumentNullException>(() => plain = new Plaintext((string)null, pool));

            Assert.ThrowsException <ArgumentNullException>(() => plain.Set((Plaintext)null));
            Assert.ThrowsException <ArgumentNullException>(() => plain.Set((string)null));

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => plain.SetZero(100000));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => plain.SetZero(1, 100000));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => plain.SetZero(100000, 1));

            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsValidFor(plain, null));
            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsMetadataValidFor(plain, null));

            Assert.ThrowsException <ArgumentNullException>(() => plain.Save(null));

            Assert.ThrowsException <ArgumentNullException>(() => plain.UnsafeLoad(null));
            Assert.ThrowsException <ArgumentException>(() => plain.UnsafeLoad(new MemoryStream()));

            Assert.ThrowsException <ArgumentNullException>(() => plain.Load(context, null));
            Assert.ThrowsException <ArgumentNullException>(() => plain.Load(null, new MemoryStream()));
        }
Exemplo n.º 2
0
        public void SaveLoadTest()
        {
            SEALContext  context = GlobalContext.BFVContext;
            KeyGenerator keygen  = new KeyGenerator(context);

            RelinKeys keys = keygen.RelinKeys();

            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.IsMetadataValidFor(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.CoeffModCount, otherCipher.Data.CoeffModCount);

                    ulong coeffCount = keysCipher.Data.Size * keysCipher.Data.PolyModulusDegree * keysCipher.Data.CoeffModCount;
                    for (ulong k = 0; k < coeffCount; k++)
                    {
                        Assert.AreEqual(keysCipher.Data[k], otherCipher.Data[k]);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void ExceptionsTest()
        {
            SEALContext      context = GlobalContext.BFVContext;
            MemoryPoolHandle pool    = MemoryManager.GetPool(MMProfOpt.ForceGlobal);
            MemoryPoolHandle poolu   = new MemoryPoolHandle();
            Ciphertext       cipher  = new Ciphertext();
            Ciphertext       copy    = null;

            Assert.ThrowsException <ArgumentNullException>(() => copy = new Ciphertext((Ciphertext)null));

            Assert.ThrowsException <ArgumentNullException>(() => cipher = new Ciphertext(context, null, pool));
            Assert.ThrowsException <ArgumentNullException>(() => cipher = new Ciphertext(null, context.FirstParmsId, pool));
            Assert.ThrowsException <ArgumentException>(() => cipher     = new Ciphertext(context, ParmsId.Zero, pool));

            Assert.ThrowsException <ArgumentNullException>(() => cipher = new Ciphertext((SEALContext)null, poolu));
            Assert.ThrowsException <ArgumentException>(() => cipher     = new Ciphertext(context, poolu));

            Assert.ThrowsException <ArgumentNullException>(() => cipher = new Ciphertext(context, null, 6ul));
            Assert.ThrowsException <ArgumentNullException>(() => cipher = new Ciphertext(null, context.FirstParmsId, 6ul, poolu));
            Assert.ThrowsException <ArgumentException>(() => cipher     = new Ciphertext(context, ParmsId.Zero, 6ul, poolu));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Reserve(context, null, 10ul));
            Assert.ThrowsException <ArgumentNullException>(() => cipher.Reserve(null, ParmsId.Zero, 10ul));
            Assert.ThrowsException <ArgumentException>(() => cipher.Reserve(context, ParmsId.Zero, 10ul));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Reserve(null, 10ul));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Resize(context, null, 10ul));
            Assert.ThrowsException <ArgumentNullException>(() => cipher.Resize(null, ParmsId.Zero, 10ul));
            Assert.ThrowsException <ArgumentException>(() => cipher.Resize(context, ParmsId.Zero, 10ul));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Resize(null, 10ul));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Set(null));

            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsValidFor(cipher, null));
            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsMetadataValidFor(cipher, null));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Save(null));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.UnsafeLoad(null));
            Assert.ThrowsException <ArgumentException>(() => cipher.UnsafeLoad(new MemoryStream()));

            Assert.ThrowsException <ArgumentNullException>(() => cipher.Load(null, new MemoryStream()));
            Assert.ThrowsException <ArgumentNullException>(() => cipher.Load(context, null));
        }
Exemplo n.º 4
0
        public void ExceptionsTest()
        {
            RelinKeys   keys    = new RelinKeys();
            SEALContext context = GlobalContext.BFVContext;

            Assert.ThrowsException <ArgumentNullException>(() => keys = new RelinKeys(null));

            Assert.ThrowsException <ArgumentNullException>(() => keys.Set(null));

            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsValidFor(keys, null));
            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsMetadataValidFor(keys, null));

            Assert.ThrowsException <ArgumentNullException>(() => keys.Save(null));

            Assert.ThrowsException <ArgumentNullException>(() => keys.Load(context, null));
            Assert.ThrowsException <ArgumentNullException>(() => keys.Load(null, new MemoryStream()));
            Assert.ThrowsException <ArgumentException>(() => keys.Load(context, new MemoryStream()));
            Assert.ThrowsException <ArgumentNullException>(() => keys.UnsafeLoad(null));
        }
Exemplo n.º 5
0
        public void ExceptionsTest()
        {
            SEALContext context = GlobalContext.BFVContext;
            SecretKey   key     = new SecretKey();

            Assert.ThrowsException <ArgumentNullException>(() => key = new SecretKey(null));

            Assert.ThrowsException <ArgumentNullException>(() => key.Set(null));

            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsValidFor(key, null));
            Assert.ThrowsException <ArgumentNullException>(() => ValCheck.IsMetadataValidFor(key, null));

            Assert.ThrowsException <ArgumentNullException>(() => key.Save(null));
            Assert.ThrowsException <ArgumentNullException>(() => key.UnsafeLoad(null));

            Assert.ThrowsException <ArgumentNullException>(() => key.Load(context, null));
            Assert.ThrowsException <ArgumentNullException>(() => key.Load(null, new MemoryStream()));
            Assert.ThrowsException <ArgumentException>(() => key.Load(context, new MemoryStream()));
        }
Exemplo n.º 6
0
        public void SaveLoadTest()
        {
            SEALContext  context   = GlobalContext.BFVContext;
            KeyGenerator keygen    = new KeyGenerator(context);
            Encryptor    encryptor = new Encryptor(context, keygen.PublicKey);
            Plaintext    plain     = new Plaintext("2x^3 + 4x^2 + 5x^1 + 6");
            Ciphertext   cipher    = new Ciphertext();

            encryptor.Encrypt(plain, cipher);

            Assert.AreEqual(2ul, cipher.Size);
            Assert.AreEqual(8192ul, cipher.PolyModulusDegree);
            Assert.AreEqual(4ul, cipher.CoeffModCount);

            Ciphertext loaded = new Ciphertext();

            Assert.AreEqual(0ul, loaded.Size);
            Assert.AreEqual(0ul, loaded.PolyModulusDegree);
            Assert.AreEqual(0ul, loaded.CoeffModCount);

            using (MemoryStream mem = new MemoryStream())
            {
                cipher.Save(mem);

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

                loaded.Load(context, mem);
            }

            Assert.AreEqual(2ul, loaded.Size);
            Assert.AreEqual(8192ul, loaded.PolyModulusDegree);
            Assert.AreEqual(4ul, loaded.CoeffModCount);
            Assert.IsTrue(ValCheck.IsMetadataValidFor(loaded, context));

            ulong ulongCount = cipher.Size * cipher.PolyModulusDegree * cipher.CoeffModCount;

            for (ulong i = 0; i < ulongCount; i++)
            {
                Assert.AreEqual(cipher[i], loaded[i]);
            }
        }
Exemplo n.º 7
0
        public void SaveLoadTest()
        {
            SEALContext context = GlobalContext.BFVContext;
            Plaintext   plain   = new Plaintext("6x^5 + 5x^4 + 4x^3 + 3x^2 + 2x^1 + 5");
            Plaintext   other   = new Plaintext();

            Assert.AreNotSame(plain, other);
            Assert.AreNotEqual(plain, other);

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

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

                other.Load(context, stream);
            }

            Assert.AreNotSame(plain, other);
            Assert.AreEqual(plain, other);
            Assert.IsTrue(ValCheck.IsMetadataValidFor(other, context));
        }