Exemple #1
0
        /// <summary>
        /// Decodes a plaintext polynomial into double-precision floating-point complex
        /// numbers. Dynamic memory allocations in the process are allocated from
        /// the memory pool pointed to by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="plain">plain The plaintext to decode</param>
        /// <param name="destination">The collection to be overwritten with the values in the slots</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either plain or destination are null</exception>
        /// <exception cref="ArgumentException">if plain is not in NTT form or is invalid for the
        /// encryption parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Decode(Plaintext plain, ICollection <Complex> destination,
                           MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            IntPtr poolPtr   = pool?.NativePtr ?? IntPtr.Zero;
            ulong  destCount = 0;

            // Find out what is the actual result size
            NativeMethods.CKKSEncoder_DecodeComplex(NativePtr, plain.NativePtr, ref destCount, null, poolPtr);

            // Now get the result
            double[] destarray = new double[destCount * 2];
            NativeMethods.CKKSEncoder_DecodeComplex(NativePtr, plain.NativePtr, ref destCount, destarray, poolPtr);

            // Transfer result to actual destination
            destination.Clear();
            for (ulong i = 0; i < destCount; i++)
            {
                destination.Add(new Complex(destarray[i * 2], destarray[i * 2 + 1]));
            }
        }
Exemple #2
0
        /// <summary>
        /// Encrypts a plaintext with the secret key and stores the result in
        /// destination.
        /// </summary>
        /// <remarks>
        /// The encryption parameters for the resulting ciphertext correspond to:
        /// 1) in BFV, 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.
        ///
        /// 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="plain">The plaintext to encrypt</param>
        /// <param name="stream">The stream to save the Ciphertext to</param>
        /// <param name="comprMode">The desired compression mode</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if plain or stream is null</exception>
        /// <exception cref="InvalidOperationException">if a secret key is not set</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>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if plain is not in default NTT
        /// form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public long EncryptSymmetricSave(
            Plaintext plain, Stream stream,
            ComprModeType?comprMode = null,
            MemoryPoolHandle pool   = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

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

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

            using (Ciphertext destination = new Ciphertext(pool))
            {
                NativeMethods.Encryptor_EncryptSymmetric(
                    NativePtr, plain.NativePtr, true, destination.NativePtr, poolHandle);
                return(destination.Save(stream, comprMode));
            }
        }
        /// <summary>
        /// Inverse of encode. This function "unbatches" a given plaintext into a matrix
        /// of integers modulo the plaintext modulus, and stores the result in the destination
        /// parameter. The input plaintext must have degress less than the polynomial modulus,
        /// and coefficients less than the plaintext modulus, i.e. it must be a valid plaintext
        /// for the encryption parameters. Dynamic memory allocations in the process are
        /// allocated from the memory pool pointed to by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="plain">The plaintext polynomial to unbatch</param>
        /// <param name="destination">The matrix to be overwritten with the values in the slots</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either plain or destination are null</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if plain is in NTT form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Decode(Plaintext plain, ICollection <long> destination, MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            IntPtr poolPtr   = pool?.NativePtr ?? IntPtr.Zero;
            ulong  destCount = 0;

            // Allocate a big enough array to hold the result
            long[] destArray = new long[SlotCount];
            NativeMethods.BatchEncoder_Decode(NativePtr, plain.NativePtr, ref destCount, destArray, poolPtr);

            // Transfer result to actual destination
            destination.Clear();
            for (ulong i = 0; i < destCount; i++)
            {
                destination.Add(destArray[i]);
            }
        }
