private int[] FindErrorLocations(GenericGfPoly errorLocator)
        {
            int numErrors = errorLocator.Degree;

            if (numErrors == 1)
            {
                return(new[] { errorLocator.GetCoefficient(1) });
            }
            int[] result = new int[numErrors];
            int   e      = 0;

            for (int i = 1; i < field.Size && e < numErrors; i++)
            {
                if (errorLocator.EvaluateAt(i) == 0)
                {
                    result[e] = field.Inverse(i);
                    e++;
                }
            }
            if (e != numErrors)
            {
                return(null);
            }
            return(result);
        }
        private int[] FindErrorMagnitudes(GenericGfPoly errorEvaluator, int[] errorLocations)
        {
            int s = errorLocations.Length;

            int[] result = new int[s];
            for (int i = 0; i < s; i++)
            {
                int xiInverse   = field.Inverse(errorLocations[i]);
                int denominator = 1;
                for (int j = 0; j < s; j++)
                {
                    if (i != j)
                    {
                        int term      = field.Multiply(errorLocations[j], xiInverse);
                        int termPlus1 = (term & 0x1) == 0 ? term | 1 : term & ~1;
                        denominator = field.Multiply(denominator, termPlus1);
                    }
                }
                result[i] = field.Multiply(errorEvaluator.EvaluateAt(xiInverse), field.Inverse(denominator));
                if (field.GeneratorBase != 0)
                {
                    result[i] = field.Multiply(result[i], xiInverse);
                }
            }
            return(result);
        }
        public bool Decode(int[] received, int twoS)
        {
            var poly = new GenericGfPoly(field, received);
            var syndromeCoefficients = new int[twoS];
            var noError = true;

            for (var i = 0; i < twoS; i++)
            {
                var eval = poly.EvaluateAt(field.Exp(i + field.GeneratorBase));
                syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval;
                if (eval != 0)
                {
                    noError = false;
                }
            }
            if (noError)
            {
                return(true);
            }
            var syndrome = new GenericGfPoly(field, syndromeCoefficients);

            var sigmaOmega = RunEuclideanAlgorithm(field.BuildMonomial(twoS, 1), syndrome, twoS);

            if (sigmaOmega == null)
            {
                return(false);
            }

            var sigma          = sigmaOmega[0];
            var errorLocations = FindErrorLocations(sigma);

            if (errorLocations == null)
            {
                return(false);
            }

            var omega           = sigmaOmega[1];
            var errorMagnitudes = FindErrorMagnitudes(omega, errorLocations);

            for (var i = 0; i < errorLocations.Length; i++)
            {
                var position = received.Length - 1 - field.Log(errorLocations[i]);
                if (position < 0)
                {
                    return(false);
                }
                received[position] = GenericGf.AddOrSubtract(received[position], errorMagnitudes[i]);
            }

            return(true);
        }