Skip to content

Commit

Permalink
Add META.sh file to extract data from META.json
Browse files Browse the repository at this point in the history
This commit adds a basic script META.sh to query the META.json.
If jq is present, it's used for the query. Otherwise, some basic
fiddling with cat, grep, cut, and tr is used.

This is used to conduct a bash-based KAT test in `make quickcheck`,
rather then running the test scripts, which already require a Python
setup.

Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Dec 22, 2024
1 parent f5703c8 commit 6a49c8a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
44 changes: 44 additions & 0 deletions META.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

Check failure on line 1 in META.sh

View workflow job for this annotation

GitHub Actions / Linting (ubuntu-latest)

Format error

META.sh require to be formatted

Check failure on line 1 in META.sh

View workflow job for this annotation

GitHub Actions / Linting (ubuntu-latest)

Missing license header error

META.sh is missing SPDX License header

# Helper script to query META.json
#
# Arguments
# - Scheme to query: ML-KEM-512, ML-KEM-768, ML-KEM-1024
# - Field to query, e.g. "kat-sha256"
#
# Optional:
# - Value to compare against

META=META.json

# Manual extraction of metadata with basic cmd line tools
VAL=$(cat $META |
grep "name\|\"$2\"" |
grep $1 -A 1 |
grep $2 |
cut -d ":" -f 2 |
tr -d '", ')

# More robust extraction using jq
if (which jq 2>&1 >/dev/null); then
QUERY=".implementations | .[] | select(.name==\"$1\") | .\"$2\""
VAL_JQ=$(cat $META | jq "$QUERY" -r)

if [[ $VAL_JQ != $VAL ]]; then
echo "ERROR parsing metadata file $META"
exit 1
fi
fi

INPUT=$3
if [[ "$INPUT" != "" ]]; then
if [[ "$INPUT" != "$VAL" ]]; then
echo "$META $1 $2: FAIL ($VAL != $INPUT)"
exit 1
else
echo "$META $1 $2: OK"
exit 0
fi
else
echo $VAL
fi
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ buildall: mlkem nistkat kat acvp
$(Q)echo " Everything builds fine!"

quickcheck: buildall
# Run basic functionality checks
$(MLKEM512_DIR)/bin/test_mlkem512
$(MLKEM768_DIR)/bin/test_mlkem768
$(MLKEM1024_DIR)/bin/test_mlkem1024
./scripts/acvp
$(Q)echo " Functionality and ACVP tests passed!"

$(MLKEM512_DIR)/bin/gen_KAT512 | sha256sum | cut -d " " -f 1 | xargs ./META.sh ML-KEM-512 kat-sha256
$(MLKEM768_DIR)/bin/gen_KAT768 | sha256sum | cut -d " " -f 1 | xargs ./META.sh ML-KEM-768 kat-sha256
$(MLKEM1024_DIR)/bin/gen_KAT1024 | sha256sum | cut -d " " -f 1 | xargs ./META.sh ML-KEM-1024 kat-sha256

lib: $(BUILD_DIR)/libmlkem.a

Expand Down

0 comments on commit 6a49c8a

Please sign in to comment.