/// <summary>
        /// Generates and saves Galois keys to an output stream.
        /// </summary>
        /// <remarks>
        /// This function creates specific Galois keys that can be used to apply
        /// specific Galois automorphisms on encrypted data. 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 complex conjugation in the
        /// CKKS scheme.
        ///
        /// Half of the polynomials in relinearization keys are randomly generated
        /// and are replaced with the seed used to compress output size. The output
        /// is in binary format and not human-readable. The output stream must have
        /// the "binary" flag set.
        /// </remarks>
        /// <param name="steps">The rotation step counts for which to generate keys</param>
        /// <param name="stream">The stream to save the Galois keys to</param>
        /// <param name="comprMode">The desired compression mode</param>
        /// <exception cref="ArgumentNullException">if steps is null</exception>
        /// <exception cref="InvalidOperationException">if the encryption parameters
        /// do not support batching and scheme is SchemeType.BFV</exception>
        /// <exception cref="ArgumentException">if the step counts are not valid</exception>
        public long GaloisKeysSave(IEnumerable <int> steps, Stream stream, ComprModeType?comprMode = null)
        {
            if (null == steps)
            {
                throw new ArgumentNullException(nameof(steps));
            }

            try
            {
                int[] stepsArr = steps.ToArray();
                NativeMethods.KeyGenerator_GaloisKeysFromSteps(NativePtr,
                                                               (ulong)stepsArr.Length, stepsArr, true, out IntPtr galoisKeysPtr);
                using (GaloisKeys galoisKeys = new GaloisKeys(galoisKeysPtr))
                {
                    return(galoisKeys.Save(stream, comprMode));
                }
            }
            catch (COMException ex)
            {
                if ((uint)ex.HResult == NativeMethods.Errors.HRInvalidOperation)
                {
                    throw new InvalidOperationException("Encryption parameters do not support batching and scheme is SchemeType.BFV", ex);
                }
                throw new InvalidOperationException("Unexpected native library error", ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Generates and saves Galois keys to an output stream.
        /// </summary>
        /// <remarks>
        /// This function creates specific Galois keys that can be used to apply
        /// specific Galois automorphisms on encrypted data. 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 complex conjugation in the
        /// CKKS scheme.
        ///
        /// Half of the polynomials in relinearization keys are randomly generated
        /// and are replaced with the seed used to compress output size. The output
        /// is in binary format and not human-readable. The output stream must have
        /// the "binary" flag set.
        /// </remarks>
        /// <param name="steps">The rotation step counts for which to generate keys</param>
        /// <param name="stream">The stream to save the Galois keys to</param>
        /// <param name="comprMode">The desired compression mode</param>
        /// <exception cref="ArgumentNullException">if steps or stream is null</exception>
        /// <exception cref="InvalidOperationException">if the encryption parameters
        /// do not support batching and scheme is SchemeType.BFV</exception>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        /// <exception cref="ArgumentException">if the step counts are not valid</exception>
        /// <exception cref="ArgumentException">if the stream is closed or does not
        /// support writing</exception>
        /// <exception cref="IOException">if I/O operations failed</exception>
        /// <exception cref="InvalidOperationException">if compression mode is not
        /// supported, or if compression failed</exception>
        public long GaloisKeysSave(IEnumerable <int> steps, Stream stream, ComprModeType?comprMode = null)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (null == steps)
            {
                throw new ArgumentNullException(nameof(steps));
            }
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            comprMode = comprMode ?? Serialization.ComprModeDefault;
            if (!Serialization.IsSupportedComprMode(comprMode.Value))
            {
                throw new InvalidOperationException("Unsupported compression mode");
            }

            int[] stepsArr = steps.ToArray();
            NativeMethods.KeyGenerator_GaloisKeysFromSteps(NativePtr,
                                                           (ulong)stepsArr.Length, stepsArr, true, out IntPtr galoisKeysPtr);

            using (GaloisKeys galoisKeys = new GaloisKeys(galoisKeysPtr))
            {
                return(galoisKeys.Save(stream, comprMode));
            }
        }
Exemple #3
0
        /// <summary>
        /// Copies a given GaloisKeys instance to the current one.
        /// </summary>
        ///
        /// <param name="assign">The GaloisKeys to copy from</param>
        /// <exception cref="ArgumentNullException">if assign is null</exception>
        public void Set(GaloisKeys assign)
        {
            if (null == assign)
            {
                throw new ArgumentNullException(nameof(assign));
            }

            NativeMethods.GaloisKeys_Set(NativePtr, assign.NativePtr);
        }
Exemple #4
0
        /// <summary>
        /// Generates Galois keys and stores the result in destination.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Generates Galois keys and stores the result in destination. Every time
        /// this function is called, new Galois keys will be generated.
        /// </para>
        /// <para>
        /// This function creates logarithmically many (in degree of the polynomial
        /// modulus) Galois keys that is sufficient to apply any Galois automorphism
        /// (e.g., rotations) on encrypted data. Most users will want to use this
        /// overload of the function.
        /// </para>
        /// <para>
        /// Precisely it generates 2*log(n)-1 number of Galois keys where n is the
        /// degree of the polynomial modulus. When used with batching, these keys
        /// support direct left and right rotations of power-of-2 steps of rows in BFV
        /// or vectors in CKKS and rotation of columns in BFV or conjugation in CKKS.
        /// </para>
        /// </remarks>
        /// <param name="destination">The Galois keys to overwrite with the generated
        /// Galois keys</param>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        public void CreateGaloisKeys(out GaloisKeys destination)
        {
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            NativeMethods.KeyGenerator_CreateGaloisKeysAll(NativePtr, false, out IntPtr galoisKeysPtr);
            destination = new GaloisKeys(galoisKeysPtr);
        }
Exemple #5
0
        /// <summary>
        /// Creates a new GaloisKeys instance by copying a given instance.
        /// </summary>
        /// <param name="copy">The GaloisKeys to copy from</param>
        /// <exception cref="ArgumentNullException">if copy is null</exception>
        public GaloisKeys(GaloisKeys copy)
        {
            if (null == copy)
            {
                throw new ArgumentNullException(nameof(copy));
            }

            NativeMethods.GaloisKeys_Create(copy.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Exemple #6
0
        /// <summary>
        /// Generates and returns Galois keys as a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Generates and returns Galois keys as a serializable object. This function
        /// creates logarithmically many (in degree of the polynomial modulus) Galois
        /// keys that is sufficient to apply any Galois automorphism (e.g. rotations)
        /// on encrypted data. Most users will want to use this overload of the function.
        /// </para>
        /// <para>
        /// Half of the key data is pseudo-randomly generated from a seed to reduce
        /// the object size. The resulting serializable object cannot be used
        /// directly and is meant to be serialized for the size reduction to have an
        /// impact.
        /// </para>
        /// </remarks>
        /// <exception cref="InvalidOperationException">if the encryption parameters
        /// do not support batching and scheme is SchemeType.BFV</exception>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        public Serializable <GaloisKeys> GaloisKeys()
        {
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            NativeMethods.KeyGenerator_GaloisKeysAll(NativePtr, true, out IntPtr galoisKeysPtr);
            GaloisKeys galoisKeys = new GaloisKeys(galoisKeysPtr);

            return(new Serializable <GaloisKeys>(galoisKeys));
        }
Exemple #7
0
        /// <summary>
        /// Check whether the given GaloisKeys is valid for a given SEALContext. If the
        /// given SEALContext is not set, the encryption parameters are invalid, or the
        /// GaloisKeys data does not match the SEALContext, this function returns false.
        /// Otherwise, returns true.
        /// </summary>
        /// <param name="galoisKeys">The GaloisKeys to check</param>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if either galoisKeys or context is null</exception>
        public static bool IsValidFor(GaloisKeys galoisKeys, SEALContext context)
        {
            if (null == galoisKeys)
            {
                throw new ArgumentNullException(nameof(galoisKeys));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_GaloisKeys_IsValidFor(galoisKeys.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
Exemple #8
0
        /// <summary>
        /// Generates Galois keys and stores the result in destination.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Generates Galois keys and stores the result in destination. Every time
        /// this function is called, new Galois keys will be generated.
        /// </para>
        /// <para>
        /// 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/BGV scheme
        /// and complex conjugation in the CKKS scheme.
        /// </para>
        /// </remarks>
        /// <param name="steps">The rotation step counts for which to generate keys</param>
        /// <param name="destination">The Galois keys to overwrite with the generated
        /// Galois keys</param>
        /// <exception cref="ArgumentNullException">if steps is null</exception>
        /// <exception cref="InvalidOperationException">if the encryption parameters
        /// do not support batching and scheme is SchemeType.BFV or SchemeType.BGV</exception>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        /// <exception cref="ArgumentException">if the step counts are not valid</exception>
        public void CreateGaloisKeys(IEnumerable <int> steps, out GaloisKeys destination)
        {
            if (null == steps)
            {
                throw new ArgumentNullException(nameof(steps));
            }
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            int[] stepsArr = steps.ToArray();
            NativeMethods.KeyGenerator_CreateGaloisKeysFromSteps(NativePtr,
                                                                 (ulong)stepsArr.Length, stepsArr, false, out IntPtr galoisKeysPtr);
            destination = new GaloisKeys(galoisKeysPtr);
        }
Exemple #9
0
        /// <summary>
        /// Generates and returns Galois keys as a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Generates and returns Galois keys as a serializable object. This function
        /// creates specific Galois keys that can be used to apply specific Galois
        /// automorphisms on encrypted data. 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 complex conjugation in the CKKS scheme.
        /// </para>
        /// <para>
        /// Half of the key data is pseudo-randomly generated from a seed to reduce
        /// the object size. The resulting serializable object cannot be used
        /// directly and is meant to be serialized for the size reduction to have an
        /// impact.
        /// </para>
        /// </remarks>
        /// <param name="steps">The rotation step counts for which to generate keys</param>
        /// <exception cref="ArgumentNullException">if steps is null</exception>
        /// <exception cref="InvalidOperationException">if the encryption parameters
        /// do not support batching and scheme is SchemeType.BFV</exception>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        /// <exception cref="ArgumentException">if the step counts are not valid</exception>
        public Serializable <GaloisKeys> GaloisKeys(IEnumerable <int> steps)
        {
            if (null == steps)
            {
                throw new ArgumentNullException(nameof(steps));
            }
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            int[] stepsArr = steps.ToArray();
            NativeMethods.KeyGenerator_GaloisKeysFromSteps(NativePtr,
                                                           (ulong)stepsArr.Length, stepsArr, true, out IntPtr galoisKeysPtr);
            GaloisKeys galoisKeys = new GaloisKeys(galoisKeysPtr);

            return(new Serializable <GaloisKeys>(galoisKeys));
        }
 /// <summary>
 /// Generates and saves Galois keys to an output stream.
 /// </summary>
 /// <remarks>
 /// This function creates logarithmically many (in degree of the polynomial modulus)
 /// Galois keys that is sufficient to apply any Galois automorphism (e.g. rotations)
 /// on encrypted data. Most users will want to use this overload of the function.
 ///
 /// Half of the polynomials in relinearization keys are randomly generated
 /// and are replaced with the seed used to compress output size. The output
 /// is in binary format and not human-readable. The output stream must have
 /// the "binary" flag set.
 /// </remarks>
 /// <param name="stream">The stream to save the Galois keys to</param>
 /// <param name="comprMode">The desired compression mode</param>
 /// <exception cref="InvalidOperationException">if the encryption parameters
 /// do not support batching and scheme is SchemeType.BFV</exception>
 public long GaloisKeysSave(Stream stream, ComprModeType?comprMode = null)
 {
     try
     {
         NativeMethods.KeyGenerator_GaloisKeysAll(NativePtr, true, out IntPtr galoisKeysPtr);
         using (GaloisKeys galoisKeys = new GaloisKeys(galoisKeysPtr))
         {
             return(galoisKeys.Save(stream, comprMode));
         }
     }
     catch (COMException ex)
     {
         if ((uint)ex.HResult == NativeMethods.Errors.HRInvalidOperation)
         {
             throw new InvalidOperationException("Encryption parameters do not support batching and scheme is SchemeType.BFV", ex);
         }
         throw new InvalidOperationException("Unexpected native library error", ex);
     }
 }
Exemple #11
0
        /// <summary>
        /// Generates and saves Galois keys to an output stream.
        /// </summary>
        /// <remarks>
        /// This function creates logarithmically many (in degree of the polynomial modulus)
        /// Galois keys that is sufficient to apply any Galois automorphism (e.g. rotations)
        /// on encrypted data. Most users will want to use this overload of the function.
        ///
        /// Half of the polynomials in relinearization keys are randomly generated
        /// and are replaced with the seed used to compress output size. The output
        /// is in binary format and not human-readable. The output stream must have
        /// the "binary" flag set.
        /// </remarks>
        /// <param name="stream">The stream to save the Galois keys to</param>
        /// <param name="comprMode">The desired compression mode</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="InvalidOperationException">if the encryption parameters
        /// do not support batching and scheme is SchemeType.BFV</exception>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        /// <exception cref="ArgumentException">if the stream is closed or does not
        /// support writing</exception>
        /// <exception cref="IOException">if I/O operations failed</exception>
        /// <exception cref="InvalidOperationException">if compression mode is not
        /// supported, or if compression failed</exception>
        public long GaloisKeysSave(Stream stream, ComprModeType?comprMode = null)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            comprMode = comprMode ?? Serialization.ComprModeDefault;
            if (!Serialization.IsSupportedComprMode(comprMode.Value))
            {
                throw new InvalidOperationException("Unsupported compression mode");
            }

            NativeMethods.KeyGenerator_GaloisKeysAll(NativePtr, true, out IntPtr galoisKeysPtr);
            using (GaloisKeys galoisKeys = new GaloisKeys(galoisKeysPtr))
            {
                return(galoisKeys.Save(stream, comprMode));
            }
        }
Exemple #12
0
 /// <summary>
 /// Creates a new GaloisKeys instance by copying a given instance.
 /// </summary>
 /// <param name="copy">The GaloisKeys to copy from</param>
 /// <exception cref="ArgumentNullException">if copy is null</exception>
 public GaloisKeys(GaloisKeys copy)
     : base(copy)
 {
 }