summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2016-11-03 23:13:28 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2016-11-03 23:13:28 +0100
commit284ef310cb4370b5892200d92b3c2e61649ff975 (patch)
tree288b6dea366515099bc6657e3fd93112dd69f82f
downloadpolybench-284ef310cb4370b5892200d92b3c2e61649ff975.tar.xz
polybench-284ef310cb4370b5892200d92b3c2e61649ff975.zip
Initial commit
-rw-r--r--Makefile14
-rw-r--r--polybench.c278
2 files changed, 292 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..526c462
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+obj-m := polybench.o
+KERNELDIR ?= /lib/modules/$(shell uname -r)/build
+PWD := $(shell pwd)
+
+all: polybench.ko
+polybench.ko: polybench.c
+ $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
+clean:
+ $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
+run: all
+ @sudo insmod polybench.ko 2>/dev/null || true
+ @dmesg | grep "poly1305 benchmark:" | tail -n 1
+
+.PHONY: all clean run
diff --git a/polybench.c b/polybench.c
new file mode 100644
index 0000000..80eb9df
--- /dev/null
+++ b/polybench.c
@@ -0,0 +1,278 @@
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/module.h>
+#include <linux/random.h>
+#include <asm/unaligned.h>
+
+#define USE_FIRST_METHOD
+
+#define POLY1305_BLOCK_SIZE 16
+#define POLY1305_KEY_SIZE 32
+#define POLY1305_MAC_SIZE 16
+
+struct poly1305_ctx {
+ /* key */
+ u32 r[5];
+ /* finalize key */
+ u32 s[4];
+ /* accumulator */
+ u32 h[5];
+ /* partial buffer */
+ u8 buf[POLY1305_BLOCK_SIZE];
+ /* bytes used in partial buffer */
+ unsigned int buflen;
+};
+
+static inline u32 le32_to_cpuvp(const void *p)
+{
+ return le32_to_cpup(p);
+}
+
+static inline u64 mlt(u64 a, u64 b)
+{
+ return a * b;
+}
+
+static inline u32 sr(u64 v, u_char n)
+{
+ return v >> n;
+}
+
+static inline u32 and(u32 v, u32 mask)
+{
+ return v & mask;
+}
+
+static void poly1305_init(struct poly1305_ctx *ctx, const u8 key[static POLY1305_KEY_SIZE])
+{
+#ifndef USE_FIRST_METHOD
+ u32 t0, t1, t2, t3;
+#endif
+ memset(ctx, 0, sizeof(struct poly1305_ctx));
+ /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
+#ifdef USE_FIRST_METHOD
+ ctx->r[0] = (le32_to_cpuvp(key + 0) >> 0) & 0x3ffffff;
+ ctx->r[1] = (get_unaligned_le32(key + 3) >> 2) & 0x3ffff03;
+ ctx->r[2] = (get_unaligned_le32(key + 6) >> 4) & 0x3ffc0ff;
+ ctx->r[3] = (get_unaligned_le32(key + 9) >> 6) & 0x3f03fff;
+ ctx->r[4] = (le32_to_cpuvp(key + 12) >> 8) & 0x00fffff;
+#else
+ t0 = le32_to_cpuvp(key + 0);
+ t1 = le32_to_cpuvp(key + 4);
+ t2 = le32_to_cpuvp(key + 8);
+ t3 = le32_to_cpuvp(key + 12);
+ ctx->r[0] = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6;
+ ctx->r[1] = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12;
+ ctx->r[2] = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18;
+ ctx->r[3] = t2 & 0x3f03fff; t3 >>= 8;
+ ctx->r[4] = t3 & 0x00fffff;
+#endif
+ ctx->s[0] = le32_to_cpuvp(key + 16);
+ ctx->s[1] = le32_to_cpuvp(key + 20);
+ ctx->s[2] = le32_to_cpuvp(key + 24);
+ ctx->s[3] = le32_to_cpuvp(key + 28);
+}
+
+static unsigned int poly1305_generic_blocks(struct poly1305_ctx *ctx, const u8 *src, unsigned int srclen, u32 hibit)
+{
+ u32 r0, r1, r2, r3, r4;
+ u32 s1, s2, s3, s4;
+ u32 h0, h1, h2, h3, h4;
+ u64 d0, d1, d2, d3, d4;
+#ifndef USE_FIRST_METHOD
+ u32 t0, t1, t2, t3;
+#endif
+
+ r0 = ctx->r[0];
+ r1 = ctx->r[1];
+ r2 = ctx->r[2];
+ r3 = ctx->r[3];
+ r4 = ctx->r[4];
+
+ s1 = r1 * 5;
+ s2 = r2 * 5;
+ s3 = r3 * 5;
+ s4 = r4 * 5;
+
+ h0 = ctx->h[0];
+ h1 = ctx->h[1];
+ h2 = ctx->h[2];
+ h3 = ctx->h[3];
+ h4 = ctx->h[4];
+
+ while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
+ /* h += m[i] */
+#ifdef USE_FIRST_METHOD
+ h0 += (le32_to_cpuvp(src + 0) >> 0) & 0x3ffffff;
+ h1 += (get_unaligned_le32(src + 3) >> 2) & 0x3ffffff;
+ h2 += (get_unaligned_le32(src + 6) >> 4) & 0x3ffffff;
+ h3 += (get_unaligned_le32(src + 9) >> 6) & 0x3ffffff;
+ h4 += (le32_to_cpuvp(src + 12) >> 8) | hibit;
+#else
+ t0 = le32_to_cpuvp(src + 0);
+ t1 = le32_to_cpuvp(src + 4);
+ t2 = le32_to_cpuvp(src + 8);
+ t3 = le32_to_cpuvp(src + 12);
+ h0 += t0 & 0x3ffffff;
+ h1 += sr((((u64)t1 << 32) | t0), 26) & 0x3ffffff;
+ h2 += sr((((u64)t2 << 32) | t1), 20) & 0x3ffffff;
+ h3 += sr((((u64)t3 << 32) | t2), 14) & 0x3ffffff;
+ h4 += (t3 >> 8) | hibit;
+#endif
+
+ /* h *= r */
+ d0 = mlt(h0, r0) + mlt(h1, s4) + mlt(h2, s3) + mlt(h3, s2) + mlt(h4, s1);
+ d1 = mlt(h0, r1) + mlt(h1, r0) + mlt(h2, s4) + mlt(h3, s3) + mlt(h4, s2);
+ d2 = mlt(h0, r2) + mlt(h1, r1) + mlt(h2, r0) + mlt(h3, s4) + mlt(h4, s3);
+ d3 = mlt(h0, r3) + mlt(h1, r2) + mlt(h2, r1) + mlt(h3, r0) + mlt(h4, s4);
+ d4 = mlt(h0, r4) + mlt(h1, r3) + mlt(h2, r2) + mlt(h3, r1) + mlt(h4, r0);
+
+ /* (partial) h %= p */
+ d1 += sr(d0, 26); h0 = and(d0, 0x3ffffff);
+ d2 += sr(d1, 26); h1 = and(d1, 0x3ffffff);
+ d3 += sr(d2, 26); h2 = and(d2, 0x3ffffff);
+ d4 += sr(d3, 26); h3 = and(d3, 0x3ffffff);
+ h0 += sr(d4, 26) * 5; h4 = and(d4, 0x3ffffff);
+ h1 += h0 >> 26; h0 = h0 & 0x3ffffff;
+
+ src += POLY1305_BLOCK_SIZE;
+ srclen -= POLY1305_BLOCK_SIZE;
+ }
+
+ ctx->h[0] = h0;
+ ctx->h[1] = h1;
+ ctx->h[2] = h2;
+ ctx->h[3] = h3;
+ ctx->h[4] = h4;
+
+ return srclen;
+}
+
+static void poly1305_update(struct poly1305_ctx *ctx, const u8 *src, unsigned int srclen)
+{
+ unsigned int bytes;
+
+ if (unlikely(ctx->buflen)) {
+ bytes = min(srclen, POLY1305_BLOCK_SIZE - ctx->buflen);
+ memcpy(ctx->buf + ctx->buflen, src, bytes);
+ src += bytes;
+ srclen -= bytes;
+ ctx->buflen += bytes;
+
+ if (ctx->buflen == POLY1305_BLOCK_SIZE) {
+ poly1305_generic_blocks(ctx, ctx->buf, POLY1305_BLOCK_SIZE, 1 << 24);
+ ctx->buflen = 0;
+ }
+ }
+
+ if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
+ bytes = poly1305_generic_blocks(ctx, src, srclen, 1 << 24);
+ src += srclen - bytes;
+ srclen = bytes;
+ }
+
+ if (unlikely(srclen)) {
+ ctx->buflen = srclen;
+ memcpy(ctx->buf, src, srclen);
+ }
+}
+
+static void poly1305_finish(struct poly1305_ctx *ctx, u8 *dst)
+{
+ __le32 *mac = (__le32 *)dst;
+ u32 h0, h1, h2, h3, h4;
+ u32 g0, g1, g2, g3, g4;
+ u32 mask;
+ u64 f = 0;
+
+ if (unlikely(ctx->buflen)) {
+ ctx->buf[ctx->buflen++] = 1;
+ memset(ctx->buf + ctx->buflen, 0, POLY1305_BLOCK_SIZE - ctx->buflen);
+ poly1305_generic_blocks(ctx, ctx->buf, POLY1305_BLOCK_SIZE, 0);
+ }
+
+ /* fully carry h */
+ h0 = ctx->h[0];
+ h1 = ctx->h[1];
+ h2 = ctx->h[2];
+ h3 = ctx->h[3];
+ h4 = ctx->h[4];
+
+ h2 += (h1 >> 26); h1 = h1 & 0x3ffffff;
+ h3 += (h2 >> 26); h2 = h2 & 0x3ffffff;
+ h4 += (h3 >> 26); h3 = h3 & 0x3ffffff;
+ h0 += (h4 >> 26) * 5; h4 = h4 & 0x3ffffff;
+ h1 += (h0 >> 26); h0 = h0 & 0x3ffffff;
+
+ /* compute h + -p */
+ g0 = h0 + 5;
+ g1 = h1 + (g0 >> 26); g0 &= 0x3ffffff;
+ g2 = h2 + (g1 >> 26); g1 &= 0x3ffffff;
+ g3 = h3 + (g2 >> 26); g2 &= 0x3ffffff;
+ g4 = h4 + (g3 >> 26) - (1 << 26); g3 &= 0x3ffffff;
+
+ /* select h if h < p, or h + -p if h >= p */
+ mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
+ g0 &= mask;
+ g1 &= mask;
+ g2 &= mask;
+ g3 &= mask;
+ g4 &= mask;
+ mask = ~mask;
+ h0 = (h0 & mask) | g0;
+ h1 = (h1 & mask) | g1;
+ h2 = (h2 & mask) | g2;
+ h3 = (h3 & mask) | g3;
+ h4 = (h4 & mask) | g4;
+
+ /* h = h % (2^128) */
+ h0 = (h0 >> 0) | (h1 << 26);
+ h1 = (h1 >> 6) | (h2 << 20);
+ h2 = (h2 >> 12) | (h3 << 14);
+ h3 = (h3 >> 18) | (h4 << 8);
+
+ /* mac = (h + s) % (2^128) */
+ f = (f >> 32) + h0 + ctx->s[0]; mac[0] = cpu_to_le32(f);
+ f = (f >> 32) + h1 + ctx->s[1]; mac[1] = cpu_to_le32(f);
+ f = (f >> 32) + h2 + ctx->s[2]; mac[2] = cpu_to_le32(f);
+ f = (f >> 32) + h3 + ctx->s[3]; mac[3] = cpu_to_le32(f);
+}
+
+static int __init mod_init(void)
+{
+ ktime_t start;
+ s64 elapsed_ns;
+ unsigned int i, j;
+ u8 key[POLY1305_KEY_SIZE];
+ u8 data[POLY1305_BLOCK_SIZE];
+ u8 big_data[8 * POLY1305_BLOCK_SIZE];
+ u8 output[POLY1305_MAC_SIZE];
+ struct poly1305_ctx ctx;
+
+ get_random_bytes(key, POLY1305_KEY_SIZE);
+ get_random_bytes(data, POLY1305_BLOCK_SIZE);
+ get_random_bytes(big_data, 8 * POLY1305_BLOCK_SIZE);
+
+ start = ktime_get();
+ for (i = 0; i < 4096 * 128; ++i) {
+ poly1305_init(&ctx, key);
+ for (j = 0; j < 128; ++j) {
+ poly1305_update(&ctx, data, POLY1305_BLOCK_SIZE);
+ poly1305_update(&ctx, big_data, 8 * POLY1305_BLOCK_SIZE);
+ }
+ poly1305_finish(&ctx, output);
+ }
+ elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
+
+ pr_info("poly1305 benchmark: %lld nanoseconds\n", elapsed_ns);
+ return -EINVAL; // We don't actually want to insert this module...
+}
+static void __exit mod_exit(void)
+{
+}
+
+module_init(mod_init);
+module_exit(mod_exit);
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Poly1305 Generic C Benchmarker");
+MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");