Пример #1
0
        /// <summary>
        /// Creates an instance of SEALContext, and performs several pre-computations
        /// on the given EncryptionParameters.
        /// </summary>
        /// <param name="parms">The encryption parameters.</param>
        /// <param name="expandModChain">Determines whether the modulus switching chain
        /// should be created</param>
        /// <exception cref="ArgumentNullException">if parms is null</exception>
        public static SEALContext Create(EncryptionParameters parms, bool expandModChain = true)
        {
            if (null == parms)
            {
                throw new ArgumentNullException(nameof(parms));
            }

            NativeMethods.SEALContext_Create(parms.NativePtr, expandModChain, out IntPtr contextPtr);
            SEALContext context = new SEALContext(contextPtr);

            return(context);
        }
Пример #2
0
        /// <summary>
        /// Creates a CKKSEncoder instance initialized with the specified SEALContext.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if context is null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption parameters
        /// are not valid</exception>
        /// <exception cref="ArgumentException">if scheme is not SchemeType.CKKS</exception>
        public CKKSEncoder(SEALContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.CKKSEncoder_Create(context.NativePtr, out IntPtr ptr);
            NativePtr = ptr;

            context_ = context;
        }
Пример #3
0
        /// <summary>
        /// Constructs an empty ciphertext with capacity 2. In addition to the
        /// capacity, the allocation size is determined by the highest-level
        /// parameters associated to the given SEALContext.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if context is null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Ciphertext(SEALContext context, MemoryPoolHandle pool = null)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            IntPtr poolPtr = pool?.NativePtr ?? IntPtr.Zero;

            NativeMethods.Ciphertext_Create3(context.NativePtr, poolPtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Пример #4
0
        /// <summary>
        /// Resizes the ciphertext to given size, reallocating if the capacity
        /// of the ciphertext is too small. The ciphertext parameters are
        /// determined by the given SEALContext and parmsId.
        ///
        /// This function is mainly intended for internal use and is called
        /// automatically by functions such as Evaluator::multiply and
        /// Evaluator::relinearize. A normal user should never have a reason
        /// to manually resize a ciphertext.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="parmsId">The ParmsId corresponding to the encryption
        /// parameters to be used</param>
        /// <param name="size">The new size</param>
        /// <exception cref="ArgumentNullException">if either context or parmsId are null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if size is less than 2 or too large</exception>
        public void Resize(SEALContext context, ParmsId parmsId, ulong size)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }

            NativeMethods.Ciphertext_Resize(NativePtr, context.NativePtr, parmsId.Block, size);
        }
Пример #5
0
        /// <summary>Loads a PublicKey from an input stream overwriting the current
        /// PublicKey.</summary>
        /// <remarks>
        /// Loads a PublicKey from an input stream overwriting the current PublicKey.
        /// The loaded PublicKey is verified to be valid for the given SEALContext.
        /// </remarks>
        /// <param name="context">The SEALContext</param>
        /// <param name="stream">The stream to load the PublicKey from</param>
        /// <exception cref="ArgumentNullException">if context or stream is
        /// null</exception>
        /// <exception cref="ArgumentException">if the stream is closed or does not
        /// support reading</exception>
        /// <exception cref="ArgumentException">if context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="EndOfStreamException">if the stream ended
        /// unexpectedly</exception>
        /// <exception cref="IOException">if I/O operations failed</exception>
        /// <exception cref="InvalidOperationException">if the loaded data is invalid
        /// or if the loaded compression mode is not supported</exception>
        public long Load(SEALContext context, Stream stream)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(Serialization.Load(
                       (byte[] outptr, ulong size, out long outBytes) =>
                       NativeMethods.PublicKey_Load(NativePtr, context.NativePtr,
                                                    outptr, size, out outBytes),
                       stream));
        }
