1 // ffmod.h: declaration of class ffmodq and Weil pairing functions
2 //////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright 1990-2012 John Cremona
5 //
6 // This file is part of the eclib package.
7 //
8 // eclib is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2 of the License, or (at your
11 // option) any later version.
12 //
13 // eclib is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with eclib; if not, write to the Free Software Foundation,
20 // Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 //
22 //////////////////////////////////////////////////////////////////////////
23 
24 
25 // ffmodq is the function field of an elliptic curve mod a prime q
26 // (or more precisely the affine coordinate ring Fq[x,y])
27 
28 // allow for multiple includes
29 #ifndef _ECLIB_FFMOD_
30 #define _ECLIB_FFMOD_
31 
32 #include "pointsmod.h"
33 
34 class ffmodq{
35 
36   public:
37   static galois_field Fq;        //  the constant field
38   static curvemodq E;            //  the curve mod q
39   static FqPoly f1, f2;          //  f2=a1*x+a3, f2=x^3+a2*x^2+a4*x+a6
40   FqPoly h1, h2;                 //  for h1+y*h2
41 
42 public:
43 
44   //  constructors
45 
46   //  special one to initialize the curve and field only:
47   ffmodq(const curvemodq& EE);
48 
49   //  normal ones:
ffmodq(void)50   ffmodq(void)
51     {
52       init_h1h2();
53       FqPolyAssign0(h1);
54       FqPolyAssign0(h2);
55     }
56 
ffmodq(const gf_element & c)57   ffmodq(const gf_element& c)
58     {
59       init_h1h2();
60       FqPolyAssignGF(h1,c);
61       FqPolyAssign0(h2);
62     }
63 
ffmodq(const bigint & c)64   ffmodq(const bigint& c)
65     {
66       init_h1h2();
67       FqPolyAssignZ(h1,c);
68       FqPolyAssign0(h2);
69     }
70 
ffmodq(const FqPoly & hh1)71   ffmodq(const FqPoly& hh1)
72     {
73       init_h1h2();
74       h1=hh1;
75       FqPolyAssign0(h2);
76     }
77 
ffmodq(const FqPoly & hh1,const FqPoly & hh2)78   ffmodq(const FqPoly& hh1, const FqPoly& hh2) {h1=hh1; h2=hh2;}
79 
80   //  initialization
81   void init_f1f2(void);
82 
init_h1h2(void)83   void init_h1h2(void)
84     {
85       FqPolySetField(h1,Fq);
86       FqPolySetField(h2,Fq);
87     }
88 
89   // equality test
90   int operator==(const ffmodq& b) const;
91   int operator!=(const ffmodq& b) const {return !((*this)==b);}
92 
93   // output
94   void output(ostream& os) const;
95 
96   // addition, subtraction, multiplication
97   ffmodq operator+(const ffmodq& b) const;
98   ffmodq operator-(const ffmodq& b) const;
99   ffmodq operator*(const ffmodq& b) const;
100   ffmodq operator*(const FqPoly& h) const;
101 
102   //  division
103   ffmodq operator/(const FqPoly& h) const;
104   ffmodq operator/(const ffmodq& b) const;
105 
106   //  evaluation at a point:
107   gf_element evaluate(const pointmodq& P) const;
operator()108   gf_element operator()(const pointmodq& P) const {return this->evaluate(P);}
109 
110   //  vertical line through a point:
111   friend ffmodq vertical(const pointmodq& P);
112 
113   //  tangent at a point:
114   friend ffmodq tangent(const pointmodq& P);
115 
116   //  chord between points:
117   friend ffmodq chord(const pointmodq& P, const pointmodq& Q);
118 
119 
120 };
121 
122 // weil_pol(T,m): T is a point of finite order m; returns a function
123 // f_T whose divisor is m(T)-m(0).
124 
125 // The second version evaluates that at another point S without
126 // actually computing the polynomial
127 
128 ffmodq weil_pol(const pointmodq& T, int m);
129 
130 gf_element evaluate_weil_pol(const pointmodq& T, int m, const pointmodq& S);
131 
132 gf_element weil_pairing(const pointmodq& S, const pointmodq& T, int m);
133 
134 inline ostream& operator<<(ostream& os, const ffmodq& f)
135 {
136   f.output(os);
137   return os;
138 }
139 
140 
141 #endif
142