Exemplo n.º 1
0
        internal GenericGFPoly Multiply(GenericGFPoly other)
        {
            if (field.Equals(other.field) == false)
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }

            if (IsZero || other.IsZero)
            {
                return(field.Zero);
            }

            int[] aCoefficients = 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));
        }
Exemplo n.º 2
0
        internal GenericGFPoly[] Divide(GenericGFPoly other)
        {
            if (field.Equals(other.field) == false)
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }

            if (other.IsZero)
            {
                throw new ArgumentException("Divide by 0");
            }

            GenericGFPoly quotient  = field.Zero;
            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 = field.BuildMonomial(degreeDifference, scale);

                quotient  = quotient.AddOrSubtract(iterationQuotient);
                remainder = remainder.AddOrSubtract(term);
            }

            return(new GenericGFPoly[] { quotient, remainder });
        }
Exemplo n.º 3
0
        // this method has been added by Sebastien ROBERT
        // this implementation makes the mathematician-friendly approach programmer-friendly
        public byte[] EncodeEx(byte[] 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");
            }

            GenericGFPoly generator = BuildGenerator(ecBytes);

            int[] infoCoefficients = toEncode.Select(x => (int)x).ToArray();

            var info = new GenericGFPoly(field, infoCoefficients);

            info = info.MultiplyByMonomial(ecBytes, 1);

            GenericGFPoly remainder = info.Divide(generator)[1];

            int[] coefficients        = remainder.Coefficients;
            int   numZeroCoefficients = ecBytes - coefficients.Length;

            return(Enumerable.Repeat <byte>(0, numZeroCoefficients)
                   .Concat(coefficients.Select(x => (byte)x))
                   .ToArray());
        }
Exemplo n.º 4
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");
            }

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

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

            var info = new GenericGFPoly(field, infoCoefficients);

            info = info.MultiplyByMonomial(ecBytes, 1);

            GenericGFPoly remainder = info.Divide(generator)[1];

            int[] coefficients        = remainder.Coefficients;
            int   numZeroCoefficients = ecBytes - coefficients.Length;

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

            Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length);
        }
Exemplo n.º 5
0
            private void Initialize()
            {
                expTable = new int[size];
                logTable = new int[size];

                int x = 1;

                for (int i = 0; i < size; i++)
                {
                    expTable[i] = x;
                    x         <<= 1; // x = x * 2; we're assuming the generator alpha is 2
                    if (x >= size)
                    {
                        x ^= primitive;
                        x &= size - 1;
                    }
                }

                for (int i = 0; i < size - 1; i++)
                {
                    logTable[expTable[i]] = i;
                }

                // logTable[0] == 0 but this should never be used
                zero = new GenericGFPoly(this, new int[] { 0 });
                one  = new GenericGFPoly(this, new int[] { 1 });

                initialized = true;
            }
Exemplo n.º 6
0
        private GenericGFPoly BuildGenerator(int degree)
        {
            if (degree >= cachedGenerators.Count)
            {
                GenericGFPoly lastGenerator = cachedGenerators[cachedGenerators.Count - 1];

                for (int d = cachedGenerators.Count; d <= degree; d++)
                {
                    var nextGenerator = lastGenerator.Multiply(new GenericGFPoly(field, new int[] { 1, field.Exp(d - 1 + field.GeneratorBase) }));
                    cachedGenerators.Add(nextGenerator);
                    lastGenerator = nextGenerator;
                }
            }

            return(cachedGenerators[degree]);
        }
Exemplo n.º 7
0
        internal GenericGFPoly AddOrSubtract(GenericGFPoly other)
        {
            if (field.Equals(other.field) == false)
            {
                throw new ArgumentException("GenericGFPolys do not have same GenericGF field");
            }

            if (IsZero)
            {
                return(other);
            }

            if (other.IsZero)
            {
                return(this);
            }

            int[] smallerCoefficients = 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));
        }