Пример #1
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 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);
            }
        }
Пример #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));
            }
        }
Пример #3
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="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);
     }
 }
Пример #4
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));
            }
        }