Exemple #4
0
        /// <summary>
        /// Inverse of encode. This function "unbatches" a given plaintext into a matrix
        /// of integers modulo the plaintext modulus, and stores the result in the destination
        /// parameter. The input plaintext must have degress less than the polynomial modulus,
        /// and coefficients less than the plaintext modulus, i.e. it must be a valid plaintext
        /// for the encryption parameters. Dynamic memory allocations in the process are
        /// allocated from the memory pool pointed to by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="plain">The plaintext polynomial to unbatch</param>
        /// <param name="destination">The matrix to be overwritten with the values in the slots</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either plain or destination are null</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if plain is in NTT form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Decode(Plaintext plain, ICollection <long> destination, MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            ulong count = 0;

            NativeMethods.BatchEncoder_Decode(NativePtr, plain.NativePtr, ref count, (long[])null, poolPtr);

            long[] dest = new long[count];
            NativeMethods.BatchEncoder_Decode(NativePtr, plain.NativePtr, ref count, dest, poolPtr);

            destination.Clear();
            for (ulong i = 0; i < count; i++)
            {
                destination.Add(dest[i]);
            }
        }
Exemple #5
0
        /// <summary>
        /// Encodes an unsigned integer (represented by ulong) into a plaintext polynomial.
        /// </summary>
        /// <param name="value">The unsigned integer to encode</param>
        public Plaintext Encode(ulong value)
        {
            Plaintext plain = new Plaintext();

            NativeMethods.IntegerEncoder_Encode(NativePtr, value, plain.NativePtr);
            return(plain);
        }
Exemple #6
0
        /// <summary>
        /// Encodes double-precision floating-point complex numbers into a plaintext
        /// polynomial. Dynamic memory allocations in the process are allocated from the
        /// memory pool pointed to by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="values">The enumeration of double-precision complex numbers
        /// to encode</param>
        /// <param name="parmsId">parmsId determining the encryption parameters to be used
        /// by the result plaintext</param>
        /// <param name="scale">Scaling parameter defining encoding precision</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either values, parmsId or destionation are null.</exception>
        /// <exception cref="ArgumentException">if values has invalid size</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption
        /// parameters </exception>
        /// <exception cref="ArgumentException">if scale is not strictly positive</exception>
        /// <exception cref="ArgumentException">if encoding is too large for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Encode(IEnumerable <Complex> values, ParmsId parmsId,
                           double scale, Plaintext destination, MemoryPoolHandle pool = null)
        {
            if (null == values)
            {
                throw new ArgumentNullException(nameof(values));
            }
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            double[] valuearray = new double[values.LongCount() * 2];
            ulong    idx        = 0;

            foreach (Complex complex in values)
            {
                valuearray[idx++] = complex.Real;
                valuearray[idx++] = complex.Imaginary;
            }

            // Note that we should pass values.Count as the length instead of valuearray.Length,
            // since we are using two doubles in the array per element.
            NativeMethods.CKKSEncoder_EncodeComplex(NativePtr, (ulong)values.LongCount(), valuearray,
                                                    parmsId.Block, scale, destination.NativePtr, poolPtr);
        }
Exemple #7
0
        /// <summary>
        /// Encodes an integer number into a plaintext polynomial without any scaling. The
        /// encryption parameters used are the top level parameters for the given context.
        /// </summary>
        /// <param name="value">The integer number to encode</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <exception cref="ArgumentNullException">if destination is null</exception>
        public void Encode(long value, Plaintext destination)
        {
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            Encode(value, context_.FirstParmsId, destination);
        }
Exemple #8
0
        /// <summary>
        /// Encrypts a plaintext with the public key and returns the ciphertext as
        /// a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Encrypts a plaintext with the public key and returns the ciphertext as
        /// a serializable object.
        /// </para>
        /// <para>
        /// The encryption parameters for the resulting ciphertext correspond to:
        /// 1) in BFV, 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.
        /// </para>
        /// </remarks>
        /// <param name="plain">The plaintext to encrypt</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if plain is null</exception>
        /// <exception cref="InvalidOperationException">if a public key is not set</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if plain is not in default NTT
        /// form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Serializable <Ciphertext> Encrypt(
            Plaintext plain,
            MemoryPoolHandle pool = null)
        {
            Ciphertext destination = new Ciphertext();

            Encrypt(plain, destination, pool);
            return(new Serializable <Ciphertext>(destination));
        }
