Пример #1
0
        private int[] findErrorLocations(GenericGFPoly errorLocator)
        {
            // This is a direct application of Chien's search
            int numErrors = errorLocator.Degree;

            if (numErrors == 1)
            {
                // shortcut
                return(new int[] { 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)
            {
                // throw new ReedSolomonException("Error locator degree does not match number of roots");
                return(null);
            }
            return(result);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
0
        internal GenericGFPoly[] divide(GenericGFPoly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }
            if (other.isZero)
            {
                throw new ArgumentException("Divide by 0", "other");
            }

            GenericGFPoly quotient  = new GenericGFPoly(field, new int[] { 0 }, encoding);
            GenericGFPoly remainder = this;

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

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

            return(new GenericGFPoly[] { quotient, remainder });
        }
Пример #5
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));
        }
Пример #6
0
        // Method added by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
        private GenericGFPoly findErrataLocator(int[] errorPositions)
        {
            GenericGFPoly errataLocator = new GenericGFPoly(field, new int[] { 1 }, false);

            foreach (int i in errorPositions)
            {
                errataLocator = errataLocator.multiply(new GenericGFPoly(field, new int[] { 1 }, false).addOrSubtract(new GenericGFPoly(field, new int[] { field.exp(i), 0 }, false)));
            }

            return(errataLocator);
        }
Пример #7
0
        // Method added by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
        private GenericGFPoly findErrorEvaluator(GenericGFPoly syndrome, GenericGFPoly errataLocations)
        {
            int[] product = syndrome.multiply(errataLocations).Coefficients;

            int[] target = new int[errataLocations.Coefficients.Length - 1];

            Array.Copy(product, product.Length - errataLocations.Coefficients.Length + 1, target, 0, target.Length);

            if (target.Length == 0)
            {
                return(null);
            }

            GenericGFPoly omega = new GenericGFPoly(field, target, false);

            return(omega);
        }
Пример #8
0
        /// <summary>
        /// Encodes given set of data codewords with Reed-Solomon.
        /// </summary>
        /// <param name="toEncode">data codewords and padding, the amount of padding should match
        /// the number of error-correction codewords to generate. After encoding, the padding is
        /// replaced with the error-correction codewords</param>
        /// <param name="ecBytes">number of error-correction codewords to generate</param>
        public void Encode(int[] toEncode, int ecBytes)
        {
            // Method modified by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
            // added check for messages that are too long for the used Galois field

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

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

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

            var generator        = buildGenerator(ecBytes);
            var infoCoefficients = new int[dataBytes];

            Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);

            var info = new GenericGFPoly(field, infoCoefficients, true);

            info = info.multiplyByMonomial(ecBytes, 1);

            var remainder           = info.divide(generator)[1];
            var coefficients        = remainder.Coefficients;
            var numZeroCoefficients = ecBytes - coefficients.Length;

            for (var i = 0; i < numZeroCoefficients; i++)
            {
                toEncode[dataBytes + i] = 0;
            }

            Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
        }
Пример #9
0
        // Method added by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123)
        // this method replaces original method "runEuclideanAlgorithm"
        internal GenericGFPoly runBerlekampMasseyAlgorithm(GenericGFPoly syndrome, int erasureCount)
        {
            GenericGFPoly sigma = new GenericGFPoly(field, new int[] { 1 }, false);
            GenericGFPoly old   = new GenericGFPoly(field, new int[] { 1 }, false);

            for (int i = 0; i < (syndrome.Coefficients.Length - erasureCount); i++)
            {
                int delta = syndrome.getCoefficient(i);
                for (int j = 1; j < sigma.Coefficients.Length; j++)
                {
                    delta ^= field.multiply(sigma.getCoefficient(j), syndrome.getCoefficient(i - j));
                }

                List <int> oldList = new List <int>(old.Coefficients);
                oldList.Add(0);
                old = new GenericGFPoly(field, oldList.ToArray(), false);

                if (delta != 0)
                {
                    if (old.Coefficients.Length > sigma.Coefficients.Length)
                    {
                        GenericGFPoly new_loc = old.multiply(delta);
                        old   = sigma.multiply(field.inverse(delta));
                        sigma = new_loc;
                    }

                    sigma = sigma.addOrSubtract(old.multiply(delta));
                }
            }

            List <int> sigmaList = new List <int>(sigma.Coefficients);

            while (Convert.ToBoolean(sigmaList.Count) && sigmaList[0] == 0)
            {
                sigmaList.RemoveAt(0);
            }

            sigma = new GenericGFPoly(field, sigmaList.ToArray(), false);

            return(sigma);
        }
Пример #10
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));
        }
Пример #11
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);
        }