Пример #6
0
        /// <summary>
        /// Check whether the given ciphertext is valid for a given SEALContext. If the
        /// given SEALContext is not set, the encryption parameters are invalid, or the
        /// ciphertext data does not match the SEALContext, this function returns false.
        /// Otherwise, returns true.
        /// </summary>
        /// <param name="ciphertext">The ciphertext to check</param>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if either ciphertext or context is null</exception>
        public static bool IsValidFor(Ciphertext ciphertext, SEALContext context)
        {
            if (null == ciphertext)
            {
                throw new ArgumentNullException(nameof(ciphertext));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_Ciphertext_IsValidFor(ciphertext.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
Пример #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);
        }
Пример #8
0
        /// <summary>
        /// Creates an Encryptor instance initialized with the specified SEALContext
        /// and public key.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="publicKey">The public key</param>
        /// <exception cref="ArgumentNullException">if either context or publicKey are null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">if publicKey is not valid</exception>
        public Encryptor(SEALContext context, PublicKey publicKey)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == publicKey)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            NativeMethods.Encryptor_Create(context.NativePtr, publicKey.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Пример #9
0
        /// <summary>
        /// Check whether the given secret key is valid for a given SEALContext. If the
        /// given SEALContext is not set, the encryption parameters are invalid, or the
        /// secret key data does not match the SEALContext, this function returns false.
        /// Otherwise, returns true. This function only checks the metadata and not the
        /// secret key data itself.
        /// </summary>
        /// <param name="secretKey">The secret key to check</param>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if either secretKey or context is null</exception>
        public static bool IsMetadataValidFor(SecretKey secretKey, SEALContext context)
        {
            if (null == secretKey)
            {
                throw new ArgumentNullException(nameof(secretKey));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_SecretKey_IsMetadataValidFor(secretKey.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
Пример #10
0
        /// <summary>
        /// Check whether the given plaintext is valid for a given SEALContext. If the
        /// given SEALContext is not set, the encryption parameters are invalid, or the
        /// plaintext data does not match the SEALContext, this function returns false.
        /// Otherwise, returns true. This function only checks the metadata and not the
        /// plaintext data itself.
        /// </summary>
        /// <param name="plaintext">The plaintext to check</param>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if either plaintext or context is null</exception>
        public static bool IsMetadataValidFor(Plaintext plaintext, SEALContext context)
        {
            if (null == plaintext)
            {
                throw new ArgumentNullException(nameof(plaintext));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_Plaintext_IsMetadataValidFor(plaintext.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
Пример #11
0
        /// <summary>
        /// Check whether the given public key is valid for a given SEALContext. If the
        /// given SEALContext is not set, the encryption parameters are invalid, or the
        /// public key data does not match the SEALContext, this function returns false.
        /// Otherwise, returns true.
        /// </summary>
        /// <param name="publicKey">The public key to check</param>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if either publicKey or context is null</exception>
        public static bool IsValidFor(PublicKey publicKey, SEALContext context)
        {
            if (null == publicKey)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_PublicKey_IsValidFor(publicKey.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
Пример #12
0
        /// <summary>
        /// Creates a KeyGenerator initialized with the specified SEALContext.
        /// </summary>
        /// <remarks>
        /// Creates a KeyGenerator initialized with the specified <see cref="SEALContext" />.
        /// Dynamically allocated member variables are allocated from the global memory pool.
        /// </remarks>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentException">if encryption parameters are not
        /// valid</exception>
        /// <exception cref="ArgumentNullException">if context is null</exception>
        public KeyGenerator(SEALContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (!context.ParametersSet)
            {
                throw new ArgumentException("Encryption parameters are not set correctly");
            }

            NativeMethods.KeyGenerator_Create(context.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Пример #13
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. This function only checks the metadata and not the
        /// RelinKeys data itself.
        /// </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 IsMetadataValidFor(RelinKeys relinKeys, SEALContext context)
        {
            if (null == relinKeys)
            {
                throw new ArgumentNullException(nameof(relinKeys));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_RelinKeys_IsMetadataValidFor(relinKeys.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
Пример #14
0
        /// <summary>
        /// Creates an KeyGenerator instance initialized with the specified
        /// SEALContext and specified previously secret key. This can e.g. be used
        /// to increase the number of relinearization keys from what had earlier
        /// been generated, or to generate Galois keys in case they had not been
        /// generated earlier.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="secretKey">A previously generated secret key</param>
        /// <exception cref="ArgumentNullException">if either context or secretKey are null</exception>
        /// <exception cref="ArgumentException">if encryption parameters are not valid</exception>
        /// <exception cref="ArgumentException">if secretKey or publicKey is not valid
        /// for encryption parameters</exception>
        public KeyGenerator(SEALContext context, SecretKey secretKey)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == secretKey)
            {
                throw new ArgumentNullException(nameof(secretKey));
            }

            NativeMethods.KeyGenerator_Create(context.NativePtr, secretKey.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Пример #15
0
        /// <summary>
        /// Loads a SecretKey from an input stream overwriting the current SecretKey.
        /// The loaded SecretKey is verified to be valid for the given SEALContext.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="stream">The stream to load the SecretKey from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">if SecretKey could not be read from
        /// stream or is invalid for the context</exception>
        public void Load(SEALContext context, Stream stream)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            UnsafeLoad(stream);
            if (!ValCheck.IsValidFor(this, context))
            {
                throw new ArgumentException("SecretKey data is invalid for context");
            }
        }
Пример #16
0
        /// <summary>
        /// Constructs an empty ciphertext with given capacity. In addition to
        /// the capacity, the allocation size is determined by the given
        /// encryption parameters.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="parmsId">The ParmsId corresponding to the encryption
        /// parameters to be used</param>
        /// <param name="sizeCapacity">The capacity</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either context or parmsId are null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if sizeCapacity is less than 2 or too large</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Ciphertext(SEALContext context, ParmsId parmsId, ulong sizeCapacity,
                          MemoryPoolHandle pool = null)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }

            IntPtr poolPtr = pool?.NativePtr ?? IntPtr.Zero;

            NativeMethods.Ciphertext_Create5(context.NativePtr, parmsId.Block, sizeCapacity, poolPtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Пример #17
0
        /// <summary>
        /// Loads a RelinKeys from an input stream overwriting the current RelinKeys.
        /// The loaded RelinKeys is verified to be valid for the given SEALContext.
        /// </summary>
        ///
        /// <param name="context">The SEALContext</param>
        /// <param name="stream">The stream to load the RelinKeys instance from</param>
        /// <exception cref="ArgumentNullException">if either stream or context are null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">If the stream data is invalid or is not
        /// valid for the context</exception>
        /// <seealso cref="Save(Stream)">See Save() to save an RelinKeys instance.</seealso>
        public void Load(SEALContext context, Stream stream)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            UnsafeLoad(stream);

            if (!IsValidFor(context))
            {
                throw new ArgumentException("RelinKeys data is invalid for the context");
            }
        }
Пример #18
0
        /// <summary>
        /// Creates an Encryptor instance initialized with the specified SEALContext
        /// and secret key.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="secretKey">The secret key</param>
        /// <exception cref="ArgumentNullException">if context is null</exception>
        /// <exception cref="ArgumentException">if the encryption parameters are not valid</exception>
        /// <exception cref="ArgumentException">if secretKey is not valid</exception>
        public Encryptor(SEALContext context, SecretKey secretKey)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (!context.ParametersSet)
            {
                throw new ArgumentException("Encryption parameters are not set correctly");
            }
            if (!ValCheck.IsValidFor(secretKey, context))
            {
                throw new ArgumentException("Secret key is not valid for encryption parameters");
            }

            NativeMethods.Encryptor_Create(context.NativePtr, IntPtr.Zero, secretKey.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Пример #19
0
        /// <summary>
        /// Creates a IntegerEncoder object. The constructor takes as input a pointer to
        /// a SEALContext object which contains the plaintext modulus.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <exception cref="ArgumentNullException">if context is null</exception>
        /// <exception cref="ArgumentException">if the context is not set</exception>
        /// <exception cref="ArgumentException">if the PlainModulus set in context is not
        /// at least 2</exception>
        public IntegerEncoder(SEALContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (!context.ParametersSet)
            {
                throw new ArgumentException("Encryption parameters are not set correctly");
            }

            SEALContext.ContextData contextData = context.FirstContextData;
            if (contextData.Parms.Scheme != SchemeType.BFV)
            {
                throw new ArgumentException("Unsupported scheme");
            }

            NativeMethods.IntegerEncoder_Create(context.NativePtr, out IntPtr encoderPtr);
            NativePtr = encoderPtr;
        }
Пример #20
0
        /// <summary>
        /// Creates a BatchEncoder. It is necessary that the encryption parameters
        /// given through the SEALContext object support batching.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// @param[in] context
        /// <exception cref="ArgumentNullException">if context is null.</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid for batching</exception>
        /// <exception cref="ArgumentException">if scheme is not SchemeType.BFV</exception>
        public BatchEncoder(SEALContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (!context.ParametersSet)
            {
                throw new ArgumentException("Encryption parameters are not set correctly");
            }

            SEALContext.ContextData contextData = context.FirstContextData;
            if (contextData.Parms.Scheme != SchemeType.BFV)
            {
                throw new ArgumentException("Unsupported scheme");
            }
            if (!contextData.Qualifiers.UsingBatching)
            {
                throw new ArgumentException("Encryption parameters are not valid for batching");
            }

            NativeMethods.BatchEncoder_Create(context.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }