AtlatestRepositorysigil-crypto

sigil-crypto / tree / test / wasmgen-fixtures.sh

1#!/bin/sh
2# Embed the minisign fixtures into test/wasm/src/crypto-probe/fixtures.sgl as
3# hex, so the probe can run where there is no filesystem (wasm in Node).
4set -eu
5HERE=$(cd "$(dirname "$0")" && pwd)
6FIX="$HERE/../fixtures/minisign"
7OUT="$HERE/src/crypto-probe/fixtures.sgl"
8hex() { od -An -tx1 -v "$1" | tr -d ' \n'; }
9{
10 echo ";;; GENERATED by test/wasm/gen-fixtures.sh from test/fixtures/minisign."
11 echo ";;; Do not edit by hand."
12 echo "(define-library (crypto-probe fixtures)"
13 echo " (export fixture)"
14 echo " (import (sigil core) (sigil math))"
15 echo " (begin"
16 echo " (define (hex->bv s)"
17 echo " (let* ((n (quotient (string-length s) 2))"
18 echo " (bv (make-bytevector n 0)))"
19 echo " (let loop ((i 0))"
20 echo " (if (< i n)"
21 echo " (begin"
22 echo " (bytevector-u8-set! bv i (string->number (substring s (* 2 i) (+ 2 (* 2 i))) 16))"
23 echo " (loop (+ i 1)))"
24 echo " bv))))"
25 echo " (define fixtures"
26 echo " (list"
27 for f in a.pub b.pub hello.txt hello.txt.ED.minisig hello.txt.Ed.minisig \
28 hello.txt.keyb.minisig binary.bin binary.bin.ED.minisig \
29 registry.json registry.json.minisig; do
30 [ -s "$FIX/$f" ] || { echo "REFUSING: missing or empty fixture $f" >&2; exit 1; }
31 printf ' (cons "%s" "%s")\n' "$f" "$(hex "$FIX/$f")"
32 done
33 echo " ))"
34 echo " ;; The fixture's bytes; raises for an unknown name."
35 echo " (define (fixture name)"
36 echo " (let ((entry (assoc name fixtures)))"
37 echo " (if entry"
38 echo " (hex->bv (cdr entry))"
39 echo " (error (string-append \"no such fixture: \" name)))))))"
40} > "$OUT"
41echo "wrote $OUT ($(wc -c < "$OUT") bytes)"