Exemple #1
0
        /// <summary>
        /// Loads a RelinKeys from an input stream overwriting the current RelinKeys.
        /// No checking of the validity of the RelinKeys data against encryption
        /// parameters is performed. This function should not be used unless the
        /// RelinKeys comes from a fully trusted source.
        /// </summary>
        /// <param name="stream">The stream to load the RelinKeys from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if valid RelinKeys could not be read
        /// from stream</exception>
        public void UnsafeLoad(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                // Read the ParmsId
                ParmsId parmsId = new ParmsId();
                parmsId.Load(stream);
                ParmsId = parmsId;

                using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true))
                {
                    // Read the decomposition bit count
                    int dbc = reader.ReadInt32();
                    DecompositionBitCount = dbc;

                    // Read the size
                    ulong size = reader.ReadUInt64();

                    // Clear current data and reserve new size
                    NativeMethods.RelinKeys_ClearDataAndReserve(NativePtr, size);

                    // Read all lists
                    for (ulong i = 0; i < size; i++)
                    {
                        // Read size of second list
                        ulong             keySize = reader.ReadUInt64();
                        List <Ciphertext> ciphers = new List <Ciphertext>((int)keySize);

                        // Load all ciphertexts
                        for (ulong j = 0; j < keySize; j++)
                        {
                            Ciphertext cipher = new Ciphertext();
                            cipher.UnsafeLoad(reader.BaseStream);
                            ciphers.Add(cipher);
                        }

                        IntPtr[] pointers = ciphers.Select(c =>
                        {
                            return(c.NativePtr);
                        }).ToArray();

                        NativeMethods.RelinKeys_AddKeyList(NativePtr, (ulong)pointers.LongLength, pointers);
                    }
                }
            }
            catch (EndOfStreamException ex)
            {
                throw new ArgumentException("Stream ended unexpectedly", ex);
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Error reading keys", ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Loads a KSwitchKeys from an input stream overwriting the current KSwitchKeys.
        /// </summary>
        /// <remarks>
        /// Loads a KSwitchKeys from an input stream overwriting the current KSwitchKeys.
        /// No checking of the validity of the KSwitchKeys data against encryption
        /// parameters is performed. This function should not be used unless the
        /// KSwitchKeys comes from a fully trusted source.
        /// </remarks>
        /// <param name="stream">The stream to load the KSwitchKeys from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if KSwitchKeys could not be read from
        /// stream</exception>
        public void UnsafeLoad(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                // Read the ParmsId
                ParmsId parmsId = new ParmsId();
                parmsId.Load(stream);
                ParmsId = parmsId;

                using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true))
                {
                    // Read the size
                    ulong size = reader.ReadUInt64();

                    // Clear current data and reserve new size
                    NativeMethods.KSwitchKeys_ClearDataAndReserve(NativePtr, size);

                    // Read all lists
                    for (ulong i = 0; i < size; i++)
                    {
                        // Read size of second list
                        ulong            keySize = reader.ReadUInt64();
                        List <PublicKey> key     = new List <PublicKey>(checked ((int)keySize));

                        // Load all ciphertexts
                        for (ulong j = 0; j < keySize; j++)
                        {
                            PublicKey pkey = new PublicKey();
                            pkey.UnsafeLoad(reader.BaseStream);
                            key.Add(pkey);
                        }

                        IntPtr[] pointers = key.Select(c =>
                        {
                            return(c.NativePtr);
                        }).ToArray();

                        NativeMethods.KSwitchKeys_AddKeyList(NativePtr, (ulong)pointers.LongLength, pointers);
                    }
                }
            }
            catch (EndOfStreamException ex)
            {
                throw new ArgumentException("Stream ended unexpectedly", ex);
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Could not load KSwitchKeys", ex);
            }
        }
Exemple #3
0
        /// <summary>
        /// Loads a ciphertext from an input stream overwriting the current ciphertext.
        /// No checking of the validity of the ciphertext data against encryption
        /// parameters is performed. This function should not be used unless the
        /// ciphertext comes from a fully trusted source.
        /// </summary>
        /// <param name="stream">The stream to load the ciphertext from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if a valid ciphertext could not be read from stream</exception>
        public void UnsafeLoad(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true))
                {
                    ParmsId parms = new ParmsId();
                    parms.Load(reader.BaseStream);
                    ParmsId = parms;

                    bool  isNTT             = reader.ReadBoolean();
                    ulong size              = reader.ReadUInt64();
                    ulong polyModulusDegree = reader.ReadUInt64();
                    ulong coeffModCount     = reader.ReadUInt64();
                    ulong ulongCount        = size * polyModulusDegree * coeffModCount;

                    IsNTTForm = isNTT;
                    Resize(size, polyModulusDegree, coeffModCount);

                    for (ulong i = 0; i < ulongCount; i++)
                    {
                        this[i] = reader.ReadUInt64();
                    }
                }
            }
            catch (EndOfStreamException ex)
            {
                throw new ArgumentException("Stream ended unexpectedly", ex);
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Error reading ciphertext", ex);
            }
        }
Exemple #4
0
        /// <summary>
        /// Loads a plaintext from an input stream overwriting the current plaintext.
        /// No checking of the validity of the plaintext data against encryption
        /// parameters is performed. This function should not be used unless the
        /// plaintext comes from a fully trusted source.
        /// </summary>
        /// <param name="stream">The stream to load the plaintext from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if a valid plaintext could not be read from stream</exception>
        public void UnsafeLoad(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                ParmsId parms = new ParmsId();
                parms.Load(stream);
                ParmsId = parms;

                using (BinaryReader reader = new BinaryReader(stream))
                {
                    double scale      = reader.ReadDouble();
                    ulong  coeffCount = reader.ReadUInt64();

                    Scale = scale;

                    ulong[] newData = new ulong[coeffCount];

                    for (ulong i = 0; i < coeffCount; i++)
                    {
                        newData[i] = reader.ReadUInt64();
                    }

                    NativeMethods.Plaintext_SwapData(NativePtr, coeffCount, newData);
                }
            }
            catch (EndOfStreamException ex)
            {
                throw new ArgumentException("Stream ended unexpectedly", ex);
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Could not read Plaintext", ex);
            }
        }