4edc47b8 |
1 | #! /usr/bin/awk -f |
4edc47b8 |
2 | |
0f00dc4c |
3 | function banner(name, s, i) |
4edc47b8 |
4 | { |
5 | s = "/*----- " name " "; |
6 | while (length(s) < 75) s = s "-"; |
7 | return (s "*/"); |
8 | } |
9 | |
10 | function fix(name) |
11 | { |
12 | gsub(/[^0-9A-Za-z_]+/, "_", name); |
13 | return (name); |
14 | } |
15 | |
16 | BEGIN { |
17 | print "/* -*-c-*-"; |
18 | print " *"; |
19 | print " * Table of elliptic curves [generated]"; |
20 | print " */"; |
21 | print ""; |
22 | print "#include \"ectab.h\""; |
23 | print ""; |
24 | print "#define N(x) (sizeof(x)/sizeof(*x))"; |
25 | print "#define MP(x) { x, x + N(x), N(x), 0, MP_CONST, 0 }"; |
26 | print "#define NOMP { 0, 0, 0, 0, 0 }"; |
27 | print ""; |
28 | print banner("Curve data"); |
29 | print ""; |
30 | |
31 | d_i = 0; |
32 | name = ""; |
33 | } |
34 | |
35 | function putmp(x, d) |
36 | { |
37 | if (!(x in data)) { |
38 | print "curve " name ": missing " x >"/dev/stderr"; |
39 | exit 1; |
40 | } |
41 | d = data[x]; |
42 | if (!(d in cache)) { |
43 | n = "c_" fix(name) "_" x; |
44 | print "static mpw " n "[] = {"; |
45 | system("./mpdump " d); |
46 | print "};"; |
47 | print ""; |
48 | cache[d] = n; |
49 | } |
50 | mp[x] = cache[d]; |
51 | } |
52 | |
53 | function flush() |
54 | { |
55 | if (name == "") return; |
56 | print "/* --- Curve " name " --- */"; |
57 | delete mp; |
58 | print ""; |
59 | putmp("p"); |
60 | if (type == "binnorm") putmp("beta"); |
61 | putmp("a"); |
62 | putmp("b"); |
63 | putmp("r"); |
64 | putmp("h"); |
65 | putmp("gx"); |
66 | putmp("gy"); |
67 | print "static ecdata c_" fix(name) " = {"; |
68 | print " FTAG_" toupper(type) ","; |
69 | print " MP(" mp["p"] ")," |
70 | if (type == "binnorm") |
71 | print " MP(" mp["beta"] "),"; |
72 | else |
73 | print " NOMP,"; |
74 | print " MP(" mp["a"] ")," |
75 | print " MP(" mp["b"] ")," |
76 | print " MP(" mp["r"] ")," |
77 | print " MP(" mp["h"] ")," |
78 | print " MP(" mp["gx"] ")," |
79 | print " MP(" mp["gy"] ")" |
80 | print "};"; |
81 | print ""; |
82 | dname[d_i++] = name; |
83 | d[name] = name; |
84 | r[name] = "c_" fix(name); |
85 | name = ""; |
86 | } |
87 | |
88 | /^[ \t]*(#|$)/ { next; } |
89 | |
90 | $1 == "alias" { flush(); dname[d_i++] = $2; d[$2] = $3; next; } |
91 | |
92 | $1 == "curve" { flush(); delete data; name = $2; type = $3; next; } |
93 | |
94 | { data[$1] = $2; next; } |
95 | |
96 | END { |
97 | flush(); |
98 | print banner("Main table"); |
99 | print ""; |
100 | print "const ecentry ectab[] = {"; |
101 | for (i = 0; i < d_i; i++) { |
102 | name = dname[i]; |
103 | rname = d[name]; |
104 | if (!rname in r) { |
105 | print "curve " rname " not found (alias from " name ")" >"/dev/stderr"; |
106 | exit 1; |
107 | } |
108 | print " { \"" name "\", &" r[rname] " },"; |
109 | } |
110 | print " { 0, 0 }"; |
111 | print "};" |
112 | print ""; |
113 | print banner("That's all, folks"); |
114 | } |