Skip to content

Commit

Permalink
Emulate arm64 binary on x86_64 machine (#60)
Browse files Browse the repository at this point in the history
* emulate arm64 binary on x86_64 machine

Signed-off-by: Thing-han, Lim <[email protected]>

* Output binaries to bin/ directory

Signed-off-by: Thing-han, Lim <[email protected]>

* add nistkat checksum back

Signed-off-by: Thing-han, Lim <[email protected]>

* use black for python formatting

Signed-off-by: Thing-han, Lim <[email protected]>

* add tests script for running a specify binary

Signed-off-by: Thing-han, Lim <[email protected]>

* factor out the common part of compiling and running a binary

Signed-off-by: Thing-han, Lim <[email protected]>

* add test command for func, nistkat and kat tests

Signed-off-by: Thing-han, Lim <[email protected]>

* compare tests result to the META file

Signed-off-by: Thing-han, Lim <[email protected]>

* add github step summary to tests script if the env var exists

Signed-off-by: Thing-han, Lim <[email protected]>

* update ci to use the tests script

Signed-off-by: Thing-han, Lim <[email protected]>

* cleanup

Signed-off-by: Thing-han, Lim <[email protected]>

* only add -static if is linux x86_64

Signed-off-by: Thing-han, Lim <[email protected]>

* still run kat/nistkat tests even if previous tests failed

Signed-off-by: Thing-han, Lim <[email protected]>

* fix uncompatible makefile macros

Signed-off-by: Thing-han, Lim <[email protected]>

* remove makefile macros

Signed-off-by: Thing-han, Lim <[email protected]>

* place binaries in test/bin instead of bin

Signed-off-by: Thing-han, Lim <[email protected]>

* add --force-qemu flag for tests script

Signed-off-by: Thing-han, Lim <[email protected]>

* add the tests script description to README

Signed-off-by: Thing-han, Lim <[email protected]>

* make ci tests output verbose

Signed-off-by: Thing-han, Lim <[email protected]>

* output full executed make command

Signed-off-by: Thing-han, Lim <[email protected]>

* fix makefile extra quoting

Signed-off-by: Thing-han, Lim <[email protected]>

---------

Signed-off-by: Thing-han, Lim <[email protected]>
  • Loading branch information
potsrevennil authored Jun 17, 2024
1 parent 45c91ee commit 3d23dda
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 99 deletions.
45 changes: 16 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ jobs:
matrix:
include:
- system: macos-latest
skipped: false
expect_arch: arm64
- system: pqcp-arm64
skipped: false
expect_arch: aarch64
- system: ubuntu-latest
cross_prefix: aarch64-none-linux-gnu-
skipped: true
expect_arch: x86_64
name: build_kat (${{ matrix.system }})
runs-on: ${{ matrix.system }}
Expand All @@ -45,38 +42,28 @@ jobs:
echo ":x: Expecting to run on ${{ matrix.expect_arch }}, but instead running on $ARCH" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Build targets
- name: Run functional tests
id: func_test
shell: nix develop .#ci -c bash -e {0}
run: |
make CROSS_PREFIX=${{ matrix.cross_prefix }} mlkem
if [ ${{ matrix.skipped }} != true ]; then
./test/test_kyber512
./test/test_kyber768
./test/test_kyber1024
fi
- name: Compare gen_KAT with known hash
tests func -v
- name: Run KAT tests
id: kat_test
if: |
success()
|| steps.func_test.conclusion == 'failure'
shell: nix develop .#ci -c bash -e {0}
run: |
cat >> $GITHUB_STEP_SUMMARY <<-EOF
## KAT tests
EOF
make CROSS_PREFIX=${{ matrix.cross_prefix }} kat
if [ ${{ matrix.skipped }} != true ]; then
cat >> $GITHUB_STEP_SUMMARY <<-EOF
$(checksum ./test/gen_KAT512 ec4ac397e595ac7457cb7d8830921faf3290898a10d7dd3864aab89ea61fe9a3)
$(checksum ./test/gen_KAT768 9a0826ad3c5232dfd3b21bc4801408655c565a491b760f509b2ee2cd7180babe)
$(checksum ./test/gen_KAT1024 6dafb867599b750a6a831b03e494cf41dea748c78a0e275e7b268bbb893cf37d)
EOF
fi
- name: Compare gen_NISTKAT with known hash
tests kat -v
- name: Run Nistkat tests
id: nistkat_test
if: |
success()
|| steps.func_test.conclusion == 'failure'
|| steps.kat_test.conclusion == 'failure'
shell: nix develop .#ci -c bash -e {0}
run: |
cat >> $GITHUB_STEP_SUMMARY <<-EOF
## NISTKAT tests
EOF
tests nistkat -v
lint:
strategy:
matrix:
Expand Down
10 changes: 1 addition & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,4 @@
.direnv
.vscode
.idea
test/test_kyber512
test/test_kyber768
test/test_kyber1024
test/gen_KAT512
test/gen_KAT768
test/gen_KAT1024
test/gen_NISTKAT512
test/gen_NISTKAT768
test/gen_NISTKAT1024
test/bin/
35 changes: 35 additions & 0 deletions META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-License-Identifier: Apache-2.0

name: Mlkem
type: kem
implementations:
- name: mlkem512
claimed-nist-level: 1
claimed-security: IND-CCA2
length-public-key: 800
length-ciphertext: 768
length-secret-key: 1632
length-shared-secret: 32
kat-sha256: ec4ac397e595ac7457cb7d8830921faf3290898a10d7dd3864aab89ea61fe9a3
nistkat-sha256: 4b88ac7643ff60209af1175e025f354272e88df827a0ce1c056e403629b88e04
nistkat-shake256-256: 4b075815c72f4984e2290a2ebb62f6aa2c42bb386b9a210a78bf6ac73ee02cb8
- name: mlkem768
claimed-nist-level: 3
claimed-security: IND-CCA2
length-public-key: 1184
length-ciphertext: 1088
length-secret-key: 2400
length-shared-secret: 32
kat-sha256: 9a0826ad3c5232dfd3b21bc4801408655c565a491b760f509b2ee2cd7180babe
nistkat-sha256: 21b4a1e1ea34a13c26a9da5eeb9325afb5ca11596ca6f3704c3f2637e3ea7524
nistkat-shake256-256: 3acd660b1b60808c1b8b02f499ffdc4bdacaaf35ec02a267b7a8e40dd4b26457
- name: mlkem1024
claimed-nist-level: 5
claimed-security: IND-CCA2
length-public-key: 1568
length-ciphertext: 1568
length-secret-key: 3168
length-shared-secret: 32
kat-sha256: 6dafb867599b750a6a831b03e494cf41dea748c78a0e275e7b268bbb893cf37d
nistkat-sha256: 6471398b0a728ee1ef39e93bb89b526fbf59587a3662edadbcfc6c88a512cd71
nistkat-shake256-256: e619f782857675c7d273139a48f8081652cc9c583c92aa4e627a2f36a7d943d1
113 changes: 67 additions & 46 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

Q ?= @
QEMU = qemu-aarch64
CROSS_PREFIX ?=
CC := $(CROSS_PREFIX)gcc

Expand All @@ -11,6 +12,12 @@ INCLUDE_NISTRANDOM = -I test/nistrng
CFLAGS += -Wall -Wextra -Wpedantic -Wmissing-prototypes -Wredundant-decls \
-Wshadow -Wpointer-arith -O3 -fomit-frame-pointer -pedantic \
${INCLUDE_MLKEM} ${INCLUDE_FIPS202}

HOST_PLATFORM := $(shell uname -s)-$(shell uname -m)
ifeq ($(HOST_PLATFORM),Linux-x86_64)
CFLAGS += -static
endif

CFLAGS_RANDOMBYTES = ${CFLAGS} ${INCLUDE_RANDOM}
CFLAGS_NISTRANDOMBYTES = ${CFLAGS} ${INCLUDE_NISTRANDOM}
NISTFLAGS += -Wno-unused-result -O3 -fomit-frame-pointer
Expand All @@ -31,56 +38,70 @@ HEADERNISTKATS = $(HEADERSKECCAK) test/nistrng/aes.h test/nistrng/randombytes.h
all: mlkem kat nistkat

mlkem: \
test/test_kyber512 \
test/test_kyber768 \
test/test_kyber1024
test/bin/test_kyber512 \
test/bin/test_kyber768 \
test/bin/test_kyber1024

nistkat: \
test/gen_NISTKAT512 \
test/gen_NISTKAT768 \
test/gen_NISTKAT1024
test/bin/gen_NISTKAT512 \
test/bin/gen_NISTKAT768 \
test/bin/gen_NISTKAT1024

kat: \
test/gen_KAT512 \
test/gen_KAT768 \
test/gen_KAT1024

test/test_kyber512: $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) test/test_kyber.c
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) test/test_kyber.c -o $@

