/// <summary>
 /// Generates and saves relinearization keys to an output stream.
 /// </summary>
 /// <remarks>
 /// 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 relinearization keys to</param>
 /// <param name="comprMode">The desired compression mode</param>
 public long RelinKeysSave(Stream stream, ComprModeType?comprMode = null)
 {
     NativeMethods.KeyGenerator_RelinKeys(NativePtr, true, out IntPtr relinKeysPtr);
     using (RelinKeys relinKeys = new RelinKeys(relinKeysPtr))
     {
         return(relinKeys.Save(stream, comprMode));
     }
 }
示例#2
0
        /// <summary>
        /// Copies a given RelinKeys instance to the current one.
        /// </summary>
        /// <param name="copy">The RelinKeys to copy from</param>
        /// <exception cref="ArgumentNullException">if copy is null</exception>
        public void Set(RelinKeys copy)
        {
            if (null == copy)
            {
                throw new ArgumentNullException(nameof(copy));
            }

            NativeMethods.RelinKeys_Set(NativePtr, copy.NativePtr);
        }
示例#3
0
        /// <summary>
        /// Creates a new RelinKeys instance by copying a given instance.
        /// </summary>
        /// <param name="copy">The RelinKeys to copy from</param>
        /// <exception cref="ArgumentNullException">if copy is null</exception>
        public RelinKeys(RelinKeys copy)
        {
            if (null == copy)
            {
                throw new ArgumentNullException(nameof(copy));
            }

            NativeMethods.RelinKeys_Create(copy.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
示例#4
0
        /// <summary>
        /// Generates relinearization keys and stores the result in destination.
        /// </summary>
        /// <remarks>
        /// Generates relinearization keys and stores the result in destination.
        /// Every time this function is called, new relinearization keys will be
        /// generated.
        /// </remarks>
        /// <param name="destination">The relinearization keys to overwrite with
        /// the generated relinearization keys</param>
        /// <exception cref="InvalidOperationException">if the encryption
        /// parameters do not support keyswitching</exception>
        public void CreateRelinKeys(out RelinKeys destination)
        {
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            NativeMethods.KeyGenerator_CreateRelinKeys(NativePtr, false, out IntPtr relinKeysPtr);
            destination = new RelinKeys(relinKeysPtr);
        }
示例#5
0
        /// <summary>
        /// Generates and returns relinearization keys as a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Generates and returns relinearization keys as a serializable object.
        /// </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 keyswitching</exception>
        public Serializable <RelinKeys> RelinKeys()
        {
            if (!UsingKeyswitching())
            {
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");
            }

            NativeMethods.KeyGenerator_RelinKeys(NativePtr, true, out IntPtr relinKeysPtr);
            RelinKeys relinKeys = new RelinKeys(relinKeysPtr);

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

            NativeMethods.ValCheck_RelinKeys_IsValidFor(relinKeys.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
示例#7
0
        /// <summary>
        /// Generates and saves relinearization keys to an output stream.
        /// </summary>
        /// <remarks>
        /// 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 relinearization 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 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 RelinKeysSave(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_RelinKeys(NativePtr, true, out IntPtr relinKeysPtr);
            using (RelinKeys relinKeys = new RelinKeys(relinKeysPtr))
            {
                return(relinKeys.Save(stream, comprMode));
            }
        }
示例#8
0
 /// <summary>
 /// Creates a new RelinKeys instance by copying a given instance.
 /// </summary>
 /// <param name="copy">The RelinKeys to copy from</param>
 /// <exception cref="ArgumentNullException">if copy is null</exception>
 public RelinKeys(RelinKeys copy)
     : base(copy)
 {
 }