File: tlss.cc

package info (click to toggle)
eclib 2014-09-21-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,216 kB
  • ctags: 4,287
  • sloc: cpp: 45,827; makefile: 222; sh: 108
file content (266 lines) | stat: -rw-r--r-- 6,615 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// tlss.cc: implementation of class TLSS for sieving E(Q)/pE(Q) at one prime q
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2012 John Cremona
// 
// This file is part of the eclib package.
// 
// eclib is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
// 
// eclib is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License
// along with eclib; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
// 
//////////////////////////////////////////////////////////////////////////
 

// NB: TLSS = Tate--Lichtenbaum--Samir-Siksek: we use a simple
// discrete log a la Siksek when the p-torsion in E(F_q) is cyclic,
// else use the Tate-Lichtenbaum pairing

#include <eclib/matrix.h>
#include <eclib/subspace.h>

#include <eclib/points.h>
#include <eclib/polys.h>
#include <eclib/curvemod.h>
#include <eclib/pointsmod.h>
#include <eclib/ffmod.h>
#include <eclib/tlss.h>

void TLSS::init(int  pp, int verb)
{
  verbose=verb;
  p=pp;

  Pi=Emodq.get_pbasis(p);
  rank=Pi.size();

  if((verbose>1)&&(rank>0))
    {
      cout<<"Generators of "<<p<<"-torsion mod "<<q<<": \n";
      cout<<"P1 = "<<Pi[0]<<endl;
      if(rank>1) cout<<"P2 = "<<Pi[1]<<endl;
    }  
  if(rank==2) init_tlpolys();
}

void TLSS::init(int  pp, const vector<bigint>& pdivpol, int verb)
{
  verbose=verb;
  p=pp;

  Pi=Emodq.get_pbasis_via_divpol(p, pdivpol);
  rank=Pi.size();

  if((verbose>1)&&(rank>0))
    {
      cout<<"Generators of "<<p<<"-torsion mod "<<q<<": \n";
      cout<<"P1 = "<<Pi[0]<<endl;
      if(rank>1) cout<<"P2 = "<<Pi[1]<<endl;
    }  
  if(rank==2) init_tlpolys();
}

void TLSS::init_tlpolys()
{
 
  if (rank<2) return;

  // case rank=2: prepare to use TL map
  // first find p'th root of 1 mod q 

  // initialize the array of all p'th roots mod q
  q1p=(q-1)/p;
  mu_p = roots_of_unity(Fq,p);

  if(verbose>1) 
    {
      cout<<"q="<<q<<endl;
      cout<<"p'th roots of unity mod q = "<<mu_p<<endl;
      cout<<"Rank of p-torsion mod q = "<<rank<<endl;
    }

  // initialize the ffmodq class
  ffmodq dummy((const curvemodq)Emodq);
  // initialize the TL-functions
  TLpolys.resize(0);
  int i;
  for(i=0; i<rank; i++) TLpolys.push_back(weil_pol(Pi[i],p));
  if(verbose>1) for(i=0; i<rank; i++) cout<<"TL poly: "<<TLpolys[i]<<endl;
}

//#define debugTL

