AddOrSubtract() static private méthode

static private AddOrSubtract ( int a, int b ) : int
a int
b int
Résultat int
Exemple #1
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());
            }
            var aCoefficients = coefficients;
            var aLength       = aCoefficients.Length;
            var bCoefficients = other.coefficients;
            var bLength       = bCoefficients.Length;
            var product       = new int[aLength + bLength - 1];

            for (var i = 0; i < aLength; i++)
            {
                var aCoeff = aCoefficients[i];
                for (var j = 0; j < bLength; j++)
                {
                    product[i + j] = GF256.AddOrSubtract(product[i + j],
                                                         field.Multiply(aCoeff, bCoefficients[j]));
                }
            }
            return(new GF256Poly(field, product));
        }
Exemple #2
0
        /**
         * @return evaluation of this polynomial at a given point
         */
        internal int EvaluateAt(int a)
        {
            if (a == 0)
            {
                // Just return the x^0 coefficient
                return(GetCoefficient(0));
            }
            var size = coefficients.Length;

            if (a == 1)
            {
                // Just the sum of the coefficients
                var result2 = 0;
                for (var i = 0; i < size; i++)
                {
                    result2 = GF256.AddOrSubtract(result2, coefficients[i]);
                }
                return(result2);
            }
            var result = coefficients[0];

            for (var i = 1; i < size; i++)
            {
                result = GF256.AddOrSubtract(field.Multiply(a, result), coefficients[i]);
            }
            return(result);
        }
Exemple #3
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);
            }

            var smallerCoefficients = coefficients;
            var largerCoefficients  = other.coefficients;

            if (smallerCoefficients.Length > largerCoefficients.Length)
            {
                var temp = smallerCoefficients;
                smallerCoefficients = largerCoefficients;
                largerCoefficients  = temp;
            }
            var sumDiff    = new int[largerCoefficients.Length];
            var 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 (var i = lengthDiff; i < largerCoefficients.Length; i++)
            {
                sumDiff[i] = GF256.AddOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
            }

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