@@@ test keccak and sha3
[secnet] / keccak1600-test.c
diff --git a/keccak1600-test.c b/keccak1600-test.c
new file mode 100644 (file)
index 0000000..e0102ac
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * keccak1600-test.c: test harness for Keccak primitive
+ *
+ * (The implementations originally came with different test arrangements,
+ * with complicated external dependencies.  This file replicates the original
+ * tests, but without the dependencies.)
+ */
+/*
+ * This file is Free Software.  It was originally written for secnet.
+ *
+ * Copyright 2019 Mark Wooding
+ *
+ * You may redistribute secnet as a whole and/or modify it under the
+ * terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3, or (at your option) any
+ * later version.
+ *
+ * You may redistribute this file and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software
+ * Foundation; either version 2, or (at your option) any later
+ * version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this software; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
+ */
+
+#include <stdio.h>
+
+#include "secnet.h"
+
+#include "keccak1600.h"
+#include "crypto-test.h"
+
+enum {
+    RZ, NROUT,
+    RX = NROUT, RN, NREG
+};
+
+static void test_p(struct reg *out, const struct reg *in, void *ctx)
+{
+    keccak1600_state u;
+    kludge64 t[25];
+    unsigned i;
+
+    allocate_bytes(&out[RZ].v, 200);
+    keccak1600_init(&u);
+    for (i = 0; i < 25; i++) LOAD64_L_(t[i], in[RX].v.bytes.p + 8*i);
+    keccak1600_mix(&u, t, 25);
+    keccak1600_p(&u, &u, in[RN].v.u);
+    keccak1600_extract(&u, t, 25);
+    for (i = 0; i < 25; i++) STORE64_L_(out[RZ].v.bytes.p + 8*i, t[i]);
+}
+
+#define REG_X { "x", RX, &regty_bytes, 0 }
+#define REG_N { "n", RN, &regty_uint, 0 }
+#define REG_Z { "z", RZ, &regty_bytes, 0 }
+static const struct regdef
+    p_regs[] = { REG_X, REG_N, REG_Z, REGLIST_END };
+
+static const struct test tests[] = {
+    { "p", run_test, p_regs, test_p },
+    { 0 }
+};
+
+int main(void)
+    { return run_test_suite(NROUT, NREG, sizeof(struct reg), tests, stdin); }