bridge/class-master/depend/pari/src/basemath/arith1.c
/* assume a reduced mod p [ otherwise correct but inefficient ] */
assume a reduced mod p [ otherwise correct but inefficient ]
assume a reduced mod p [ otherwise correct but inefficient ]
static GEN
sqrt_Cipolla(GEN a, GEN p)
{
  pari_sp av1;
  GEN u, v, n, y, pov2;
  ulong t;
  if (kronecker(a, p) < 0) return NULL;
  pov2 = shifti(p,-1);
  if (cmpii(a,pov2) > 0) a = subii(a,p); 
  av1 = avma;
  for(t=1; ; t++)
  {
    n = subsi((long)(t*t), a);
    if (kronecker(n, p) < 0) break;
    avma = av1;
  }
  u = utoipos(t);
  y = gen_pow_fold(mkvec2(u, gen_1), pov2, mkvec4(a,p,n,u),
                         sqrt_Cipolla_sqr, sqrt_Cipolla_msqr);
  v = Fp_mul(gel(y, 2), a, p);
  if (cmpii(v,pov2) > 0) v = subii(p,v);
  return v;
}
static GEN sqrt_Cipolla ( GEN a , GEN p ) { pari_sp av1 ; GEN u , v , n , y , pov2 ; ulong t ; if ( kronecker ( a , p ) < 0 ) return NULL ; pov2 = shifti ( p , -1 ) ; if ( cmpii ( a , pov2 ) > 0 ) a = subii ( a , p ) ; av1 = avma ; for ( t = 1 ; ; t ++ ) { n = subsi ( ( long ) ( t * t ) , a ) ; if ( kronecker ( n , p ) < 0 ) break ; avma = av1 ; } u = utoipos ( t ) ; y = gen_pow_fold ( mkvec2 ( u , gen_1 ) , pov2 , mkvec4 ( a , p , n , u ) , sqrt_Cipolla_sqr , sqrt_Cipolla_msqr ) ; v = Fp_mul ( gel ( y , 2 ) , a , p ) ; if ( cmpii ( v , pov2 ) > 0 ) v = subii ( p , v ) ; return v ; }
assume a reduced mod p [ otherwise correct but inefficient ]
assume a reduced mod p [ otherwise correct but inefficient ]
/* center: avoid multiplying by huge base*/ /* compute (t+X)^((p-1)/2) =: u+vX */ /* Now u+vX = (t+X)^((p-1)/2); thus
   *   (u+vX)(t+X) = sqrt(a) + 0 X
   * Whence,
   *   sqrt(a) = (u+vt)t - v*a
   *   0       = (u+vt)
   * Thus a square root is v*a */