test/test_kyber768: $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) test/test_kyber.c
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) test/test_kyber.c -o $@

test/test_kyber1024: $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) test/test_kyber.c
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) test/test_kyber.c -o $@

test/gen_KAT512: $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) test/gen_KAT.c
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) test/gen_KAT.c -o $@

test/gen_KAT768: $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) test/gen_KAT.c
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) test/gen_KAT.c -o $@

test/gen_KAT1024: $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) test/gen_KAT.c
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) test/gen_KAT.c -o $@

test/gen_NISTKAT512: $(SOURCESNISTKATS) $(HEADERNISTKATS) test/gen_NISTKAT.c
$(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=2 $(SOURCESNISTKATS) test/gen_NISTKAT.c -o $@

test/gen_NISTKAT768: $(SOURCESNISTKATS) $(HEADERNISTKATS) test/gen_NISTKAT.c
$(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=3 $(SOURCESNISTKATS) test/gen_NISTKAT.c -o $@

test/gen_NISTKAT1024: $(SOURCESNISTKATS) $(HEADERNISTKATS) test/gen_NISTKAT.c
$(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=4 $(SOURCESNISTKATS) test/gen_NISTKAT.c -o $@

test/bin/gen_KAT512 \
test/bin/gen_KAT768 \
test/bin/gen_KAT1024

test/bin/test_kyber512: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@

test/bin/test_kyber768: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@

test/bin/test_kyber1024: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@

test/bin/gen_KAT512: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@

test/bin/gen_KAT768: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@

test/bin/gen_KAT1024: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@

test/bin/gen_NISTKAT512: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=2 $(SOURCESNISTKATS) $< -o $@

test/bin/gen_NISTKAT768: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=3 $(SOURCESNISTKATS) $< -o $@

test/bin/gen_NISTKAT1024: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS)
$(Q)echo " CC $@"
$(Q)[ -d $(@D) ] || mkdir -p $(@D)
$(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=4 $(SOURCESNISTKATS) $< -o $@

# emulate ARM64 binary on x86_64 machine
emulate:
$(Q)$(MAKE) --quiet CROSS_PREFIX=aarch64-none-linux-gnu- $(TARGET)
$(Q)$(QEMU) $(TARGET)

clean:
-$(RM) -rf *.gcno *.gcda *.lcov *.o *.so
-$(RM) -rf test/test_kyber512
-$(RM) -rf test/test_kyber768
-$(RM) -rf test/test_kyber1024
-$(RM) -rf test/gen_KAT512
-$(RM) -rf test/gen_KAT768
-$(RM) -rf test/gen_KAT1024
-$(RM) -rf test/gen_NISTKAT512
-$(RM) -rf test/gen_NISTKAT768
-$(RM) -rf test/gen_NISTKAT1024
-$(RM) -rf test/bin
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ experimental-features = nix-command flakes
After running `nix develop` you should automatically have a number of support scripts in your PATH:

- [`format`](scripts/format) formats all files. The format is enforced by our CI, so you should run this script prior to committing.
- [`tests`](scripts/tests) run functional, kat tests natively or emulate them using `QEMU`. For information on how to use the script, please refer to the `--help` option.

## Call for contributors

Expand Down
8 changes: 7 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@
cbmc = cbmc;

inherit (pkgs)
yq
ninja# 1.11.1

qemu# 8.2.4
# formatter & linters
cadical
nixpkgs-fmt
shfmt;

inherit (pkgs.python3Packages)
python
black
click;
}
++ {
"x86_64-linux" = [ (pkgs.callPackage ./arm-gnu-gcc.nix { }) ];
Expand Down
14 changes: 0 additions & 14 deletions scripts/checksum

This file was deleted.

10 changes: 10 additions & 0 deletions scripts/ci/lint
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ echo "::group::Linting shell scripts with shfmt"
checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
echo "::endgroup::"

echo "::group::Linting python scripts with black"
if ! diff=$(black --check --diff -q --include scripts/tests "$ROOT"); then
echo "::error title=Format error::$diff"
SUCCESS=false
echo ":x: Lint python" >>"$GITHUB_STEP_SUMMARY"
else
echo ":white_check_mark: Lint Python" >>"$GITHUB_STEP_SUMMARY"
fi
echo "::endgroup::"

echo "::group::Linting c files with astyle"
checkerr "Lint C" "$(astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --dry-run --formatted | awk '{print $2}')"
echo "::endgroup::"
Expand Down
3 changes: 3 additions & 0 deletions scripts/format
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ nixpkgs-fmt "$ROOT"
info "Formatting shell scripts"
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))

info "Formatting python scripts"
black --include scripts/tests "$ROOT"

info "Formatting c files"
astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --formatted | awk '{print $2}'

Expand Down
Loading

0 comments on commit 3d23dda

Please sign in to comment.