public void testEncodeDecodeModQ()
 {
     int[]  coeffs  = PolynomialGenerator.GenerateRandom(1000, 2048).coeffs;
     byte[] data    = ArrayEncoder.EncodeModQ(coeffs, 2048);
     int[]  coeffs2 = ArrayEncoder.DecodeModQ(data, 1000, 2048);
     Assert.True(coeffs.SequenceEqual(coeffs2));
 }
        private void EncodeDecodeModQ()
        {
            int[]  coeffs  = PolynomialGeneratorForTesting.generateRandomPositive(1000, 2048).Coeffs;
            byte[] data    = ArrayEncoder.EncodeModQ(coeffs, 2048);
            int[]  coeffs2 = ArrayEncoder.DecodeModQ(data, 1000, 2048);

            if (!Compare.AreEqual(coeffs, coeffs2))
            {
                throw new Exception("ArrayEncoder EncodeDecodeModQ test failed!");
            }
        }
示例#3
0
        /**
         * Decodes a byte array encoded with {@link #toBinary()} to a ploynomial.
         *
         * @param is         an input stream containing an encoded polynomial
         * @param N          number of coefficients including zeros
         * @param numOnes    number of coefficients equal to 1
         * @param numNegOnes number of coefficients equal to -1
         * @return the decoded polynomial
         * @throws IOException
         */
        public static SparseTernaryPolynomial FromBinary(Stream stream, int N, int numOnes, int numNegOnes)
        {
            int maxIndex     = 1 << BITS_PER_INDEX;
            int bitsPerIndex = 32 - Integers.NumberOfLeadingZeros(maxIndex - 1);

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

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

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

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

            return(new SparseTernaryPolynomial(N, ones, negOnes));
        }
示例#4
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));
        }
示例#5
0
 /**
  * Returns a polynomial with N coefficients between <code>0</code> and <code>q-1</code>.<br>
  * <code>q</code> must be a power of 2.<br>
  * Ignores any excess bytes.
  *
  * @param is an encoded ternary polynomial
  * @param N  number of coefficients
  * @param q
  * @return the decoded polynomial
  */
 public static IntegerPolynomial FromBinary(Stream stream, int N, int q)
 {
     return(new IntegerPolynomial(ArrayEncoder.DecodeModQ(stream, N, q)));
 }
示例#6
0
 /**
  * Returns a polynomial with N coefficients between <code>0</code> and <code>q-1</code>.<br>
  * <code>q</code> must be a power of 2.<br>
  * Ignores any excess bytes.
  *
  * @param data an encoded ternary polynomial
  * @param N    number of coefficients
  * @param q
  * @return the decoded polynomial
  */
 public static IntegerPolynomial FromBinary(byte[] data, int N, int q)
 {
     return(new IntegerPolynomial(ArrayEncoder.DecodeModQ(data, N, q)));
 }