1 /* Genus 2 arithmetic demo cgi */
6 #include <g2hec_Genus2_ops.h>
7 #include <g2hec_rand.h>
8 #include <g2hec_nsdebug.h>
11 #define MAX_STRING_LEN 500
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];
21 char n[MAX_STRING_LEN];
22 char buttontype[MAX_STRING_LEN];
26 /* Subroutine to generate random prime, curve and divisor */
27 static void generate_random()
32 SetSeed(g2hec_rand()); // Get random seed from /dev/urandom.
34 RandomPrime(prime, (long) primesize, 10);
35 RandomBnd(scalar, prime);
61 cout << "<!-- Genus 2 arithmetic demo -->" << endl;
62 cout << "<html>" << endl << 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
79 << "<form name=\"demoform\" method=\"POST\" action=\"arithdemo.cgi\">" << 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
87 << "<label> prime p = </label>" << endl
88 << "<textarea name=\"p\" cols=\"60\" rows=\"3\" id=\"p\" wrap=\"virtual\">"
90 << "</textarea>" << endl
95 << "<label>h(x) = </label>" << endl
97 << "<textarea name=\"h2\" cols=\"10\" rows=\"1\" id=\"h2\" wrap=\"virtual\">"
99 << "</textarea> x^2 + <textarea name=\"h1\" cols=\"10\" rows=\"1\" id=\"h1\" wrap=\"virtual\">"
101 << "</textarea> x + <textarea name=\"h0\" cols=\"10\" rows=\"1\" id=\"h0\" wrap=\"virtual\">"
102 << coeff(h, 0) << "</textarea>" << endl;
104 cout << "</div>" << endl;
106 cout << "<div>" << endl;
108 cout << "<label>f(x) = </label>" << endl;
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;
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()
189 if (! ProbPrime(to_ZZ(p)) ) {
190 cout << "p = " << p << " is not prime";
194 struct timeb start, finish;
196 int rc_start, rc_finish;
198 field_t::init(to_ZZ(p));
204 divisor div, div_out;
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)));
228 if (!curve.is_valid_curve()) {
229 cout << "<p>Curve is not valid</p>" << endl;
233 div.set_curve(curve);
238 if (!div.is_valid_divisor()) {
239 cout << "<p>Divisor D is not valid</p>" << endl;
246 /* scalar multiplication: NAF */
247 rc_start = ftime(&start);
250 printf("ftime() failed, errno = %d\n",
255 /* Actuall computation */
256 scalar_mul(div_out, div, nn, NAF);
257 /* End of computation */
259 rc_finish = ftime(&finish);
262 printf("ftime() failed, errno = %d\n",
267 /* Time in seconds */
268 double time = finish.time-start.time;
270 time += finish.millitm-start.millitm;
273 /* print html page */
274 cout << "<!-- Scalar multiplication result -->" << endl
275 << "<html>" << endl << endl
277 << "<title>Scalar multiplication result</title>" << endl
278 << "</head>" << endl << 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
289 << "input divisor D: " << endl
292 << "input scalar n = " << n << endl
295 << "output divisor [n]D: " << endl
297 << "Computation took " << time << " sec " << endl
301 << "<form><input type=\"button\" value=\" <-- Back \" onClick=\"history.go(-1);return false;\" /></form>" << endl
304 << "</html>" << endl;
306 /* end print html page*/
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);
314 if (!getenv("CONTENT_LENGTH")) {
315 cout << "Error reading data" << endl;
319 char data[MAX_STRING_LEN*20];
320 int datalen = atoi( getenv("CONTENT_LENGTH") );
321 fread(data, datalen, 1, stdin);
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;
334 if ( !strcmp(buttontype, "Random") ) generate_random();
336 if (!strcmp(buttontype, "Calculate") ) calculate();