def gf_pow(f, n, p, K):
 if (not n):
  return [K.one]
 elif (n == 1):
  return f
 elif (n == 2):
  return gf_sqr(f, p, K)
 h = [K.one]
 while True:
  if (n & 1):
   h = gf_mul(h, f, p, K)
   n -= 1
  n >>= 1
  if (not n):
   break
  f = gf_sqr(f, p, K)
 return h

def gf_pow ( f , n , p , K ) : if ( not n ) : return [ K . one ] elif ( n == 1 ) : return f elif ( n == 2 ) : return gf_sqr ( f , p , K ) h = [ K . one ] while True : if ( n & 1 ) : h = gf_mul ( h , f , p , K ) n -= 1 n >>= 1 if ( not n ) : break f = gf_sqr ( f , p , K ) return h
compute f**n in gf(p)[x] using repeated squaring .