Initial GIT commit
[libg2hec.git] / demo / .svn / text-base / arithdemo.C.svn-base
blob401e5d34c434a55fe0ee68c8a7be56c7afb9ea8c
1 /* Genus 2 arithmetic demo cgi */
3 #include <sys/timeb.h>
4 #include <errno.h>
5 #include <time.h>
6 #include <g2hec_Genus2_ops.h>
7 #include <g2hec_rand.h>
8 #include <g2hec_nsdebug.h>
10 #undef MAX_STRING_LEN
11 #define MAX_STRING_LEN 500
13 /* Global data */
14 char p[MAX_STRING_LEN];
15 char h2[MAX_STRING_LEN], h1[MAX_STRING_LEN], h0[MAX_STRING_LEN];
16 char f5[MAX_STRING_LEN],f4[MAX_STRING_LEN],f3[MAX_STRING_LEN],
17   f2[MAX_STRING_LEN],f1[MAX_STRING_LEN],f0[MAX_STRING_LEN];
18 char u2[MAX_STRING_LEN], u1[MAX_STRING_LEN], u0[MAX_STRING_LEN];
19 char v1[MAX_STRING_LEN], v0[MAX_STRING_LEN];
20 size_t primesize;
21 char n[MAX_STRING_LEN];
22 char buttontype[MAX_STRING_LEN];
24 NS_G2_CLIENT
26 /* Subroutine to generate random prime, curve and divisor */
27 static void generate_random() 
29   ZZ prime, scalar; 
31   /* PRNG seeding */
32   SetSeed(g2hec_rand()); // Get random seed from /dev/urandom.
34   RandomPrime(prime, (long) primesize, 10);
35   RandomBnd(scalar, prime);
37   field_t::init(prime);
39   poly_t f, h, u, v;
41   g2hcurve curve;
43   divisor div;
45   curve.random();
47   div.set_curve(curve);
49   div.random();
51   f = curve.get_f();
53   h = curve.get_h();
55   u = div.get_upoly();
57   v = div.get_vpoly();
59   /* Print web page */
61   cout << "<!-- Genus 2 arithmetic demo -->" << endl;
62   cout << "<html>" << endl << endl
63   << "<head>" << endl
64   << "<meta http-equiv=\"Content-Type\" content=\"text/html; \
65 charset=utf-8\" />" << endl 
66   << "<script language=\"javascript\" type=\"text/javascript\" src=\"tools.js\">" << endl
67   << "</script>" << endl
69   << endl << "<!-- Workaround for IE7 prompt() blocking -->" << endl
70   << "<script language=\"javascript\" type=\"text/javascript\" src=\"IEprompt.js\">" << endl
71   << "</script>" << endl
73   << "<script language=\"javascript\" type=\"text/javascript\" src=\"promptcb.js\">" << endl
74   << "</script>" << endl
76   << "<title> Genus 2 arithmetic demo </title>" << endl
77   << "</head>" << endl
78   << "<body>" << endl
79   << "<form name=\"demoform\" method=\"POST\" action=\"arithdemo.cgi\">" << endl
80   << "<div>" << endl
81   << "<label><h4>libg2hec Demo: genus 2 curve C: y^2+h(x)y=f(x) over GF(p)\
82 scalar multiplication</h4></label>" << endl
83   << "</div>" << endl
84   << "<hr />" << endl
86   <<"<div>" << endl
87   << "<label> prime p = </label>" << endl
88   << "<textarea name=\"p\" cols=\"60\" rows=\"3\" id=\"p\" wrap=\"virtual\">" 
89   << prime 
90   << "</textarea>" << endl
91        << "</div>" << endl
93        << "<div>" << endl
95        << "<label>h(x) = </label>" << endl
97        << "<textarea name=\"h2\" cols=\"10\" rows=\"1\" id=\"h2\" wrap=\"virtual\">" 
98        << coeff(h, 2) 
99        << "</textarea> x^2 + <textarea name=\"h1\" cols=\"10\" rows=\"1\" id=\"h1\" wrap=\"virtual\">" 
100        << coeff(h, 1) 
101        << "</textarea> x + <textarea name=\"h0\" cols=\"10\" rows=\"1\" id=\"h0\" wrap=\"virtual\">"
102        << coeff(h, 0) << "</textarea>" << endl;
103   
104   cout << "</div>" << endl;
106   cout << "<div>" << endl;
108   cout << "<label>f(x) = </label>" << endl;
110   cout << "x^5 + "  
111        << "<textarea name=\"f4\" cols=\"10\" rows=\"1\" id=\"f4\" wrap=\"virtual\">"
112        << coeff(f, 4) << "</textarea> x^4 + "
113        << "<textarea name=\"f3\" cols=\"10\" rows=\"1\" id=\"f3\" wrap=\"virtual\">"
114        << coeff(f, 3) <<"</textarea> x^3 + "
115        << "<textarea name=\"f2\" cols=\"10\" rows=\"1\" id=\"f2\" wrap=\"virtual\">"
116        << coeff(f, 2) <<"</textarea> x^2 + "
117        << "<textarea name=\"f1\" cols=\"10\" rows=\"1\" id=\"f1\" wrap=\"virtual\">"
118        << coeff(f, 1) << "</textarea> x + "
119        << "<textarea name=\"f0\" cols=\"10\" rows=\"1\" id=\"f0\" wrap=\"virtual\">"
120        << coeff(f, 0) << "</textarea>" << endl;
122   cout << "</div>" << endl;
124   cout << "<br />" << endl;
126   cout << "<div>" << endl;
128   cout << "divisor D = [u(x), v(x)]" << endl;
129   cout << "</div>" << endl;
131   cout << "<div>" << endl;
133   cout << "<label> u(x) = </label>" << endl;
134   cout << "<textarea name=\"u2\" cols=\"10\" rows=\"1\" id=\"u2\" wrap=\"virtual\">"
135        << coeff(u, 2) << "</textarea> x^2 + "
136        << "<textarea name=\"u1\" cols=\"10\" rows=\"1\" id=\"u1\" wrap=\"virtual\">"
137        << coeff(u, 1) << "</textarea> x + "
138        << "<textarea name=\"u0\" cols=\"10\" rows=\"1\" id=\"u0\" wrap=\"virtual\">"
139        << coeff(u, 0) << "</textarea>" << endl;
140   cout << "</div>" << endl;
142   cout << "<div>" << endl;
144   cout << "<label> v(x) = </label>" << endl;
145   cout << "<textarea name=\"v1\" cols=\"10\" rows=\"1\" id=\"v1\" wrap=\"virtual\">"
146        << coeff(v, 1) << "</textarea> x + "
147        << "<textarea name=\"v0\" cols=\"10\" rows=\"1\" id=\"v0\" wrap=\"virtual\">"
148        << coeff(v, 0) << "</textarea>" << endl;
149   cout << "</div>" << endl;
151   cout << "<br />" << endl;
153   cout << "<div>" << endl;
155   cout << "<label> scalar n = </label>" << endl;
157   cout << "<textarea name=\"n\" cols=\"60\" rows=\"3\" id=\"n\" wrap=\"virtual\">"
158        << scalar << "</textarea>" << endl;
159   
160   cout << "<hr />" << endl << "<br />" << endl;
162   cout << "</div>" << endl;
164   cout << "<div>" << endl;
165   cout << "<input type=\"hidden\" name=\"primesize\" value=\""
166        << primesize << "\" />" << endl;
167   cout << "<input type=\"hidden\" name=\"buttontype\" />" << endl;
169   cout << "<input type=\"button\" value=\"Choose random\" onClick=\'Choose_prime_size()\'/>" << endl;
171   cout << "<input type=\"button\" value=\"Reset\" onClick=\'self.location=\"demo.html\"\' />" << endl;
173   cout << "<input type=\"button\" value=\"Calculate [n]D\" onClick=\'Calculate()\' />" << endl;
174   cout << "</div>" << endl;
175   cout << "</form>" << endl;
177   cout << "</body>" << endl;
179    cout <<   " </html>" << endl;
181   /* End print web page */
185 /* Subroutine to do computation */
186 static void calculate()
188     /* Primility test */
189     if (! ProbPrime(to_ZZ(p)) ) {
190       cout << "p = " << p << " is not prime";
191       exit(1);
192     }
194     struct timeb start, finish;
196     int rc_start, rc_finish;
198     field_t::init(to_ZZ(p));
200     poly_t f, h, u, v;
202     g2hcurve curve;
204     divisor div, div_out;
206     SetCoeff(f, 5, 1);
207     SetCoeff(f, 4, to_ZZ_p(to_ZZ(f4)));
208     SetCoeff(f, 3, to_ZZ_p(to_ZZ(f3)));
209     SetCoeff(f, 2, to_ZZ_p(to_ZZ(f2)));
210     SetCoeff(f, 1, to_ZZ_p(to_ZZ(f1)));
211     SetCoeff(f, 0, to_ZZ_p(to_ZZ(f0)));
213     SetCoeff(h, 2, to_ZZ_p(to_ZZ(h2)));
214     SetCoeff(h, 1, to_ZZ_p(to_ZZ(h1)));
215     SetCoeff(h, 0, to_ZZ_p(to_ZZ(h0)));
217     SetCoeff(u, 2, to_ZZ_p(to_ZZ(u2)));
218     SetCoeff(u, 1, to_ZZ_p(to_ZZ(u1)));
219     SetCoeff(u, 0, to_ZZ_p(to_ZZ(u0)));
221     SetCoeff(v, 1, to_ZZ_p(to_ZZ(v1)));
222     SetCoeff(v, 0, to_ZZ_p(to_ZZ(v0)));
224     curve.set_h(h);
225     curve.set_f(f);
226     curve.update();
228     if (!curve.is_valid_curve()) {
229       cout << "<p>Curve is not valid</p>" << endl;
230       exit(1);
231     }
233     div.set_curve(curve);
234     div.set_upoly(u);
235     div.set_vpoly(v);
236     div.update();
238     if (!div.is_valid_divisor()) {
239       cout << "<p>Divisor D is not valid</p>" << endl;
240       cout << div << endl;
241       exit(1);
242     }
244     ZZ nn = to_ZZ(n);
246     /* scalar multiplication: NAF */
247     rc_start = ftime(&start);
249     if (rc_start) {
250       printf("ftime() failed, errno = %d\n",
251              errno);
252       exit(1);
253     }
255     /* Actuall computation */
256     scalar_mul(div_out, div, nn, NAF);
257     /* End of computation */
259     rc_finish = ftime(&finish);
261     if (rc_finish) {
262       printf("ftime() failed, errno = %d\n",
263              errno);
264       exit(1);
265     }
267     /* Time in seconds */
268     double time = finish.time-start.time;
269     time *= 1000;
270     time += finish.millitm-start.millitm;
271     time /= 1000;
273     /* print html page */
274     cout << "<!-- Scalar multiplication result -->" << endl
275          << "<html>" << endl << endl
276          << "<head>" << endl
277          << "<title>Scalar multiplication result</title>" << endl
278          << "</head>" << endl << endl
280          << "<body>" << endl
282          << "<h4>Scalar multiplication result</h4>" << endl
283          << "<div>Genus 2 curve C: y^2+h(x)y=f(x) over GF(p)</div>" << endl
284          << "<div>" << primesize << "-bit prime p = " << p << endl
285          << "<hr />" << endl
287          << "<pre>" << endl
289          << "input divisor D: " << endl
290          << "<pre>" << endl
291          << div << endl
292          << "input scalar n = " << n << endl
293          << "<hr />" << endl
295          << "output divisor [n]D: " << endl
296          << div_out << endl
297          << "Computation took " << time << " sec " << endl
298          << "<hr />" << endl
299          << "</pre>" << endl
300       /* "Back button" */
301          << "<form><input type=\"button\" value=\" &lt;-- Back \" onClick=\"history.go(-1);return false;\" /></form>" << endl
303          << "</body>" << endl
304          << "</html>" << endl;
306     /* end print html page*/
310 int main() 
311 { /* The following must be the first line of output */
312   printf("%s%c%c\n\n", "Content-Type:text/html; charset=utf-8", 13, 10);
313   
314   if (!getenv("CONTENT_LENGTH")) {
315     cout << "Error reading data" << endl;
316     exit(1);
317   }  
319   char data[MAX_STRING_LEN*20];
320   int datalen = atoi( getenv("CONTENT_LENGTH") );
321   fread(data, datalen, 1, stdin);
323   /* for GET method */
324   /* data = getenv("QUERY_STRING"); */
326   if (!data) cout << "<p>Error reading data</p>" << endl;
327   else if (sscanf(data, "p=%[0-9]&h2=%[0-9]&h1=%[0-9]&h0=%[0-9]&f4=%[0-9]&\
328 f3=%[0-9]&f2=%[0-9]&f1=%[0-9]&f0=%[0-9]&u2=%[0-9]&u1=%[0-9]&u0=%[0-9]&\
329 v1=%[0-9]&v0=%[0-9]&n=%[0-9-]&primesize=%u&buttontype=%s", p, h2, h1, h0, \
330 f4, f3, f2, f1, f0, u2, u1, u0, v1, v0, n, &primesize, buttontype) != 17) \
331                                 cout << "<p>Invalid input data. </p>" << endl;
333   else {
334     if ( !strcmp(buttontype, "Random") ) generate_random();
336     if (!strcmp(buttontype, "Calculate") ) calculate();
337   }
339   return 0;