// apply map to P, result in (rank*)[0..p-1]:
vector<int> TLSS::map1point(const Point& P) const
{
#ifdef debugTL
  cout<<"Applying TLSS::map1point (q="<<q<<") to P="<<P<<endl;
#endif
  pointmodq Pmodq = reduce_point(P,(const curvemodq&)Emodq);
#ifdef debugTL
  cout<<"P mod q ="<<Pmodq<<endl;
#endif
  int i, sw;
  vector<int> ans;
  ans.resize(rank);
  for(i=0; i<rank; i++) ans[i]=0;
  if(Pmodq.is_zero()) return ans;
  
  if(rank==1)
    {
      pointmodq P1=Pi[0];
      bigint n1 = Emodq.n1; // order of relevant cyclic factor if known
      if(n1==0) n1 = Emodq.n; // else full group order if not
      Pmodq = I2long(n1/p) * Pmodq;
      if(Pmodq.is_zero()) return ans;

#ifdef debugTL
      cout<<"after multiplying by "<<I2long(n1/p)<<", get "<<Pmodq<<endl;
      cout<<"finding discrete log of "<<Pmodq<<" w.r.t. "<<P1<<endl;
#endif
      pointmodq Q(Emodq);
      pointmodq minusPmodq=-Pmodq;
      for(i=0; i<p; i++) 
	{
	  if(Q==Pmodq) {ans[0]=i; return ans;}
	  if(i) if(Q==minusPmodq) {ans[0]=p-i; return ans;}
	  Q+=P1;
	}

      // Optional check:      

#ifdef debugTL
      int check = ans[0]*P1 == Pmodq;
      if(!check)
	{
	  cout<<"Error: discrete log of "<<Pmodq<<" w.r.t. "<<P1<<" returns "<<ans[0]<<endl;
	  abort();
	}
#endif

      return ans;
    }

  // else apply TL maps

  gf_element xP=Pmodq.get_x();
  gf_element yP=Pmodq.get_y();
  gf_element lambda, mu, t;
  gf_element a1,a2,a3,a4,a6;
  Emodq.get_ai(a1,a2,a3,a4,a6);
  gf_element b2 = a1*a1 + 4*a2; 
  gf_element b4 = 2*a4 + a1*a3;

  for(i=0; i<rank; i++)
    {  
      const pointmodq& T = Pi[i];
      gf_element xT=T.get_x(), yT=T.get_y();
#ifdef debugTL
      cout<<"(xT,yT)=("<<xT<<","<<yT<<")"<<endl;
#endif
      switch(p) {
      case 2:{
	t=xP-xT;
	if(t==0)
	  {
	    lambda=4*xT+b2;
	    mu=xT*lambda+b4+b4;
	    t=4*xP*xP+lambda*xP+mu;
	  }
#ifdef debugTL
	cout<<"calling power_mod with (t,q1p,q) = ("<<mu<<","<<t<<","<<q1p<<","<<q<<")"<<endl;
#endif
	power(mu,t,q1p);
#ifdef debugTL
	cout<<"mu = "<<mu<<endl;
#endif
	ans[i] = ((mu==1)? 0 : 1);	
	break;
      }
      default:{
#ifdef debugTL
	cout<<"applying TL poly = "<<TLpolys[i]<<" to "<<Pmodq<<endl;
#endif
	t=TLpolys[i](Pmodq);
	sw=0;
	if(t==0) 
	  {
	    sw=1; 
	    t=TLpolys[i](-Pmodq);
	  }
	if(t==0) 
	  {
	    cout<<"Error: both P and -P map to 0"<<endl;
	    abort();
	  }
	power(mu,t,q1p);

#ifdef debugTL
	cout<<"t="<<t<<endl;
	cout<<"mu = "<<mu<<endl;
	gf_element t2, mu2;
	if(!(p*Pmodq).is_zero()) t2 = evaluate_weil_pol(T,p,Pmodq);
	else
	  {
	    cout<<"(new method having to use shifting trick)"<<endl;
	    pointmodq R=Pmodq.get_curve().random_point();
	    while((p*R).is_zero())
	      R=Pmodq.get_curve().random_point();
	    t2 = evaluate_weil_pol(T,p,R+Pmodq)/evaluate_weil_pol(T,p,R);
	  }
	power(mu2,t2,q1p);
	if(mu==mu2) cout<<"New method agrees"<<endl;
	else cout<<"New method gives mu value "<<mu2<<" instead!"<<endl;
#endif

	if(mu==1) ans[i]=0; else // discrete log: mu is some power of mu_p
	  {
	    int dl=find(mu_p.begin(),mu_p.end(),mu)-mu_p.begin();
#ifdef debugTL
	    cout<<"discrete log of mu = "<<dl<<endl;
#endif
	    if(dl==p) 
	      {
		cout<<"Error:  mu="<<mu<<" (mod "<<q<<") is not a "<<p<<"'th root of unity!"<<endl;
		abort();
	      }
	    ans[i] = (sw? p-dl: dl);
	  }
	break;
      }  //  end of default case
      } // switch(p)
    }
#ifdef debugTL
  cout<<"ans = "<<ans<<endl;
#endif
  return ans;
}

// apply map to all P in Plist, result is a (rank*#Plist) matrix:
mat TLSS::map_points(const vector<Point>& Plist) const
{
  int npts = Plist.size();
  mat TLim(rank,npts);
  int i,j;
  for(i=0; i<npts; i++)
    {
      Point P=Plist[i];
      vector<int> tlP = map1point(P);
      if(verbose>1) cout<<"P="<<P<<" -> "<<tlP<<endl;
      for(j=0; j<rank; j++)
	{
	  TLim(j+1,i+1)=tlP[j];
	}
    }
  return TLim; 
}