Esempio n. 1
0
        /// <summary>
        /// Encrypts a zero plaintext with the public key and returns the ciphertext
        /// as a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Encrypts a zero plaintext with the public key and returns the ciphertext
        /// as a serializable object.
        /// </para>
        /// <para>
        /// The encryption parameters for the resulting ciphertext correspond to the
        /// highest (data) level in the modulus switching chain. Dynamic memory
        /// allocations in the process are allocated from the memory pool pointed to
        /// by the given MemoryPoolHandle.
        /// </para>
        /// </remarks>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory
        /// pool</param>
        /// <exception cref="InvalidOperationException">if a public key is not
        /// set</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Serializable <Ciphertext> EncryptZero(MemoryPoolHandle pool = null)
        {
            Ciphertext destination = new Ciphertext();

            EncryptZero(destination, pool);
            return(new Serializable <Ciphertext>(destination));
        }
Esempio n. 2
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]);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Constructs an empty ciphertext allocating no memory.
        /// </summary>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="System.ArgumentException">if pool is uninitialized</exception>
        public Ciphertext(MemoryPoolHandle pool)
        {
            IntPtr poolPtr = pool?.NativePtr ?? IntPtr.Zero;

            NativeMethods.Ciphertext_Create1(poolPtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Esempio n. 4
0
        /// <summary>
        /// Returns a MemoryPoolHandle pointing to a pool selected by internal logic
        /// in a derived class.
        /// </summary>
        public MemoryPoolHandle GetPool()
        {
            NativeMethods.MMProf_GetPool(NativePtr, out IntPtr pool);
            MemoryPoolHandle handle = new MemoryPoolHandle(pool);

            return(handle);
        }
Esempio n. 5
0
        /// <summary>
        /// Encodes a vector of double-precision floating-point complex 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.
        /// </remark>
        /// <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);
        }
Esempio n. 6
0
        /// <summary>
        /// Decodes a plaintext polynomial into double-precision floating-point complex numbers.
        /// </summary>
        /// <remark>
        /// Dynamic memory allocations in the process are allocated from the memory pool pointed to
        /// by the given MemoryPoolHandle.
        /// </remark>
        /// <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]));
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Returns a MemoryPoolHandle pointing to a new thread-safe memory pool.
        /// </summary>
        /// <param name="clearOnDestruction">Indicates whether the memory pool data
        /// should be cleared when destroyed.This can be important when memory pools
        /// are used to store private data.</param>
        public static MemoryPoolHandle New(bool clearOnDestruction = false)
        {
            NativeMethods.MemoryPoolHandle_New(clearOnDestruction, out IntPtr handlePtr);
            MemoryPoolHandle handle = new MemoryPoolHandle(handlePtr);

            return(handle);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns a MemoryPoolHandle pointing to the thread-local memory pool. Note
        /// that the thread-local memory pool cannot be used to communicate across
        /// different threads.
        /// </summary>
        public static MemoryPoolHandle ThreadLocal()
        {
            NativeMethods.MemoryPoolHandle_ThreadLocal(out IntPtr handlePtr);
            MemoryPoolHandle handle = new MemoryPoolHandle(handlePtr);

            return(handle);
        }
Esempio n. 9
0
        /// <summary>
        /// Constructs a plaintext representing a constant polynomial 0. The
        /// coefficient count of the polynomial is set to the given value. The
        /// capacity is set to the same value.
        /// </summary>
        /// <param name="coeffCount">The number of (zeroed) coefficients in the
        /// plaintext polynomial</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentException">if coeffCount is negative</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Plaintext(ulong coeffCount, MemoryPoolHandle pool = null)
        {
            IntPtr poolPtr = pool?.NativePtr ?? IntPtr.Zero;

            NativeMethods.Plaintext_Create(coeffCount, poolPtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Esempio n. 10
0
        /// <summary>
        /// Constructs an empty plaintext allocating no memory.
        /// </summary>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Plaintext(MemoryPoolHandle pool = null)
        {
            IntPtr poolPtr = pool?.NativePtr ?? IntPtr.Zero;

            NativeMethods.Plaintext_Create1(poolPtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Esempio n. 11
0
        /// <summary>
        /// Encrypts a zero plaintext with the secret key and returns the ciphertext
        /// as a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Encrypts a zero 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 the
        /// highest (data) level in the modulus switching chain. Dynamic memory
        /// allocations in the process are allocated from the memory pool pointed to
        /// by the given MemoryPoolHandle.
        /// </para>
        /// </remarks>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory
        /// pool</param>
        /// <exception cref="InvalidOperationException">if a secret key is not
        /// set</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Serializable <Ciphertext> EncryptZeroSymmetric(MemoryPoolHandle pool = null)
        {
            IntPtr     poolHandle  = pool?.NativePtr ?? IntPtr.Zero;
            Ciphertext destination = new Ciphertext();

            NativeMethods.Encryptor_EncryptZeroSymmetric2(
                NativePtr, true, destination.NativePtr, poolHandle);
            return(new Serializable <Ciphertext>(destination));
        }
Esempio n. 12
0
        /// <summary>
        /// Constructs a new plaintext by copying a given one.
        /// </summary>
        /// <param name="copy">The plaintext to copy from</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if either copy or pool are null</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Plaintext(Plaintext copy, MemoryPoolHandle pool) : this(pool)
        {
            if (null == copy)
            {
                throw new ArgumentNullException(nameof(copy));
            }

            Set(copy);
        }
Esempio n. 13
0
        /// <summary>
        /// Overwrites the MemoryPoolHandle instance with the specified instance. As
        /// a result, the current MemoryPoolHandle will point to the same underlying
        /// memory pool as the assigned instance.
        /// </summary>
        /// <param name="assign">The MemoryPoolHandle instance to assign to the current
        /// instance</param>
        /// <exception cref="ArgumentNullException">if assign is null.</exception>
        public void Set(MemoryPoolHandle assign)
        {
            if (null == assign)
            {
                throw new ArgumentNullException(nameof(assign));
            }

            NativeMethods.MemoryPoolHandle_Set(NativePtr, assign.NativePtr);
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        /// <summary>
        /// Creates a copy of a given MemoryPoolHandle. As a result, the created
        /// MemoryPoolHandle will point to the same underlying memory pool as the
        /// copied instance.
        /// </summary>
        /// <param name="copy">The MemoryPoolHandle to copy from.</param>
        /// <exception cref="ArgumentNullException">if copy is null.</exception>
        public MemoryPoolHandle(MemoryPoolHandle copy)
        {
            if (null == copy)
            {
                throw new ArgumentNullException(nameof(copy));
            }

            NativeMethods.MemoryPoolHandle_Create(copy.NativePtr, out IntPtr handlePtr);
            NativePtr = handlePtr;
        }
Esempio n. 16
0
        /// <summary>
        /// Encrypts a zero plaintext with the public key and stores the result in
        /// destination.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Encrypts a zero plaintext with the public key and stores the result in
        /// destination.
        /// </para>
        /// <para>
        /// The encryption parameters for the resulting ciphertext correspond to the
        /// highest (data) level in the modulus switching chain. Dynamic memory allocations
        /// in the process are allocated from the memory pool pointed to by the given
        /// MemoryPoolHandle.
        /// </para>
        /// </remarks>
        /// <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 destination is null</exception>
        /// <exception cref="InvalidOperationException">if a public key is not set</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void EncryptZero(Ciphertext destination, MemoryPoolHandle pool = null)
        {
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            NativeMethods.Encryptor_EncryptZero2(NativePtr, destination.NativePtr, poolHandle);
        }
Esempio n. 17
0
        /// <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);
        }
Esempio n. 18
0
        /// <summary>
        /// Compares MemoryPoolHandles. This function returns whether the current
        /// MemoryPoolHandle points to the same memory pool as a given MemoryPoolHandle.
        /// </summary>
        /// <param name="obj">Object to compare to.</param>
        public override bool Equals(object obj)
        {
            MemoryPoolHandle other = obj as MemoryPoolHandle;

            if (null == other)
            {
                return(false);
            }

            NativeMethods.MemoryPoolHandle_Equals(NativePtr, other.NativePtr, out bool result);
            return(result);
        }
Esempio n. 19
0
        /// <summary>
        /// Constructs a plaintext from a given hexadecimal string describing the
        /// plaintext polynomial.
        /// </summary>
        /// <remarks>
        /// The string description of the polynomial must adhere to the format
        /// returned by ToString(), which is of the form "7FFx^3 + 1x^1 + 3"
        /// and summarized by the following
        /// rules:
        /// 1. Terms are listed in order of strictly decreasing exponent
        /// 2. Coefficient values are non-negative and in hexadecimal format (upper
        /// and lower case letters are both supported)
        /// 3. Exponents are positive and in decimal format
        /// 4. Zero coefficient terms (including the constant term) may be (but do
        /// not have to be) omitted
        /// 5. Term with the exponent value of one must be exactly written as x^1
        /// 6. Term with the exponent value of zero (the constant term) must be written
        /// as just a hexadecimal number without exponent
        /// 7. Terms must be separated by exactly [space]+[space] and minus is not
        /// allowed
        /// 8. Other than the +, no other terms should have whitespace
        /// </remarks>
        /// <param name="hexPoly">The formatted polynomial string specifying the plaintext
        /// polynomial</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
        /// <exception cref="ArgumentNullException">if hexPoly is null</exception>
        /// <exception cref="ArgumentException">if hexPoly does not adhere to the expected
        /// format</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Plaintext(string hexPoly, MemoryPoolHandle pool = null)
        {
            if (null == hexPoly)
            {
                throw new ArgumentNullException(nameof(hexPoly));
            }

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

            NativeMethods.Plaintext_Create4(hexPoly, poolPtr, out IntPtr ptr);
            NativePtr = ptr;
        }
Esempio n. 20
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;
        }
Esempio n. 21
0
        /// <summary>
        /// Create a new instance of MMProfFixed
        /// </summary>
        /// <param name="pool">Fixed memory pool handle to use</param>
        /// <exception cref="ArgumentNullException">if pool is null</exception>
        public MMProfFixed(MemoryPoolHandle pool)
        {
            if (null == pool)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            // Create a copy of MemoryPoolHandle, as the profile will take ownership
            // of the pointer and will attempt to delete it when destroyed.
            NativeMethods.MemoryPoolHandle_Create(pool.NativePtr, out IntPtr poolCopy);
            NativeMethods.MMProf_CreateFixed(poolCopy, out IntPtr profile);
            NativePtr = profile;
        }
Esempio n. 22
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.
        /// </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);
        }
Esempio n. 23
0
        /// <summary>
        /// Encrypts a zero plaintext with the secret key and stores the result in destination.
        /// </summary>
        /// <remarks>
        /// The encryption parameters for the resulting ciphertext correspond to the given
        /// parms_id. Dynamic memory allocations in the process are allocated from the memory
        /// pool pointed to by the given MemoryPoolHandle.
        /// </remarks>
        /// <param name="parmsId">The ParmsId for the resulting ciphertext</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 parmsId or destination are null</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the encryption parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public void EncryptZeroSymmetric(ParmsId parmsId, Ciphertext destination, MemoryPoolHandle pool = null)
        {
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }
            if (null == destination)
            {
                throw new ArgumentNullException(nameof(destination));
            }

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

            NativeMethods.Encryptor_EncryptZeroSymmetric1(NativePtr, parmsId.Block, false, destination.NativePtr, poolHandle);
        }
Esempio n. 24
0
        /// <summary>
        /// Encrypts a zero plaintext with the secret key and returns the ciphertext
        /// as a serializable object.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Encrypts a zero 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
        /// the given ParmsId. Dynamic memory allocations in the process are allocated
        /// from the memory pool pointed to by the given MemoryPoolHandle.
        /// </para>
        /// </remarks>
        /// <param name="parmsId">The ParmsId for the resulting ciphertext</param>
        /// <param name="pool">The MemoryPoolHandle pointing to a valid memory
        /// pool</param>
        /// <exception cref="ArgumentNullException">if parmsId is null</exception>
        /// <exception cref="InvalidOperationException">if a secret key is not
        /// set</exception>
        /// <exception cref="ArgumentException">if parmsId is not valid for the
        /// encryption parameters</exception>
        /// <exception cref="ArgumentException">if pool is uninitialized</exception>
        public Serializable <Ciphertext> EncryptZeroSymmetric(
            ParmsId parmsId,
            MemoryPoolHandle pool = null)
        {
            if (null == parmsId)
            {
                throw new ArgumentNullException(nameof(parmsId));
            }

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

            NativeMethods.Encryptor_EncryptZeroSymmetric1(
                NativePtr, parmsId.Block, true, destination.NativePtr, poolHandle);
            return(new Serializable <Ciphertext>(destination));
        }
Esempio n. 25
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));
        }
