Standard curves and curve checking.
[u/mdw/catacomb] / ec-gentab.sh
diff --git a/ec-gentab.sh b/ec-gentab.sh
new file mode 100755 (executable)
index 0000000..ea63094
--- /dev/null
@@ -0,0 +1,143 @@
+#! /bin/sh
+
+set -e
+
+cat <<EOF
+/* -*-c-*-
+ *
+ * Table of standard elliptic curves [generated]
+ */
+
+#include "ectab.h"
+
+#define N(x) (sizeof(x)/sizeof(*x))
+
+/*----- Curve data --------------------------------------------------------*/
+
+EOF
+
+names=""
+while read t n f; do
+
+  case $t in
+    curve) ;;
+    alias) names="$names $n=$f" continue;;
+    \#* | "") continue;;
+    *) echo >&2 "$0: unknown keyword $t"; exit 1;;
+  esac
+
+  names="$names $n=$n"
+  cat <<EOF
+/* --- Curve $n --- */
+
+EOF
+
+  n=`echo $n | sed 's/[^a-zA-Z0-9_][^a-zA-Z0-9_]*/_/g'`
+  case $f in
+    prime | niceprime | binpoly)
+      read t p;
+      if [ $t != p ]; then echo >&2 "$0: wanted p; found $t"; exit 1; fi
+      read t a
+      if [ $t != a ]; then echo >&2 "$0: wanted a; found $t"; exit 1; fi
+      read t b
+      if [ $t != b ]; then echo >&2 "$0: wanted b; found $t"; exit 1; fi
+
+      cat <<EOF
+static const mpw c_${n}_p[] = {
+EOF
+      ./mpdump $p
+      cat <<EOF
+};
+
+static const mpw c_${n}_a[] = {
+EOF
+      ./mpdump $a
+      cat <<EOF
+};
+
+static const mpw c_${n}_b[] = {
+EOF
+      ./mpdump $b
+      cat <<EOF
+};
+
+EOF
+      ;;
+    *) echo >&2 "$0: unknown field type $f"; exit 1;;
+  esac
+
+  read t r
+  if [ $t != r ]; then echo >&2 "$0: wanted r; found $t"; exit 1; fi
+  read t h
+  if [ $t != h ]; then echo >&2 "$0: wanted h; found $t"; exit 1; fi
+  read t gx
+  if [ $t != gx ]; then echo >&2 "$0: wanted gx; found $t"; exit 1; fi
+  read t gy
+  if [ $t != gy ]; then echo >&2 "$0: wanted gy; found $t"; exit 1; fi
+
+  cat <<EOF
+static const mpw c_${n}_r[] = {
+EOF
+  ./mpdump $r
+  cat <<EOF
+};
+
+static const mpw c_${n}_h[] = {
+EOF
+  ./mpdump $h
+  cat <<EOF
+};
+
+static const mpw c_${n}_gx[] = {
+EOF
+  ./mpdump $gx
+  cat <<EOF
+};
+
+static const mpw c_${n}_gy[] = {
+EOF
+  ./mpdump $gy
+  cat <<EOF
+};
+
+EOF
+
+  case $f in
+    prime) ftag=FTAG_PRIME;;
+    niceprime) ftag=FTAG_NICEPRIME;;
+    binpoly) ftag=FTAG_BINPOLY;;
+  esac
+  cat <<EOF
+static const ecdata c_$n = {
+  $ftag,
+  c_${n}_p, N(c_${n}_p),
+  c_${n}_a, N(c_${n}_a),
+  c_${n}_b, N(c_${n}_b),
+  c_${n}_r, N(c_${n}_r),
+  c_${n}_h, N(c_${n}_h),
+  c_${n}_gx, N(c_${n}_gx),
+  c_${n}_gy, N(c_${n}_gy)
+};
+
+EOF
+
+done
+
+cat <<EOF
+/*----- Main table --------------------------------------------------------*/
+
+const ecentry ectab[] = {
+EOF
+for i in $names; do
+  a=`echo $i | sed 's/=.*$//'`
+  n=`echo $i | sed 's/^.*=//; s/[^a-zA-Z0-9_][^a-zA-Z0-9_]*/_/g'`
+  cat <<EOF
+  { "$a", &c_$n },
+EOF
+done
+cat <<EOF
+  { 0, 0 }
+};
+
+/*----- That's all, folks -------------------------------------------------*/
+EOF