sfinx

sphincs+ inspired signing utility
git clone git://git.finwo.net/app/sfinx
Log | Files | Refs | README

commit dd80abd95d63eb3c26e09acec92366a733e9d362
parent 0cb4ad7fd4929de95c7106ca7d09f041c061c00e
Author: finwo <finwo@pm.me>
Date:   Fri,  9 Oct 2026 15:34:18 +0200

e4m4 encoding built and tested

Diffstat:
MMakefile | 4++++
Asrc/sfinx.c | 41+++++++++++++++++++++++++++++++++++++++++
Atest/00-e4m4.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/Makefile | 30++++++++++++++++++++++++++++++
Atest/_tap.c | 34++++++++++++++++++++++++++++++++++
5 files changed, 178 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -29,6 +29,10 @@ targets: clean: rm -rf build +.PHONY: test +test: + @$(MAKE) --no-print-directory --directory test run + # Never let the catch-all try to rebuild the makefiles themselves Makefile: ; diff --git a/src/sfinx.c b/src/sfinx.c @@ -0,0 +1,41 @@ +#include <stdint.h> + +// E4M4 {{{ +// +// Log-ish number format, only positive +// E=0: M +// E>0: (16+M)<<(E-1) + +static inline uint32_t _sfinx_e4m4_decode(uint8_t byte) { + uint8_t exponent = (uint8_t)(byte >> 4); + uint8_t mantissa = (uint8_t)(byte & 0x0f); + if (exponent == 0) { + return mantissa; + } + return (uint32_t)(16 + mantissa) << (exponent - 1); +} + +static inline uint32_t _sfinx_e4m4_encode(uint32_t value, uint8_t *out) { + if (value <= 15) { + *out = (uint8_t)value; + return 1; + } + + uint32_t base = value; + uint32_t shift = 0; + while (base > 31) { + base >>= 1; + if (++shift > 14) { + return 0; + } + } + if (base < 16 || (base << shift) != value) { + return 0; + } + + *out = (uint8_t)(((shift + 1) << 4) | (base - 16)); + return 1; +} +// }}} + +// vim:fdm=marker:fdl=0 diff --git a/test/00-e4m4.c b/test/00-e4m4.c @@ -0,0 +1,69 @@ +#include "src/sfinx.c" +#include "_tap.c" + +#include <inttypes.h> +#include <limits.h> + +static void check_u32(uint32_t got, uint32_t want, const char *what) { + tap(got == want, what, "got %" PRIu32 ", want %" PRIu32, got, want); +} + +static void check_encode(uint32_t value, uint8_t want, const char *what) { + uint8_t got = 0xaa; + if (_sfinx_e4m4_encode(value, &got)) { + tap(got == want, what, "got 0x%02x, want 0x%02x", got, want); + } else { + tap(0, what, "encode(%" PRIu32 ") not representable", value); + } +} + +int main(void) { + uint8_t scratch = 0; + uint32_t round_ok = 1; + int bad = -1; + + tap_begin("sfinx E4M4"); + + check_u32(_sfinx_e4m4_decode(0x00), 0, "decode 0x00"); + check_u32(_sfinx_e4m4_decode(0x0f), 15, "decode 0x0f"); + check_u32(_sfinx_e4m4_decode(0x10), 16, "decode 0x10"); + check_u32(_sfinx_e4m4_decode(0x1c), 28, "decode 0x1c"); + check_u32(_sfinx_e4m4_decode(0x1f), 31, "decode 0x1f"); + check_u32(_sfinx_e4m4_decode(0x20), 32, "decode 0x20"); + check_u32(_sfinx_e4m4_decode(0x28), 48, "decode 0x28"); + check_u32(_sfinx_e4m4_decode(0x2f), 62, "decode 0x2f"); + check_u32(_sfinx_e4m4_decode(0x30), 64, "decode 0x30"); + check_u32(_sfinx_e4m4_decode(0x3f), 124, "decode 0x3f"); + check_u32(_sfinx_e4m4_decode(0x40), 128, "decode 0x40"); + check_u32(_sfinx_e4m4_decode(0xff), 507904, "decode 0xff"); + + check_encode(0, 0x00, "encode 0"); + check_encode(15, 0x0f, "encode 15"); + check_encode(16, 0x10, "encode 16"); + check_encode(28, 0x1c, "encode 28 (L 224)"); + check_encode(32, 0x20, "encode 32 (N default)"); + check_encode(48, 0x28, "encode 48 (L 384)"); + check_encode(64, 0x30, "encode 64 (L 512)"); + check_encode(507904, 0xff, "encode max"); + + check_u32(_sfinx_e4m4_encode(33, &scratch), 0, "reject 33"); + check_u32(_sfinx_e4m4_encode(63, &scratch), 0, "reject 63"); + check_u32(_sfinx_e4m4_encode(125, &scratch), 0, "reject 125"); + check_u32(_sfinx_e4m4_encode(507905, &scratch), 0, "reject max + 1"); + check_u32(_sfinx_e4m4_encode(524288, &scratch), 0, "reject 16 << 15"); + check_u32(_sfinx_e4m4_encode(INT_MAX, &scratch), 0, "reject INT_MAX"); + check_u32(_sfinx_e4m4_encode(UINT32_MAX, &scratch), 0, "reject UINT32_MAX"); + + for (int i = 0; i < 256; i++) { + uint8_t round = 0; + uint32_t value = _sfinx_e4m4_decode((uint8_t)i); + if (!_sfinx_e4m4_encode(value, &round) || round != (uint8_t)i) { + round_ok = 0; + bad = i; + break; + } + } + tap(round_ok, "every byte round trips", "byte 0x%02x did not", (unsigned)bad); + + return tap_plan(); +} diff --git a/test/Makefile b/test/Makefile @@ -0,0 +1,30 @@ +CC ?= cc +CFLAGS ?= -Wall -Wextra -O2 +CFLAGS += -I.. + +OUT := ../build/test +SRCS := $(filter-out _tap.c,$(wildcard *.c)) +TESTS := $(patsubst %.c,%,$(SRCS)) +BINS := $(addprefix $(OUT)/,$(TESTS)) + +.PHONY: all +all: $(BINS) + +$(OUT): + @mkdir -p $(OUT) + +$(OUT)/%: %.c ../src/sfinx.c _tap.c | $(OUT) + @$(CC) $(CFLAGS) $< -o $@ + +.PHONY: run +run: all + @rc=0; \ + for t in $(BINS); do \ + echo "# $$t"; \ + $$t || rc=1; \ + done; \ + exit $$rc + +.PHONY: clean +clean: + rm -rf $(OUT) diff --git a/test/_tap.c b/test/_tap.c @@ -0,0 +1,34 @@ +#include <stdarg.h> +#include <stdint.h> +#include <stdio.h> + +static unsigned test_no; +static unsigned test_failures; + +static void tap_begin(const char *name) { + printf("TAP version 13\n"); + printf("# %s\n", name); +} + +static void tap(uint32_t ok, const char *what, const char *fmt, ...) { + test_no++; + printf("%s %u - %s\n", ok ? "ok" : "not ok", test_no, what); + if (!ok) { + va_list ap; + va_start(ap, fmt); + fputs("# ", stdout); + vprintf(fmt, ap); + fputc('\n', stdout); + va_end(ap); + test_failures++; + } +} + +static int tap_plan(void) { + printf("1..%u\n", test_no); + if (test_failures) { + printf("# %u of %u assertions failed\n", test_failures, test_no); + return 1; + } + return 0; +}