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