Example #1
0
        /// <summary>
        /// The calculate method.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="numEcc">
        /// The number of <c>ecc</c> codes.
        /// </param>
        /// <returns>
        /// The <see cref="byte"/> array.
        /// </returns>
        public byte[] Calculate(Poly message, int numEcc)
        {
            // Get message poly coefficients
            int[] messageCoefficients = new int[message.Terms.Length];

            for (int i = 0; i < message.Terms.Length; i++)
            {
                messageCoefficients[i] = message.Terms[i].Coefficient;
            }

            // Convert message poly coefficients into Galois Field (get a coefficients from numbers)
            QrGaloisFieldPoly info = new QrGaloisFieldPoly(this.field, messageCoefficients);

            // Increase level of the poly by number of ECC
            info = info.MultiplyByMonomial(numEcc, 1);

            // Get generator poly
            QrGaloisFieldPoly generatorPoly = this.BuildGeneratorPolynomial(numEcc);

            // Divite by generator poly
            QrGaloisFieldPoly remainder = info.Divide(generatorPoly)[1];

            // Return remainder coefficients in bytes
            int[] coefficients = remainder.GetCoefficients();

            byte[] eccCodes = new byte[coefficients.Length];

            for (int i = 0; i < coefficients.Length; i++)
            {
                eccCodes[i] = Convert.ToByte(coefficients[i]);
            }

            return(eccCodes);
        }
Example #2
0
        /// <summary>
        /// The calculate method.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="numEcc">
        /// The number of <c>ecc</c> codes.
        /// </param>
        /// <returns>
        /// The <see cref="byte"/> array.
        /// </returns>
        public byte[] Calculate(Poly message, int numEcc)
        {
            // Get message poly coefficients
            int[] messageCoefficients = new int[message.Terms.Length];

            for (int i = 0; i < message.Terms.Length; i++)
            {
                messageCoefficients[i] = message.Terms[i].Coefficient;
            }

            // Convert message poly coefficients into Galois Field (get a coefficients from numbers)
            QrGaloisFieldPoly info = new QrGaloisFieldPoly(this.field, messageCoefficients);

            // Increase level of the poly by number of ECC
            info = info.MultiplyByMonomial(numEcc, 1);

            // Get generator poly
            QrGaloisFieldPoly generatorPoly = this.BuildGeneratorPolynomial(numEcc);

            // Divite by generator poly
            QrGaloisFieldPoly remainder = info.Divide(generatorPoly)[1];

            // Return remainder coefficients in bytes
            int[] coefficients = remainder.GetCoefficients();

            byte[] eccCodes = new byte[coefficients.Length];

            for (int i = 0; i < coefficients.Length; i++)
            {
                eccCodes[i] = Convert.ToByte(coefficients[i]);
            }

            return eccCodes;
        }
        /// <summary>
        /// The divide.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// The <see cref="QrGaloisFieldPoly" /> array.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Exception if division by zero.
        /// </exception>
        public QrGaloisFieldPoly[] Divide(QrGaloisFieldPoly other)
        {
            if (other.IsZero())
            {
                throw new ArgumentException("Divide by 0");
            }

            QrGaloisFieldPoly quotient  = this.field.Zero;
            QrGaloisFieldPoly remainder = this;

            int denominatorLeadingTerm        = other.GetCoefficient(other.GetPower());
            int inverseDenominatorLeadingTerm = this.field.Inverse(denominatorLeadingTerm);

            while ((remainder.GetPower() >= other.GetPower()) && !remainder.IsZero())
            {
                int powerDifference = remainder.GetPower() - other.GetPower();
                int scale           = this.field.Multiply(
                    remainder.GetCoefficient(remainder.GetPower()), inverseDenominatorLeadingTerm);

                QrGaloisFieldPoly term = other.MultiplyByMonomial(powerDifference, scale);
                QrGaloisFieldPoly iterationQuotient = this.field.BuildMonomial(powerDifference, scale);

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

            return(new[] { quotient, remainder });
        }
Example #4
0
        /// <summary>
        /// The divide.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// The <see cref="QrGaloisFieldPoly" /> array.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Exception if division by zero.
        /// </exception>
        public QrGaloisFieldPoly[] Divide(QrGaloisFieldPoly other)
        {
            if (other.IsZero())
            {
                throw new ArgumentException("Divide by 0");
            }

            QrGaloisFieldPoly quotient = this.field.Zero;
            QrGaloisFieldPoly remainder = this;

            int denominatorLeadingTerm = other.GetCoefficient(other.GetPower());
            int inverseDenominatorLeadingTerm = this.field.Inverse(denominatorLeadingTerm);

            while ((remainder.GetPower() >= other.GetPower()) && !remainder.IsZero())
            {
                int powerDifference = remainder.GetPower() - other.GetPower();
                int scale = this.field.Multiply(
                    remainder.GetCoefficient(remainder.GetPower()), inverseDenominatorLeadingTerm);

                QrGaloisFieldPoly term = other.MultiplyByMonomial(powerDifference, scale);
                QrGaloisFieldPoly iterationQuotient = this.field.BuildMonomial(powerDifference, scale);

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

            return new[] { quotient, remainder };
        }