// Adapted from https://github.com/microsoft/SEAL/blob/master/dotnet/tests/CiphertextTests.cs#L113 static void SaveLoadTest(Func <Ciphertext, SEALContext, Ciphertext> roundtrip) { SEALContext context = GlobalContext.Context; 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); var loaded = roundtrip(cipher, context); Assert.AreEqual(2ul, loaded.Size); Assert.AreEqual(8192ul, loaded.PolyModulusDegree); Assert.AreEqual(4ul, loaded.CoeffModCount); Assert.IsTrue(ValCheck.IsValidFor(loaded, context)); ulong ulongCount = cipher.Size * cipher.PolyModulusDegree * cipher.CoeffModCount; for (ulong i = 0; i < ulongCount; i++) { Assert.AreEqual(cipher[i], loaded[i]); } }
public void ExceptionsTest() { SEALContext context = GlobalContext.BFVContext; Plaintext plain = new Plaintext(); MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal); MemoryPoolHandle pool_uninit = new MemoryPoolHandle(); Utilities.AssertThrows <ArgumentException>(() => plain = new Plaintext(pool_uninit)); Utilities.AssertThrows <ArgumentNullException>(() => plain = new Plaintext((string)null, pool)); Utilities.AssertThrows <ArgumentNullException>(() => plain.Set((Plaintext)null)); Utilities.AssertThrows <ArgumentNullException>(() => plain.Set((string)null)); Utilities.AssertThrows <ArgumentOutOfRangeException>(() => plain.SetZero(100000)); Utilities.AssertThrows <ArgumentOutOfRangeException>(() => plain.SetZero(1, 100000)); Utilities.AssertThrows <ArgumentOutOfRangeException>(() => plain.SetZero(100000, 1)); Utilities.AssertThrows <ArgumentNullException>(() => ValCheck.IsValidFor(plain, null)); Utilities.AssertThrows <ArgumentNullException>(() => plain.Save(null)); Utilities.AssertThrows <ArgumentNullException>(() => plain.UnsafeLoad(null, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => plain.UnsafeLoad(context, null)); Utilities.AssertThrows <EndOfStreamException>(() => plain.UnsafeLoad(context, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => plain.Load(context, null)); Utilities.AssertThrows <ArgumentNullException>(() => plain.Load(null, new MemoryStream())); }
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]); } } } }
public void SaveLoadTest() { SEALContext context = GlobalContext.BFVContext; KeyGenerator keygen = new KeyGenerator(context); keygen.CreateGaloisKeys(out GaloisKeys keys); GaloisKeys other = new GaloisKeys(); Assert.IsNotNull(keys); Assert.AreEqual(24ul, keys.Size); using (MemoryStream ms = new MemoryStream()) { keys.Save(ms); ms.Seek(offset: 0, loc: SeekOrigin.Begin); other.Load(context, ms); } Assert.AreEqual(24ul, other.Size); Assert.IsTrue(ValCheck.IsValidFor(other, context)); List <IEnumerable <PublicKey> > keysData = new List <IEnumerable <PublicKey> >(keys.Data); List <IEnumerable <PublicKey> > otherData = new List <IEnumerable <PublicKey> >(other.Data); Assert.AreEqual(keysData.Count, otherData.Count); for (int i = 0; i < keysData.Count; i++) { List <PublicKey> keysCiphers = new List <PublicKey>(keysData[i]); List <PublicKey> otherCiphers = new List <PublicKey>(otherData[i]); Assert.AreEqual(keysCiphers.Count, otherCiphers.Count); for (int j = 0; j < keysCiphers.Count; j++) { PublicKey keysCipher = keysCiphers[j]; PublicKey otherCipher = otherCiphers[j]; Assert.AreEqual(keysCipher.Data.Size, otherCipher.Data.Size); Assert.AreEqual(keysCipher.Data.PolyModulusDegree, otherCipher.Data.PolyModulusDegree); Assert.AreEqual(keysCipher.Data.CoeffModulusSize, otherCipher.Data.CoeffModulusSize); ulong coeffCount = keysCipher.Data.Size * keysCipher.Data.PolyModulusDegree * keysCipher.Data.CoeffModulusSize; for (ulong k = 0; k < coeffCount; k++) { Assert.AreEqual(keysCipher.Data[k], otherCipher.Data[k]); } } } }
public void ExceptionsTest() { SEALContext context = GlobalContext.BFVContext; MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal); MemoryPoolHandle poolu = new MemoryPoolHandle(); Ciphertext cipher = new Ciphertext(); Ciphertext copy = null; Utilities.AssertThrows <ArgumentNullException>(() => copy = new Ciphertext((Ciphertext)null)); Utilities.AssertThrows <ArgumentNullException>(() => cipher = new Ciphertext(context, null, pool)); Utilities.AssertThrows <ArgumentNullException>(() => cipher = new Ciphertext(null, context.FirstParmsId, pool)); Utilities.AssertThrows <ArgumentException>(() => cipher = new Ciphertext(context, ParmsId.Zero, pool)); Utilities.AssertThrows <ArgumentNullException>(() => cipher = new Ciphertext((SEALContext)null, poolu)); Utilities.AssertThrows <ArgumentException>(() => cipher = new Ciphertext(context, poolu)); Utilities.AssertThrows <ArgumentNullException>(() => cipher = new Ciphertext(context, null, 6ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher = new Ciphertext(null, context.FirstParmsId, 6ul, poolu)); Utilities.AssertThrows <ArgumentException>(() => cipher = new Ciphertext(context, ParmsId.Zero, 6ul, poolu)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Reserve(context, null, 10ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Reserve(null, ParmsId.Zero, 10ul)); Utilities.AssertThrows <ArgumentException>(() => cipher.Reserve(context, ParmsId.Zero, 10ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Reserve(null, 10ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Resize(context, null, 10ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Resize(null, ParmsId.Zero, 10ul)); Utilities.AssertThrows <ArgumentException>(() => cipher.Resize(context, ParmsId.Zero, 10ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Resize(null, 10ul)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Set(null)); Utilities.AssertThrows <ArgumentNullException>(() => ValCheck.IsValidFor(cipher, null)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Save(null)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.UnsafeLoad(context, null)); Utilities.AssertThrows <ArgumentNullException>(() => cipher.UnsafeLoad(null, new MemoryStream())); Utilities.AssertThrows <EndOfStreamException>(() => cipher.UnsafeLoad(context, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Load(null, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => cipher.Load(context, null)); }
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.IsValidFor(other, context)); } { SEALContext context = GlobalContext.BGVContext; 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.IsValidFor(other, context)); } }
public void ExceptionsTest() { RelinKeys keys = new RelinKeys(); SEALContext context = GlobalContext.BFVContext; Utilities.AssertThrows <ArgumentNullException>(() => keys = new RelinKeys(null)); Utilities.AssertThrows <ArgumentNullException>(() => keys.Set(null)); Utilities.AssertThrows <ArgumentNullException>(() => ValCheck.IsValidFor(keys, null)); Utilities.AssertThrows <ArgumentNullException>(() => keys.Save(null)); Utilities.AssertThrows <ArgumentNullException>(() => keys.Load(context, null)); Utilities.AssertThrows <ArgumentNullException>(() => keys.Load(null, new MemoryStream())); Utilities.AssertThrows <EndOfStreamException>(() => keys.Load(context, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => keys.UnsafeLoad(null, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => keys.UnsafeLoad(context, null)); }
public void BGVSaveLoadTest() { SEALContext context = GlobalContext.BGVContext; KeyGenerator keygen = new KeyGenerator(context); keygen.CreatePublicKey(out PublicKey publicKey); Encryptor encryptor = new Encryptor(context, 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.CoeffModulusSize); Ciphertext loaded = new Ciphertext(); Assert.AreEqual(0ul, loaded.Size); Assert.AreEqual(0ul, loaded.PolyModulusDegree); Assert.AreEqual(0ul, loaded.CoeffModulusSize); 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.CoeffModulusSize); Assert.IsTrue(ValCheck.IsValidFor(loaded, context)); ulong ulongCount = cipher.Size * cipher.PolyModulusDegree * cipher.CoeffModulusSize; for (ulong i = 0; i < ulongCount; i++) { Assert.AreEqual(cipher[i], loaded[i]); } }
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)); }
public void ExceptionsTest() { SEALContext context = GlobalContext.BFVContext; SecretKey key = new SecretKey(); Utilities.AssertThrows <ArgumentNullException>(() => key = new SecretKey(null)); Utilities.AssertThrows <ArgumentNullException>(() => key.Set(null)); Utilities.AssertThrows <ArgumentNullException>(() => ValCheck.IsValidFor(key, null)); Utilities.AssertThrows <ArgumentNullException>(() => key.Save(null)); Utilities.AssertThrows <ArgumentNullException>(() => key.UnsafeLoad(null, new MemoryStream())); Utilities.AssertThrows <ArgumentNullException>(() => key.UnsafeLoad(context, null)); Utilities.AssertThrows <ArgumentNullException>(() => key.Load(context, null)); Utilities.AssertThrows <ArgumentNullException>(() => key.Load(null, new MemoryStream())); Utilities.AssertThrows <EndOfStreamException>(() => key.Load(context, new MemoryStream())); }
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())); }