public int[] Encode(int[] data, int eccCount) { GFPoly generator = GetPolynomial(eccCount); GFPoly info = new GFPoly(GaloisField, data); info = info.MultiplyByMonominal(eccCount, 1); (_, GFPoly remainder) = info.Divide(generator); int[] result = new int[eccCount]; int numZero = eccCount - remainder.Coefficients.Length; Array.Copy(remainder.Coefficients, 0, result, numZero, remainder.Coefficients.Length); return(result); }
public (GFPoly Quotient, GFPoly Remainder) Divide(GFPoly other) { GFPoly quotient = Zero(GaloisField); GFPoly remainder = this; int denomLeadTerm = other.GetCoefficient(other.Degree()); int inversDenomLeadTerm = GaloisField.Inverse(denomLeadTerm); while (remainder.Degree() >= other.Degree() && !remainder.IsZero()) { int degreeDiff = remainder.Degree() - other.Degree(); int scale = GaloisField.Multiply(remainder.GetCoefficient(remainder.Degree()), inversDenomLeadTerm); GFPoly term = other.MultiplyByMonominal(degreeDiff, scale); GFPoly itQuot = MonominalPoly(GaloisField, degreeDiff, scale); quotient = quotient.AddOrSubtract(itQuot); remainder = remainder.AddOrSubtract(term); } return(quotient, remainder); }