Exemple #9
0
        /// <summary>
        /// Encodes an unsigned integer(represented by ulong) into a plaintext polynomial.
        /// </summary>
        /// <param name="value">The unsigned integer to encode</param>
        /// <param name="destination">The plaintext to overwrite with the encoding</param>
        /// <exception cref="ArgumentNullException">if destination is null</exception>
        public void Encode(ulong value, Plaintext destination)
        {
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            NativeMethods.IntegerEncoder_Encode(NativePtr, value, destination.NativePtr);
        }
Exemple #10
0
        /// <summary>
        /// Encodes a double-precision complex number into a plaintext polynomial. The
        /// encryption parameters used are the top level parameters for the given context.
        /// Dynamic memory allocations in the process are allocated from the memory pool
        /// pointed to by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="value">The double-precision complex number to encode</param>
        /// <param name="scale">Scaling parameter defining encoding precision</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if destination is null</exception>
        /// <exception cref="ArgumentException">if scale is not strictly positive</exception>
        /// <exception cref="ArgumentException">if encoding is too large for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Encode(Complex value, double scale, Plaintext destination,
                           MemoryPoolHandle pool = null)
        {
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            Encode(value, context_.FirstParmsId, scale, destination, pool);
        }
Exemple #11
0
        /// <summary>
        /// Decodes a plaintext polynomial and returns the result as uint. Mathematically
        /// this amounts to evaluating the input polynomial at X = 2.
        /// </summary>
        /// <param name="plain">The plaintext to be decoded</param>
        /// <exception cref="ArgumentNullException">if plain is null</exception>
        /// <exception cref="ArgumentException">if plain does not represent a valid plaintext polynomial</exception>
        /// <exception cref="ArgumentException">if the output does not fit in uint</exception>
        public uint DecodeUInt32(Plaintext plain)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }

            NativeMethods.IntegerEncoder_DecodeUInt32(NativePtr, plain.NativePtr, out uint result);
            return(result);
        }
Exemple #12
0
        /// <summary>
        /// Decodes a plaintext polynomial and returns the result as long. Mathematically
        /// this amounts to evaluating the input polynomial at X = 2.
        /// </summary>
        /// <param name="plain">The plaintext to be decoded</param>
        /// <exception cref="ArgumentNullException">if plain is null</exception>
        /// <exception cref="ArgumentException">if plain does not represent a valid plaintext polynomial</exception>
        /// <exception cref="ArgumentException">if the output does not fit in long</exception>
        public long DecodeInt64(Plaintext plain)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }

            NativeMethods.IntegerEncoder_DecodeInt64(NativePtr, plain.NativePtr, out long result);
            return(result);
        }
        /// <summary>
        /// Inverse of encode. This function "unbatches" a given plaintext in-place into
        /// a matrix of integers modulo the plaintext modulus. The input plaintext must have
        /// degress less than the polynomial modulus, and coefficients less than the plaintext
        /// modulus, i.e. it must be a valid plaintext for the encryption parameters. Dynamic
        /// memory allocations in the process are allocated from the memory pool pointed to by
        /// the given MemoryPoolHandle.
        /// </summary>
        /// <param name="plain">The plaintext polynomial to unbatch</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if plain is null</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if plain is in NTT form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Decode(Plaintext plain, MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }

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

            NativeMethods.BatchEncoder_Decode(NativePtr, plain.NativePtr, poolPtr);
        }
Exemple #14
0
        /// <summary>
        /// Decodes a plaintext polynomial and returns the result as BigUInt.
        /// Mathematically this amounts to evaluating the input polynomial at X = 2.
        /// </summary>
        /// <param name="plain">The plaintext to be decoded</param>
        /// <exception cref="ArgumentNullException">if plain is null</exception>
        /// <exception cref="ArgumentException">if plain does not represent a valid plaintext polynomial</exception>
        /// <exception cref="ArgumentException">if the output is negative</exception>
        public BigUInt DecodeBigUInt(Plaintext plain)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }

            NativeMethods.IntegerEncoder_DecodeBigUInt(NativePtr, plain.NativePtr, out IntPtr buiPtr);
            BigUInt bui = new BigUInt(buiPtr);

            return(bui);
        }
