Exemple #1
0
        /// <summary>
        /// Decodes a polynomial encoded with ToBinary()
        /// </summary>
        ///
        /// <param name="InputStream">An input stream containing an encoded polynomial</param>
        /// <param name="N">Number of coefficients in the polynomial</param>
        ///
        /// <returns>The decoded polynomial</returns>
        public static SparseTernaryPolynomial FromBinary(MemoryStream InputStream, int N)
        {
            BinaryReader br = new BinaryReader(InputStream);
            // number of coefficients equal to 1
            int numOnes = IntUtils.ReadShort(InputStream);
            // number of coefficients equal to -1
            int numNegOnes   = IntUtils.ReadShort(InputStream);
            int maxIndex     = 1 << BITS_PER_INDEX;
            int bitsPerIndex = 32 - IntUtils.NumberOfLeadingZeros(maxIndex - 1);

            int data1Len = (numOnes * bitsPerIndex + 7) / 8;

            byte[] data1 = ArrayEncoder.ReadFullLength(InputStream, data1Len);
            int[]  ones  = ArrayEncoder.DecodeModQ(data1, numOnes, maxIndex);

            int data2Len = (numNegOnes * bitsPerIndex + 7) / 8;

            byte[] data2   = ArrayEncoder.ReadFullLength(InputStream, data2Len);
            int[]  negOnes = ArrayEncoder.DecodeModQ(data2, numNegOnes, maxIndex);

            return(new SparseTernaryPolynomial(N, ones, negOnes));
        }