private int[] findErrorLocations(GF256Poly 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 < 256 && 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(result);
        }
Example #2
0
        public GF256Poly[] divide(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GF256Polys do not have same GF256 field");
            }
            if (other.isZero())
            {
                throw new ArgumentException("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 });
        }
Example #3
0
		internal GF256Poly[] divide(GF256Poly other)
		{
			if (!field.Equals(other.field))
			{
				throw new System.ArgumentException("GF256Polys do not have same GF256 field");
			}
			if (other.Zero)
			{
				throw new System.ArgumentException("Divide by 0");
			}
			
			GF256Poly quotient = field.Zero;
			GF256Poly remainder = this;
			
			int denominatorLeadingTerm = other.getCoefficient(other.Degree);
			int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm);
			
			while (remainder.Degree >= other.Degree && !remainder.Zero)
			{
				int degreeDifference = remainder.Degree - other.Degree;
				int scale = field.multiply(remainder.getCoefficient(remainder.Degree), 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};
		}
        private GF256Poly[] runEuclideanAlgorithm(GF256Poly a, GF256Poly b, int R)
        {
            // Assume a's degree is >= b's
            if (a.Degree < b.Degree)
            {
                GF256Poly temp = a;
                a = b;
                b = temp;
            }

            GF256Poly rLast = a;
            GF256Poly r     = b;
            GF256Poly sLast = field.One;
            GF256Poly s     = field.Zero;
            GF256Poly tLast = field.Zero;
            GF256Poly t     = field.One;

            // Run Euclidean algorithm until r's degree is less than R/2
            while (r.Degree >= R / 2)
            {
                GF256Poly rLastLast = rLast;
                GF256Poly sLastLast = sLast;
                GF256Poly tLastLast = tLast;
                rLast = r;
                sLast = s;
                tLast = t;

                // Divide rLastLast by rLast, with quotient in q and remainder in r
                if (rLast.Zero)
                {
                    // Oops, Euclidean algorithm already terminated?
                    throw new ReedSolomonException("r_{i-1} was zero");
                }
                r = rLastLast;
                GF256Poly q = field.Zero;
                int       denominatorLeadingTerm = rLast.getCoefficient(rLast.Degree);
                int       dltInverse             = field.inverse(denominatorLeadingTerm);
                while (r.Degree >= rLast.Degree && !r.Zero)
                {
                    int degreeDiff = r.Degree - rLast.Degree;
                    int scale      = field.multiply(r.getCoefficient(r.Degree), dltInverse);
                    q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale));
                    r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale));
                }

                s = q.multiply(sLast).addOrSubtract(sLastLast);
                t = q.multiply(tLast).addOrSubtract(tLastLast);
            }

            int sigmaTildeAtZero = t.getCoefficient(0);

            if (sigmaTildeAtZero == 0)
            {
                throw new ReedSolomonException("sigmaTilde(0) was zero");
            }

            int       inverse = field.inverse(sigmaTildeAtZero);
            GF256Poly sigma   = t.multiply(inverse);
            GF256Poly omega   = r.multiply(inverse);

            return(new GF256Poly[] { sigma, omega });
        }
		private int[] findErrorLocations(GF256Poly 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 < 256 && 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 result;
		}