Overview
The first step is to implement arithmetic in . I built mine on top of the GMP library, which conveniently provides various number theoretic functions such as inversion modulo a prime and the Jacobi symbol.
Basic elliptic curve arithmetic is built on top, then finally a pairing can be coded. For some curves, field extensions must be implemented.
For the MNT curve construction method various operations on polynomials need to be implemented, such as finding roots modulo a given prime and irreducibility tests, along with an algorithm to compute Hilbert polynomials (which requires high precision complex floating point arithmetic) and also an algorithm to solve a Pell-type equation.
Arithmetic on Elliptic Curves
Curves over fields of characteristic greater than 3 can be written in the form . For such curves, point addition and multiplication routines are straightforward. (See Blake, Seroussi and Smart.) We give the explicit formulae here.
Suppose we wish to compute . If , then and similarly is easily handled. If then either in which case (for we have ), or in which case the point doubling algorithm is invoked. Note that the point doubling algorithm assumes the input does not have order 2, i.e. the -coordinate is nonzero. (If has order 2, the result is .)
Point addition for :
Point doubling:
Generating Random Points
The standard method to generate a random point on an elliptic curve is to choose a random -coordinate and solve a quadratic equation for . (If no solution exists, a new -coordinate is chosen.) For odd characteristics, this can be done once one is able to find square roots of elements. In a finite field of prime order, square roots can be obtained via the Tonelli-Shanks algorithm. In general fields, see D.J. Bernstein's draft of a paper entitled "Faster square roots in annoying finite fields". At the moment I just solve using the Cantor-Zassenhaus method (which is referred to as Legendre's method in the aforementioned reference).
Easier ways to generate random points exist for particular cases. For example if is a prime with then the cube root of is simply . Thus if in the equation of the elliptic curve, then it is a simple matter to pick first and then solve for .