Exemple #15
0
        /// <summary>
        /// Encodes an unsigned integer(represented by BigUInt) into a plaintext polynomial.
        /// </summary>
        /// <param name="value">The unsigned integer to encode</param>
        /// <exception cref="ArgumentNullException">if value is null</exception>
        public Plaintext Encode(BigUInt value)
        {
            if (null == value)
            {
                throw new ArgumentNullException(nameof(value));
            }

            Plaintext plain = new Plaintext();

            NativeMethods.IntegerEncoder_Encode(NativePtr, value.NativePtr, plain.NativePtr);
            return(plain);
        }
Exemple #16
0
        /// <summary>
        /// Encodes an integer number into a plaintext polynomial without any scaling.
        /// </summary>
        /// <param name="value">The integer number to encode</param>
        /// <param name="parmsId">parmsId determining the encryption parameters to be used
        /// by the result plaintext</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <exception cref="ArgumentNullException">if either parmsId or destionation are null</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption
        /// parameters </exception>
        public void Encode(long value, ParmsId parmsId, Plaintext destination)
        {
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            NativeMethods.CKKSEncoder_Encode(NativePtr, value, parmsId.Block, destination.NativePtr);
        }
Exemple #17
0
        /// <summary>
        /// Decrypts a Ciphertext and stores the result in the destination parameter. Dynamic
        /// memory allocations in the process are allocated from the memory pool pointed to by
        /// the given MemoryPoolHandle.
        /// </summary>
        /// <param name="encrypted">The ciphertext to decrypt</param>
        /// <param name="destination">The plaintext to overwrite with the decrypted ciphertext</param>
        /// <exception cref="ArgumentNullException">if either encrypted or destination are null</exception>
        /// <exception cref="ArgumentException">if encrypted is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if encrypted is not in the default NTT form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Decrypt(Ciphertext encrypted, Plaintext destination)
        {
            if (null == encrypted)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            NativeMethods.Decryptor_Decrypt(NativePtr, encrypted.NativePtr, destination.NativePtr);
        }
        /// <summary>
        /// Creates a plaintext from a given matrix. This function "batches" a given matrix
        /// of integers modulo the plaintext modulus into a plaintext element, and stores
        /// the result in the destination parameter. The input vector must have size at most equal
        /// to the degree of the polynomial modulus. The first half of the elements represent the
        /// first row of the matrix, and the second half represent the second row. The numbers
        /// in the matrix can be at most equal to the plaintext modulus for it to represent
        /// a valid plaintext.
        ///
        /// If the destination plaintext overlaps the input values in memory, the behavior of
        /// this function is undefined.
        /// </summary>
        /// <param name="values">The matrix of integers modulo plaintext modulus to batch</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <exception cref="ArgumentNullException">if either values or destionation are null</exception>
        /// <exception cref="ArgumentException">if values is too large</exception>
        public void Encode(IEnumerable <long> values, Plaintext destination)
        {
            if (null == values)
            {
                throw new ArgumentNullException(nameof(values));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            long[] valarray = values.ToArray();
            NativeMethods.BatchEncoder_Encode(NativePtr, (ulong)valarray.LongLength, valarray, destination.NativePtr);
        }
Exemple #19
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.
        /// </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 IsValidFor(Plaintext plaintext, SEALContext context)
        {
            if (null == plaintext)
            {
                throw new ArgumentNullException(nameof(plaintext));
            }
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            NativeMethods.ValCheck_Plaintext_IsValidFor(plaintext.NativePtr, context.NativePtr, out bool result);
            return(result);
        }
        /// <summary>
        /// Encrypts a plaintext with the secret key and stores the result in destination.
        /// </summary>
        /// <remarks>
        /// The encryption parameters for the resulting ciphertext correspond to:
        /// 1) in BFV, 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.
        /// </remarks>
        /// <param name="plain">The plaintext to encrypt</param>
        /// <param name="destination">The ciphertext to overwrite with the encrypted plaintext</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either plain or destination are null</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if plain is not in default NTT form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void EncryptSymmetric(Plaintext plain, Ciphertext destination, MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            NativeMethods.Encryptor_EncryptSymmetric(NativePtr, plain.NativePtr, false, destination.NativePtr, poolHandle);
        }
