Пример #1
0
        // Method modified by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
        // added missing "if (denominator == 0)" check
        private int[] findErrorMagnitudes(GenericGFPoly errorEvaluator, int[] errorLocations)
        {
            // This is directly applying Forney's Formula
            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)
                    {
                        denominator = field.multiply(denominator, GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)));
                    }
                }

                if (denominator == 0)
                {
                    return(null);
                }

                result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator));
                if (field.GeneratorBase != 0)
                {
                    result[i] = field.multiply(result[i], xiInverse);
                }
            }
            return(result);
        }
Пример #2
0
        // Method added by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
        internal GenericGFPoly calculateForneySyndromes(GenericGFPoly syndromes, int[] positions, int messageLength)
        {
            int[] positionsReversed = new int[positions.Length];

            for (int i = 0; i < positions.Length; i++)
            {
                positionsReversed[i] = messageLength - 1 - positions[i];
            }

            int forneySyndromesLength = syndromes.Coefficients.Length;

            int[] syndromeCoefficients = new int[syndromes.Coefficients.Length];
            Array.Copy(syndromes.Coefficients, 0, syndromeCoefficients, 0, syndromes.Coefficients.Length);

            GenericGFPoly forneySyndromes = new GenericGFPoly(field, syndromeCoefficients, false);

            for (int i = 0; i < positions.Length; i++)
            {
                int x = field.exp(positionsReversed[i]);
                for (int j = 0; j < forneySyndromes.Coefficients.Length - 1; j++)
                {
                    forneySyndromes.Coefficients[forneySyndromesLength - j - 1] = GenericGF.addOrSubtract(field.multiply(forneySyndromes.getCoefficient(j), x), forneySyndromes.getCoefficient(j + 1));
                }
            }

            return(forneySyndromes);
        }
Пример #3
0
        internal GenericGFPoly multiply(GenericGFPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }
            if (isZero || other.isZero)
            {
                return(new GenericGFPoly(field, new int[] { 0 }, encoding));
            }
            int[] aCoefficients = this.coefficients;
            int   aLength       = aCoefficients.Length;

            int[] bCoefficients = other.coefficients;
            int   bLength       = bCoefficients.Length;

            int[] product = new int[aLength + bLength - 1];
            for (int i = 0; i < aLength; i++)
            {
                int aCoeff = aCoefficients[i];
                for (int j = 0; j < bLength; j++)
                {
                    product[i + j] = GenericGF.addOrSubtract(product[i + j],
                                                             field.multiply(aCoeff, bCoefficients[j]));
                }
            }
            return(new GenericGFPoly(field, product, encoding));
        }
Пример #4
0
        /// <summary>
        /// evaluation of this polynomial at a given point
        /// </summary>
        /// <param name="a">A.</param>
        /// <returns>evaluation of this polynomial at a given point</returns>
        internal int evaluateAt(int a)
        {
            int result = 0;

            if (a == 0)
            {
                // Just return the x^0 coefficient
                return(getCoefficient(0));
            }
            int size = coefficients.Length;

            if (a == 1)
            {
                // Just the sum of the coefficients
                foreach (var coefficient in coefficients)
                {
                    result = GenericGF.addOrSubtract(result, coefficient);
                }
                return(result);
            }
            result = coefficients[0];
            for (int i = 1; i < size; i++)
            {
                result = GenericGF.addOrSubtract(field.multiply(a, result), coefficients[i]);
            }
            return(result);
        }
Пример #5
0
        internal GenericGFPoly addOrSubtract(GenericGFPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }
            if (isZero)
            {
                return(other);
            }
            if (other.isZero)
            {
                return(this);
            }

            int[] smallerCoefficients = this.coefficients;
            int[] largerCoefficients  = other.coefficients;
            if (smallerCoefficients.Length > largerCoefficients.Length)
            {
                int[] temp = smallerCoefficients;
                smallerCoefficients = largerCoefficients;
                largerCoefficients  = temp;
            }
            int[] sumDiff    = new int[largerCoefficients.Length];
            int   lengthDiff = largerCoefficients.Length - smallerCoefficients.Length;

            // Copy high-order terms only found in higher-degree polynomial's coefficients
            Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);

            for (int i = lengthDiff; i < largerCoefficients.Length; i++)
            {
                sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
            }

            return(new GenericGFPoly(field, sumDiff, encoding));
        }
