(a, p)
return x
Returns the square root in the case of ``p`` prime with ``p == 1 (mod 8)``

    References
    ==========

    R. Crandall and C. Pomerance "Prime Numbers", 2nt Ed., page 101
Returns the square root in the case of ``p`` prime with ``p == 1 (mod 8)``
Returns the square root in the case of p prime with p == 1 ( mod 8 )
def _sqrt_mod_tonelli_shanks(a, p):
    """
    Returns the square root in the case of ``p`` prime with ``p == 1 (mod 8)``

    References
    ==========

    R. Crandall and C. Pomerance "Prime Numbers", 2nt Ed., page 101
    """
    s = trailing(p - 1)
    t = p >> s
    # find a non-quadratic residue
    while 1:
        d = randint(2, p - 1)
        r = legendre_symbol(d, p)
        if r == -1:
            break
    #assert legendre_symbol(d, p) == -1
    A = pow(a, t, p)
    D = pow(d, t, p)
    m = 0
    for i in xrange(s):
        adm = A*pow(D, m, p) % p
        adm = pow(adm, 2**(s - 1 - i), p)
        if adm % p == p - 1:
            m += 2**i
    #assert A*pow(D, m, p) % p == 1
    x = pow(a, (t + 1)//2, p)*pow(D, m//2, p) % p
    return x
def _sqrt_mod_tonelli_shanks ( a , p ) : s = trailing ( p - 1 ) t = p >> s # find a non-quadratic residue while 1 : d = randint ( 2 , p - 1 ) r = legendre_symbol ( d , p ) if r == - 1 : break #assert legendre_symbol(d, p) == -1 A = pow ( a , t , p ) D = pow ( d , t , p ) m = 0 for i in xrange ( s ) : adm = A * pow ( D , m , p ) % p adm = pow ( adm , 2 ** ( s - 1 - i ) , p ) if adm % p == p - 1 : m += 2 ** i #assert A*pow(D, m, p) % p == 1 x = pow ( a , ( t + 1 ) // 2 , p ) * pow ( D , m // 2 , p ) % p return x