Missed off <ctype.h>\!
[u/mdw/catacomb] / ec-gentab.sh
1 #! /bin/sh
2
3 set -e
4
5 cat <<EOF
6 /* -*-c-*-
7 *
8 * Table of standard elliptic curves [generated]
9 */
10
11 #include "ectab.h"
12
13 #define N(x) (sizeof(x)/sizeof(*x))
14 #define MP(x) { x, x + N(x), N(x), 0, MP_CONST, 0 }
15
16 /*----- Curve data --------------------------------------------------------*/
17
18 EOF
19
20 names=""
21 while read t n f; do
22
23 case $t in
24 curve) ;;
25 alias) names="$names $n=$f" continue;;
26 \#* | "") continue;;
27 *) echo >&2 "$0: unknown keyword $t"; exit 1;;
28 esac
29
30 names="$names $n=$n"
31 cat <<EOF
32 /* --- Curve $n --- */
33
34 EOF
35
36 n=`echo $n | sed 's/[^a-zA-Z0-9_][^a-zA-Z0-9_]*/_/g'`
37 case $f in
38 prime | niceprime | binpoly)
39 read t p;
40 if [ $t != p ]; then echo >&2 "$0: wanted p; found $t"; exit 1; fi
41 read t a
42 if [ $t != a ]; then echo >&2 "$0: wanted a; found $t"; exit 1; fi
43 read t b
44 if [ $t != b ]; then echo >&2 "$0: wanted b; found $t"; exit 1; fi
45
46 cat <<EOF
47 static mpw c_${n}_p[] = {
48 EOF
49 ./mpdump $p
50 cat <<EOF
51 };
52
53 static mpw c_${n}_a[] = {
54 EOF
55 ./mpdump $a
56 cat <<EOF
57 };
58
59 static mpw c_${n}_b[] = {
60 EOF
61 ./mpdump $b
62 cat <<EOF
63 };
64
65 EOF
66 ;;
67 *) echo >&2 "$0: unknown field type $f"; exit 1;;
68 esac
69
70 read t r
71 if [ $t != r ]; then echo >&2 "$0: wanted r; found $t"; exit 1; fi
72 read t h
73 if [ $t != h ]; then echo >&2 "$0: wanted h; found $t"; exit 1; fi
74 read t gx
75 if [ $t != gx ]; then echo >&2 "$0: wanted gx; found $t"; exit 1; fi
76 read t gy
77 if [ $t != gy ]; then echo >&2 "$0: wanted gy; found $t"; exit 1; fi
78
79 cat <<EOF
80 static mpw c_${n}_r[] = {
81 EOF
82 ./mpdump $r
83 cat <<EOF
84 };
85
86 static mpw c_${n}_h[] = {
87 EOF
88 ./mpdump $h
89 cat <<EOF
90 };
91
92 static mpw c_${n}_gx[] = {
93 EOF
94 ./mpdump $gx
95 cat <<EOF
96 };
97
98 static mpw c_${n}_gy[] = {
99 EOF
100 ./mpdump $gy
101 cat <<EOF
102 };
103
104 EOF
105
106 case $f in
107 prime) ftag=FTAG_PRIME;;
108 niceprime) ftag=FTAG_NICEPRIME;;
109 binpoly) ftag=FTAG_BINPOLY;;
110 esac
111 cat <<EOF
112 static ecdata c_$n = {
113 $ftag,
114 MP(c_${n}_p),
115 MP(c_${n}_a),
116 MP(c_${n}_b),
117 MP(c_${n}_r),
118 MP(c_${n}_h),
119 MP(c_${n}_gx),
120 MP(c_${n}_gy)
121 };
122
123 EOF
124
125 done
126
127 cat <<EOF
128 /*----- Main table --------------------------------------------------------*/
129
130 const ecentry ectab[] = {
131 EOF
132 for i in $names; do
133 a=`echo $i | sed 's/=.*$//'`
134 n=`echo $i | sed 's/^.*=//; s/[^a-zA-Z0-9_][^a-zA-Z0-9_]*/_/g'`
135 cat <<EOF
136 { "$a", &c_$n },
137 EOF
138 done
139 cat <<EOF
140 { 0, 0 }
141 };
142
143 /*----- That's all, folks -------------------------------------------------*/
144 EOF