Example #1
0
        public static IPolynomial Pow(IPolynomial poly, int exponent)
        {
            if (exponent < 0)
            {
                throw new NotImplementedException("Raising a polynomial to a negative exponent not supported. Build this functionality if it is needed.");
            }
            else if (exponent == 0)
            {
                return(new Polynomial(new Term[] { new Term(1, 0) }));
            }
            else if (exponent == 1)
            {
                return(poly.Clone());
            }
            else if (exponent == 2)
            {
                return(Square(poly));
            }

            IPolynomial total = Polynomial.Square(poly);

            int counter = exponent - 2;

            while (counter != 0)
            {
                total    = Polynomial.Multiply(total, poly);
                counter -= 1;
            }

            return(total);
        }
            public static IPolynomial ExponentiateMod(IPolynomial startPoly, BigInteger s2, IPolynomial f, BigInteger p)
            {
                IPolynomial result = Polynomial.One;

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

                IPolynomial A = startPoly.Clone();

                byte[] byteArray = s2.ToByteArray();
                bool[] bitArray  = new BitArray(byteArray).Cast <bool>().ToArray();

                // Remove trailing zeros ?
                if (bitArray[0] == true)
                {
                    result = startPoly;
                }

                int i = 1;
                int t = bitArray.Length;

                while (i < t)
                {
                    A = Field.ModMod(Polynomial.Square(A), f, p);
                    if (bitArray[i] == true)
                    {
                        result = Field.ModMod(Polynomial.Multiply(A, result), f, p);
                    }
                    i++;
                }

                return(result);
            }
            public static IPolynomial ModPow(IPolynomial poly, BigInteger exponent, IPolynomial mod)
            {
                if (exponent < 0)
                {
                    throw new NotImplementedException("Raising a polynomial to a negative exponent not supported. Build this functionality if it is needed.");
                }
                else if (exponent == 0)
                {
                    return(Polynomial.One);
                }
                else if (exponent == 1)
                {
                    return(poly.Clone());
                }
                else if (exponent == 2)
                {
                    return(Polynomial.Square(poly));
                }

                IPolynomial total = Polynomial.Square(poly);

                BigInteger counter = exponent - 2;

                while (counter != 0)
                {
                    total = Polynomial.Multiply(poly, total);

                    if (total.CompareTo(mod) < 0)
                    {
                        total = Field.Modulus(total, mod);
                    }

                    counter -= 1;
                }

                return(total);
            }