Example #1
0
        internal GenericGFPoly[] divide(GenericGFPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }
            if (other.Zero)
            {
                throw new ArgumentException("Divide by 0");
            }

            GenericGFPoly quotient  = field.Zero;
            GenericGFPoly remainder = this;

            int denominatorLeadingTerm        = other.getCoefficient(other.Degree);
            int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm);

            while (remainder.Degree >= other.Degree && !remainder.Zero)
            {
                int           degreeDifference  = remainder.Degree - other.Degree;
                int           scale             = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
                GenericGFPoly term              = other.multiplyByMonomial(degreeDifference, scale);
                GenericGFPoly iterationQuotient = field.buildMonomial(degreeDifference, scale);
                quotient  = quotient.addOrSubtract(iterationQuotient);
                remainder = remainder.addOrSubtract(term);
            }

            return(new GenericGFPoly[] { quotient, remainder });
        }
Example #2
0
        public void encode(int[] toEncode, int ecBytes)
        {
            if (ecBytes == 0)
            {
                throw new System.ArgumentException("No error correction bytes");
            }
            int dataBytes = toEncode.Length - ecBytes;

            if (dataBytes <= 0)
            {
                throw new System.ArgumentException("No data bytes provided");
            }
            GenericGFPoly generator = buildGenerator(ecBytes);

            int[] infoCoefficients = new int[dataBytes];
            Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);
            GenericGFPoly info = new GenericGFPoly(field, infoCoefficients);

            info = info.multiplyByMonomial(ecBytes, 1);
            GenericGFPoly remainder = info.divide(generator)[1];

            int[] coefficients        = remainder.Coefficients;
            int   numZeroCoefficients = ecBytes - coefficients.Length;

            for (int i = 0; i < numZeroCoefficients; i++)
            {
                toEncode[dataBytes + i] = 0;
            }
            Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
        }