示例#1
0
        /// <summary>
        /// Calculates the multiplicative inverse of <c>this</c> and returns the result in a new GF2nPolynomialElement
        /// </summary>
        ///
        /// <returns>Returns <c>this</c>^(-1)</returns>
        public GF2nPolynomialElement InvertSquare()
        {
            GF2nPolynomialElement n;
            GF2nPolynomialElement u;
            int i, j, k, b;

            if (IsZero())
            {
                throw new ArithmeticException();
            }

            // b = (n-1)
            b = mField.Degree - 1;
            // n = a
            n = new GF2nPolynomialElement(this);
            n.polynomial.ExpandN((mDegree << 1) + 32); // increase performance
            n.polynomial.ReduceN();
            // k = 1
            k = 1;

            // for i = (r-1) downto 0 do, r=bitlength(b)
            for (i = BigMath.FloorLog(b) - 1; i >= 0; i--)
            {
                // u = n
                u = new GF2nPolynomialElement(n);
                // for j = 1 to k do
                for (j = 1; j <= k; j++)
                {
                    u.SquareThisPreCalc(); // u = u^2
                }
                // n = nu
                n.MultiplyThisBy(u);
                // k = 2k
                k <<= 1;
                // if b(i)==1
                if ((b & _bitMask[i]) != 0)
                {
                    // n = n^2 * b
                    n.SquareThisPreCalc();
                    n.MultiplyThisBy(this);
                    // k = k+1
                    k += 1;
                }
            }

            // outpur n^2
            n.SquareThisPreCalc();

            return(n);
        }
示例#2
0
        /// <summary>
        /// Calculates <c>this</c> to the power of <c>K</c> and returns the result in a new GF2nPolynomialElement
        /// </summary>
        ///
        /// <param name="K">The power</param>
        ///
        /// <returns>Returns <c>this</c>^<c>K</c> in a new GF2nPolynomialElement</returns>
        public GF2nPolynomialElement Power(int K)
        {
            if (K == 1)
            {
                return(new GF2nPolynomialElement(this));
            }

            GF2nPolynomialElement result = GF2nPolynomialElement.One((GF2nPolynomialField)mField);

            if (K == 0)
            {
                return(result);
            }

            GF2nPolynomialElement x = new GF2nPolynomialElement(this);

            x.polynomial.ExpandN((x.mDegree << 1) + 32); // increase performance
            x.polynomial.ReduceN();

            for (int i = 0; i < mDegree; i++)
            {
                if ((K & (1 << i)) != 0)
                {
                    result.MultiplyThisBy(x);
                }

                x.Square();
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// Compute the product of this element and <c>factor</c>
        /// </summary>
        ///
        /// <param name="Factor">he factor</param>
        ///
        /// <returns>Returns <c>this * factor</c> </returns>
        public override IGFElement Multiply(IGFElement Factor)
        {
            GF2nPolynomialElement result = new GF2nPolynomialElement(this);

            result.MultiplyThisBy(Factor);

            return(result);
        }
        /// <summary>
        /// Compute the product of this element and <c>factor</c>
        /// </summary>
        /// 
        /// <param name="Factor">he factor</param>
        /// 
        /// <returns>Returns <c>this * factor</c> </returns>
        public override IGFElement Multiply(IGFElement Factor)
        {
            GF2nPolynomialElement result = new GF2nPolynomialElement(this);
            result.MultiplyThisBy(Factor);

            return result;
        }
        /// <summary>
        /// Calculates the multiplicative inverse of <c>this</c> and returns the result in a new GF2nPolynomialElement
        /// </summary>
        /// 
        /// <returns>Returns <c>this</c>^(-1)</returns>
        public GF2nPolynomialElement InvertSquare()
        {
            GF2nPolynomialElement n;
            GF2nPolynomialElement u;
            int i, j, k, b;

            if (IsZero())
                throw new ArithmeticException();

            // b = (n-1)
            b = mField.Degree - 1;
            // n = a
            n = new GF2nPolynomialElement(this);
            n.polynomial.ExpandN((mDegree << 1) + 32); // increase performance
            n.polynomial.ReduceN();
            // k = 1
            k = 1;

            // for i = (r-1) downto 0 do, r=bitlength(b)
            for (i = BigMath.FloorLog(b) - 1; i >= 0; i--)
            {
                // u = n
                u = new GF2nPolynomialElement(n);
                // for j = 1 to k do
                for (j = 1; j <= k; j++)
                    u.SquareThisPreCalc(); // u = u^2

                // n = nu
                n.MultiplyThisBy(u);
                // k = 2k
                k <<= 1;
                // if b(i)==1
                if ((b & _bitMask[i]) != 0)
                {
                    // n = n^2 * b
                    n.SquareThisPreCalc();
                    n.MultiplyThisBy(this);
                    // k = k+1
                    k += 1;
                }
            }

            // outpur n^2
            n.SquareThisPreCalc();

            return n;
        }