Пример #6
0
        /// <summary>
        ///   <p>Decodes given set of received codewords, which include both data and error-correction
        /// codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place,
        /// in the input.</p>
        /// </summary>
        /// <param name="received">data and error-correction codewords</param>
        /// <param name="twoS">number of error-correction codewords available</param>
        /// <param name="erasurePos">array of zero-based erasure indices</param>
        /// <returns>false: decoding fails</returns>
        public bool Decode(int[] received, int twoS, int[] erasurePos)
        {
            // Method modified by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
            // to add support for erasure and errata correction
            // most code ported to C# from the python code at http://en.wikiversity.org/wiki/Reed–Solomon_codes_for_coders

            if (received.Length >= field.Size)
            {
                throw new ArgumentException("Message is too long for this field", "received");
            }

            if (twoS <= 0)
            {
                throw new ArgumentException("No error correction bytes provided", "twoS");
            }
            var dataBytes = received.Length - twoS;

            if (dataBytes <= 0)
            {
                throw new ArgumentException("No data bytes provided", "twoS");
            }

            var syndromeCoefficients = new int[twoS];
            var noError = true;

            if (erasurePos == null)
            {
                erasurePos = new int[] { };
            }
            else
            {
                for (var i = 0; i < erasurePos.Length; i++)
                {
                    received[erasurePos[i]] = 0;
                }
            }

            if (erasurePos.Length > twoS)
            {
                return(false);
            }

            var poly = new GenericGFPoly(field, received, false);

            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, false);

            var forneySyndrome = calculateForneySyndromes(syndrome, erasurePos, received.Length);

            var sigma = runBerlekampMasseyAlgorithm(forneySyndrome, erasurePos.Length);

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

            var errorLocations = findErrorLocations(sigma);

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

            // Prepare errors
            int[] errorPositions = new int[errorLocations.Length];

            for (int i = 0; i < errorLocations.Length; i++)
            {
                errorPositions[i] = field.log(errorLocations[i]);
            }

            // Prepare erasures
            int[] erasurePositions = new int[erasurePos.Length];

            for (int i = 0; i < erasurePos.Length; i++)
            {
                erasurePositions[i] = received.Length - 1 - erasurePos[i];
            }

            // Combine errors and erasures
            int[] errataPositions = new int[errorPositions.Length + erasurePositions.Length];

            Array.Copy(errorPositions, 0, errataPositions, 0, errorPositions.Length);
            Array.Copy(erasurePositions, 0, errataPositions, errorPositions.Length, erasurePositions.Length);

            var errataLocator = findErrataLocator(errataPositions);
            var omega         = findErrorEvaluator(syndrome, errataLocator);

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

            int[] errata = new int[errataPositions.Length];

            for (int i = 0; i < errataPositions.Length; i++)
            {
                errata[i] = field.exp(errataPositions[i]);
            }

            var errorMagnitudes = findErrorMagnitudes(omega, errata);

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

            for (var i = 0; i < errata.Length; i++)
            {
                var position = received.Length - 1 - field.log(errata[i]);
                if (position < 0)
                {
                    // throw new ReedSolomonException("Bad error location");
                    return(false);
                }
                received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i]);
            }

            var checkPoly = new GenericGFPoly(field, received, false);

            var error = false;

            for (var i = 0; i < twoS; i++)
            {
                var eval = checkPoly.evaluateAt(field.exp(i + field.GeneratorBase));
                if (eval != 0)
                {
                    error = true;
                }
            }
            if (error)
            {
                return(false);
            }

            return(true);
        }