Exemple #1
0
        /// <summary>
        /// Saves the plaintext to an output stream.
        /// </summary>
        /// <remarks>
        /// Saves the plaintext to an output stream. The output is in binary format
        /// and not human-readable. The output stream must have the "binary" flag set.
        /// </remarks>
        /// <param name="stream">The stream to save the plaintext to</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if the plaintext could not be written
        /// to stream</exception>
        public void Save(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
                {
                    ParmsId.Save(stream);
                    writer.Write(Scale);
                    writer.Write(CoeffCount);
                    for (ulong i = 0; i < CoeffCount; i++)
                    {
                        ulong data = this[i];
                        writer.Write(data);
                    }
                }
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Could not write KSwitchKeys", ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Saves the RelinKeys instance to an output stream.
        /// </summary>
        ///
        /// <remarks>
        /// Saves the RelinKeys instance to an output stream. The output is in binary format
        /// and not human-readable. The output stream must have the "binary" flag set.
        /// </remarks>
        /// <param name="stream">The stream to save the RelinKeys to</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <seealso cref="Load(SEALContext, Stream)">See Load() to load a saved RelinKeys instance.</seealso>
        public void Save(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
            {
                // Save ParmsId
                ParmsId.Save(writer.BaseStream);

                // Save the decomposition bit count
                writer.Write(DecompositionBitCount);

                // Save the size of Keys
                IEnumerable <IEnumerable <Ciphertext> > data = Data;
                writer.Write((ulong)data.LongCount());

                // Loop over entries in the first list
                foreach (IEnumerable <Ciphertext> keyList in data)
                {
                    writer.Write((ulong)keyList.LongCount());

                    // Loop over ciphertexts and save all
                    foreach (Ciphertext cipher in keyList)
                    {
                        cipher.Save(writer.BaseStream);
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Saves the ciphertext to an output stream. The output is in binary
        /// format and not human-readable. The output stream must have the
        /// "binary" flag set.
        /// </summary>
        /// <param name="stream">The stream to save the ciphertext to</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if the ciphertext could not be written to stream</exception>
        public void Save(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
                {
                    ParmsId.Save(writer.BaseStream);
                    writer.Write(IsNTTForm);
                    writer.Write(Size);
                    writer.Write(PolyModulusDegree);
                    writer.Write(CoeffModCount);

                    ulong ulongCount = checked (Size * PolyModulusDegree * CoeffModCount);
                    for (ulong i = 0; i < ulongCount; i++)
                    {
                        writer.Write(this[i]);
                    }
                }
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Could not write Ciphertext", ex);
            }
        }
Exemple #4
0
        /// <summary>
        /// Saves the plaintext to an output stream.
        /// </summary>
        ///
        /// <remarks>
        /// Saves the plaintext to an output stream. The output is in binary format and not human-readable.
        /// The output stream must have the "binary" flag set.
        /// </remarks>
        /// <param name="stream">The stream to save the plaintext to</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <seealso cref="Load(SEALContext, Stream)">See Load() to load a saved plaintext.</seealso>
        public void Save(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // First the ParmsId
            ParmsId.Save(stream);

            using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
            {
                writer.Write(Scale);
                writer.Write(CoeffCount);
                for (ulong i = 0; i < CoeffCount; i++)
                {
                    ulong data = this[i];
                    writer.Write(data);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Saves the KSwitchKeys instance to an output stream.
        /// </summary>
        /// <remarks>
        /// Saves the KSwitchKeys instance to an output stream. The output is in binary
        /// format and not human-readable. The output stream must have the "binary" flag set.
        /// </remarks>
        /// <param name="stream">The stream to save the KSwitchKeys to</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if the KSwitchKeys could not be written to stream</exception>
        public void Save(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
                {
                    // Save the ParmsId
                    ParmsId.Save(writer.BaseStream);

                    // Save the size of Keys
                    IEnumerable <IEnumerable <PublicKey> > data = Data;
                    writer.Write((ulong)data.LongCount());

                    // Loop over entries in the first list
                    foreach (IEnumerable <PublicKey> key in data)
                    {
                        writer.Write((ulong)key.LongCount());

                        // Loop over keys and save all
                        foreach (PublicKey pkey in key)
                        {
                            pkey.Save(writer.BaseStream);
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Could not write KSwitchKeys", ex);
            }
        }