Exemple #21
0
        /// <summary>
        /// Encrypts a plaintext with the secret key and returns the ciphertext as
        /// a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Encrypts a plaintext with the secret key and returns the ciphertext as
        /// a serializable object.
        /// </para>
        /// <para>
        /// Half of the ciphertext 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>
        /// <para>
        /// The encryption parameters for the resulting ciphertext correspond to:
        /// 1) in BFV, 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.
        /// </para>
        /// </remarks>
        /// <param name="plain">The plaintext to encrypt</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if plain is null</exception>
        /// <exception cref="InvalidOperationException">if a secret key is not set</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if plain is not in default NTT
        /// form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Serializable <Ciphertext> EncryptSymmetric(
            Plaintext plain,
            MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }

            IntPtr     poolHandle  = pool?.NativePtr ?? IntPtr.Zero;
            Ciphertext destination = new Ciphertext();

            NativeMethods.Encryptor_EncryptSymmetric(
                NativePtr, plain.NativePtr, true, destination.NativePtr, poolHandle);
            return(new Serializable <Ciphertext>(destination));
        }
Exemple #22
0
        /// <summary>
        /// Encodes a double-precision complex number into a plaintext polynomial. Dynamic
        /// memory allocations in the process are allocated from the memory pool pointed to
        /// by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="value">The double-precision complex number to encode</param>
        /// <param name="parmsId">parmsId determining the encryption parameters to be used
        /// by the result plaintext</param>
        /// <param name="scale">Scaling parameter defining encoding precision</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either parmsId or destination are null</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption
        /// parameters </exception>
        /// <exception cref="ArgumentException">if scale is not strictly positive</exception>
        /// <exception cref="ArgumentException">if encoding is too large for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Encode(Complex value, ParmsId parmsId, double scale,
                           Plaintext destination, MemoryPoolHandle pool = null)
        {
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            NativeMethods.CKKSEncoder_Encode(NativePtr, value.Real, value.Imaginary, parmsId.Block, scale, destination.NativePtr, poolPtr);
        }
        /// <summary>
        /// Encrypts a plaintext with the secret key and stores the result in destination.
        /// </summary>
        /// <remarks>
        /// The encryption parameters for the resulting ciphertext correspond to:
        /// 1) in BFV, 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.
        ///
        /// 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="plain">The plaintext to encrypt</param>
        /// <param name="stream">The stream to save the Ciphertext to</param>
        /// <param name="comprMode">The desired compression mode</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if plain or stream is null</exception>
        /// <exception cref="ArgumentException">if plain is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if plain is not in default NTT form</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public long EncryptSymmetricSave(Plaintext plain, Stream stream, ComprModeType?comprMode = null, MemoryPoolHandle pool = null)
        {
            if (null == plain)
            {
                throw new ArgumentNullException(nameof(plain));
            }
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

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

            using (Ciphertext destination = new Ciphertext(pool))
            {
                NativeMethods.Encryptor_EncryptSymmetric(NativePtr, plain.NativePtr, true, destination.NativePtr, poolHandle);
                return(destination.Save(stream, comprMode));
            }
        }
