public void KeyTest() { SEALContext context = GlobalContext.BFVContext; KeyGenerator keygen = new KeyGenerator(context); GaloisKeys keys = keygen.GaloisKeysLocal(); MemoryPoolHandle handle = keys.Pool; Assert.IsNotNull(keys); Assert.AreEqual(24ul, keys.Size); Assert.IsFalse(keys.HasKey(galoisElt: 1)); Assert.IsTrue(keys.HasKey(galoisElt: 3)); Assert.IsFalse(keys.HasKey(galoisElt: 5)); Assert.IsFalse(keys.HasKey(galoisElt: 7)); Assert.IsTrue(keys.HasKey(galoisElt: 9)); Assert.IsFalse(keys.HasKey(galoisElt: 11)); IEnumerable <PublicKey> key = keys.Key(3); Assert.AreEqual(4, key.Count()); IEnumerable <PublicKey> key2 = keys.Key(9); Assert.AreEqual(4, key2.Count()); Assert.IsTrue(handle.AllocByteCount > 0ul); }
public void ExceptionsTest() { SEALContext context = GlobalContext.Context; 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>(() => plain.IsValidFor(null)); Assert.ThrowsException <ArgumentNullException>(() => plain.IsMetadataValidFor(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())); }
public void KeyTest() { SEALContext context = GlobalContext.Context; KeyGenerator keygen = new KeyGenerator(context); GaloisKeys keys = keygen.GaloisKeys(decompositionBitCount: 30); MemoryPoolHandle handle = keys.Pool; Assert.IsNotNull(keys); Assert.AreEqual(30, keys.DecompositionBitCount); Assert.AreEqual(22ul, keys.Size); Assert.IsFalse(keys.HasKey(galoisElt: 1)); Assert.IsTrue(keys.HasKey(galoisElt: 3)); Assert.IsFalse(keys.HasKey(galoisElt: 5)); Assert.IsFalse(keys.HasKey(galoisElt: 7)); Assert.IsTrue(keys.HasKey(galoisElt: 9)); Assert.IsFalse(keys.HasKey(galoisElt: 11)); IEnumerable <Ciphertext> key = keys.Key(3); Assert.AreEqual(2, key.Count()); IEnumerable <Ciphertext> key2 = keys.Key(9); Assert.AreEqual(2, key2.Count()); Assert.IsTrue(handle.AllocByteCount > 0ul); }
public void SwitchProfileTest() { MemoryPoolHandle handle = MemoryManager.GetPool(MMProfOpt.ForceNew); MMProfFixed fixedProfile = new MMProfFixed(handle); MMProf oldProfile = MemoryManager.SwitchProfile(fixedProfile); Assert.IsInstanceOfType(oldProfile, typeof(MMProfGlobal)); MMProfNew newProfile = new MMProfNew(); oldProfile = MemoryManager.SwitchProfile(newProfile); Assert.IsInstanceOfType(oldProfile, typeof(MMProfFixed)); MMProfGlobal globalProfile = new MMProfGlobal(); oldProfile = MemoryManager.SwitchProfile(globalProfile); Assert.IsInstanceOfType(oldProfile, typeof(MMProfNew)); MemoryPoolHandle globalHandle = globalProfile.GetPool(); Assert.IsNotNull(globalHandle); Assert.IsTrue(globalHandle.IsInitialized); }
public void CreateTest() { MemoryPoolHandle handle = MemoryManager.GetPool(); Assert.IsNotNull(handle); Assert.IsTrue(handle.IsInitialized); MemoryPoolHandle handle2 = new MemoryPoolHandle(handle); Assert.IsTrue(handle2.IsInitialized); Assert.AreEqual(handle.PoolCount, handle2.PoolCount); Assert.AreEqual(handle.AllocByteCount, handle2.AllocByteCount); MemoryPoolHandle handle5 = new MemoryPoolHandle(); handle5.Set(handle); Assert.IsTrue(handle5.IsInitialized); Assert.AreEqual(handle.PoolCount, handle5.PoolCount); Assert.AreEqual(handle.AllocByteCount, handle5.AllocByteCount); MemoryPoolHandle handle3 = MemoryManager.GetPool(MMProfOpt.ForceNew, clearOnDestruction: true); Assert.IsNotNull(handle3); Assert.AreEqual(0ul, handle3.PoolCount); Assert.AreEqual(0ul, handle3.AllocByteCount); MemoryPoolHandle handle4 = MemoryManager.GetPool(MMProfOpt.ForceThreadLocal); Assert.IsNotNull(handle4); Assert.AreEqual(0ul, handle4.PoolCount); Assert.AreEqual(0ul, handle4.AllocByteCount); }
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); }
public void ExceptionsTest() { MemoryPoolHandle handle = new MemoryPoolHandle(); Utilities.AssertThrows <ArgumentNullException>(() => handle = new MemoryPoolHandle(null)); Utilities.AssertThrows <ArgumentNullException>(() => handle.Set(null)); }
public void ExceptionsTest() { MemoryPoolHandle handle = new MemoryPoolHandle(); Assert.ThrowsException <ArgumentNullException>(() => handle = new MemoryPoolHandle(null)); Assert.ThrowsException <ArgumentNullException>(() => handle.Set(null)); }
public void TransformEncryptedToFromNTTNET() { var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("FFFFFFFFC001"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); parms.Validate(); var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew()); keygen.Generate(); var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew()); var evaluator = new Evaluator(parms, MemoryPoolHandle.AcquireNew()); var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew()); var plain = new BigPoly(65, 1); var cipher = new BigPolyArray(2, 65, 1); plain.Set("0"); encryptor.Encrypt(plain, cipher); evaluator.TransformToNTT(cipher); evaluator.TransformFromNTT(cipher); decryptor.Decrypt(cipher, plain); Assert.IsTrue(plain.ToString() == "0"); plain.Set("1"); encryptor.Encrypt(plain, cipher); evaluator.TransformToNTT(cipher); evaluator.TransformFromNTT(cipher); decryptor.Decrypt(cipher, plain); Assert.IsTrue(plain.ToString() == "1"); plain.Set("Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5"); encryptor.Encrypt(plain, cipher); evaluator.TransformToNTT(cipher); evaluator.TransformFromNTT(cipher); decryptor.Decrypt(cipher, plain); Assert.IsTrue(plain.ToString() == "Fx^10 + Ex^9 + Dx^8 + Cx^7 + Bx^6 + Ax^5 + 1x^4 + 2x^3 + 3x^2 + 4x^1 + 5"); }
public void AllocByteCountUninitializedTest() { MemoryPoolHandle handle = new MemoryPoolHandle(); Assert.IsFalse(handle.IsInitialized); ulong count = handle.AllocByteCount; Assert.AreEqual(0ul, count); }
public void FVEncryptSquareDecryptNET() { var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("FFFFFFFFC001"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); parms.Validate(); var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew()); keygen.Generate(); var encoder = new BalancedEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew()); var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew()); var evaluator = new Evaluator(parms, MemoryPoolHandle.AcquireNew()); var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew()); var encrypted1 = encryptor.Encrypt(encoder.Encode(1)); var product = evaluator.Square(encrypted1); Assert.AreEqual(1UL, encoder.DecodeUInt64(decryptor.Decrypt(product))); encrypted1 = encryptor.Encrypt(encoder.Encode(0)); product = evaluator.Square(encrypted1); Assert.AreEqual(0UL, encoder.DecodeUInt64(decryptor.Decrypt(product))); encrypted1 = encryptor.Encrypt(encoder.Encode(-5)); product = evaluator.Square(encrypted1); Assert.AreEqual(25UL, encoder.DecodeUInt64(decryptor.Decrypt(product))); encrypted1 = encryptor.Encrypt(encoder.Encode(-1)); product = evaluator.Square(encrypted1); Assert.AreEqual(1UL, encoder.DecodeUInt64(decryptor.Decrypt(product))); encrypted1 = encryptor.Encrypt(encoder.Encode(123)); product = evaluator.Square(encrypted1); Assert.AreEqual(15129UL, encoder.DecodeUInt64(decryptor.Decrypt(product))); }
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())); } { SEALContext context = GlobalContext.BGVContext; 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 FVEncryptAddsNoiseNET() { var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("FFFFFFFFC001"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); parms.Validate(); var Encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew()); var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew()); keygen.Generate(); var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew()); // however, this line is fine Assert.AreEqual(encryptor.PublicKey[0], keygen.PublicKey[0]); Assert.AreEqual(encryptor.PublicKey[1], keygen.PublicKey[1]); var encrypted1 = encryptor.Encrypt(Encoder.Encode(0x12345678)); var encrypted2 = encryptor.Encrypt(Encoder.Encode(0x12345678)); // this is what we want to check Assert.AreNotEqual(encrypted1[0], encrypted2[0]); Assert.AreNotEqual(encrypted1[1], encrypted2[1]); var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew()); Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted1))); Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted2))); }
public void StaticMethodsTest() { MemoryPoolHandle handle1 = MemoryPoolHandle.Global(); Assert.IsNotNull(handle1); MemoryPoolHandle handle2 = MemoryPoolHandle.New(clearOnDestruction: true); Assert.IsNotNull(handle2); MemoryPoolHandle handle3 = MemoryPoolHandle.ThreadLocal(); Assert.IsNotNull(handle3); }
public void FVEncryptExponentiateDecryptNET() { var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("FFFFFFFFC001"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); parms.Validate(); var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew()); keygen.Generate(1); var encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew()); var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew()); var evaluator = new Evaluator(parms, keygen.EvaluationKeys, MemoryPoolHandle.AcquireNew()); var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew()); var encrypted = encryptor.Encrypt(encoder.Encode(5)); var power = evaluator.Exponentiate(encrypted, 1); Assert.AreEqual(5, encoder.DecodeInt32(decryptor.Decrypt(power))); encrypted = encryptor.Encrypt(encoder.Encode(7)); power = evaluator.Exponentiate(encrypted, 2); Assert.AreEqual(49, encoder.DecodeInt32(decryptor.Decrypt(power))); encrypted = encryptor.Encrypt(encoder.Encode(-7)); power = evaluator.Exponentiate(encrypted, 3); Assert.AreEqual(-343, encoder.DecodeInt32(decryptor.Decrypt(power))); }
public void UseCountTest() { MemoryPoolHandle pool = MemoryPoolHandle.New(); Assert.AreEqual(1L, pool.UseCount); Plaintext plain = new Plaintext(pool); Assert.AreEqual(2L, pool.UseCount); Plaintext plain2 = new Plaintext(pool); Assert.AreEqual(3L, pool.UseCount); plain.Dispose(); plain2.Dispose(); Assert.AreEqual(1L, pool.UseCount); }
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 SaveLoadEncryptionParamsNET() { var stream = new MemoryStream(); // Create encryption parameters. var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("FFFFFFFFC001"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); var parms2 = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); stream.Seek(0, SeekOrigin.Begin); parms.Save(stream); stream.Seek(0, SeekOrigin.Begin); parms2.Load(stream); Assert.AreEqual(parms.DecompositionBitCount, parms2.DecompositionBitCount); Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation); Assert.AreEqual(parms.NoiseMaxDeviation, parms2.NoiseMaxDeviation); Assert.AreEqual(parms.CoeffModulus, parms2.CoeffModulus); // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY //Assert.AreEqual(parms.AuxCoeffModulus, parms2.AuxCoeffModulus); Assert.AreEqual(parms.PlainModulus, parms2.PlainModulus); Assert.AreEqual(parms.PolyModulus, parms2.PolyModulus); }
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); }
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)); }
public void EqualsTest() { MemoryPoolHandle handle1 = MemoryManager.GetPool(MMProfOpt.ForceNew); MemoryPoolHandle handle2 = MemoryManager.GetPool(MMProfOpt.Default); MemoryPoolHandle handle3 = MemoryManager.GetPool(); Assert.IsNotNull(handle1); Assert.IsNotNull(handle2); Assert.IsNotNull(handle3); Assert.AreNotEqual(handle1, handle2); Assert.AreNotEqual(handle1, handle3); Assert.AreEqual(handle2, handle3); Assert.AreNotEqual(handle1.GetHashCode(), handle2.GetHashCode()); Assert.IsFalse(handle3.Equals(null)); }
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); }
public void ExceptionsTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, CoeffModulus = CoeffModulus.Create(64, new int[] { 60 }), PlainModulus = new Modulus(257) }; SEALContext context = new SEALContext(parms, expandModChain: false, secLevel: SecLevelType.None); 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(); Utilities.AssertThrows <ArgumentNullException>(() => enc = new BatchEncoder(null)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Encode(valu, plain_null)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Encode(valu_null, plain)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Encode(vall, plain_null)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Encode(vall_null, plain)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Encode(plain_null)); Utilities.AssertThrows <ArgumentException>(() => enc.Encode(plain, pool_uninit)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Decode(plain, valu_null)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Decode(plain_null, valu)); Utilities.AssertThrows <ArgumentException>(() => enc.Decode(plain, valu, pool_uninit)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Decode(plain, vall_null)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Decode(plain_null, vall)); Utilities.AssertThrows <ArgumentException>(() => enc.Decode(plain, vall, pool_uninit)); Utilities.AssertThrows <ArgumentNullException>(() => enc.Decode(plain_null)); Utilities.AssertThrows <ArgumentException>(() => enc.Decode(plain, pool_uninit)); }
public void ExceptionsTest() { SEALContext context = GlobalContext.Context; KeyGenerator keygen = new KeyGenerator(context); PublicKey pubKey = keygen.PublicKey; PublicKey pubKey_invalid = new PublicKey(); Encryptor encryptor = new Encryptor(context, pubKey); Plaintext plain = new Plaintext(); Ciphertext cipher = new Ciphertext(); MemoryPoolHandle pool_invalid = new MemoryPoolHandle(); Assert.ThrowsException <ArgumentNullException>(() => encryptor = new Encryptor(context, null)); Assert.ThrowsException <ArgumentNullException>(() => encryptor = new Encryptor(null, pubKey)); Assert.ThrowsException <ArgumentException>(() => encryptor = new Encryptor(context, pubKey_invalid)); Assert.ThrowsException <ArgumentNullException>(() => encryptor.Encrypt(plain, null)); Assert.ThrowsException <ArgumentNullException>(() => encryptor.Encrypt(null, cipher)); Assert.ThrowsException <ArgumentException>(() => encryptor.Encrypt(plain, cipher, pool_invalid)); }
public void SaveLoadTest() { 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); 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); }
public void ExceptionsTest() { SEALContext context = GlobalContext.BFVContext; KeyGenerator keygen = new KeyGenerator(context); PublicKey pubKey = keygen.PublicKey; PublicKey pubKey_invalid = new PublicKey(); SecretKey secKey = keygen.SecretKey; SecretKey secKey_invalid = new SecretKey(); Encryptor encryptor = new Encryptor(context, pubKey); Plaintext plain = new Plaintext(); Ciphertext cipher = new Ciphertext(); MemoryPoolHandle pool_invalid = new MemoryPoolHandle(); ParmsId parmsId_invalid = new ParmsId(); Utilities.AssertThrows <ArgumentNullException>(() => encryptor = new Encryptor(context, null)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor = new Encryptor(null, pubKey)); Utilities.AssertThrows <ArgumentException>(() => encryptor = new Encryptor(context, pubKey_invalid)); Utilities.AssertThrows <ArgumentException>(() => encryptor = new Encryptor(context, pubKey_invalid, secKey)); encryptor = new Encryptor(context, pubKey, secKey); Utilities.AssertThrows <ArgumentException>(() => encryptor.SetPublicKey(pubKey_invalid)); Utilities.AssertThrows <ArgumentException>(() => encryptor.SetSecretKey(secKey_invalid)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor.Encrypt(plain, null)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor.Encrypt(null, cipher)); Utilities.AssertThrows <ArgumentException>(() => encryptor.Encrypt(plain, cipher, pool_invalid)); Utilities.AssertThrows <ArgumentException>(() => encryptor.EncryptZero(cipher, pool_invalid)); Utilities.AssertThrows <ArgumentException>(() => encryptor.EncryptZero(parmsId_invalid, cipher)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor.EncryptSymmetric(plain, null)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor.EncryptSymmetric(null, cipher)); Utilities.AssertThrows <ArgumentException>(() => encryptor.EncryptSymmetric(plain, cipher, pool_invalid)); Utilities.AssertThrows <ArgumentException>(() => encryptor.EncryptZeroSymmetric(cipher, pool_invalid)); Utilities.AssertThrows <ArgumentException>(() => encryptor.EncryptZeroSymmetric(parmsId_invalid, cipher)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor.EncryptSymmetricSave(plain, null)); Utilities.AssertThrows <ArgumentNullException>(() => encryptor.EncryptZeroSymmetricSave(null)); Utilities.AssertThrows <ArgumentException>(() => encryptor.EncryptZeroSymmetricSave(parmsId_invalid, null)); }
public void EncryptionParamsWriteReadNET() { // Create encryption parameters. var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("7FFFFC801"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); Assert.AreEqual(4, parms.DecompositionBitCount); Assert.AreEqual(3.19, parms.NoiseStandardDeviation); Assert.AreEqual(35.06, parms.NoiseMaxDeviation); Assert.AreEqual("7FFFFC801", parms.CoeffModulus.ToString()); // Commented out due to dependence on the SEAL library #define DISABLE_NTT_IN_MULTIPLY //Assert.AreEqual("FFFFFFFFC001", parms.AuxCoeffModulus.ToString()); Assert.AreEqual("40", parms.PlainModulus.ToString()); Assert.AreEqual("1x^64 + 1", parms.PolyModulus.ToString()); }
public void ReserveResizeTest() { Plaintext plain = new Plaintext(); MemoryPoolHandle handle = plain.Pool; Assert.AreEqual(0ul, plain.CoeffCount); Assert.AreEqual(0ul, plain.Capacity); plain.Reserve(capacity: 10); ulong alloced = handle.AllocByteCount; Assert.IsTrue(alloced > 0ul); Assert.AreEqual(0ul, plain.CoeffCount); Assert.AreEqual(10ul, plain.Capacity); plain.Resize(coeffCount: 11); Assert.AreEqual(11ul, plain.CoeffCount); Assert.AreEqual(11ul, plain.Capacity); Assert.AreEqual(0ul, plain.SignificantCoeffCount); Assert.IsTrue(handle.AllocByteCount > 0ul); }
public void FVEncryptDecryptNET() { var parms = new EncryptionParameters(MemoryPoolHandle.AcquireNew()); parms.SetDecompositionBitCount(4); parms.SetNoiseStandardDeviation(3.19); parms.SetNoiseMaxDeviation(35.06); var coeffModulus = new BigUInt(48); coeffModulus.Set("FFFFFFFFC001"); parms.SetCoeffModulus(coeffModulus); var plainModulus = new BigUInt(7); plainModulus.Set(1 << 6); parms.SetPlainModulus(plainModulus); var polyModulus = new BigPoly(65, 1); polyModulus[0].Set(1); polyModulus[64].Set(1); parms.SetPolyModulus(polyModulus); parms.Validate(); var Encoder = new BinaryEncoder(parms.PlainModulus, MemoryPoolHandle.AcquireNew()); var keygen = new KeyGenerator(parms, MemoryPoolHandle.AcquireNew()); keygen.Generate(); var encryptor = new Encryptor(parms, keygen.PublicKey, MemoryPoolHandle.AcquireNew()); Assert.AreEqual(encryptor.PublicKey[0], keygen.PublicKey[0]); Assert.AreEqual(encryptor.PublicKey[1], keygen.PublicKey[1]); var decryptor = new Decryptor(parms, keygen.SecretKey, MemoryPoolHandle.AcquireNew()); Assert.AreEqual(decryptor.SecretKey, keygen.SecretKey); var encrypted = encryptor.Encrypt(Encoder.Encode(0x12345678)); Assert.AreEqual(0x12345678U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted))); encrypted = encryptor.Encrypt(Encoder.Encode(0)); Assert.AreEqual(0U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted))); encrypted = encryptor.Encrypt(Encoder.Encode(1)); Assert.AreEqual(1U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted))); encrypted = encryptor.Encrypt(Encoder.Encode(2)); Assert.AreEqual(2U, Encoder.DecodeUInt32(decryptor.Decrypt(encrypted))); encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFDUL)); Assert.AreEqual(0x7FFFFFFFFFFFFFFDUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted))); encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFEUL)); Assert.AreEqual(0x7FFFFFFFFFFFFFFEUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted))); encrypted = encryptor.Encrypt(Encoder.Encode(0x7FFFFFFFFFFFFFFFUL)); Assert.AreEqual(0x7FFFFFFFFFFFFFFFUL, Encoder.DecodeUInt64(decryptor.Decrypt(encrypted))); }