Пример #1
0
        public static IPolynomial SquareRoot(IPolynomial startPolynomial, IPolynomial f, BigInteger p, int degree, BigInteger m)
        {
            BigInteger q = BigInteger.Pow(p, degree);
            BigInteger s = q - 1;

            int r = 0;

            while (s.Mod(2) == 0)
            {
                s /= 2;
                r++;
            }

            BigInteger halfS = ((s + 1) / 2);

            if (r == 1 && q.Mod(4) == 3)
            {
                halfS = ((q + 1) / 4);
            }

            BigInteger quadraticNonResidue = Legendre.SymbolSearch(m + 1, q, -1);
            BigInteger theta    = quadraticNonResidue;
            BigInteger minusOne = BigInteger.ModPow(theta, ((q - 1) / 2), p);

            IPolynomial omegaPoly = Polynomial.Field.ExponentiateMod(startPolynomial, halfS, f, p);

            BigInteger lambda = minusOne;
            BigInteger zeta   = 0;

            int i = 0;

            do
            {
                i++;

                zeta = BigInteger.ModPow(theta, (i * s), p);

                lambda = (lambda * BigInteger.Pow(zeta, (int)Math.Pow(2, (r - i)))).Mod(p);

                omegaPoly = Polynomial.Field.Multiply(omegaPoly, BigInteger.Pow(zeta, (int)Math.Pow(2, ((r - i) - 1))), p);
            }while (!((lambda == 1) || (i > (r))));

            return(omegaPoly);
        }