public void Encode(int[] toEncode, int ecBytes)
        {
            if (ecBytes == 0)
            {
                throw new ArgumentException("No error correction bytes");
            }
            int dataBytes = toEncode.Length - ecBytes;

            if (dataBytes <= 0)
            {
                throw new ArgumentException("No data bytes provided");
            }
            GF256Poly generator = BuildGenerator(ecBytes);

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

            info = info.MultiplyByMonomial(ecBytes, 1);
            GF256Poly remainder = info.Divide(generator)[1];

            int[] coefficients        = remainder.GetCoefficients();
            int   numZeroCoefficients = ecBytes - coefficients.Length;

            for (int i = 0; i < numZeroCoefficients; i++)
            {
                toEncode[dataBytes + i] = 0;
            }
            System.Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
        }
Exemple #2
0
        internal GF256Poly[] Divide(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GF256Polys do not have same GF256 field");
            }
            if (other.IsZero())
            {
                throw new DivideByZeroException("Divide by 0");
            }

            GF256Poly quotient  = field.GetZero();
            GF256Poly remainder = this;

            int denominatorLeadingTerm        = other.GetCoefficient(other.GetDegree());
            int inverseDenominatorLeadingTerm = field.Inverse(denominatorLeadingTerm);

            while (remainder.GetDegree() >= other.GetDegree() && !remainder.IsZero())
            {
                int       degreeDifference  = remainder.GetDegree() - other.GetDegree();
                int       scale             = field.Multiply(remainder.GetCoefficient(remainder.GetDegree()), inverseDenominatorLeadingTerm);
                GF256Poly term              = other.MultiplyByMonomial(degreeDifference, scale);
                GF256Poly iterationQuotient = field.BuildMonomial(degreeDifference, scale);
                quotient  = quotient.AddOrSubtract(iterationQuotient);
                remainder = remainder.AddOrSubtract(term);
            }

            return(new GF256Poly[] { quotient, remainder });
        }