/// <summary> /// Convert a GaloisKeys object to a Base64 string /// </summary> /// <param name="galk">GaloisKeys to convert</param> /// <returns>Base64 string representing the GaloisKeys</returns> public static string GaloisKeysToBase64(KeyGenerator keygen, List <int> rotations) { using (MemoryStream ms = new MemoryStream()) { // Saving directly to stream; this compresses the size in half keygen.GaloisKeysSave(rotations, ms); byte[] bytes = ms.ToArray(); return(Convert.ToBase64String(bytes)); } }
public void SeededKeyTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 8, PlainModulus = new SmallModulus(257), CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 }) }; SEALContext context = new SEALContext(parms, expandModChain: false, secLevel: SecLevelType.None); KeyGenerator keygen = new KeyGenerator(context); Encryptor encryptor = new Encryptor(context, keygen.PublicKey); Decryptor decryptor = new Decryptor(context, keygen.SecretKey); Evaluator evaluator = new Evaluator(context); BatchEncoder encoder = new BatchEncoder(context); GaloisKeys galoisKeys = new GaloisKeys(); using (MemoryStream stream = new MemoryStream()) { keygen.GaloisKeysSave(stream); stream.Seek(0, SeekOrigin.Begin); galoisKeys.Load(context, stream); } Plaintext plain = new Plaintext(); List <ulong> vec = new List <ulong> { 1, 2, 3, 4, 5, 6, 7, 8 }; encoder.Encode(vec, plain); Ciphertext encrypted = new Ciphertext(); Ciphertext encdest = new Ciphertext(); Plaintext plaindest = new Plaintext(); encryptor.Encrypt(plain, encrypted); evaluator.RotateColumns(encrypted, galoisKeys, encdest); decryptor.Decrypt(encdest, plaindest); encoder.Decode(plaindest, vec); Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong> { 5, 6, 7, 8, 1, 2, 3, 4 })); evaluator.RotateRows(encdest, -1, galoisKeys, encrypted); decryptor.Decrypt(encrypted, plaindest); encoder.Decode(plaindest, vec); Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong> { 8, 5, 6, 7, 4, 1, 2, 3 })); evaluator.RotateRowsInplace(encrypted, 2, galoisKeys); decryptor.Decrypt(encrypted, plaindest); encoder.Decode(plaindest, vec); Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong> { 6, 7, 8, 5, 2, 3, 4, 1 })); evaluator.RotateColumnsInplace(encrypted, galoisKeys); decryptor.Decrypt(encrypted, plaindest); encoder.Decode(plaindest, vec); Assert.IsTrue(AreCollectionsEqual(vec, new List <ulong> { 2, 3, 4, 1, 6, 7, 8, 5 })); }