Exemple #24
0
        /// <summary>
        /// Encodes double-precision floating-point real numbers into a plaintext
        /// polynomial. Dynamic memory allocations in the process are allocated from the
        /// memory pool pointed to by the given MemoryPoolHandle.
        /// </summary>
        /// <param name="values">The enumeration of double-precision floating-point numbers
        /// to encode</param>
        /// <param name="parmsId">parmsId determining the encryption parameters to be used
        /// by the result plaintext</param>
        /// <param name="scale">Scaling parameter defining encoding precision</param>
        /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either values, parmsId or destionation are null.</exception>
        /// <exception cref="ArgumentException">if values has invalid size</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption
        /// parameters </exception>
        /// <exception cref="ArgumentException">if scale is not strictly positive</exception>
        /// <exception cref="ArgumentException">if encoding is too large for the encryption
        /// parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void Encode(IEnumerable <double> values, ParmsId parmsId,
                           double scale, Plaintext destination, MemoryPoolHandle pool = null)
        {
            if (null == values)
            {
                throw new ArgumentNullException(nameof(values));
            }
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            double[] valuearray = values.ToArray();
            NativeMethods.CKKSEncoder_EncodeDouble(NativePtr, (ulong)valuearray.LongLength, valuearray,
                                                   parmsId.Block, scale, destination.NativePtr, poolPtr);
        }
Exemple #25
0
 /// <summary>
 /// Encodes a vector of double-precision floating-point real numbers into a plaintext
 /// polynomial.
 /// </summary>
 /// <remark>
 /// Append zeros if vector size is less than N/2. Dynamic memory allocations in the process
 /// are allocated from the memory pool pointed to by the given MemoryPoolHandle.
 /// The encryption parameters used are the top level parameters for the given context.
 /// </remark>
 /// <param name="values">The enumeration of double-precision floating-point numbers
 /// to encode</param>
 /// <param name="scale">Scaling parameter defining encoding precision</param>
 /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
 /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
 /// <exception cref="ArgumentNullException">if either values or destionation are null.</exception>
 /// <exception cref="ArgumentException">if values has invalid size</exception>
 /// <exception cref="ArgumentException">if scale is not strictly positive</exception>
 /// <exception cref="ArgumentException">if encoding is too large for the encryption
 /// parameters</exception>
 /// <exception cref="ArgumentException">if pool is uninitialized</exception>
 public void Encode(IEnumerable <double> values, double scale,
                    Plaintext destination, MemoryPoolHandle pool = null)
 {
     Encode(values, context_.FirstParmsId, scale, destination, pool);
 }
Exemple #26
0
 /// <summary>
 /// Encodes a double-precision floating-point complex number into a plaintext polynomial.
 /// </summary>
 /// <remark>
 /// The number repeats for N/2 times to fill all slots. Dynamic memory allocations in the
 /// process are allocated from the memory pool pointed to by the given MemoryPoolHandle.
 /// The encryption parameters used are the top level parameters for the given context.
 /// </remark>
 /// <param name="value">The double-precision complex number to encode</param>
 /// <param name="scale">Scaling parameter defining encoding precision</param>
 /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
 /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
 /// <exception cref="ArgumentNullException">if destination is null</exception>
 /// <exception cref="ArgumentException">if scale is not strictly positive</exception>
 /// <exception cref="ArgumentException">if encoding is too large for the encryption
 /// parameters</exception>
 /// <exception cref="ArgumentException">if pool is uninitialized</exception>
 public void Encode(Complex value, double scale, Plaintext destination,
                    MemoryPoolHandle pool = null)
 {
     Encode(value, context_.FirstParmsId, scale, destination, pool);
 }
Exemple #27
0
 /// <summary>
 /// Encodes an integer number into a plaintext polynomial without any scaling.
 /// </summary>
 /// <remark>
 /// The number repeats for N/2 times to fill all slots. The encryption parameters used are
 /// the top level parameters for the given context.
 /// </remark>
 /// <param name="value">The integer number to encode</param>
 /// <param name="destination">The plaintext polynomial to overwrite with the result</param>
 /// <exception cref="ArgumentNullException">if destination is null</exception>
 public void Encode(long value, Plaintext destination)
 {
     Encode(value, context_.FirstParmsId, destination);
 }