diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 000000000..052c1de65
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,92 @@
+{
+ "files.associations": {
+ "stdexcept": "cpp",
+ "__bit_reference": "cpp",
+ "__config": "cpp",
+ "__debug": "cpp",
+ "__errc": "cpp",
+ "__functional_base": "cpp",
+ "__hash_table": "cpp",
+ "__locale": "cpp",
+ "__mutex_base": "cpp",
+ "__node_handle": "cpp",
+ "__nullptr": "cpp",
+ "__split_buffer": "cpp",
+ "__string": "cpp",
+ "__threading_support": "cpp",
+ "__tree": "cpp",
+ "__tuple": "cpp",
+ "algorithm": "cpp",
+ "array": "cpp",
+ "atomic": "cpp",
+ "bit": "cpp",
+ "bitset": "cpp",
+ "cctype": "cpp",
+ "chrono": "cpp",
+ "cinttypes": "cpp",
+ "cmath": "cpp",
+ "complex": "cpp",
+ "condition_variable": "cpp",
+ "cstdarg": "cpp",
+ "cstddef": "cpp",
+ "cstdint": "cpp",
+ "cstdio": "cpp",
+ "cstdlib": "cpp",
+ "cstring": "cpp",
+ "ctime": "cpp",
+ "cwchar": "cpp",
+ "cwctype": "cpp",
+ "deque": "cpp",
+ "exception": "cpp",
+ "forward_list": "cpp",
+ "fstream": "cpp",
+ "functional": "cpp",
+ "initializer_list": "cpp",
+ "iomanip": "cpp",
+ "ios": "cpp",
+ "iosfwd": "cpp",
+ "iostream": "cpp",
+ "istream": "cpp",
+ "iterator": "cpp",
+ "limits": "cpp",
+ "list": "cpp",
+ "locale": "cpp",
+ "map": "cpp",
+ "memory": "cpp",
+ "mutex": "cpp",
+ "new": "cpp",
+ "numeric": "cpp",
+ "optional": "cpp",
+ "ostream": "cpp",
+ "queue": "cpp",
+ "random": "cpp",
+ "ratio": "cpp",
+ "regex": "cpp",
+ "set": "cpp",
+ "shared_mutex": "cpp",
+ "sstream": "cpp",
+ "stack": "cpp",
+ "streambuf": "cpp",
+ "string": "cpp",
+ "string_view": "cpp",
+ "system_error": "cpp",
+ "thread": "cpp",
+ "tuple": "cpp",
+ "type_traits": "cpp",
+ "typeinfo": "cpp",
+ "unordered_map": "cpp",
+ "unordered_set": "cpp",
+ "utility": "cpp",
+ "vector": "cpp",
+ "pointers": "cpp",
+ "__functional_03": "cpp",
+ "multi_span": "cpp",
+ "*.tcc": "cpp",
+ "clocale": "cpp",
+ "compare": "cpp",
+ "concepts": "cpp",
+ "memory_resource": "cpp",
+ "ranges": "cpp",
+ "stop_token": "cpp"
+ }
+}
\ No newline at end of file
diff --git a/dotnet/src/BatchEncoder.cs b/dotnet/src/BatchEncoder.cs
index 34927e738..5fbbdfdbf 100644
--- a/dotnet/src/BatchEncoder.cs
+++ b/dotnet/src/BatchEncoder.cs
@@ -59,7 +59,7 @@ public class BatchEncoder : NativeObject
/// @param[in] context
/// if context is null.
/// if the encryption parameters are not valid for batching
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
public BatchEncoder(SEALContext context)
{
if (null == context)
@@ -68,7 +68,7 @@ public BatchEncoder(SEALContext context)
throw new ArgumentException("Encryption parameters are not set correctly");
SEALContext.ContextData contextData = context.FirstContextData;
- if (contextData.Parms.Scheme != SchemeType.BFV)
+ if (contextData.Parms.Scheme != SchemeType.BFV && contextData.Parms.Scheme != SchemeType.BGV)
throw new ArgumentException("Unsupported scheme");
if (!contextData.Qualifiers.UsingBatching)
throw new ArgumentException("Encryption parameters are not valid for batching");
diff --git a/dotnet/src/Decryptor.cs b/dotnet/src/Decryptor.cs
index 1e28f1b32..7ab3fa5f5 100644
--- a/dotnet/src/Decryptor.cs
+++ b/dotnet/src/Decryptor.cs
@@ -103,7 +103,7 @@ public void Decrypt(Ciphertext encrypted, Plaintext destination)
///
/// The ciphertext
/// if encrypted is null
- /// if the scheme is not BFV
+ /// if the scheme is not BFV or BGV
/// if encrypted is not valid for the encryption parameters
/// if encrypted is in NTT form
/// if pool is uninitialized
diff --git a/dotnet/src/EncryptionParameters.cs b/dotnet/src/EncryptionParameters.cs
index 1e55bd940..8f299dd53 100644
--- a/dotnet/src/EncryptionParameters.cs
+++ b/dotnet/src/EncryptionParameters.cs
@@ -28,7 +28,14 @@ public enum SchemeType : byte
///
/// Cheon-Kim-Kim-Song scheme
///
- CKKS = 0x2
+ CKKS = 0x2,
+
+ ///
+ /// Brakerski-Gentry-Vaikuntanathan
+ ///
+ BGV = 0x3
+
+
}
///
@@ -223,7 +230,7 @@ public IEnumerable CoeffModulus
/// of a particular form.
///
/// if the value being set is null
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
public Modulus PlainModulus
{
get
diff --git a/dotnet/src/Encryptor.cs b/dotnet/src/Encryptor.cs
index 12138926e..7038940c1 100644
--- a/dotnet/src/Encryptor.cs
+++ b/dotnet/src/Encryptor.cs
@@ -129,7 +129,7 @@ public void SetSecretKey(SecretKey secretKey)
///
///
/// The encryption parameters for the resulting ciphertext correspond to:
- /// 1) in BFV, the highest (data) level in the modulus switching chain,
+ /// 1) in BFV or BGV, the highest (data) level in the modulus switching chain,
/// 2) in CKKS, the encryption parameters of the plaintext.
/// Dynamic memory allocations in the process are allocated from the memory
/// pool pointed to by the given MemoryPoolHandle.
@@ -173,7 +173,7 @@ public void Encrypt(
///
///
/// The encryption parameters for the resulting ciphertext correspond to:
- /// 1) in BFV, the highest (data) level in the modulus switching chain,
+ /// 1) in BFV or BGV, the highest (data) level in the modulus switching chain,
/// 2) in CKKS, the encryption parameters of the plaintext.
/// Dynamic memory allocations in the process are allocated from the memory
/// pool pointed to by the given MemoryPoolHandle.
@@ -337,7 +337,7 @@ public Serializable EncryptZero(MemoryPoolHandle pool = null)
///
///
/// The encryption parameters for the resulting ciphertext correspond to:
- /// 1) in BFV, the highest (data) level in the modulus switching chain,
+ /// 1) in BFV or BGV, the highest (data) level in the modulus switching chain,
/// 2) in CKKS, the encryption parameters of the plaintext.
/// Dynamic memory allocations in the process are allocated from the memory
/// pool pointed to by the given MemoryPoolHandle.
@@ -385,7 +385,7 @@ public void EncryptSymmetric(
///
///
/// The encryption parameters for the resulting ciphertext correspond to:
- /// 1) in BFV, the highest (data) level in the modulus switching chain,
+ /// 1) in BFV or BGV, the highest (data) level in the modulus switching chain,
/// 2) in CKKS, the encryption parameters of the plaintext.
/// Dynamic memory allocations in the process are allocated from the memory
/// pool pointed to by the given MemoryPoolHandle.
diff --git a/dotnet/src/Evaluator.cs b/dotnet/src/Evaluator.cs
index 67d76a302..6efd561b9 100644
--- a/dotnet/src/Evaluator.cs
+++ b/dotnet/src/Evaluator.cs
@@ -58,7 +58,7 @@ namespace Microsoft.Research.SEAL
///
///
/// NTT form
- /// When using the BFV scheme (SchemeType.BFV), all plaintexts and ciphertexts should remain by default in the usual
+ /// When using the BFV or BGV scheme (SchemeType.BFV or SchemeType.BGV), all plaintexts and ciphertexts should remain by default in the usual
/// coefficient representation, i.e., not in NTT form. When using the CKKS scheme (SchemeType.CKKS), all plaintexts
/// and ciphertexts should remain by default in NTT form. We call these scheme-specific NTT states the "default NTT
/// form". Some functions, such as add, work even if the inputs are not in the default state, but others, such as
@@ -740,7 +740,7 @@ public void RescaleTo(Ciphertext encrypted, ParmsId parmsId, Ciphertext destinat
/// The ciphertext to overwrite with the multiplication result
/// The MemoryPoolHandle pointing to a valid memory pool
/// if encrypteds, relinKeys, or destination is null
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypteds is empty
/// if encrypteds or relinKeys are not valid for the encryption
/// parameters
@@ -784,7 +784,7 @@ public void MultiplyMany(IEnumerable encrypteds, RelinKeys relinKeys
/// The relinearization keys
/// The MemoryPoolHandle pointing to a valid memory pool
/// if encrypted or relinKeys is null
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypted or relinKeys is not valid for the encryption
/// parameters
/// if encrypted is not in the default NTT form
@@ -816,7 +816,7 @@ public void ExponentiateInplace(Ciphertext encrypted, ulong exponent,
/// The relinearization keys
/// The ciphertext to overwrite with the power
/// The MemoryPoolHandle pointing to a valid memory pool
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypted or relinKeys is not valid for the encryption
/// parameters
/// if encrypted is not in the default NTT form
@@ -1148,7 +1148,7 @@ public void TransformFromNTT(Ciphertext encryptedNTT, Ciphertext destination)
/// The desired Galois automorphism is given as a Galois element, and must be an odd integer in the interval
/// [1, M-1], where M = 2*N, and N = PolyModulusDegree. Used with batching, a Galois element 3^i % M corresponds
/// to a cyclic row rotation i steps to the left, and a Galois element 3^(N/2-i) % M corresponds to a cyclic row
- /// rotation i steps to the right. The Galois element M-1 corresponds to a column rotation (row swap) in BFV,
+ /// rotation i steps to the right. The Galois element M-1 corresponds to a column rotation (row swap) in BFV or BGV,
/// and complex conjugation in CKKS. In the polynomial view (not batching), a Galois automorphism by a Galois
/// element p changes Enc(plain(x)) to Enc(plain(x^p)).
///
@@ -1188,7 +1188,7 @@ public void ApplyGaloisInplace(Ciphertext encrypted, uint galoisElt,
/// The desired Galois automorphism is given as a Galois element, and must be an odd integer in the interval
/// [1, M-1], where M = 2*N, and N = PolyModulusDegree. Used with batching, a Galois element 3^i % M corresponds
/// to a cyclic row rotation i steps to the left, and a Galois element 3^(N/2-i) % M corresponds to a cyclic row
- /// rotation i steps to the right. The Galois element M-1 corresponds to a column rotation (row swap) in BFV,
+ /// rotation i steps to the right. The Galois element M-1 corresponds to a column rotation (row swap) in BFV or BGV,
/// and complex conjugation in CKKS. In the polynomial view (not batching), a Galois automorphism by a Galois
/// element p changes Enc(plain(x)) to Enc(plain(x^p)).
///
@@ -1232,7 +1232,7 @@ public void ApplyGalois(Ciphertext encrypted, uint galoisElt, GaloisKeys galoisK
/// Rotates plaintext matrix rows cyclically.
///
///
- /// When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix rows
+ /// When batching is used with the BFV or BGV scheme, this function rotates the encrypted plaintext matrix rows
/// cyclically to the left (steps > 0) or to the right (steps < 0). Since the size of the batched matrix
/// is 2-by-(N/2), where N is the degree of the polynomial modulus, the number of steps to rotate must have
/// absolute value at most N/2-1. Dynamic memory allocations in the process are allocated from the memory pool
@@ -1244,7 +1244,7 @@ public void ApplyGalois(Ciphertext encrypted, uint galoisElt, GaloisKeys galoisK
/// The MemoryPoolHandle pointing to a valid memory pool
/// if encrypted or galoisKeys is null
/// if the encryption parameters do not support batching
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypted or galoisKeys is not valid for the encryption
/// parameters
/// if galoisKeys do not correspond to the top level parameters in the
@@ -1266,7 +1266,7 @@ public void RotateRowsInplace(Ciphertext encrypted,
/// Rotates plaintext matrix rows cyclically.
///
///
- /// When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix rows
+ /// When batching is used with the BFV or BGV scheme, this function rotates the encrypted plaintext matrix rows
/// cyclically to the left (steps > 0) or to the right (steps < 0) and writes the result to the
/// destination parameter. Since the size of the batched matrix is 2-by-(N/2), where N is the degree of the
/// polynomial modulus, the number of steps to rotate must have absolute value at most N/2-1. Dynamic memory
@@ -1279,7 +1279,7 @@ public void RotateRowsInplace(Ciphertext encrypted,
/// The MemoryPoolHandle pointing to a valid memory pool
/// if encrypted, galoisKeys, or destination is null
/// if the encryption parameters do not support batching
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypted or galoisKeys is not valid for the encryption
/// parameters
/// if galoisKeys do not correspond to the top level parameters in the
@@ -1313,7 +1313,7 @@ public void RotateRows(Ciphertext encrypted, int steps, GaloisKeys galoisKeys,
/// Rotates plaintext matrix columns cyclically.
///
///
- /// When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix columns
+ /// When batching is used with the BFV or BGV scheme, this function rotates the encrypted plaintext matrix columns
/// cyclically. Since the size of the batched matrix is 2-by-(N/2), where N is the degree of the polynomial
/// modulus, this means simply swapping the two rows. Dynamic memory allocations in the process are allocated
/// from the memory pool pointed to by the given MemoryPoolHandle.
@@ -1323,7 +1323,7 @@ public void RotateRows(Ciphertext encrypted, int steps, GaloisKeys galoisKeys,
/// The MemoryPoolHandle pointing to a valid memory pool
/// if encrypted or galoisKeys is null
/// if the encryption parameters do not support batching
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypted or galoisKeys is not valid for the encryption
/// parameters
/// if galoisKeys do not correspond to the top level parameters in the
@@ -1343,7 +1343,7 @@ public void RotateColumnsInplace(Ciphertext encrypted, GaloisKeys galoisKeys, Me
/// Rotates plaintext matrix columns cyclically.
///
///
- /// When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix columns
+ /// When batching is used with the BFV or BGV scheme, this function rotates the encrypted plaintext matrix columns
/// cyclically, and writes the result to the destination parameter. Since the size of the batched matrix is
/// 2-by-(N/2), where N is the degree of the polynomial modulus, this means simply swapping the two rows.
/// Dynamic memory allocations in the process are allocated from the memory pool pointed to by the given
@@ -1355,7 +1355,7 @@ public void RotateColumnsInplace(Ciphertext encrypted, GaloisKeys galoisKeys, Me
/// The MemoryPoolHandle pointing to a valid memory pool
/// if encrypted, galoisKeys, or destination is null
/// if the encryption parameters do not support batching
- /// if scheme is not SchemeType.BFV
+ /// if scheme is not SchemeType.BFV or SchemeType.BGV
/// if encrypted or galoisKeys is not valid for the encryption
/// parameters
/// if galoisKeys do not correspond to the top level parameters in the
diff --git a/dotnet/src/KeyGenerator.cs b/dotnet/src/KeyGenerator.cs
index d56a7a95b..d9770a1ef 100644
--- a/dotnet/src/KeyGenerator.cs
+++ b/dotnet/src/KeyGenerator.cs
@@ -200,7 +200,7 @@ public Serializable CreateRelinKeys()
/// The Galois keys to overwrite with the generated
/// Galois keys
/// if the encryption parameters
- /// do not support batching and scheme is SchemeType.BFV
+ /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV
/// if the encryption
/// parameters do not support keyswitching
/// if the Galois elements are not valid
@@ -250,7 +250,7 @@ public void CreateGaloisKeys(IEnumerable galoisElts, out GaloisKeys destin
/// The Galois elements for which to generate keys
/// if galoisElts is null
/// if the encryption parameters
- /// do not support batching and scheme is SchemeType.BFV
+ /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV
/// if the encryption
/// parameters do not support keyswitching
/// if the Galois elements are not valid
@@ -279,7 +279,7 @@ public Serializable CreateGaloisKeys(IEnumerable galoisElts)
/// The user needs to give as input a vector of desired Galois rotation step
/// counts, where negative step counts correspond to rotations to the right
/// and positive step counts correspond to rotations to the left. A step
- /// count of zero can be used to indicate a column rotation in the BFV scheme
+ /// count of zero can be used to indicate a column rotation in the BFV/BGV scheme
/// and complex conjugation in the CKKS scheme.
///
///
@@ -288,7 +288,7 @@ public Serializable CreateGaloisKeys(IEnumerable galoisElts)
/// Galois keys
/// if steps is null
/// if the encryption parameters
- /// do not support batching and scheme is SchemeType.BFV
+ /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV
/// if the encryption
/// parameters do not support keyswitching
/// if the step counts are not valid
@@ -323,14 +323,14 @@ public void CreateGaloisKeys(IEnumerable steps, out GaloisKeys destination)
/// The user needs to give as input a vector of desired Galois rotation step
/// counts, where negative step counts correspond to rotations to the right
/// and positive step counts correspond to rotations to the left. A step
- /// count of zero can be used to indicate a column rotation in the BFV scheme
+ /// count of zero can be used to indicate a column rotation in the BFV or BGV scheme
/// and complex conjugation in the CKKS scheme.
///
///
/// The rotation step counts for which to generate keys
/// if steps is null
/// if the encryption parameters
- /// do not support batching and scheme is SchemeType.BFV
+ /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV
/// if the encryption
/// parameters do not support keyswitching
/// if the step counts are not valid
@@ -365,7 +365,7 @@ public Serializable CreateGaloisKeys(IEnumerable steps)
/// The Galois keys to overwrite with the generated
/// Galois keys
/// if the encryption parameters
- /// do not support batching and scheme is SchemeType.BFV
+ /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV
/// if the encryption
/// parameters do not support keyswitching
public void CreateGaloisKeys(out GaloisKeys destination)
@@ -399,7 +399,7 @@ public void CreateGaloisKeys(out GaloisKeys destination)
///
///
/// if the encryption parameters
- /// do not support batching and scheme is SchemeType.BFV
+ /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV
/// if the encryption
/// parameters do not support keyswitching
public Serializable CreateGaloisKeys()
diff --git a/dotnet/src/Modulus.cs b/dotnet/src/Modulus.cs
index b8310a289..b78eca324 100644
--- a/dotnet/src/Modulus.cs
+++ b/dotnet/src/Modulus.cs
@@ -469,6 +469,52 @@ static public IEnumerable BFVDefault(
return result;
}
+ ///
+ /// Returns a default coefficient modulus for the BGV scheme that guarantees
+ /// a given security level when using a given PolyModulusDegree, according
+ /// to the HomomorphicEncryption.org security standard.
+ ///
+ ///
+ ///
+ /// Returns a default coefficient modulus for the BGV scheme that guarantees
+ /// a given security level when using a given PolyModulusDegree, according
+ /// to the HomomorphicEncryption.org security standard. Note that all security
+ /// guarantees are lost if the output is used with encryption parameters with
+ /// a mismatching value for the PolyModulusDegree. Currently, we just use the
+ /// parameters of BFV.
+ ///
+ ///
+ /// The coefficient modulus returned by this function will not perform well
+ /// if used with the CKKS scheme.
+ ///
+ ///
+ /// The value of the PolyModulusDegree
+ /// encryption parameter
+ /// The desired standard security level
+ /// if polyModulusDegree is not
+ /// a power-of-two or is too large
+ /// if secLevel is SecLevelType.None
+ static public IEnumerable BGVDefault(
+ ulong polyModulusDegree, SecLevelType secLevel = SecLevelType.TC128)
+ {
+ List result = null;
+
+ ulong length = 0;
+ NativeMethods.CoeffModulus_BFVDefault(polyModulusDegree, (int)secLevel, ref length, null);
+
+ IntPtr[] coeffArray = new IntPtr[length];
+ NativeMethods.CoeffModulus_BFVDefault(polyModulusDegree, (int)secLevel, ref length, coeffArray);
+
+ result = new List(checked((int)length));
+ foreach (IntPtr sm in coeffArray)
+ {
+ result.Add(new Modulus(sm));
+ }
+
+ return result;
+ }
+
+
///
/// Returns a custom coefficient modulus suitable for use with the specified
/// PolyModulusDegree.
diff --git a/dotnet/src/NativeMethods.cs b/dotnet/src/NativeMethods.cs
index 3502705a1..a5466781a 100644
--- a/dotnet/src/NativeMethods.cs
+++ b/dotnet/src/NativeMethods.cs
@@ -196,7 +196,7 @@ internal static extern void CoeffModulus_BFVDefault(
int secLevel,
ref ulong length,
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] coeffArray);
-
+
[DllImport(sealc, PreserveSig = false)]
internal static extern void CoeffModulus_Create(
ulong polyModulusDegree,
diff --git a/dotnet/src/Plaintext.cs b/dotnet/src/Plaintext.cs
index c89ac7921..7c5a94309 100644
--- a/dotnet/src/Plaintext.cs
+++ b/dotnet/src/Plaintext.cs
@@ -32,7 +32,7 @@ namespace Microsoft.Research.SEAL
/// providing the desired capacity to the constructor as an extra argument, or
/// by calling the reserve function at any time.
///
- /// When the scheme is SchemeType.BFV each coefficient of a plaintext is
+ /// When the scheme is SchemeType.BFV or SchemeType.BGV, each coefficient of a plaintext is
/// a 64-bit word, but when the scheme is SchemeType.CKKS the plaintext is
/// by default stored in an NTT transformed form with respect to each of the
/// primes in the coefficient modulus. Thus, the size of the allocation that
diff --git a/dotnet/tests/CiphertextTests.cs b/dotnet/tests/CiphertextTests.cs
index 7d7d07ce9..de143d0ea 100644
--- a/dotnet/tests/CiphertextTests.cs
+++ b/dotnet/tests/CiphertextTests.cs
@@ -62,7 +62,39 @@ public void Create3Test()
}
[TestMethod]
- public void ResizeTest()
+ public void Create4Test()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ ParmsId parms = context.FirstParmsId;
+
+ Assert.AreNotEqual(0ul, parms.Block[0]);
+ Assert.AreNotEqual(0ul, parms.Block[1]);
+ Assert.AreNotEqual(0ul, parms.Block[2]);
+ Assert.AreNotEqual(0ul, parms.Block[3]);
+
+ Ciphertext cipher = new Ciphertext(context, parms);
+
+ Assert.AreEqual(parms, cipher.ParmsId);
+ }
+
+ [TestMethod]
+ public void Create5Test()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ ParmsId parms = context.FirstParmsId;
+
+ Assert.AreNotEqual(0ul, parms.Block[0]);
+ Assert.AreNotEqual(0ul, parms.Block[1]);
+ Assert.AreNotEqual(0ul, parms.Block[2]);
+ Assert.AreNotEqual(0ul, parms.Block[3]);
+
+ Ciphertext cipher = new Ciphertext(context, parms, sizeCapacity: 5);
+
+ Assert.AreEqual(5ul, cipher.SizeCapacity);
+ }
+
+ [TestMethod]
+ public void BFVResizeTest()
{
SEALContext context = GlobalContext.BFVContext;
ParmsId parms = context.FirstParmsId;
@@ -97,6 +129,42 @@ public void ResizeTest()
Assert.AreEqual(6ul, cipher5.SizeCapacity);
}
+ [TestMethod]
+ public void BGVResizeTest()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ ParmsId parms = context.FirstParmsId;
+
+ Ciphertext cipher = new Ciphertext(context, parms);
+
+ Assert.AreEqual(2ul, cipher.SizeCapacity);
+
+ cipher.Reserve(context, parms, sizeCapacity: 10);
+ Assert.AreEqual(10ul, cipher.SizeCapacity);
+
+ Ciphertext cipher2 = new Ciphertext();
+
+ Assert.AreEqual(0ul, cipher2.SizeCapacity);
+
+ cipher2.Reserve(context, 5);
+ Assert.AreEqual(5ul, cipher2.SizeCapacity);
+
+ Ciphertext cipher3 = new Ciphertext();
+
+ Assert.AreEqual(0ul, cipher3.SizeCapacity);
+
+ cipher3.Reserve(4);
+ Assert.AreEqual(0ul, cipher3.SizeCapacity);
+
+ Ciphertext cipher4 = new Ciphertext(context);
+ cipher4.Resize(context, context.GetContextData(context.FirstParmsId).NextContextData.ParmsId, 4);
+ Assert.AreEqual(10ul, cipher.SizeCapacity);
+
+ Ciphertext cipher5 = new Ciphertext(context);
+ cipher5.Resize(context, 6ul);
+ Assert.AreEqual(6ul, cipher5.SizeCapacity);
+ }
+
[TestMethod]
public void ReleaseTest()
{
@@ -110,7 +178,7 @@ public void ReleaseTest()
}
[TestMethod]
- public void SaveLoadTest()
+ public void BFVSaveLoadTest()
{
SEALContext context = GlobalContext.BFVContext;
KeyGenerator keygen = new KeyGenerator(context);
@@ -154,7 +222,51 @@ public void SaveLoadTest()
}
[TestMethod]
- public void IndexTest()
+ 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]);
+ }
+ }
+
+ [TestMethod]
+ public void BFVIndexTest()
{
SEALContext context = GlobalContext.BFVContext;
KeyGenerator keygen = new KeyGenerator(context);
@@ -176,7 +288,29 @@ public void IndexTest()
}
[TestMethod]
- public void IndexRangeFail1Test()
+ public void BGVIndexTest()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Plaintext plain = new Plaintext("1");
+ Ciphertext cipher = new Ciphertext();
+
+ encryptor.Encrypt(plain, cipher);
+
+ Assert.AreEqual(2ul, cipher.Size);
+ Assert.AreNotEqual(0ul, cipher[0, 0]);
+ Assert.AreNotEqual(0ul, cipher[0, 1]);
+ Assert.AreNotEqual(0ul, cipher[0, 2]);
+ Assert.AreNotEqual(0ul, cipher[1, 0]);
+ Assert.AreNotEqual(0ul, cipher[1, 1]);
+ Assert.AreNotEqual(0ul, cipher[1, 2]);
+ }
+
+ [TestMethod]
+ public void BFVIndexRangeFail1Test()
{
SEALContext context = GlobalContext.BFVContext;
KeyGenerator keygen = new KeyGenerator(context);
@@ -196,7 +330,27 @@ public void IndexRangeFail1Test()
}
[TestMethod]
- public void IndexRangeFail2Test()
+ public void BGVIndexRangeFail1Test()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Plaintext plain = new Plaintext("1");
+ Ciphertext cipher = new Ciphertext(context);
+
+ encryptor.Encrypt(plain, cipher);
+
+ Utilities.AssertThrows(() =>
+ {
+ // We only have 2 polynomials
+ ulong data = cipher[2, 0];
+ });
+ }
+
+ [TestMethod]
+ public void BFVIndexRangeFail2Test()
{
SEALContext context = GlobalContext.BFVContext;
KeyGenerator keygen = new KeyGenerator(context);
@@ -221,7 +375,32 @@ public void IndexRangeFail2Test()
}
[TestMethod]
- public void IndexRangeFail3Test()
+ public void BGVIndexRangeFail2Test()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Plaintext plain = new Plaintext("1");
+ Ciphertext cipher = new Ciphertext();
+
+ encryptor.Encrypt(plain, cipher);
+
+ // We only have 2 polynomials
+ ulong data = cipher[1, 0];
+
+ // We should have 8192 coefficients
+ data = cipher[0, 32767]; // This will succeed
+
+ Utilities.AssertThrows(() =>
+ {
+ data = cipher[0, 32768]; // This will fail
+ });
+ }
+
+ [TestMethod]
+ public void BFVIndexRangeFail3Test()
{
SEALContext context = GlobalContext.BFVContext;
KeyGenerator keygen = new KeyGenerator(context);
@@ -238,6 +417,24 @@ public void IndexRangeFail3Test()
Utilities.AssertThrows(() => cipher[65536] = 10ul);
}
+ [TestMethod]
+ public void BGVIndexRangeFail3Test()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Plaintext plain = new Plaintext("1");
+ Ciphertext cipher = new Ciphertext();
+
+ encryptor.Encrypt(plain, cipher);
+ ulong data = 0;
+
+ Utilities.AssertThrows(() => data = cipher[65536]);
+ Utilities.AssertThrows(() => cipher[65536] = 10ul);
+ }
+
[TestMethod]
public void ScaleTest()
{
@@ -295,7 +492,7 @@ public void ScaleTest()
}
[TestMethod]
- public void ExceptionsTest()
+ public void BFVExceptionsTest()
{
SEALContext context = GlobalContext.BFVContext;
MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal);
@@ -341,5 +538,53 @@ public void ExceptionsTest()
Utilities.AssertThrows(() => cipher.Load(null, new MemoryStream()));
Utilities.AssertThrows(() => cipher.Load(context, null));
}
+
+ [TestMethod]
+ public void BGVExceptionsTest()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal);
+ MemoryPoolHandle poolu = new MemoryPoolHandle();
+ Ciphertext cipher = new Ciphertext();
+ Ciphertext copy = null;
+
+ Utilities.AssertThrows(() => copy = new Ciphertext((Ciphertext)null));
+
+ Utilities.AssertThrows(() => cipher = new Ciphertext(context, null, pool));
+ Utilities.AssertThrows(() => cipher = new Ciphertext(null, context.FirstParmsId, pool));
+ Utilities.AssertThrows(() => cipher = new Ciphertext(context, ParmsId.Zero, pool));
+
+ Utilities.AssertThrows(() => cipher = new Ciphertext((SEALContext)null, poolu));
+ Utilities.AssertThrows(() => cipher = new Ciphertext(context, poolu));
+
+ Utilities.AssertThrows(() => cipher = new Ciphertext(context, null, 6ul));
+ Utilities.AssertThrows(() => cipher = new Ciphertext(null, context.FirstParmsId, 6ul, poolu));
+ Utilities.AssertThrows(() => cipher = new Ciphertext(context, ParmsId.Zero, 6ul, poolu));
+
+ Utilities.AssertThrows(() => cipher.Reserve(context, null, 10ul));
+ Utilities.AssertThrows(() => cipher.Reserve(null, ParmsId.Zero, 10ul));
+ Utilities.AssertThrows(() => cipher.Reserve(context, ParmsId.Zero, 10ul));
+
+ Utilities.AssertThrows(() => cipher.Reserve(null, 10ul));
+
+ Utilities.AssertThrows(() => cipher.Resize(context, null, 10ul));
+ Utilities.AssertThrows(() => cipher.Resize(null, ParmsId.Zero, 10ul));
+ Utilities.AssertThrows(() => cipher.Resize(context, ParmsId.Zero, 10ul));
+
+ Utilities.AssertThrows(() => cipher.Resize(null, 10ul));
+
+ Utilities.AssertThrows(() => cipher.Set(null));
+
+ Utilities.AssertThrows(() => ValCheck.IsValidFor(cipher, null));
+
+ Utilities.AssertThrows(() => cipher.Save(null));
+
+ Utilities.AssertThrows(() => cipher.UnsafeLoad(context, null));
+ Utilities.AssertThrows(() => cipher.UnsafeLoad(null, new MemoryStream()));
+ Utilities.AssertThrows(() => cipher.UnsafeLoad(context, new MemoryStream()));
+
+ Utilities.AssertThrows(() => cipher.Load(null, new MemoryStream()));
+ Utilities.AssertThrows(() => cipher.Load(context, null));
+ }
}
}
diff --git a/dotnet/tests/DecryptorTests.cs b/dotnet/tests/DecryptorTests.cs
index dcf3e1302..41b72db71 100644
--- a/dotnet/tests/DecryptorTests.cs
+++ b/dotnet/tests/DecryptorTests.cs
@@ -8,7 +8,7 @@
namespace SEALNetTest
{
[TestClass]
- public class DecryptorTests
+ public class BFVDecryptorTests
{
SEALContext context_;
KeyGenerator keyGen_;
@@ -91,4 +91,89 @@ public void ExceptionsTest()
Utilities.AssertThrows(() => decryptor.InvariantNoiseBudget(null));
}
}
+
+ [TestClass]
+ public class BGVDecryptorTests
+ {
+ SEALContext context_;
+ KeyGenerator keyGen_;
+ SecretKey secretKey_;
+ PublicKey publicKey_;
+
+ [TestInitialize]
+ public void TestInit()
+ {
+ context_ = GlobalContext.BGVContext;
+ keyGen_ = new KeyGenerator(context_);
+ secretKey_ = keyGen_.SecretKey;
+ keyGen_.CreatePublicKey(out publicKey_);
+ }
+
+ [TestMethod]
+ public void CreateTest()
+ {
+ Decryptor decryptor = new Decryptor(context_, secretKey_);
+
+ Assert.IsNotNull(decryptor);
+ }
+
+ [TestMethod]
+ public void DecryptTest()
+ {
+ Encryptor encryptor = new Encryptor(context_, publicKey_);
+ Decryptor decryptor = new Decryptor(context_, secretKey_);
+
+ Plaintext plain = new Plaintext("1x^1 + 2");
+ Ciphertext cipher = new Ciphertext();
+
+ Assert.AreEqual(0ul, cipher.Size);
+
+ encryptor.Encrypt(plain, cipher);
+
+ Assert.AreEqual(2ul, cipher.Size);
+
+ Plaintext decrypted = new Plaintext();
+ Assert.AreEqual(0ul, decrypted.CoeffCount);
+
+ decryptor.Decrypt(cipher, decrypted);
+
+ Assert.AreEqual(2ul, decrypted.CoeffCount);
+ Assert.AreEqual(2ul, decrypted[0]);
+ Assert.AreEqual(1ul, decrypted[1]);
+ }
+
+ [TestMethod]
+ public void InvariantNoiseBudgetTest()
+ {
+ Encryptor encryptor = new Encryptor(context_, publicKey_);
+ Decryptor decryptor = new Decryptor(context_, secretKey_);
+
+ Plaintext plain = new Plaintext("1");
+ Ciphertext cipher = new Ciphertext();
+
+ encryptor.Encrypt(plain, cipher);
+
+ int budget = decryptor.InvariantNoiseBudget(cipher);
+ Assert.IsTrue(budget > 80);
+ }
+
+ [TestMethod]
+ public void ExceptionsTest()
+ {
+ Decryptor decryptor = new Decryptor(context_, secretKey_);
+ SecretKey secret = new SecretKey();
+ Ciphertext cipher = new Ciphertext();
+ Plaintext plain = new Plaintext();
+
+ Utilities.AssertThrows(() => decryptor = new Decryptor(context_, null));
+ Utilities.AssertThrows(() => decryptor = new Decryptor(null, secretKey_));
+ Utilities.AssertThrows(() => decryptor = new Decryptor(context_, secret));
+
+ Utilities.AssertThrows(() => decryptor.Decrypt(cipher, null));
+ Utilities.AssertThrows(() => decryptor.Decrypt(null, plain));
+ Utilities.AssertThrows(() => decryptor.Decrypt(cipher, plain));
+
+ Utilities.AssertThrows(() => decryptor.InvariantNoiseBudget(null));
+ }
+ }
}
diff --git a/dotnet/tests/EncryptionParameterQualifiersTests.cs b/dotnet/tests/EncryptionParameterQualifiersTests.cs
index a026da1be..8c3ade75f 100644
--- a/dotnet/tests/EncryptionParameterQualifiersTests.cs
+++ b/dotnet/tests/EncryptionParameterQualifiersTests.cs
@@ -50,10 +50,21 @@ public void PropertiesTest()
Assert.AreEqual(SecLevelType.TC128, qualifiers.SecLevel);
Assert.IsTrue(qualifiers.UsingDescendingModulusChain);
Assert.IsTrue(qualifiers.UsingNTT);
+
+ SEALContext context3 = GlobalContext.BGVContext;
+
+ Assert.IsTrue(context.FirstContextData.Qualifiers.ParametersSet);
+ Assert.IsTrue(context.FirstContextData.Qualifiers.UsingBatching);
+ Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFastPlainLift);
+ Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFFT);
+ Assert.AreEqual(SecLevelType.TC128, context.FirstContextData.Qualifiers.SecLevel);
+ Assert.IsFalse(context.FirstContextData.Qualifiers.UsingDescendingModulusChain);
+ Assert.IsTrue(context.FirstContextData.Qualifiers.UsingNTT);
+ Assert.IsTrue(context.UsingKeyswitching);
}
[TestMethod]
- public void ParameterErrorTest()
+ public void BFVParameterErrorTest()
{
SEALContext context = GlobalContext.BFVContext;
EncryptionParameterQualifiers qualifiers = context.FirstContextData.Qualifiers;
@@ -74,12 +85,42 @@ public void ParameterErrorTest()
}
[TestMethod]
- public void ExceptionsTest()
+ public void BGVParameterErrorTest()
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ EncryptionParameterQualifiers qualifiers = context.FirstContextData.Qualifiers;
+
+ Assert.AreEqual(qualifiers.ParametersErrorName(), "success");
+ Assert.AreEqual(qualifiers.ParametersErrorMessage(), "valid");
+
+ EncryptionParameters encParam = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 127,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30 })
+ };
+ context = new SEALContext(encParam, expandModChain: true, secLevel: SecLevelType.None);
+ qualifiers = context.FirstContextData.Qualifiers;
+ Assert.AreEqual(qualifiers.ParametersErrorName(), "invalid_poly_modulus_degree_non_power_of_two");
+ Assert.AreEqual(qualifiers.ParametersErrorMessage(), "poly_modulus_degree is not a power of two");
+ }
+
+ [TestMethod]
+ public void BFVExceptionsTest()
{
EncryptionParameterQualifiers epq1 = GlobalContext.BFVContext.FirstContextData.Qualifiers;
EncryptionParameterQualifiers epq2 = null;
Utilities.AssertThrows(() => epq2 = new EncryptionParameterQualifiers(null));
}
+
+ [TestMethod]
+ public void BGVExceptionsTest()
+ {
+ EncryptionParameterQualifiers epq1 = GlobalContext.BGVContext.FirstContextData.Qualifiers;
+ EncryptionParameterQualifiers epq2 = null;
+
+ Utilities.AssertThrows(() => epq2 = new EncryptionParameterQualifiers(null));
+ }
}
}
diff --git a/dotnet/tests/EncryptionParametersTests.cs b/dotnet/tests/EncryptionParametersTests.cs
index ee4778a22..5d37dcd9d 100644
--- a/dotnet/tests/EncryptionParametersTests.cs
+++ b/dotnet/tests/EncryptionParametersTests.cs
@@ -32,18 +32,36 @@ public void CreateTest()
Assert.IsNotNull(encParams3);
Assert.AreEqual(SchemeType.CKKS, encParams3.Scheme);
+ EncryptionParameters encParams4 = new EncryptionParameters(SchemeType.BGV);
+
+ Assert.IsNotNull(encParams4);
+ Assert.AreEqual(SchemeType.BGV, encParams4.Scheme);
+
EncryptionParameters copy = new EncryptionParameters(encParams);
Assert.AreEqual(SchemeType.BFV, copy.Scheme);
Assert.AreEqual(encParams, copy);
Assert.AreEqual(encParams.GetHashCode(), copy.GetHashCode());
+ EncryptionParameters copy4 = new EncryptionParameters(encParams4);
+
+ Assert.AreEqual(SchemeType.BGV, copy4.Scheme);
+ Assert.AreEqual(encParams4, copy4);
+ Assert.AreEqual(encParams4.GetHashCode(), copy4.GetHashCode());
+
EncryptionParameters third = new EncryptionParameters(SchemeType.CKKS);
third.Set(copy);
Assert.AreEqual(SchemeType.BFV, third.Scheme);
Assert.AreEqual(encParams, third);
Assert.AreEqual(encParams.GetHashCode(), third.GetHashCode());
+
+ EncryptionParameters forth = new EncryptionParameters(SchemeType.CKKS);
+ forth.Set(copy4);
+
+ Assert.AreEqual(SchemeType.BGV, forth.Scheme);
+ Assert.AreEqual(encParams4, forth);
+ Assert.AreEqual(encParams4.GetHashCode(), forth.GetHashCode());
}
[TestMethod]
@@ -63,7 +81,7 @@ public void SetPlainModulusCKKSTest()
}
[TestMethod]
- public void CoeffModulusTest()
+ public void BFVCoeffModulusTest()
{
EncryptionParameters encParams = new EncryptionParameters(SchemeType.BFV);
@@ -83,6 +101,27 @@ public void CoeffModulusTest()
Assert.AreEqual(0x1ffffe0001ul, newCoeffs[2].Value);
}
+ [TestMethod]
+ public void BGVCoeffModulusTest()
+ {
+ EncryptionParameters encParams = new EncryptionParameters(SchemeType.BGV);
+
+ Assert.IsNotNull(encParams);
+
+ List coeffs = new List(encParams.CoeffModulus);
+ Assert.IsNotNull(coeffs);
+ Assert.AreEqual(0, coeffs.Count);
+
+ encParams.CoeffModulus = CoeffModulus.BGVDefault(4096);
+
+ List newCoeffs = new List(encParams.CoeffModulus);
+ Assert.IsNotNull(newCoeffs);
+ Assert.AreEqual(3, newCoeffs.Count);
+ Assert.AreEqual(0xffffee001ul, newCoeffs[0].Value);
+ Assert.AreEqual(0xffffc4001ul, newCoeffs[1].Value);
+ Assert.AreEqual(0x1ffffe0001ul, newCoeffs[2].Value);
+ }
+
[TestMethod]
public void SaveLoadTest()
{
@@ -121,6 +160,7 @@ public void SaveLoadTest()
Assert.AreEqual(coeffModulus[1], loadedCoeffModulus[1]);
};
save_load_test(SchemeType.BFV);
+ save_load_test(SchemeType.BGV);
save_load_test(SchemeType.CKKS);
}
@@ -136,7 +176,16 @@ public void EqualsTest()
EncryptionParameters parms2 = new EncryptionParameters(SchemeType.CKKS);
+ EncryptionParameters parms3 = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 8,
+ PlainModulus = new Modulus(257),
+ CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
+ };
+
Assert.AreNotEqual(parms, parms2);
+ Assert.AreNotEqual(parms, parms3);
+ Assert.AreNotEqual(parms2, parms3);
Assert.IsFalse(parms.Equals(null));
}
@@ -150,6 +199,14 @@ public void ExceptionsTest()
Utilities.AssertThrows(() => parms.Save(null));
Utilities.AssertThrows(() => parms.Load(null));
Utilities.AssertThrows(() => parms.Load(new MemoryStream()));
+
+ EncryptionParameters parms2 = new EncryptionParameters(SchemeType.BGV);
+ Utilities.AssertThrows(() => parms2 = new EncryptionParameters(null));
+ Utilities.AssertThrows(() => parms2.Set(null));
+ Utilities.AssertThrows(() => parms2.CoeffModulus = null);
+ Utilities.AssertThrows(() => parms2.Save(null));
+ Utilities.AssertThrows(() => parms2.Load(null));
+ Utilities.AssertThrows(() => parms2.Load(new MemoryStream()));
}
}
}
diff --git a/dotnet/tests/EncryptorTests.cs b/dotnet/tests/EncryptorTests.cs
index e1bf01090..d6d5690fb 100644
--- a/dotnet/tests/EncryptorTests.cs
+++ b/dotnet/tests/EncryptorTests.cs
@@ -73,6 +73,71 @@ public void EncryptTest()
Assert.IsNotNull(encryptor);
+ Plaintext plain = new Plaintext("1x^1 + 1");
+ encryptor.EncryptSymmetric(plain).Save(stream);
+ stream.Seek(0, SeekOrigin.Begin);
+ Ciphertext cipher = new Ciphertext();
+ cipher.Load(context, stream);
+ Assert.IsNotNull(cipher);
+ Assert.AreEqual(2ul, cipher.Size);
+ }
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ SecretKey secretKey = keygen.SecretKey;
+ Encryptor encryptor = new Encryptor(context, publicKey, secretKey);
+
+ Assert.IsNotNull(encryptor);
+
+ Plaintext plain = new Plaintext("1x^1 + 1");
+ Ciphertext cipher = new Ciphertext();
+ Assert.AreEqual(0ul, cipher.Size);
+ encryptor.Encrypt(plain, cipher);
+ Assert.IsNotNull(cipher);
+ Assert.AreEqual(2ul, cipher.Size);
+ }
+ using (MemoryStream stream = new MemoryStream())
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ Encryptor encryptor = new Encryptor(context, publicKey);
+
+ Assert.IsNotNull(encryptor);
+
+ Plaintext plain = new Plaintext("1x^1 + 1");
+ encryptor.Encrypt(plain).Save(stream);
+ stream.Seek(0, SeekOrigin.Begin);
+ Ciphertext cipher = new Ciphertext();
+ cipher.Load(context, stream);
+ Assert.IsNotNull(cipher);
+ Assert.AreEqual(2ul, cipher.Size);
+ }
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ SecretKey secretKey = keygen.SecretKey;
+ Encryptor encryptor = new Encryptor(context, secretKey);
+
+ Assert.IsNotNull(encryptor);
+
+ Plaintext plain = new Plaintext("1x^1 + 1");
+ Ciphertext cipher = new Ciphertext();
+ Assert.AreEqual(0ul, cipher.Size);
+ encryptor.EncryptSymmetric(plain, cipher);
+ Assert.IsNotNull(cipher);
+ Assert.AreEqual(2ul, cipher.Size);
+ }
+ using (MemoryStream stream = new MemoryStream())
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ SecretKey secretKey = keygen.SecretKey;
+ Encryptor encryptor = new Encryptor(context, secretKey);
+
+ Assert.IsNotNull(encryptor);
+
Plaintext plain = new Plaintext("1x^1 + 1");
encryptor.EncryptSymmetric(plain).Save(stream);
stream.Seek(0, SeekOrigin.Begin);
@@ -191,6 +256,111 @@ public void EncryptZeroTest()
Assert.IsTrue(plain.IsZero);
}
}
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ SecretKey secretKey = keygen.SecretKey;
+
+ Decryptor decryptor = new Decryptor(context, secretKey);
+ Assert.IsNotNull(decryptor);
+
+ Ciphertext cipher = new Ciphertext();
+ Plaintext plain = new Plaintext();
+ ParmsId nextParms = context.FirstContextData.NextContextData.ParmsId;
+
+ {
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Assert.IsNotNull(encryptor);
+
+ encryptor.EncryptZero(cipher);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+
+ encryptor.EncryptZero(nextParms, cipher);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ Assert.AreEqual(cipher.ParmsId, nextParms);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+ }
+ {
+ Encryptor encryptor = new Encryptor(context, secretKey);
+
+ encryptor.EncryptZeroSymmetric(cipher);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+
+ encryptor.EncryptZeroSymmetric(nextParms, cipher);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ Assert.AreEqual(cipher.ParmsId, nextParms);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+ }
+ using (MemoryStream stream = new MemoryStream())
+ {
+ Encryptor encryptor = new Encryptor(context, publicKey);
+
+ encryptor.EncryptZero().Save(stream);
+ stream.Seek(0, SeekOrigin.Begin);
+ cipher.Load(context, stream);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+ }
+ using (MemoryStream stream = new MemoryStream())
+ {
+ Encryptor encryptor = new Encryptor(context, publicKey);
+
+ encryptor.EncryptZero(nextParms).Save(stream);
+ stream.Seek(0, SeekOrigin.Begin);
+ cipher.Load(context, stream);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ Assert.AreEqual(cipher.ParmsId, nextParms);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+ }
+ using (MemoryStream stream = new MemoryStream())
+ {
+ Encryptor encryptor = new Encryptor(context, secretKey);
+
+ encryptor.EncryptZeroSymmetric().Save(stream);
+ stream.Seek(0, SeekOrigin.Begin);
+ cipher.Load(context, stream);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+ }
+ using (MemoryStream stream = new MemoryStream())
+ {
+ Encryptor encryptor = new Encryptor(context, secretKey);
+
+ encryptor.EncryptZeroSymmetric(nextParms).Save(stream);
+ stream.Seek(0, SeekOrigin.Begin);
+ cipher.Load(context, stream);
+ Assert.IsFalse(cipher.IsNTTForm);
+ Assert.IsFalse(cipher.IsTransparent);
+ Assert.AreEqual(cipher.Scale, 1.0, double.Epsilon);
+ Assert.AreEqual(cipher.ParmsId, nextParms);
+ decryptor.Decrypt(cipher, plain);
+ Assert.IsTrue(plain.IsZero);
+ }
+ }
{
SEALContext context = GlobalContext.CKKSContext;
KeyGenerator keygen = new KeyGenerator(context);
@@ -363,39 +533,76 @@ public void EncryptZeroTest()
[TestMethod]
public void ExceptionsTest()
{
- SEALContext context = GlobalContext.BFVContext;
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey pubKey);
- 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(() => encryptor = new Encryptor(context, null));
- Utilities.AssertThrows(() => encryptor = new Encryptor(null, pubKey));
- Utilities.AssertThrows(() => encryptor = new Encryptor(context, pubKey_invalid));
- Utilities.AssertThrows(() => encryptor = new Encryptor(context, pubKey_invalid, secKey));
- encryptor = new Encryptor(context, pubKey, secKey);
- Utilities.AssertThrows(() => encryptor.SetPublicKey(pubKey_invalid));
- Utilities.AssertThrows(() => encryptor.SetSecretKey(secKey_invalid));
-
- Utilities.AssertThrows(() => encryptor.Encrypt(null, cipher));
- Utilities.AssertThrows(() => encryptor.Encrypt(plain, cipher, pool_invalid));
- Utilities.AssertThrows(() => encryptor.EncryptZero(cipher, pool_invalid));
- Utilities.AssertThrows(() => encryptor.EncryptZero(parmsId_invalid, cipher));
-
- Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain, destination: null));
- Utilities.AssertThrows(() => encryptor.EncryptSymmetric(null, cipher));
- Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain, cipher, pool_invalid));
- Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric(cipher, pool_invalid));
- Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric(parmsId_invalid, cipher));
-
- Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain).Save(null));
- Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric().Save(null));
+ {
+ SEALContext context = GlobalContext.BFVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey pubKey);
+ 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(() => encryptor = new Encryptor(context, null));
+ Utilities.AssertThrows(() => encryptor = new Encryptor(null, pubKey));
+ Utilities.AssertThrows(() => encryptor = new Encryptor(context, pubKey_invalid));
+ Utilities.AssertThrows(() => encryptor = new Encryptor(context, pubKey_invalid, secKey));
+ encryptor = new Encryptor(context, pubKey, secKey);
+ Utilities.AssertThrows(() => encryptor.SetPublicKey(pubKey_invalid));
+ Utilities.AssertThrows(() => encryptor.SetSecretKey(secKey_invalid));
+
+ Utilities.AssertThrows(() => encryptor.Encrypt(null, cipher));
+ Utilities.AssertThrows(() => encryptor.Encrypt(plain, cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZero(cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZero(parmsId_invalid, cipher));
+
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain, destination: null));
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(null, cipher));
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain, cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric(cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric(parmsId_invalid, cipher));
+
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain).Save(null));
+ Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric().Save(null));
+ }
+ {
+ SEALContext context = GlobalContext.BGVContext;
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey pubKey);
+ 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(() => encryptor = new Encryptor(context, null));
+ Utilities.AssertThrows(() => encryptor = new Encryptor(null, pubKey));
+ Utilities.AssertThrows(() => encryptor = new Encryptor(context, pubKey_invalid));
+ Utilities.AssertThrows(() => encryptor = new Encryptor(context, pubKey_invalid, secKey));
+ encryptor = new Encryptor(context, pubKey, secKey);
+ Utilities.AssertThrows(() => encryptor.SetPublicKey(pubKey_invalid));
+ Utilities.AssertThrows(() => encryptor.SetSecretKey(secKey_invalid));
+
+ Utilities.AssertThrows(() => encryptor.Encrypt(null, cipher));
+ Utilities.AssertThrows(() => encryptor.Encrypt(plain, cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZero(cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZero(parmsId_invalid, cipher));
+
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain, destination: null));
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(null, cipher));
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain, cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric(cipher, pool_invalid));
+ Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric(parmsId_invalid, cipher));
+
+ Utilities.AssertThrows(() => encryptor.EncryptSymmetric(plain).Save(null));
+ Utilities.AssertThrows(() => encryptor.EncryptZeroSymmetric().Save(null));
+ }
}
}
}
diff --git a/dotnet/tests/EvaluatorTests.cs b/dotnet/tests/EvaluatorTests.cs
index 347833d5c..c6cbef757 100644
--- a/dotnet/tests/EvaluatorTests.cs
+++ b/dotnet/tests/EvaluatorTests.cs
@@ -16,854 +16,1720 @@ public class EvaluatorTests
[TestMethod]
public void CreateTest()
{
- Evaluator evaluator = new Evaluator(GlobalContext.BFVContext);
- Assert.IsNotNull(evaluator);
+ {
+ Evaluator evaluator = new Evaluator(GlobalContext.BFVContext);
+ Assert.IsNotNull(evaluator);
+ }
+ {
+ Evaluator evaluator = new Evaluator(GlobalContext.BGVContext);
+ Assert.IsNotNull(evaluator);
+ }
}
[TestMethod]
public void NegateTest()
{
- 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);
- Assert.IsTrue(context.ParametersSet);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdestination = new Ciphertext();
- Plaintext plain = new Plaintext("3x^2 + 2x^1 + 1");
- Plaintext plaindest = new Plaintext();
- encryptor.Encrypt(plain, encrypted);
- evaluator.Negate(encrypted, encdestination);
- decryptor.Decrypt(encdestination, plaindest);
-
- // coefficients are negated (modulo 64)
- Assert.AreEqual(0x3Ful, plaindest[0]);
- Assert.AreEqual(0x3Eul, plaindest[1]);
- Assert.AreEqual(0x3Dul, plaindest[2]);
-
- plain = new Plaintext("6x^3 + 7x^2 + 8x^1 + 9");
- encryptor.Encrypt(plain, encrypted);
- evaluator.NegateInplace(encrypted);
- decryptor.Decrypt(encrypted, plain);
-
- // coefficients are negated (modulo 64)
- Assert.AreEqual(0x37ul, plain[0]);
- Assert.AreEqual(0x38ul, plain[1]);
- Assert.AreEqual(0x39ul, plain[2]);
- Assert.AreEqual(0x3Aul, plain[3]);
+ 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);
+ Assert.IsTrue(context.ParametersSet);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdestination = new Ciphertext();
+ Plaintext plain = new Plaintext("3x^2 + 2x^1 + 1");
+ Plaintext plaindest = new Plaintext();
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.Negate(encrypted, encdestination);
+ decryptor.Decrypt(encdestination, plaindest);
+
+ // coefficients are negated (modulo 64)
+ Assert.AreEqual(0x3Ful, plaindest[0]);
+ Assert.AreEqual(0x3Eul, plaindest[1]);
+ Assert.AreEqual(0x3Dul, plaindest[2]);
+
+ plain = new Plaintext("6x^3 + 7x^2 + 8x^1 + 9");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.NegateInplace(encrypted);
+ decryptor.Decrypt(encrypted, plain);
+
+ // coefficients are negated (modulo 64)
+ Assert.AreEqual(0x37ul, plain[0]);
+ Assert.AreEqual(0x38ul, plain[1]);
+ Assert.AreEqual(0x39ul, plain[2]);
+ Assert.AreEqual(0x3Aul, plain[3]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ Assert.IsTrue(context.ParametersSet);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdestination = new Ciphertext();
+ Plaintext plain = new Plaintext("3x^2 + 2x^1 + 1");
+ Plaintext plaindest = new Plaintext();
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.Negate(encrypted, encdestination);
+ decryptor.Decrypt(encdestination, plaindest);
+
+ // coefficients are negated (modulo 64)
+ Assert.AreEqual(0x3Ful, plaindest[0]);
+ Assert.AreEqual(0x3Eul, plaindest[1]);
+ Assert.AreEqual(0x3Dul, plaindest[2]);
+
+ plain = new Plaintext("6x^3 + 7x^2 + 8x^1 + 9");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.NegateInplace(encrypted);
+ decryptor.Decrypt(encrypted, plain);
+
+ // coefficients are negated (modulo 64)
+ Assert.AreEqual(0x37ul, plain[0]);
+ Assert.AreEqual(0x38ul, plain[1]);
+ Assert.AreEqual(0x39ul, plain[2]);
+ Assert.AreEqual(0x3Aul, plain[3]);
+ }
}
[TestMethod]
public void AddTest()
{
- 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);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted1 = new Ciphertext();
- Ciphertext encrypted2 = new Ciphertext();
- Ciphertext encdestination = new Ciphertext();
-
- Plaintext plain1 = new Plaintext("5x^4 + 4x^3 + 3x^2 + 2x^1 + 1");
- Plaintext plain2 = new Plaintext("4x^7 + 5x^6 + 6x^5 + 7x^4 + 8x^3 + 9x^2 + Ax^1 + B");
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(plain1, encrypted1);
- encryptor.Encrypt(plain2, encrypted2);
- evaluator.Add(encrypted1, encrypted2, encdestination);
- decryptor.Decrypt(encdestination, plaindest);
-
- Assert.AreEqual(12ul, plaindest[0]);
- Assert.AreEqual(12ul, plaindest[1]);
- Assert.AreEqual(12ul, plaindest[2]);
- Assert.AreEqual(12ul, plaindest[3]);
- Assert.AreEqual(12ul, plaindest[4]);
- Assert.AreEqual(6ul, plaindest[5]);
- Assert.AreEqual(5ul, plaindest[6]);
- Assert.AreEqual(4ul, plaindest[7]);
-
- plain1 = new Plaintext("1x^2 + 2x^1 + 3");
- plain2 = new Plaintext("2x^3 + 2x^2 + 2x^1 + 2");
-
- encryptor.Encrypt(plain1, encrypted1);
- encryptor.Encrypt(plain2, encrypted2);
- evaluator.AddInplace(encrypted1, encrypted2);
- decryptor.Decrypt(encrypted1, plaindest);
-
- Assert.AreEqual(5ul, plaindest[0]);
- Assert.AreEqual(4ul, plaindest[1]);
- Assert.AreEqual(3ul, plaindest[2]);
- Assert.AreEqual(2ul, plaindest[3]);
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encdestination = new Ciphertext();
+
+ Plaintext plain1 = new Plaintext("5x^4 + 4x^3 + 3x^2 + 2x^1 + 1");
+ Plaintext plain2 = new Plaintext("4x^7 + 5x^6 + 6x^5 + 7x^4 + 8x^3 + 9x^2 + Ax^1 + B");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.Add(encrypted1, encrypted2, encdestination);
+ decryptor.Decrypt(encdestination, plaindest);
+
+ Assert.AreEqual(12ul, plaindest[0]);
+ Assert.AreEqual(12ul, plaindest[1]);
+ Assert.AreEqual(12ul, plaindest[2]);
+ Assert.AreEqual(12ul, plaindest[3]);
+ Assert.AreEqual(12ul, plaindest[4]);
+ Assert.AreEqual(6ul, plaindest[5]);
+ Assert.AreEqual(5ul, plaindest[6]);
+ Assert.AreEqual(4ul, plaindest[7]);
+
+ plain1 = new Plaintext("1x^2 + 2x^1 + 3");
+ plain2 = new Plaintext("2x^3 + 2x^2 + 2x^1 + 2");
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.AddInplace(encrypted1, encrypted2);
+ decryptor.Decrypt(encrypted1, plaindest);
+
+ Assert.AreEqual(5ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+ Assert.AreEqual(2ul, plaindest[3]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encdestination = new Ciphertext();
+
+ Plaintext plain1 = new Plaintext("5x^4 + 4x^3 + 3x^2 + 2x^1 + 1");
+ Plaintext plain2 = new Plaintext("4x^7 + 5x^6 + 6x^5 + 7x^4 + 8x^3 + 9x^2 + Ax^1 + B");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.Add(encrypted1, encrypted2, encdestination);
+ decryptor.Decrypt(encdestination, plaindest);
+
+ Assert.AreEqual(12ul, plaindest[0]);
+ Assert.AreEqual(12ul, plaindest[1]);
+ Assert.AreEqual(12ul, plaindest[2]);
+ Assert.AreEqual(12ul, plaindest[3]);
+ Assert.AreEqual(12ul, plaindest[4]);
+ Assert.AreEqual(6ul, plaindest[5]);
+ Assert.AreEqual(5ul, plaindest[6]);
+ Assert.AreEqual(4ul, plaindest[7]);
+
+ plain1 = new Plaintext("1x^2 + 2x^1 + 3");
+ plain2 = new Plaintext("2x^3 + 2x^2 + 2x^1 + 2");
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.AddInplace(encrypted1, encrypted2);
+ decryptor.Decrypt(encrypted1, plaindest);
+
+ Assert.AreEqual(5ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+ Assert.AreEqual(2ul, plaindest[3]);
+ }
}
[TestMethod]
public void AddPlainTest()
{
- 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);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plain = new Plaintext("3x^2 + 2x^1 + 1");
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("2x^2 + 2x^1 + 2"), encrypted);
- evaluator.AddPlain(encrypted, plain, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.AreEqual(3ul, plaindest[0]);
- Assert.AreEqual(4ul, plaindest[1]);
- Assert.AreEqual(5ul, plaindest[2]);
-
- plain.Set("1x^2 + 1x^1 + 1");
- encryptor.Encrypt(new Plaintext("2x^3 + 2x^2 + 2x^1 + 2"), encrypted);
- evaluator.AddPlainInplace(encrypted, plain);
- decryptor.Decrypt(encrypted, plaindest);
-
- Assert.AreEqual(4ul, plaindest.CoeffCount);
- Assert.AreEqual(3ul, plaindest[0]);
- Assert.AreEqual(3ul, plaindest[1]);
- Assert.AreEqual(3ul, plaindest[2]);
- Assert.AreEqual(2ul, plaindest[3]);
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("3x^2 + 2x^1 + 1");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("2x^2 + 2x^1 + 2"), encrypted);
+ evaluator.AddPlain(encrypted, plain, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(3ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(5ul, plaindest[2]);
+
+ plain.Set("1x^2 + 1x^1 + 1");
+ encryptor.Encrypt(new Plaintext("2x^3 + 2x^2 + 2x^1 + 2"), encrypted);
+ evaluator.AddPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(3ul, plaindest[0]);
+ Assert.AreEqual(3ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+ Assert.AreEqual(2ul, plaindest[3]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("3x^2 + 2x^1 + 1");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("2x^2 + 2x^1 + 2"), encrypted);
+ evaluator.AddPlain(encrypted, plain, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(3ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(5ul, plaindest[2]);
+
+ plain.Set("1x^2 + 1x^1 + 1");
+ encryptor.Encrypt(new Plaintext("2x^3 + 2x^2 + 2x^1 + 2"), encrypted);
+ evaluator.AddPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(3ul, plaindest[0]);
+ Assert.AreEqual(3ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+ Assert.AreEqual(2ul, plaindest[3]);
+ }
}
[TestMethod]
public void AddManyTest()
{
- 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);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext[] encrypteds = new Ciphertext[6];
-
- for(int i = 0; i < encrypteds.Length; i++)
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext[] encrypteds = new Ciphertext[6];
+
+ for(int i = 0; i < encrypteds.Length; i++)
+ {
+ encrypteds[i] = new Ciphertext();
+ encryptor.Encrypt(new Plaintext((i + 1).ToString()), encrypteds[i]);
+ }
+
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+ evaluator.AddMany(encrypteds, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // 1+2+3+4+5+6
+ Assert.AreEqual(21ul, plaindest[0]);
+ }
{
- encrypteds[i] = new Ciphertext();
- encryptor.Encrypt(new Plaintext((i + 1).ToString()), encrypteds[i]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext[] encrypteds = new Ciphertext[6];
+
+ for(int i = 0; i < encrypteds.Length; i++)
+ {
+ encrypteds[i] = new Ciphertext();
+ encryptor.Encrypt(new Plaintext((i + 1).ToString()), encrypteds[i]);
+ }
+
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+ evaluator.AddMany(encrypteds, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // 1+2+3+4+5+6
+ Assert.AreEqual(21ul, plaindest[0]);
}
-
- Ciphertext encdest = new Ciphertext();
- Plaintext plaindest = new Plaintext();
- evaluator.AddMany(encrypteds, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- // 1+2+3+4+5+6
- Assert.AreEqual(21ul, plaindest[0]);
}
[TestMethod]
public void SubTest()
{
- 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);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted1 = new Ciphertext();
- Ciphertext encrypted2 = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plain1 = new Plaintext("Ax^2 + Bx^1 + C");
- Plaintext plain2 = new Plaintext("5x^3 + 5x^2 + 5x^1 + 5");
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(plain1, encrypted1);
- encryptor.Encrypt(plain2, encrypted2);
- evaluator.Sub(encrypted1, encrypted2, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.AreEqual(7ul, plaindest[0]);
- Assert.AreEqual(6ul, plaindest[1]);
- Assert.AreEqual(5ul, plaindest[2]);
- Assert.AreEqual(0x3Bul, plaindest[3]);
-
- plain1.Set("Ax^3 + Bx^2 + Cx^1 + D");
- plain2.Set("5x^2 + 5x^1 + 5");
-
- encryptor.Encrypt(plain1, encrypted1);
- encryptor.Encrypt(plain2, encrypted2);
- evaluator.SubInplace(encrypted1, encrypted2);
- decryptor.Decrypt(encrypted1, plaindest);
-
- Assert.AreEqual(8ul, plaindest[0]);
- Assert.AreEqual(7ul, plaindest[1]);
- Assert.AreEqual(6ul, plaindest[2]);
- Assert.AreEqual(10ul, plaindest[3]);
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain1 = new Plaintext("Ax^2 + Bx^1 + C");
+ Plaintext plain2 = new Plaintext("5x^3 + 5x^2 + 5x^1 + 5");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.Sub(encrypted1, encrypted2, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(7ul, plaindest[0]);
+ Assert.AreEqual(6ul, plaindest[1]);
+ Assert.AreEqual(5ul, plaindest[2]);
+ Assert.AreEqual(0x3Bul, plaindest[3]);
+
+ plain1.Set("Ax^3 + Bx^2 + Cx^1 + D");
+ plain2.Set("5x^2 + 5x^1 + 5");
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.SubInplace(encrypted1, encrypted2);
+ decryptor.Decrypt(encrypted1, plaindest);
+
+ Assert.AreEqual(8ul, plaindest[0]);
+ Assert.AreEqual(7ul, plaindest[1]);
+ Assert.AreEqual(6ul, plaindest[2]);
+ Assert.AreEqual(10ul, plaindest[3]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain1 = new Plaintext("Ax^2 + Bx^1 + C");
+ Plaintext plain2 = new Plaintext("5x^3 + 5x^2 + 5x^1 + 5");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.Sub(encrypted1, encrypted2, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(7ul, plaindest[0]);
+ Assert.AreEqual(6ul, plaindest[1]);
+ Assert.AreEqual(5ul, plaindest[2]);
+ Assert.AreEqual(0x3Bul, plaindest[3]);
+
+ plain1.Set("Ax^3 + Bx^2 + Cx^1 + D");
+ plain2.Set("5x^2 + 5x^1 + 5");
+
+ encryptor.Encrypt(plain1, encrypted1);
+ encryptor.Encrypt(plain2, encrypted2);
+ evaluator.SubInplace(encrypted1, encrypted2);
+ decryptor.Decrypt(encrypted1, plaindest);
+
+ Assert.AreEqual(8ul, plaindest[0]);
+ Assert.AreEqual(7ul, plaindest[1]);
+ Assert.AreEqual(6ul, plaindest[2]);
+ Assert.AreEqual(10ul, plaindest[3]);
+ }
}
[TestMethod]
public void SubPlainTest()
{
- 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);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plain = new Plaintext("5x^2 + 4x^1 + 3");
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("3x^1 + 4"), encrypted);
- evaluator.SubPlain(encrypted, plain, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.AreEqual(3ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
- Assert.AreEqual(0x3Ful, plaindest[1]); // -1
- Assert.AreEqual(0x3Bul, plaindest[2]); // -5
-
- plain.Set("6x^3 + 1x^2 + 7x^1 + 2");
- encryptor.Encrypt(new Plaintext("Ax^2 + Bx^1 + C"), encrypted);
- evaluator.SubPlainInplace(encrypted, plain);
- decryptor.Decrypt(encrypted, plaindest);
-
- Assert.AreEqual(4ul, plaindest.CoeffCount);
- Assert.AreEqual(10ul, plaindest[0]);
- Assert.AreEqual(4ul, plaindest[1]);
- Assert.AreEqual(9ul, plaindest[2]);
- Assert.AreEqual(0x3Aul, plaindest[3]); // -6
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("5x^2 + 4x^1 + 3");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("3x^1 + 4"), encrypted);
+ evaluator.SubPlain(encrypted, plain, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(0x3Ful, plaindest[1]); // -1
+ Assert.AreEqual(0x3Bul, plaindest[2]); // -5
+
+ plain.Set("6x^3 + 1x^2 + 7x^1 + 2");
+ encryptor.Encrypt(new Plaintext("Ax^2 + Bx^1 + C"), encrypted);
+ evaluator.SubPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(10ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(9ul, plaindest[2]);
+ Assert.AreEqual(0x3Aul, plaindest[3]); // -6
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("5x^2 + 4x^1 + 3");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("3x^1 + 4"), encrypted);
+ evaluator.SubPlain(encrypted, plain, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(0x3Ful, plaindest[1]); // -1
+ Assert.AreEqual(0x3Bul, plaindest[2]); // -5
+
+ plain.Set("6x^3 + 1x^2 + 7x^1 + 2");
+ encryptor.Encrypt(new Plaintext("Ax^2 + Bx^1 + C"), encrypted);
+ evaluator.SubPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(10ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(9ul, plaindest[2]);
+ Assert.AreEqual(0x3Aul, plaindest[3]); // -6
+ }
}
[TestMethod]
public void MultiplyTest()
{
- 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);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted1 = new Ciphertext();
- Ciphertext encrypted2 = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("1x^4 + 2x^3 + 3x^2 + 4x^1 + 5"), encrypted1);
- encryptor.Encrypt(new Plaintext("3x^2 + 2x^1 + 1"), encrypted2);
- evaluator.Multiply(encrypted1, encrypted2, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- // {3x^6 + 8x^5 + Ex^4 + 14x^3 + 1Ax^2 + Ex^1 + 5}
- Assert.AreEqual(7ul, plaindest.CoeffCount);
- Assert.AreEqual(5ul, plaindest[0]);
- Assert.AreEqual(14ul, plaindest[1]);
- Assert.AreEqual(26ul, plaindest[2]);
- Assert.AreEqual(20ul, plaindest[3]);
- Assert.AreEqual(14ul, plaindest[4]);
- Assert.AreEqual(8ul, plaindest[5]);
- Assert.AreEqual(3ul, plaindest[6]);
-
- encryptor.Encrypt(new Plaintext("2x^2 + 3x^1 + 4"), encrypted1);
- encryptor.Encrypt(new Plaintext("4x^1 + 5"), encrypted2);
- evaluator.MultiplyInplace(encrypted1, encrypted2);
- decryptor.Decrypt(encrypted1, plaindest);
-
- // {8x^3 + 16x^2 + 1Fx^1 + 14}
- Assert.AreEqual(4ul, plaindest.CoeffCount);
- Assert.AreEqual(20ul, plaindest[0]);
- Assert.AreEqual(31ul, plaindest[1]);
- Assert.AreEqual(22ul, plaindest[2]);
- Assert.AreEqual(8ul, plaindest[3]);
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("1x^4 + 2x^3 + 3x^2 + 4x^1 + 5"), encrypted1);
+ encryptor.Encrypt(new Plaintext("3x^2 + 2x^1 + 1"), encrypted2);
+ evaluator.Multiply(encrypted1, encrypted2, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {3x^6 + 8x^5 + Ex^4 + 14x^3 + 1Ax^2 + Ex^1 + 5}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(5ul, plaindest[0]);
+ Assert.AreEqual(14ul, plaindest[1]);
+ Assert.AreEqual(26ul, plaindest[2]);
+ Assert.AreEqual(20ul, plaindest[3]);
+ Assert.AreEqual(14ul, plaindest[4]);
+ Assert.AreEqual(8ul, plaindest[5]);
+ Assert.AreEqual(3ul, plaindest[6]);
+
+ encryptor.Encrypt(new Plaintext("2x^2 + 3x^1 + 4"), encrypted1);
+ encryptor.Encrypt(new Plaintext("4x^1 + 5"), encrypted2);
+ evaluator.MultiplyInplace(encrypted1, encrypted2);
+ decryptor.Decrypt(encrypted1, plaindest);
+
+ // {8x^3 + 16x^2 + 1Fx^1 + 14}
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(20ul, plaindest[0]);
+ Assert.AreEqual(31ul, plaindest[1]);
+ Assert.AreEqual(22ul, plaindest[2]);
+ Assert.AreEqual(8ul, plaindest[3]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ 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);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("1x^4 + 2x^3 + 3x^2 + 4x^1 + 5"), encrypted1);
+ encryptor.Encrypt(new Plaintext("3x^2 + 2x^1 + 1"), encrypted2);
+ evaluator.Multiply(encrypted1, encrypted2, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {3x^6 + 8x^5 + Ex^4 + 14x^3 + 1Ax^2 + Ex^1 + 5}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(5ul, plaindest[0]);
+ Assert.AreEqual(14ul, plaindest[1]);
+ Assert.AreEqual(26ul, plaindest[2]);
+ Assert.AreEqual(20ul, plaindest[3]);
+ Assert.AreEqual(14ul, plaindest[4]);
+ Assert.AreEqual(8ul, plaindest[5]);
+ Assert.AreEqual(3ul, plaindest[6]);
+
+ encryptor.Encrypt(new Plaintext("2x^2 + 3x^1 + 4"), encrypted1);
+ encryptor.Encrypt(new Plaintext("4x^1 + 5"), encrypted2);
+ evaluator.MultiplyInplace(encrypted1, encrypted2);
+ decryptor.Decrypt(encrypted1, plaindest);
+
+ // {8x^3 + 16x^2 + 1Fx^1 + 14}
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(20ul, plaindest[0]);
+ Assert.AreEqual(31ul, plaindest[1]);
+ Assert.AreEqual(22ul, plaindest[2]);
+ Assert.AreEqual(8ul, plaindest[3]);
+ }
}
[TestMethod]
public void MultiplyManyTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
- {
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
- keygen.CreateRelinKeys(out RelinKeys relinKeys);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext[] encrypteds = new Ciphertext[4];
- Ciphertext encdest = new Ciphertext();
- Plaintext plaindest = new Plaintext();
-
- for (int i = 0; i < encrypteds.Length; i++)
{
- encrypteds[i] = new Ciphertext();
- encryptor.Encrypt(new Plaintext((i + 1).ToString()), encrypteds[i]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext[] encrypteds = new Ciphertext[4];
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+
+ for (int i = 0; i < encrypteds.Length; i++)
+ {
+ encrypteds[i] = new Ciphertext();
+ encryptor.Encrypt(new Plaintext((i + 1).ToString()), encrypteds[i]);
+ }
+
+ evaluator.MultiplyMany(encrypteds, relinKeys, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(24ul, plaindest[0]);
+
+ Utilities.AssertThrows(() =>
+ {
+ // Uninitialized memory pool handle
+ MemoryPoolHandle pool = new MemoryPoolHandle();
+ evaluator.MultiplyMany(encrypteds, relinKeys, encdest, pool);
+ });
}
-
- evaluator.MultiplyMany(encrypteds, relinKeys, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.AreEqual(1ul, plaindest.CoeffCount);
- Assert.AreEqual(24ul, plaindest[0]);
-
- Utilities.AssertThrows(() =>
{
- // Uninitialized memory pool handle
- MemoryPoolHandle pool = new MemoryPoolHandle();
- evaluator.MultiplyMany(encrypteds, relinKeys, encdest, pool);
- });
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext[] encrypteds = new Ciphertext[4];
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+
+ for (int i = 0; i < encrypteds.Length; i++)
+ {
+ encrypteds[i] = new Ciphertext();
+ encryptor.Encrypt(new Plaintext((i + 1).ToString()), encrypteds[i]);
+ }
+
+ evaluator.MultiplyMany(encrypteds, relinKeys, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(24ul, plaindest[0]);
+
+ Utilities.AssertThrows(() =>
+ {
+ // Uninitialized memory pool handle
+ MemoryPoolHandle pool = new MemoryPoolHandle();
+ evaluator.MultiplyMany(encrypteds, relinKeys, encdest, pool);
+ });
+ }
}
[TestMethod]
public void MultiplyPlainTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
- keygen.CreateRelinKeys(out RelinKeys relinKeys);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plain = new Plaintext("2x^2 + 1");
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("3x^2 + 2"), encrypted);
- evaluator.MultiplyPlain(encrypted, plain, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- // {6x^4 + 7x^2 + 2}
- Assert.AreEqual(5ul, plaindest.CoeffCount);
- Assert.AreEqual(2ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(7ul, plaindest[2]);
- Assert.AreEqual(0ul, plaindest[3]);
- Assert.AreEqual(6ul, plaindest[4]);
-
- encryptor.Encrypt(new Plaintext("4x^1 + 3"), encrypted);
- plain.Set("2x^2 + 1");
- evaluator.MultiplyPlainInplace(encrypted, plain);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {8x^3 + 6x^2 + 4x^1 + 3}
- Assert.AreEqual(4ul, plaindest.CoeffCount);
- Assert.AreEqual(3ul, plaindest[0]);
- Assert.AreEqual(4ul, plaindest[1]);
- Assert.AreEqual(6ul, plaindest[2]);
- Assert.AreEqual(8ul, plaindest[3]);
-
- encryptor.Encrypt(new Plaintext("4x^1 + 3"), encrypted);
- plain.Set("3x^5");
- evaluator.MultiplyPlainInplace(encrypted, plain);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {Cx^6 + 9x^5}
- Assert.AreEqual(7ul, plaindest.CoeffCount);
- Assert.AreEqual(2ul, plaindest.NonZeroCoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(0ul, plaindest[2]);
- Assert.AreEqual(0ul, plaindest[3]);
- Assert.AreEqual(0ul, plaindest[4]);
- Assert.AreEqual(9ul, plaindest[5]);
- Assert.AreEqual(12ul, plaindest[6]);
-
- Utilities.AssertThrows(() =>
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("2x^2 + 1");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("3x^2 + 2"), encrypted);
+ evaluator.MultiplyPlain(encrypted, plain, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {6x^4 + 7x^2 + 2}
+ Assert.AreEqual(5ul, plaindest.CoeffCount);
+ Assert.AreEqual(2ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(7ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(6ul, plaindest[4]);
+
+ encryptor.Encrypt(new Plaintext("4x^1 + 3"), encrypted);
+ plain.Set("2x^2 + 1");
+ evaluator.MultiplyPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {8x^3 + 6x^2 + 4x^1 + 3}
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(3ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(6ul, plaindest[2]);
+ Assert.AreEqual(8ul, plaindest[3]);
+
+ encryptor.Encrypt(new Plaintext("4x^1 + 3"), encrypted);
+ plain.Set("3x^5");
+ evaluator.MultiplyPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {Cx^6 + 9x^5}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(2ul, plaindest.NonZeroCoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(9ul, plaindest[5]);
+ Assert.AreEqual(12ul, plaindest[6]);
+
+ Utilities.AssertThrows(() =>
+ {
+ // Uninitialized pool
+ MemoryPoolHandle pool = new MemoryPoolHandle();
+ evaluator.MultiplyPlain(encrypted, plain, encdest, pool);
+ });
+ }
{
- // Uninitialized pool
- MemoryPoolHandle pool = new MemoryPoolHandle();
- evaluator.MultiplyPlain(encrypted, plain, encdest, pool);
- });
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("2x^2 + 1");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("3x^2 + 2"), encrypted);
+ evaluator.MultiplyPlain(encrypted, plain, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {6x^4 + 7x^2 + 2}
+ Assert.AreEqual(5ul, plaindest.CoeffCount);
+ Assert.AreEqual(2ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(7ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(6ul, plaindest[4]);
+
+ encryptor.Encrypt(new Plaintext("4x^1 + 3"), encrypted);
+ plain.Set("2x^2 + 1");
+ evaluator.MultiplyPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {8x^3 + 6x^2 + 4x^1 + 3}
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(3ul, plaindest[0]);
+ Assert.AreEqual(4ul, plaindest[1]);
+ Assert.AreEqual(6ul, plaindest[2]);
+ Assert.AreEqual(8ul, plaindest[3]);
+
+ encryptor.Encrypt(new Plaintext("4x^1 + 3"), encrypted);
+ plain.Set("3x^5");
+ evaluator.MultiplyPlainInplace(encrypted, plain);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {Cx^6 + 9x^5}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(2ul, plaindest.NonZeroCoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(9ul, plaindest[5]);
+ Assert.AreEqual(12ul, plaindest[6]);
+
+ Utilities.AssertThrows(() =>
+ {
+ // Uninitialized pool
+ MemoryPoolHandle pool = new MemoryPoolHandle();
+ evaluator.MultiplyPlain(encrypted, plain, encdest, pool);
+ });
+ }
}
[TestMethod]
public void SquareTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plain = new Plaintext("2x^2 + 3x^1 + 4");
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(plain, encrypted);
- evaluator.Square(encrypted, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- // {4x^4 + Cx^3 + 19x^2 + 18x^1 + 10}
- Assert.AreEqual(5ul, plaindest.CoeffCount);
- Assert.AreEqual(16ul, plaindest[0]);
- Assert.AreEqual(24ul, plaindest[1]);
- Assert.AreEqual(25ul, plaindest[2]);
- Assert.AreEqual(12ul, plaindest[3]);
- Assert.AreEqual(4ul, plaindest[4]);
-
- encryptor.Encrypt(new Plaintext("3x^1 + 2"), encrypted);
- evaluator.SquareInplace(encrypted);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {9x^2 + Cx^1 + 4}
- Assert.AreEqual(3ul, plaindest.CoeffCount);
- Assert.AreEqual(4ul, plaindest[0]);
- Assert.AreEqual(12ul, plaindest[1]);
- Assert.AreEqual(9ul, plaindest[2]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("2x^2 + 3x^1 + 4");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.Square(encrypted, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {4x^4 + Cx^3 + 19x^2 + 18x^1 + 10}
+ Assert.AreEqual(5ul, plaindest.CoeffCount);
+ Assert.AreEqual(16ul, plaindest[0]);
+ Assert.AreEqual(24ul, plaindest[1]);
+ Assert.AreEqual(25ul, plaindest[2]);
+ Assert.AreEqual(12ul, plaindest[3]);
+ Assert.AreEqual(4ul, plaindest[4]);
+
+ encryptor.Encrypt(new Plaintext("3x^1 + 2"), encrypted);
+ evaluator.SquareInplace(encrypted);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {9x^2 + Cx^1 + 4}
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(4ul, plaindest[0]);
+ Assert.AreEqual(12ul, plaindest[1]);
+ Assert.AreEqual(9ul, plaindest[2]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext("2x^2 + 3x^1 + 4");
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.Square(encrypted, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {4x^4 + Cx^3 + 19x^2 + 18x^1 + 10}
+ Assert.AreEqual(5ul, plaindest.CoeffCount);
+ Assert.AreEqual(16ul, plaindest[0]);
+ Assert.AreEqual(24ul, plaindest[1]);
+ Assert.AreEqual(25ul, plaindest[2]);
+ Assert.AreEqual(12ul, plaindest[3]);
+ Assert.AreEqual(4ul, plaindest[4]);
+
+ encryptor.Encrypt(new Plaintext("3x^1 + 2"), encrypted);
+ evaluator.SquareInplace(encrypted);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {9x^2 + Cx^1 + 4}
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(4ul, plaindest[0]);
+ Assert.AreEqual(12ul, plaindest[1]);
+ Assert.AreEqual(9ul, plaindest[2]);
+ }
}
[TestMethod]
public void ExponentiateTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
- keygen.CreateRelinKeys(out RelinKeys relinKeys);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Plaintext plain = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("2x^2 + 1"), encrypted);
- evaluator.Exponentiate(encrypted, 3, relinKeys, encdest);
- decryptor.Decrypt(encdest, plain);
-
- // {8x^6 + Cx^4 + 6x^2 + 1}
- Assert.AreEqual(7ul, plain.CoeffCount);
- Assert.AreEqual(1ul, plain[0]);
- Assert.AreEqual(0ul, plain[1]);
- Assert.AreEqual(6ul, plain[2]);
- Assert.AreEqual(0ul, plain[3]);
- Assert.AreEqual(12ul, plain[4]);
- Assert.AreEqual(0ul, plain[5]);
- Assert.AreEqual(8ul, plain[6]);
-
- encryptor.Encrypt(new Plaintext("3x^3 + 2"), encrypted);
- evaluator.ExponentiateInplace(encrypted, 4, relinKeys);
- decryptor.Decrypt(encrypted, plain);
-
- // {11x^12 + 18x^9 + 18x^6 + 20x^3 + 10}
- Assert.AreEqual(13ul, plain.CoeffCount);
- Assert.AreEqual(16ul, plain[0]);
- Assert.AreEqual(0ul, plain[1]);
- Assert.AreEqual(0ul, plain[2]);
- Assert.AreEqual(32ul, plain[3]);
- Assert.AreEqual(0ul, plain[4]);
- Assert.AreEqual(0ul, plain[5]);
- Assert.AreEqual(24ul, plain[6]);
- Assert.AreEqual(0ul, plain[7]);
- Assert.AreEqual(0ul, plain[8]);
- Assert.AreEqual(24ul, plain[9]);
- Assert.AreEqual(0ul, plain[10]);
- Assert.AreEqual(0ul, plain[11]);
- Assert.AreEqual(17ul, plain[12]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("2x^2 + 1"), encrypted);
+ evaluator.Exponentiate(encrypted, 3, relinKeys, encdest);
+ decryptor.Decrypt(encdest, plain);
+
+ // {8x^6 + Cx^4 + 6x^2 + 1}
+ Assert.AreEqual(7ul, plain.CoeffCount);
+ Assert.AreEqual(1ul, plain[0]);
+ Assert.AreEqual(0ul, plain[1]);
+ Assert.AreEqual(6ul, plain[2]);
+ Assert.AreEqual(0ul, plain[3]);
+ Assert.AreEqual(12ul, plain[4]);
+ Assert.AreEqual(0ul, plain[5]);
+ Assert.AreEqual(8ul, plain[6]);
+
+ encryptor.Encrypt(new Plaintext("3x^3 + 2"), encrypted);
+ evaluator.ExponentiateInplace(encrypted, 4, relinKeys);
+ decryptor.Decrypt(encrypted, plain);
+
+ // {11x^12 + 18x^9 + 18x^6 + 20x^3 + 10}
+ Assert.AreEqual(13ul, plain.CoeffCount);
+ Assert.AreEqual(16ul, plain[0]);
+ Assert.AreEqual(0ul, plain[1]);
+ Assert.AreEqual(0ul, plain[2]);
+ Assert.AreEqual(32ul, plain[3]);
+ Assert.AreEqual(0ul, plain[4]);
+ Assert.AreEqual(0ul, plain[5]);
+ Assert.AreEqual(24ul, plain[6]);
+ Assert.AreEqual(0ul, plain[7]);
+ Assert.AreEqual(0ul, plain[8]);
+ Assert.AreEqual(24ul, plain[9]);
+ Assert.AreEqual(0ul, plain[10]);
+ Assert.AreEqual(0ul, plain[11]);
+ Assert.AreEqual(17ul, plain[12]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("2x^2 + 1"), encrypted);
+ evaluator.Exponentiate(encrypted, 3, relinKeys, encdest);
+ decryptor.Decrypt(encdest, plain);
+
+ // {8x^6 + Cx^4 + 6x^2 + 1}
+ Assert.AreEqual(7ul, plain.CoeffCount);
+ Assert.AreEqual(1ul, plain[0]);
+ Assert.AreEqual(0ul, plain[1]);
+ Assert.AreEqual(6ul, plain[2]);
+ Assert.AreEqual(0ul, plain[3]);
+ Assert.AreEqual(12ul, plain[4]);
+ Assert.AreEqual(0ul, plain[5]);
+ Assert.AreEqual(8ul, plain[6]);
+
+ encryptor.Encrypt(new Plaintext("3x^3 + 2"), encrypted);
+ evaluator.ExponentiateInplace(encrypted, 4, relinKeys);
+ decryptor.Decrypt(encrypted, plain);
+
+ // {11x^12 + 18x^9 + 18x^6 + 20x^3 + 10}
+ Assert.AreEqual(13ul, plain.CoeffCount);
+ Assert.AreEqual(16ul, plain[0]);
+ Assert.AreEqual(0ul, plain[1]);
+ Assert.AreEqual(0ul, plain[2]);
+ Assert.AreEqual(32ul, plain[3]);
+ Assert.AreEqual(0ul, plain[4]);
+ Assert.AreEqual(0ul, plain[5]);
+ Assert.AreEqual(24ul, plain[6]);
+ Assert.AreEqual(0ul, plain[7]);
+ Assert.AreEqual(0ul, plain[8]);
+ Assert.AreEqual(24ul, plain[9]);
+ Assert.AreEqual(0ul, plain[10]);
+ Assert.AreEqual(0ul, plain[11]);
+ Assert.AreEqual(17ul, plain[12]);
+ }
}
[TestMethod]
public void ApplyGaloisTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 8,
- PlainModulus = new Modulus(257),
- CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
- keygen.CreateGaloisKeys(galoisElts: new uint[] { 1u, 3u, 5u, 15u }, out GaloisKeys galoisKeys);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Plaintext plain = new Plaintext("1");
- Plaintext plaindest = new Plaintext();
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
-
- encryptor.Encrypt(plain, encrypted);
- evaluator.ApplyGalois(encrypted, galoisElt: 1, galoisKeys: galoisKeys, destination: encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.AreEqual(1ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
-
- plain.Set("1x^1");
- encryptor.Encrypt(plain, encrypted);
- evaluator.ApplyGalois(encrypted, galoisElt: 1, galoisKeys: galoisKeys, destination: encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- // {1x^1}
- Assert.AreEqual(2ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(1ul, plaindest[1]);
-
- evaluator.ApplyGalois(encdest, galoisElt: 3, galoisKeys: galoisKeys, destination: encrypted);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {1x^3}
- Assert.AreEqual(4ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(0ul, plaindest[2]);
- Assert.AreEqual(1ul, plaindest[3]);
-
- evaluator.ApplyGalois(encrypted, galoisElt: 5, galoisKeys: galoisKeys, destination: encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- // {100x^7}
- Assert.AreEqual(8ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(0ul, plaindest[2]);
- Assert.AreEqual(0ul, plaindest[3]);
- Assert.AreEqual(0ul, plaindest[4]);
- Assert.AreEqual(0ul, plaindest[5]);
- Assert.AreEqual(0ul, plaindest[6]);
- Assert.AreEqual(256ul, plaindest[7]);
-
- plain.Set("1x^2");
- encryptor.Encrypt(plain, encrypted);
- evaluator.ApplyGaloisInplace(encrypted, 1, galoisKeys);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {1x^2}
- Assert.AreEqual(3ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(1ul, plaindest[2]);
-
- evaluator.ApplyGaloisInplace(encrypted, 3, galoisKeys);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {1x^6}
- Assert.AreEqual(7ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(0ul, plaindest[2]);
- Assert.AreEqual(0ul, plaindest[3]);
- Assert.AreEqual(0ul, plaindest[4]);
- Assert.AreEqual(0ul, plaindest[5]);
- Assert.AreEqual(1ul, plaindest[6]);
-
- evaluator.ApplyGaloisInplace(encrypted, 5, galoisKeys);
- decryptor.Decrypt(encrypted, plaindest);
-
- // {100x^6}
- Assert.AreEqual(7ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(0ul, plaindest[1]);
- Assert.AreEqual(0ul, plaindest[2]);
- Assert.AreEqual(0ul, plaindest[3]);
- Assert.AreEqual(0ul, plaindest[4]);
- Assert.AreEqual(0ul, plaindest[5]);
- Assert.AreEqual(256ul, plaindest[6]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 8,
+ PlainModulus = new Modulus(257),
+ CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateGaloisKeys(galoisElts: new uint[] { 1u, 3u, 5u, 15u }, out GaloisKeys galoisKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Plaintext plain = new Plaintext("1");
+ Plaintext plaindest = new Plaintext();
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ApplyGalois(encrypted, galoisElt: 1, galoisKeys: galoisKeys, destination: encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+
+ plain.Set("1x^1");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ApplyGalois(encrypted, galoisElt: 1, galoisKeys: galoisKeys, destination: encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {1x^1}
+ Assert.AreEqual(2ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(1ul, plaindest[1]);
+
+ evaluator.ApplyGalois(encdest, galoisElt: 3, galoisKeys: galoisKeys, destination: encrypted);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {1x^3}
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(1ul, plaindest[3]);
+
+ evaluator.ApplyGalois(encrypted, galoisElt: 5, galoisKeys: galoisKeys, destination: encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {100x^7}
+ Assert.AreEqual(8ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(0ul, plaindest[5]);
+ Assert.AreEqual(0ul, plaindest[6]);
+ Assert.AreEqual(256ul, plaindest[7]);
+
+ plain.Set("1x^2");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ApplyGaloisInplace(encrypted, 1, galoisKeys);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {1x^2}
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(1ul, plaindest[2]);
+
+ evaluator.ApplyGaloisInplace(encrypted, 3, galoisKeys);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {1x^6}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(0ul, plaindest[5]);
+ Assert.AreEqual(1ul, plaindest[6]);
+
+ evaluator.ApplyGaloisInplace(encrypted, 5, galoisKeys);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {100x^6}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(0ul, plaindest[5]);
+ Assert.AreEqual(256ul, plaindest[6]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 8,
+ PlainModulus = new Modulus(257),
+ CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+ keygen.CreateGaloisKeys(galoisElts: new uint[] { 1u, 3u, 5u, 15u }, out GaloisKeys galoisKeys);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Plaintext plain = new Plaintext("1");
+ Plaintext plaindest = new Plaintext();
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ApplyGalois(encrypted, galoisElt: 1, galoisKeys: galoisKeys, destination: encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+
+ plain.Set("1x^1");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ApplyGalois(encrypted, galoisElt: 1, galoisKeys: galoisKeys, destination: encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {1x^1}
+ Assert.AreEqual(2ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(1ul, plaindest[1]);
+
+ evaluator.ApplyGalois(encdest, galoisElt: 3, galoisKeys: galoisKeys, destination: encrypted);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {1x^3}
+ Assert.AreEqual(4ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(1ul, plaindest[3]);
+
+ evaluator.ApplyGalois(encrypted, galoisElt: 5, galoisKeys: galoisKeys, destination: encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ // {100x^7}
+ Assert.AreEqual(8ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(0ul, plaindest[5]);
+ Assert.AreEqual(0ul, plaindest[6]);
+ Assert.AreEqual(256ul, plaindest[7]);
+
+ plain.Set("1x^2");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ApplyGaloisInplace(encrypted, 1, galoisKeys);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {1x^2}
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(1ul, plaindest[2]);
+
+ evaluator.ApplyGaloisInplace(encrypted, 3, galoisKeys);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {1x^6}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(0ul, plaindest[5]);
+ Assert.AreEqual(1ul, plaindest[6]);
+
+ evaluator.ApplyGaloisInplace(encrypted, 5, galoisKeys);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ // {100x^6}
+ Assert.AreEqual(7ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(0ul, plaindest[1]);
+ Assert.AreEqual(0ul, plaindest[2]);
+ Assert.AreEqual(0ul, plaindest[3]);
+ Assert.AreEqual(0ul, plaindest[4]);
+ Assert.AreEqual(0ul, plaindest[5]);
+ Assert.AreEqual(256ul, plaindest[6]);
+ }
}
[TestMethod]
public void TransformPlainToNTTTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- Evaluator evaluator = new Evaluator(context);
-
- Plaintext plain = new Plaintext("0");
- Plaintext plaindest = new Plaintext();
- Assert.IsFalse(plain.IsNTTForm);
-
- evaluator.TransformToNTT(plain, context.FirstParmsId, plaindest);
- Assert.IsTrue(plaindest.IsZero);
- Assert.IsTrue(plaindest.IsNTTForm);
- Assert.IsTrue(plaindest.ParmsId == context.FirstParmsId);
-
- plain = new Plaintext("1");
- Assert.IsFalse(plain.IsNTTForm);
-
- evaluator.TransformToNTTInplace(plain, context.FirstParmsId);
- Assert.IsTrue(plain.IsNTTForm);
-
- for (ulong i = 0; i < 256; i++)
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ Evaluator evaluator = new Evaluator(context);
+
+ Plaintext plain = new Plaintext("0");
+ Plaintext plaindest = new Plaintext();
+ Assert.IsFalse(plain.IsNTTForm);
+
+ evaluator.TransformToNTT(plain, context.FirstParmsId, plaindest);
+ Assert.IsTrue(plaindest.IsZero);
+ Assert.IsTrue(plaindest.IsNTTForm);
+ Assert.IsTrue(plaindest.ParmsId == context.FirstParmsId);
+
+ plain = new Plaintext("1");
+ Assert.IsFalse(plain.IsNTTForm);
+
+ evaluator.TransformToNTTInplace(plain, context.FirstParmsId);
+ Assert.IsTrue(plain.IsNTTForm);
+
+ for (ulong i = 0; i < 256; i++)
+ {
+ Assert.AreEqual(1ul, plain[i]);
+ }
+ }
{
- Assert.AreEqual(1ul, plain[i]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ Evaluator evaluator = new Evaluator(context);
+
+ Plaintext plain = new Plaintext("0");
+ Plaintext plaindest = new Plaintext();
+ Assert.IsFalse(plain.IsNTTForm);
+
+ evaluator.TransformToNTT(plain, context.FirstParmsId, plaindest);
+ Assert.IsTrue(plaindest.IsZero);
+ Assert.IsTrue(plaindest.IsNTTForm);
+ Assert.IsTrue(plaindest.ParmsId == context.FirstParmsId);
+
+ plain = new Plaintext("1");
+ Assert.IsFalse(plain.IsNTTForm);
+
+ evaluator.TransformToNTTInplace(plain, context.FirstParmsId);
+ Assert.IsTrue(plain.IsNTTForm);
+
+ for (ulong i = 0; i < 256; i++)
+ {
+ Assert.AreEqual(1ul, plain[i]);
+ }
}
}
[TestMethod]
public void TransformEncryptedToNTTTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext();
- Ciphertext encdest = new Ciphertext();
- Ciphertext encdest2 = new Ciphertext();
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("0"), encrypted);
- Assert.IsFalse(encrypted.IsNTTForm);
-
- evaluator.TransformToNTT(encrypted, encdest);
- Assert.IsTrue(encdest.IsNTTForm);
-
- evaluator.TransformFromNTT(encdest, encdest2);
- Assert.IsFalse(encdest2.IsNTTForm);
-
- decryptor.Decrypt(encdest2, plaindest);
- Assert.AreEqual(1ul, plaindest.CoeffCount);
- Assert.AreEqual(0ul, plaindest[0]);
- Assert.AreEqual(context.FirstParmsId, encdest2.ParmsId);
-
- encryptor.Encrypt(new Plaintext("1"), encrypted);
- Assert.IsFalse(encrypted.IsNTTForm);
-
- evaluator.TransformToNTTInplace(encrypted);
- Assert.IsTrue(encrypted.IsNTTForm);
-
- evaluator.TransformFromNTTInplace(encrypted);
- Assert.IsFalse(encrypted.IsNTTForm);
-
- decryptor.Decrypt(encrypted, plaindest);
-
- Assert.AreEqual(1ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
- Assert.AreEqual(context.FirstParmsId, encrypted.ParmsId);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Ciphertext encdest2 = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("0"), encrypted);
+ Assert.IsFalse(encrypted.IsNTTForm);
+
+ evaluator.TransformToNTT(encrypted, encdest);
+ Assert.IsTrue(encdest.IsNTTForm);
+
+ evaluator.TransformFromNTT(encdest, encdest2);
+ Assert.IsFalse(encdest2.IsNTTForm);
+
+ decryptor.Decrypt(encdest2, plaindest);
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(context.FirstParmsId, encdest2.ParmsId);
+
+ encryptor.Encrypt(new Plaintext("1"), encrypted);
+ Assert.IsFalse(encrypted.IsNTTForm);
+
+ evaluator.TransformToNTTInplace(encrypted);
+ Assert.IsTrue(encrypted.IsNTTForm);
+
+ evaluator.TransformFromNTTInplace(encrypted);
+ Assert.IsFalse(encrypted.IsNTTForm);
+
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(context.FirstParmsId, encrypted.ParmsId);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext();
+ Ciphertext encdest = new Ciphertext();
+ Ciphertext encdest2 = new Ciphertext();
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("0"), encrypted);
+ Assert.IsFalse(encrypted.IsNTTForm);
+
+ evaluator.TransformToNTT(encrypted, encdest);
+ Assert.IsTrue(encdest.IsNTTForm);
+
+ evaluator.TransformFromNTT(encdest, encdest2);
+ Assert.IsFalse(encdest2.IsNTTForm);
+
+ decryptor.Decrypt(encdest2, plaindest);
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(0ul, plaindest[0]);
+ Assert.AreEqual(context.FirstParmsId, encdest2.ParmsId);
+
+ encryptor.Encrypt(new Plaintext("1"), encrypted);
+ Assert.IsFalse(encrypted.IsNTTForm);
+
+ evaluator.TransformToNTTInplace(encrypted);
+ Assert.IsTrue(encrypted.IsNTTForm);
+
+ evaluator.TransformFromNTTInplace(encrypted);
+ Assert.IsFalse(encrypted.IsNTTForm);
+
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(context.FirstParmsId, encrypted.ParmsId);
+ }
}
[TestMethod]
public void ModSwitchToNextTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: true,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext(context);
- Ciphertext encdest = new Ciphertext();
- Plaintext plain = new Plaintext();
-
- plain.Set("0");
- encryptor.Encrypt(plain, encrypted);
- evaluator.ModSwitchToNext(encrypted, encdest);
- decryptor.Decrypt(encdest, plain);
-
- Assert.AreEqual(1ul, plain.CoeffCount);
- Assert.AreEqual(0ul, plain[0]);
-
- plain.Set("1");
- encryptor.Encrypt(plain, encrypted);
- evaluator.ModSwitchToNextInplace(encrypted);
- decryptor.Decrypt(encrypted, plain);
-
- Assert.AreEqual(1ul, plain.CoeffCount);
- Assert.AreEqual(1ul, plain[0]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: true,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext(context);
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext();
+
+ plain.Set("0");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ModSwitchToNext(encrypted, encdest);
+ decryptor.Decrypt(encdest, plain);
+
+ Assert.AreEqual(1ul, plain.CoeffCount);
+ Assert.AreEqual(0ul, plain[0]);
+
+ plain.Set("1");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ModSwitchToNextInplace(encrypted);
+ decryptor.Decrypt(encrypted, plain);
+
+ Assert.AreEqual(1ul, plain.CoeffCount);
+ Assert.AreEqual(1ul, plain[0]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: true,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext(context);
+ Ciphertext encdest = new Ciphertext();
+ Plaintext plain = new Plaintext();
+
+ plain.Set("0");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ModSwitchToNext(encrypted, encdest);
+ decryptor.Decrypt(encdest, plain);
+
+ Assert.AreEqual(1ul, plain.CoeffCount);
+ Assert.AreEqual(0ul, plain[0]);
+
+ plain.Set("1");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ModSwitchToNextInplace(encrypted);
+ decryptor.Decrypt(encrypted, plain);
+
+ Assert.AreEqual(1ul, plain.CoeffCount);
+ Assert.AreEqual(1ul, plain[0]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 8192,
+ PlainModulus = new Modulus(786433),
+ CoeffModulus = CoeffModulus.BGVDefault(8192)
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: true,
+ secLevel: SecLevelType.TC128);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext(context);
+ Ciphertext encdest_1 = new Ciphertext();
+ Ciphertext encdest_2 = new Ciphertext();
+ Plaintext plain = new Plaintext();
+
+ plain.Set("1");
+ encryptor.Encrypt(plain, encrypted);
+ evaluator.ModSwitchToNext(encrypted, encdest_1);
+ evaluator.ModSwitchToNext(encdest_1, encdest_2);
+ decryptor.Decrypt(encdest_2, plain);
+ Assert.AreEqual(1ul, plain.CoeffCount);
+ Assert.AreEqual(1ul, plain[0]);
+ }
}
[TestMethod]
public void ModSwitchToTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30, 30 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: true,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted = new Ciphertext(context);
- Ciphertext encdest = new Ciphertext(context);
- Plaintext plaindest = new Plaintext();
-
- encryptor.Encrypt(new Plaintext("1"), encrypted);
- ParmsId destParmsId = context.FirstContextData.NextContextData
- .NextContextData.ParmsId;
-
- evaluator.ModSwitchTo(encrypted, context.FirstParmsId, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
- Assert.IsTrue(encdest.ParmsId == context.FirstParmsId);
- Assert.AreEqual(1ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
-
- evaluator.ModSwitchTo(encrypted, destParmsId, encdest);
- decryptor.Decrypt(encdest, plaindest);
-
- Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
- Assert.IsTrue(encdest.ParmsId == destParmsId);
- Assert.AreEqual(1ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
-
- encryptor.Encrypt(new Plaintext("3x^2 + 2x^1 + 1"), encrypted);
- evaluator.ModSwitchToInplace(encrypted, context.FirstParmsId);
- decryptor.Decrypt(encrypted, plaindest);
-
- Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
- Assert.AreEqual(3ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
- Assert.AreEqual(2ul, plaindest[1]);
- Assert.AreEqual(3ul, plaindest[2]);
-
- evaluator.ModSwitchToInplace(encrypted, destParmsId);
- decryptor.Decrypt(encrypted, plaindest);
-
- Assert.IsTrue(encrypted.ParmsId == destParmsId);
- Assert.AreEqual(3ul, plaindest.CoeffCount);
- Assert.AreEqual(1ul, plaindest[0]);
- Assert.AreEqual(2ul, plaindest[1]);
- Assert.AreEqual(3ul, plaindest[2]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30, 30 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: true,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext(context);
+ Ciphertext encdest = new Ciphertext(context);
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("1"), encrypted);
+ ParmsId destParmsId = context.FirstContextData.NextContextData
+ .NextContextData.ParmsId;
+
+ evaluator.ModSwitchTo(encrypted, context.FirstParmsId, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
+ Assert.IsTrue(encdest.ParmsId == context.FirstParmsId);
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+
+ evaluator.ModSwitchTo(encrypted, destParmsId, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
+ Assert.IsTrue(encdest.ParmsId == destParmsId);
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+
+ encryptor.Encrypt(new Plaintext("3x^2 + 2x^1 + 1"), encrypted);
+ evaluator.ModSwitchToInplace(encrypted, context.FirstParmsId);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(2ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+
+ evaluator.ModSwitchToInplace(encrypted, destParmsId);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == destParmsId);
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(2ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 30, 30, 30, 30 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: true,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted = new Ciphertext(context);
+ Ciphertext encdest = new Ciphertext(context);
+ Plaintext plaindest = new Plaintext();
+
+ encryptor.Encrypt(new Plaintext("1"), encrypted);
+ ParmsId destParmsId = context.FirstContextData.NextContextData
+ .NextContextData.ParmsId;
+
+ evaluator.ModSwitchTo(encrypted, context.FirstParmsId, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
+ Assert.IsTrue(encdest.ParmsId == context.FirstParmsId);
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+
+ evaluator.ModSwitchTo(encrypted, destParmsId, encdest);
+ decryptor.Decrypt(encdest, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
+ Assert.IsTrue(encdest.ParmsId == destParmsId);
+ Assert.AreEqual(1ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+
+ encryptor.Encrypt(new Plaintext("3x^2 + 2x^1 + 1"), encrypted);
+ evaluator.ModSwitchToInplace(encrypted, context.FirstParmsId);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == context.FirstParmsId);
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(2ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+
+ evaluator.ModSwitchToInplace(encrypted, destParmsId);
+ decryptor.Decrypt(encrypted, plaindest);
+
+ Assert.IsTrue(encrypted.ParmsId == destParmsId);
+ Assert.AreEqual(3ul, plaindest.CoeffCount);
+ Assert.AreEqual(1ul, plaindest[0]);
+ Assert.AreEqual(2ul, plaindest[1]);
+ Assert.AreEqual(3ul, plaindest[2]);
+ }
}
[TestMethod]
@@ -944,128 +1810,253 @@ public void ModSwitchToPlainTest()
[TestMethod]
public void RotateMatrixTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 8,
- PlainModulus = new Modulus(257),
- CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreateGaloisKeys(out GaloisKeys galoisKeys);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
- BatchEncoder encoder = new BatchEncoder(context);
-
- Plaintext plain = new Plaintext();
- List vec = new List
- {
- 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
- {
- 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
- {
- 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
- {
- 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
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 8,
+ PlainModulus = new Modulus(257),
+ CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreateGaloisKeys(out GaloisKeys galoisKeys);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+ BatchEncoder encoder = new BatchEncoder(context);
+
+ Plaintext plain = new Plaintext();
+ List vec = new List
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 2, 3, 4, 1,
+ 6, 7, 8, 5
+ }));
+ }
{
- 2, 3, 4, 1,
- 6, 7, 8, 5
- }));
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 8,
+ PlainModulus = new Modulus(257),
+ CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreateGaloisKeys(out GaloisKeys galoisKeys);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+ BatchEncoder encoder = new BatchEncoder(context);
+
+ Plaintext plain = new Plaintext();
+ List vec = new List
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 2, 3, 4, 1,
+ 6, 7, 8, 5
+ }));
+ }
}
[TestMethod]
public void RelinearizeTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 128,
- PlainModulus = new Modulus(1 << 6),
- CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreateRelinKeys(out RelinKeys relinKeys);
- keygen.CreatePublicKey(out PublicKey publicKey);
-
- Encryptor encryptor = new Encryptor(context, publicKey);
- Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
- Evaluator evaluator = new Evaluator(context);
-
- Ciphertext encrypted1 = new Ciphertext(context);
- Ciphertext encrypted2 = new Ciphertext(context);
- Plaintext plain1 = new Plaintext();
- Plaintext plain2 = new Plaintext();
-
- plain1.Set(0);
- encryptor.Encrypt(plain1, encrypted1);
- evaluator.SquareInplace(encrypted1);
- evaluator.RelinearizeInplace(encrypted1, relinKeys);
- decryptor.Decrypt(encrypted1, plain2);
-
- Assert.AreEqual(1ul, plain2.CoeffCount);
- Assert.AreEqual(0ul, plain2[0]);
-
- plain1.Set("1x^10 + 2");
- encryptor.Encrypt(plain1, encrypted1);
- evaluator.SquareInplace(encrypted1);
- evaluator.RelinearizeInplace(encrypted1, relinKeys);
- evaluator.SquareInplace(encrypted1);
- evaluator.Relinearize(encrypted1, relinKeys, encrypted2);
- decryptor.Decrypt(encrypted2, plain2);
-
- // {1x^40 + 8x^30 + 18x^20 + 20x^10 + 10}
- Assert.AreEqual(41ul, plain2.CoeffCount);
- Assert.AreEqual(16ul, plain2[0]);
- Assert.AreEqual(32ul, plain2[10]);
- Assert.AreEqual(24ul, plain2[20]);
- Assert.AreEqual(8ul, plain2[30]);
- Assert.AreEqual(1ul, plain2[40]);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext(context);
+ Ciphertext encrypted2 = new Ciphertext(context);
+ Plaintext plain1 = new Plaintext();
+ Plaintext plain2 = new Plaintext();
+
+ plain1.Set(0);
+ encryptor.Encrypt(plain1, encrypted1);
+ evaluator.SquareInplace(encrypted1);
+ evaluator.RelinearizeInplace(encrypted1, relinKeys);
+ decryptor.Decrypt(encrypted1, plain2);
+
+ Assert.AreEqual(1ul, plain2.CoeffCount);
+ Assert.AreEqual(0ul, plain2[0]);
+
+ plain1.Set("1x^10 + 2");
+ encryptor.Encrypt(plain1, encrypted1);
+ evaluator.SquareInplace(encrypted1);
+ evaluator.RelinearizeInplace(encrypted1, relinKeys);
+ evaluator.SquareInplace(encrypted1);
+ evaluator.Relinearize(encrypted1, relinKeys, encrypted2);
+ decryptor.Decrypt(encrypted2, plain2);
+
+ // {1x^40 + 8x^30 + 18x^20 + 20x^10 + 10}
+ Assert.AreEqual(41ul, plain2.CoeffCount);
+ Assert.AreEqual(16ul, plain2[0]);
+ Assert.AreEqual(32ul, plain2[10]);
+ Assert.AreEqual(24ul, plain2[20]);
+ Assert.AreEqual(8ul, plain2[30]);
+ Assert.AreEqual(1ul, plain2[40]);
+ }
+ {
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BGV)
+ {
+ PolyModulusDegree = 128,
+ PlainModulus = new Modulus(1 << 6),
+ CoeffModulus = CoeffModulus.Create(128, new int[] { 40, 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+ keygen.CreatePublicKey(out PublicKey publicKey);
+
+ Encryptor encryptor = new Encryptor(context, publicKey);
+ Decryptor decryptor = new Decryptor(context, keygen.SecretKey);
+ Evaluator evaluator = new Evaluator(context);
+
+ Ciphertext encrypted1 = new Ciphertext(context);
+ Ciphertext encrypted2 = new Ciphertext(context);
+ Plaintext plain1 = new Plaintext();
+ Plaintext plain2 = new Plaintext();
+
+ plain1.Set(0);
+ encryptor.Encrypt(plain1, encrypted1);
+ evaluator.SquareInplace(encrypted1);
+ evaluator.RelinearizeInplace(encrypted1, relinKeys);
+ decryptor.Decrypt(encrypted1, plain2);
+
+ Assert.AreEqual(1ul, plain2.CoeffCount);
+ Assert.AreEqual(0ul, plain2[0]);
+
+ plain1.Set("1x^10 + 2");
+ encryptor.Encrypt(plain1, encrypted1);
+ evaluator.SquareInplace(encrypted1);
+ evaluator.RelinearizeInplace(encrypted1, relinKeys);
+ evaluator.SquareInplace(encrypted1);
+ evaluator.Relinearize(encrypted1, relinKeys, encrypted2);
+ decryptor.Decrypt(encrypted2, plain2);
+
+ // {1x^40 + 8x^30 + 18x^20 + 20x^10 + 10}
+ Assert.AreEqual(41ul, plain2.CoeffCount);
+ Assert.AreEqual(16ul, plain2[0]);
+ Assert.AreEqual(32ul, plain2[10]);
+ Assert.AreEqual(24ul, plain2[20]);
+ Assert.AreEqual(8ul, plain2[30]);
+ Assert.AreEqual(1ul, plain2[40]);
+ }
}
[TestMethod]
@@ -1184,220 +2175,438 @@ public void ComplexConjugateTest()
[TestMethod]
public void ExceptionsTest()
{
- EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
{
- PolyModulusDegree = 64,
- PlainModulus = new Modulus(65537ul),
- CoeffModulus = CoeffModulus.Create(64, new int[] { 40, 40 })
- };
- SEALContext context = new SEALContext(parms,
- expandModChain: false,
- secLevel: SecLevelType.None);
+ EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV)
+ {
+ PolyModulusDegree = 64,
+ PlainModulus = new Modulus(65537ul),
+ CoeffModulus = CoeffModulus.Create(64, new int[] { 40, 40 })
+ };
+ SEALContext context = new SEALContext(parms,
+ expandModChain: false,
+ secLevel: SecLevelType.None);
- Evaluator evaluator = null;
- Utilities.AssertThrows(() => evaluator = new Evaluator(null));
- evaluator = new Evaluator(context);
+ Evaluator evaluator = null;
+ Utilities.AssertThrows(() => evaluator = new Evaluator(null));
+ evaluator = new Evaluator(context);
- KeyGenerator keygen = new KeyGenerator(context);
- keygen.CreateGaloisKeys(out GaloisKeys galoisKeys);
- keygen.CreateRelinKeys(out RelinKeys relinKeys);
+ KeyGenerator keygen = new KeyGenerator(context);
+ keygen.CreateGaloisKeys(out GaloisKeys galoisKeys);
+ keygen.CreateRelinKeys(out RelinKeys relinKeys);
+
+ Ciphertext encrypted1 = new Ciphertext();
+ Ciphertext encrypted2 = new Ciphertext();
+ Ciphertext encrypted3 = new Ciphertext();
+ Plaintext plain1 = new Plaintext();
+ Plaintext plain2 = new Plaintext();
+ List encrypteds = new List();
- Ciphertext encrypted1 = new Ciphertext();
- Ciphertext encrypted2 = new Ciphertext();
- Ciphertext encrypted3 = new Ciphertext();
- Plaintext plain1 = new Plaintext();
- Plaintext plain2 = new Plaintext();
- List encrypteds = new List();
+ MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal);
- MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal);
+ Utilities.AssertThrows(() => evaluator.Add(null, encrypted2, encrypted3));
+ Utilities.AssertThrows(() => evaluator.Add(encrypted1, null, encrypted3));
+ Utilities.AssertThrows(() => evaluator.Add(encrypted1, encrypted2, null));
+ Utilities.AssertThrows(() => evaluator.Add(encrypted1, encrypted2, encrypted3));
- Utilities.AssertThrows(() => evaluator.Add(null, encrypted2, encrypted3));
- Utilities.AssertThrows(() => evaluator.Add(encrypted1, null, encrypted3));
- Utilities.AssertThrows(() => evaluator.Add(encrypted1, encrypted2, null));
- Utilities.AssertThrows(() => evaluator.Add(encrypted1, encrypted2, encrypted3));
+ Utilities.AssertThrows(() => evaluator.AddInplace(encrypted1, null));
+ Utilities.AssertThrows(() => evaluator.AddInplace(null, encrypted2));
- Utilities.AssertThrows(() => evaluator.AddInplace(encrypted1, null));
- Utilities.AssertThrows(() => evaluator.AddInplace(null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.AddMany(encrypteds, null));
+ Utilities.AssertThrows(() => evaluator.AddMany(null, encrypted2));
- Utilities.AssertThrows(() => evaluator.AddMany(encrypteds, null));
- Utilities.AssertThrows(() => evaluator.AddMany(null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.AddPlain(encrypted1, plain1, null));
+ Utilities.AssertThrows(() => evaluator.AddPlain(encrypted1, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.AddPlain(null, plain1, encrypted2));
+ Utilities.AssertThrows(() => evaluator.AddPlain(encrypted1, plain1, encrypted2));
- Utilities.AssertThrows(() => evaluator.AddPlain(encrypted1, plain1, null));
- Utilities.AssertThrows(() => evaluator.AddPlain(encrypted1, null, encrypted2));
- Utilities.AssertThrows(() => evaluator.AddPlain(null, plain1, encrypted2));
- Utilities.AssertThrows(() => evaluator.AddPlain(encrypted1, plain1, encrypted2));
+ Utilities.AssertThrows(() => evaluator.AddPlainInplace(encrypted1, null));
- Utilities.AssertThrows(() => evaluator.AddPlainInplace(encrypted1, null));
+ Utilities.AssertThrows(() => evaluator.ApplyGalois(encrypted1, 1, galoisKeys, null));
+ Utilities.AssertThrows(() => evaluator.ApplyGalois(encrypted1, 1, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ApplyGalois(null, 1, galoisKeys, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ApplyGalois(encrypted1, 1, galoisKeys, encrypted2, pool));
- Utilities.AssertThrows(() => evaluator.ApplyGalois(encrypted1, 1, galoisKeys, null));
- Utilities.AssertThrows(() => evaluator.ApplyGalois(encrypted1, 1, null, encrypted2));
- Utilities.AssertThrows(() => evaluator.ApplyGalois(null, 1, galoisKeys, encrypted2));
- Utilities.AssertThrows(() => evaluator.ApplyGalois(encrypted1, 1, galoisKeys, encrypted2, pool));
+ Utilities.AssertThrows(() => evaluator.ApplyGaloisInplace(encrypted1, 1, null));
+ Utilities.AssertThrows(() => evaluator.ApplyGaloisInplace(null, 1, galoisKeys));
- Utilities.AssertThrows(() => evaluator.ApplyGaloisInplace(encrypted1, 1, null));
- Utilities.AssertThrows(() => evaluator.ApplyGaloisInplace(null, 1, galoisKeys));
+ Utilities.AssertThrows(() => evaluator.ComplexConjugate(encrypted1, galoisKeys, null));
+ Utilities.AssertThrows(() => evaluator.ComplexConjugate(encrypted1, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ComplexConjugate(null, galoisKeys, encrypted2));
- Utilities.AssertThrows(() => evaluator.ComplexConjugate(encrypted1, galoisKeys, null));
- Utilities.AssertThrows(() => evaluator.ComplexConjugate(encrypted1, null, encrypted2));
- Utilities.AssertThrows(() => evaluator.ComplexConjugate(null, galoisKeys, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ComplexConjugateInplace(encrypted1, null));
+ Utilities.AssertThrows(() => evaluator.ComplexConjugateInplace(null, galoisKeys));
+
+ Utilities.AssertThrows(() => evaluator.Exponentiate(encrypted1, 2, relinKeys, null));
+ Utilities.AssertThrows(() => evaluator.Exponentiate(encrypted1, 2, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.Exponentiate(null, 2, relinKeys, encrypted2));
+
+ Utilities.AssertThrows(() => evaluator.ExponentiateInplace(encrypted1, 2, null));
+ Utilities.AssertThrows(() => evaluator.ExponentiateInplace(null, 2, relinKeys));
- Utilities.AssertThrows(() => evaluator.ComplexConjugateInplace(encrypted1, null));
- Utilities.AssertThrows(() => evaluator.ComplexConjugateInplace(null, galoisKeys));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(plain1, ParmsId.Zero, null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(plain1, null, plain2));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(null, ParmsId.Zero, plain2));
- Utilities.AssertThrows(() => evaluator.Exponentiate(encrypted1, 2, relinKeys, null));
- Utilities.AssertThrows(() => evaluator.Exponentiate(encrypted1, 2, null, encrypted2));
- Utilities.AssertThrows(() => evaluator.Exponentiate(null, 2, relinKeys, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(encrypted1, ParmsId.Zero, null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(encrypted1, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(null, ParmsId.Zero, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ModSwitchTo(encrypted1, ParmsId.Zero, encrypted2, pool));
+
+ Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(encrypted1, null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(encrypted: null, parmsId: ParmsId.Zero));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(encrypted1, ParmsId.Zero, pool));
- Utilities.AssertThrows(() => evaluator.ExponentiateInplace(encrypted1, 2, null));
- Utilities.AssertThrows(() => evaluator.ExponentiateInplace(null, 2, relinKeys));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(plain1, null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(plain: null, parmsId: ParmsId.Zero));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(plain1, ParmsId.Zero, null));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(plain1, null, plain2));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(null, ParmsId.Zero, plain2));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNext(plain1, null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNext(null, plain2));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(encrypted1, ParmsId.Zero, null));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(encrypted1, null, encrypted2));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(null, ParmsId.Zero, encrypted2));
- Utilities.AssertThrows(() => evaluator.ModSwitchTo(encrypted1, ParmsId.Zero, encrypted2, pool));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNextInplace(null));
- Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(encrypted1, null));
- Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(encrypted: null, parmsId: ParmsId.Zero));
- Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(encrypted1, ParmsId.Zero, pool));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNext(encrypted1, null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNext(null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNext(encrypted1, encrypted2, pool));
+
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNextInplace(encrypted: null));
+ Utilities.AssertThrows(() => evaluator.ModSwitchToNextInplace(encrypted1, pool));
- Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(plain1, null));
- Utilities.AssertThrows(() => evaluator.ModSwitchToInplace(plain: null, parmsId: ParmsId.Zero));
+ Utilities.AssertThrows(() => evaluator.Multiply(encrypted1, encrypted2, null));
+ Utilities.AssertThrows(() => evaluator.Multiply(encrypted1, null, encrypted3));
+ Utilities.AssertThrows(() => evaluator.Multiply(null, encrypted2, encrypted3));
+ Utilities.AssertThrows(() => evaluator.Multiply(encrypted1, encrypted2, encrypted3, pool));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNext(plain1, null));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNext(null, plain2));
+ Utilities.AssertThrows(() => evaluator.MultiplyInplace(encrypted1, null));
+ Utilities.AssertThrows(() => evaluator.MultiplyInplace(null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.MultiplyInplace(encrypted1, encrypted2, pool));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNextInplace(null));
+ Utilities.AssertThrows(() => evaluator.MultiplyMany(encrypteds, relinKeys, null));
+ Utilities.AssertThrows(() => evaluator.MultiplyMany(encrypteds, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.MultiplyMany(null, relinKeys, encrypted2));
+ Utilities.AssertThrows(() => evaluator.MultiplyMany(encrypteds, relinKeys, encrypted2, pool));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNext(encrypted1, null));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNext(null, encrypted2));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNext(encrypted1, encrypted2, pool));
+ Utilities.AssertThrows(() => evaluator.MultiplyPlain(encrypted1, plain1, null));
+ Utilities.AssertThrows(() => evaluator.MultiplyPlain(encrypted1, null, encrypted2));
+ Utilities.AssertThrows(() => evaluator.MultiplyPlain(null, plain1, encrypted2));
+ Utilities.AssertThrows(() => evaluator.MultiplyPlain(encrypted1, plain1, encrypted2, pool));
- Utilities.AssertThrows(() => evaluator.ModSwitchToNextInplace(encrypted: null));
- Utilities.AssertThrows