Esempio n. 26
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);
        }
Esempio n. 27
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;
        }
Esempio n. 28
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);
        }
Esempio n. 29
0
 /// <summary>
 /// Constructs a plaintext representing a polynomial with given coefficient values.
 /// </summary>
 /// <remarks>
 /// Constructs a plaintext representing a polynomial with given coefficient values.
 /// The coefficient count of the polynomial is set to the number of coefficient
 /// values provided, and the capacity is set to the same value.
 /// </remarks>
 /// <param name="coeffs">Desired values of the plaintext coefficients</param>
 /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
 /// <exception cref="ArgumentException">if pool is uninitialized</exception>
 /// <exception cref="ArgumentNullException">if coeffs is null</exception>
 public Plaintext(IEnumerable <ulong> coeffs, MemoryPoolHandle pool = null) : this(pool)
 {
     Set(coeffs);
 }
Esempio n. 30
0
 /// <summary>
 /// Constructs a plaintext representing a polynomial with given coefficient values.
 /// </summary>
 /// <remarks>
 /// Constructs a plaintext representing a polynomial with given coefficient values.
 /// The coefficient count of the polynomial is set to the number of coefficient
 /// values provided, and the capacity is set to the given value.
 /// </remarks>
 /// <param name="coeffs">Desired values of the plaintext coefficients</param>
 /// <param name="capacity">The capacity</param>
 /// <param name="pool">The MemoryPoolHandle pointing to a valid memory pool</param>
 /// <exception cref="ArgumentException">if capacity is less than the size of coeffs</exception>
 /// <exception cref="ArgumentException">if pool is uninitialized</exception>
 /// <exception cref="ArgumentNullException">if coeffs is null</exception>
 public Plaintext(IEnumerable <ulong> coeffs, ulong capacity, MemoryPoolHandle pool = null)
     : this(capacity, 0, pool)
 {
     Set(coeffs);
 }