Example #1
0
        public void Encode(int[] toEncode, int ecBytes)
        {
            if (ecBytes == 0)
            {
                throw new ArgumentException("No error correction bytes");
            }
            int dataBytes = toEncode.Length - ecBytes;

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

            int[] infoCoefficients = new int[dataBytes];
            System.Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes);
            GF256Poly info = new GF256Poly(field, infoCoefficients);

            info = info.MultiplyByMonomial(ecBytes, 1);
            GF256Poly remainder = info.Divide(generator)[1];

            int[] coefficients        = remainder.GetCoefficients();
            int   numZeroCoefficients = ecBytes - coefficients.Length;

            for (int i = 0; i < numZeroCoefficients; i++)
            {
                toEncode[dataBytes + i] = 0;
            }
            System.Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
        }
Example #2
0
        internal GF256Poly[] Divide(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GF256Polys do not have same GF256 field");
            }
            if (other.IsZero())
            {
                throw new DivideByZeroException("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 Multiply(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GF256Polys do not have same GF256 field");
            }
            if (IsZero() || other.IsZero())
            {
                return(field.GetZero());
            }
            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] = GF256.AddOrSubtract(product[i + j],
                                                         field.Multiply(aCoeff, bCoefficients[j]));
                }
            }
            return(new GF256Poly(field, product));
        }
Example #4
0
 private GF256Poly BuildGenerator(int degree)
 {
     if (degree >= cachedGenerators.Count)
     {
         GF256Poly lastGenerator = cachedGenerators[cachedGenerators.Count - 1];
         for (int d = cachedGenerators.Count; d <= degree; d++)
         {
             GF256Poly nextGenerator = lastGenerator.Multiply(new GF256Poly(field, new int[] { 1, field.Exp(d - 1) }));
             cachedGenerators.Add(nextGenerator);
             lastGenerator = nextGenerator;
         }
     }
     return(cachedGenerators[degree]);
 }
Example #5
0
        /**
         * Create a representation of GF(256) using the given primitive polynomial.
         *
         * @param primitive irreducible polynomial whose coefficients are represented by
         *  the bits of an int, where the least-significant bit represents the constant
         *  coefficient
         */
        private GF256(int primitive)
        {
            expTable = new int[256];
            logTable = new int[256];
            int x = 1;

            for (int i = 0; i < 256; i++)
            {
                expTable[i] = x;
                x         <<= 1; // x = x * 2; we're assuming the generator alpha is 2
                if (x >= 0x100)
                {
                    x ^= primitive;
                }
            }
            for (int i = 0; i < 255; i++)
            {
                logTable[expTable[i]] = i;
            }
            // logTable[0] == 0 but this should never be used
            zero = new GF256Poly(this, new int[] { 0 });
            one  = new GF256Poly(this, new int[] { 1 });
        }
Example #6
0
        internal GF256Poly AddOrSubtract(GF256Poly other)
        {
            if (!field.Equals(other.field))
            {
                throw new ArgumentException("GF256Polys do not have same GF256 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
            System.Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);

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

            return(new GF256Poly(field, sumDiff));
        }