public void BFVKeyGenerationNET() { 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); var galks = new GaloisKeys(); keygen.GenerateGaloisKeys(60, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.AreEqual(2, galks.Key(3)[0].Size); Assert.AreEqual(10, galks.Size); keygen.GenerateGaloisKeys(30, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.AreEqual(4, galks.Key(3)[0].Size); Assert.AreEqual(10, galks.Size); keygen.GenerateGaloisKeys(2, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.AreEqual(60, galks.Key(3)[0].Size); Assert.AreEqual(10, galks.Size); keygen.GenerateGaloisKeys(60, new List <UInt64> { 1, 3, 5, 7 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(3)); Assert.IsTrue(galks.HasKey(5)); Assert.IsTrue(galks.HasKey(7)); Assert.IsFalse(galks.HasKey(9)); Assert.IsFalse(galks.HasKey(127)); Assert.AreEqual(2, galks.Key(1)[0].Size); Assert.AreEqual(2, galks.Key(3)[0].Size); Assert.AreEqual(2, galks.Key(5)[0].Size); Assert.AreEqual(2, galks.Key(7)[0].Size); Assert.AreEqual(4, galks.Size); keygen.GenerateGaloisKeys(30, new List <UInt64> { 1, 3, 5, 7 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(3)); Assert.IsTrue(galks.HasKey(5)); Assert.IsTrue(galks.HasKey(7)); Assert.IsFalse(galks.HasKey(9)); Assert.IsFalse(galks.HasKey(127)); Assert.AreEqual(4, galks.Key(1)[0].Size); Assert.AreEqual(4, galks.Key(3)[0].Size); Assert.AreEqual(4, galks.Key(5)[0].Size); Assert.AreEqual(4, galks.Key(7)[0].Size); Assert.AreEqual(4, galks.Size); keygen.GenerateGaloisKeys(2, new List <UInt64> { 1, 3, 5, 7 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(3)); Assert.IsTrue(galks.HasKey(5)); Assert.IsTrue(galks.HasKey(7)); Assert.IsFalse(galks.HasKey(9)); Assert.IsFalse(galks.HasKey(127)); Assert.AreEqual(60, galks.Key(1)[0].Size); Assert.AreEqual(60, galks.Key(3)[0].Size); Assert.AreEqual(60, galks.Key(5)[0].Size); Assert.AreEqual(60, galks.Key(7)[0].Size); Assert.AreEqual(4, galks.Size); keygen.GenerateGaloisKeys(30, new List <UInt64> { 1 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsFalse(galks.HasKey(3)); Assert.IsFalse(galks.HasKey(127)); Assert.AreEqual(4, galks.Key(1)[0].Size); Assert.AreEqual(1, galks.Size); keygen.GenerateGaloisKeys(30, new List <UInt64> { 127 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsFalse(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(127)); Assert.AreEqual(4, galks.Key(127)[0].Size); Assert.AreEqual(1, galks.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); var galks = new GaloisKeys(); keygen.GenerateGaloisKeys(60, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.AreEqual(2, galks.Key(3)[0].Size); Assert.AreEqual(14, galks.Size); keygen.GenerateGaloisKeys(30, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.AreEqual(4, galks.Key(3)[0].Size); Assert.AreEqual(14, galks.Size); keygen.GenerateGaloisKeys(2, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.AreEqual(60, galks.Key(3)[0].Size); Assert.AreEqual(14, galks.Size); keygen.GenerateGaloisKeys(60, new List <UInt64> { 1, 3, 5, 7 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(3)); Assert.IsTrue(galks.HasKey(5)); Assert.IsTrue(galks.HasKey(7)); Assert.IsFalse(galks.HasKey(9)); Assert.IsFalse(galks.HasKey(511)); Assert.AreEqual(2, galks.Key(1)[0].Size); Assert.AreEqual(2, galks.Key(3)[0].Size); Assert.AreEqual(2, galks.Key(5)[0].Size); Assert.AreEqual(2, galks.Key(7)[0].Size); Assert.AreEqual(4, galks.Size); keygen.GenerateGaloisKeys(30, new List <UInt64> { 1, 3, 5, 7 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(3)); Assert.IsTrue(galks.HasKey(5)); Assert.IsTrue(galks.HasKey(7)); Assert.IsFalse(galks.HasKey(9)); Assert.IsFalse(galks.HasKey(511)); Assert.AreEqual(4, galks.Key(1)[0].Size); Assert.AreEqual(4, galks.Key(3)[0].Size); Assert.AreEqual(4, galks.Key(5)[0].Size); Assert.AreEqual(4, galks.Key(7)[0].Size); Assert.AreEqual(4, galks.Size); keygen.GenerateGaloisKeys(2, new List <UInt64> { 1, 3, 5, 7 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(3)); Assert.IsTrue(galks.HasKey(5)); Assert.IsTrue(galks.HasKey(7)); Assert.IsFalse(galks.HasKey(9)); Assert.IsFalse(galks.HasKey(511)); Assert.AreEqual(60, galks.Key(1)[0].Size); Assert.AreEqual(60, galks.Key(3)[0].Size); Assert.AreEqual(60, galks.Key(5)[0].Size); Assert.AreEqual(60, galks.Key(7)[0].Size); Assert.AreEqual(4, galks.Size); keygen.GenerateGaloisKeys(30, new List <UInt64> { 1 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsTrue(galks.HasKey(1)); Assert.IsFalse(galks.HasKey(3)); Assert.IsFalse(galks.HasKey(511)); Assert.AreEqual(4, galks.Key(1)[0].Size); Assert.AreEqual(1, galks.Size); keygen.GenerateGaloisKeys(30, new List <UInt64> { 511 }, galks); Assert.AreEqual(galks.HashBlock, parms.HashBlock); Assert.IsFalse(galks.HasKey(1)); Assert.IsTrue(galks.HasKey(511)); Assert.AreEqual(4, galks.Key(511)[0].Size); Assert.AreEqual(1, galks.Size); } }
public void GaloisKeysSaveLoadNET() { var stream = new MemoryStream(); { var parms = new EncryptionParameters(); parms.NoiseStandardDeviation = 3.19; parms.PolyModulus = "1x^64 + 1"; parms.PlainModulus = 65537; parms.CoeffModulus = new List <SmallModulus> { DefaultParams.SmallMods60Bit(0) }; var context = new SEALContext(parms); var keygen = new KeyGenerator(context); var keys = new GaloisKeys(); Assert.AreEqual(keys.DecompositionBitCount, 0); var test_keys = new GaloisKeys(); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); Assert.AreEqual(0, keys.Size); keygen.GenerateGaloisKeys(1, keys); Assert.AreEqual(keys.DecompositionBitCount, 1); stream.Seek(0, SeekOrigin.Begin); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); for (int j = 0; j < test_keys.Size; j++) { for (int i = 0; i < test_keys.Data[j].Count; i++) { Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size); Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count); } } Assert.AreEqual(10, keys.Size); keygen.GenerateGaloisKeys(8, keys); Assert.AreEqual(keys.DecompositionBitCount, 8); stream.Seek(0, SeekOrigin.Begin); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); for (int j = 0; j < test_keys.Size; j++) { for (int i = 0; i < test_keys.Data[j].Count; i++) { Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size); Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count); } } Assert.AreEqual(10, keys.Size); keygen.GenerateGaloisKeys(60, keys); Assert.AreEqual(keys.DecompositionBitCount, 60); stream.Seek(0, SeekOrigin.Begin); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); for (int j = 0; j < test_keys.Size; j++) { for (int i = 0; i < test_keys.Data[j].Count; i++) { Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size); Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count); } } Assert.AreEqual(10, keys.Size); } { var parms = new EncryptionParameters(); parms.NoiseStandardDeviation = 3.19; parms.PolyModulus = "1x^256 + 1"; parms.PlainModulus = 65537; parms.CoeffModulus = new List <SmallModulus> { DefaultParams.SmallMods60Bit(0), DefaultParams.SmallMods50Bit(0) }; var context = new SEALContext(parms); var keygen = new KeyGenerator(context); var keys = new GaloisKeys(); Assert.AreEqual(keys.DecompositionBitCount, 0); var test_keys = new GaloisKeys(); stream.Seek(0, SeekOrigin.Begin); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); Assert.AreEqual(0, keys.Size); keygen.GenerateGaloisKeys(8, keys); Assert.AreEqual(keys.DecompositionBitCount, 8); stream.Seek(0, SeekOrigin.Begin); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); for (int j = 0; j < test_keys.Size; j++) { for (int i = 0; i < test_keys.Data[j].Count; i++) { Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size); Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count); } } Assert.AreEqual(14, keys.Size); keygen.GenerateGaloisKeys(60, keys); Assert.AreEqual(keys.DecompositionBitCount, 60); stream.Seek(0, SeekOrigin.Begin); keys.Save(stream); stream.Seek(0, SeekOrigin.Begin); test_keys.Load(stream); Assert.AreEqual(keys.Size, test_keys.Size); Assert.AreEqual(keys.HashBlock, test_keys.HashBlock); Assert.AreEqual(keys.DecompositionBitCount, test_keys.DecompositionBitCount); for (int j = 0; j < test_keys.Size; j++) { for (int i = 0; i < test_keys.Data[j].Count; i++) { Assert.AreEqual(keys.Data[j][i].Size, test_keys.Data[j][i].Size); Assert.AreEqual(keys.Data[j][i].UInt64Count, test_keys.Data[j][i].UInt64Count); } } Assert.AreEqual(14, keys.Size); } }