Example #1
0
def pinch_method(p, n, m, f = None, g = None):
    '''
    Function that given three integers p, n and m such as :

    - p is prime,
    - m is dividing n and there exists a primitive mth root that spans F_{p^n} 
    and none of its subfields,

    returns the image, in a finite field G, of the polynomial generator of a 
    finite field F with the same cardinality as G.
    '''
    c, w = cputime(), walltime()
    R = PolynomialRing(GF(p), 'X')

    # If no polynomials are given, we compute both of them randomly.
    if f is None:
        f = R.irreducible_element(n, algorithm='random')
    if g is None:
        g = R.irreducible_element(n, algorithm='random')
    while f == g:
        g = R.irreductible_element(n, algorithm='random')

    # We compute two fields of cardinality p^n and two primitive m-rooth
    rootmf, rootmg, F, G = find_mroots_and_fields(p, n, m, f, g)

    # The matrixes will contain the coefficients of rootmf and rootmg in the 
    # basis x^i and y^i respectively
    A = matrix(GF(p), n, n)
    B = matrix(GF(p), n, n)

    for i in range(n):
    	A[i,:] = (rootmf**i).vector()

    # Failsafe, but probably outdated
    try:
    	Ainv = A.inverse()
    except ZeroDivisionError:
    	print 'erreur'
        return A

    # We will try to find the power s such as phi(rootmf) = rootmg^s, since 
    # rootmg and rootmf are both primitive mrooth it is bound to happen 
    # since the multiplicative group is cyclic.
    s = 1	


    while s <= m :	
    	for i in range(n):
		B[i,:] = ((rootmg**s)**i).vector()


        # This will be the isomorphism's matrix
    	C = Ainv*B	

    	v = C[1,:]	  # The second line correponds to the image of x
        res = G(v[0])

	# I realized that you could try to find if the image of 
	# rootmf is also a zero of the minimal polynomial of rootmf 
	# but it would force us to compute yet another minimal polynomials.
	# Instead, if you find that the image of x is a root of f, 
	# then you win!
        if f(res) == 0:
	    	print 'CPU %s, Wall %s' % (cputime(c), walltime(w))
	    	# Some of what is returned is probably useless and only here 
	    	# for testing purposes.
	    	return (res, C, rootmf, rootmg, s, f, F, G)

    	s = s + 1
	
    print 'No isomorphism found, check your m.'
    return 1