-
Notifications
You must be signed in to change notification settings - Fork 54
Add kat test Windows support (C backend only) #1698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
willieyz
wants to merge
1
commit into
main
Choose a base branch
from
win-sup-kat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) The mlkem-native project authors | ||
| # SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT | ||
|
|
||
| import hashlib | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| # Check if we need to use a wrapper for execution (e.g. QEMU) | ||
| exec_prefix = os.environ.get("EXEC_WRAPPER", "") | ||
| exec_prefix = exec_prefix.split(" ") if exec_prefix != "" else [] | ||
|
|
||
|
|
||
| def err(msg, **kwargs): | ||
| print(msg, file=sys.stderr, **kwargs) | ||
|
|
||
|
|
||
| def info(msg, **kwargs): | ||
| print(msg, **kwargs) | ||
|
|
||
|
|
||
| def read_meta_hashes(): | ||
| hashes = {} | ||
| current_name = None | ||
| with open("META.yml") as f: | ||
| for line in f: | ||
| line = line.strip() | ||
| if line.startswith("- name:"): | ||
| current_name = line.split(":", 1)[1].strip() | ||
| elif line.startswith("kat-sha256:") and current_name: | ||
| hashes[current_name] = line.split(":", 1)[1].strip() | ||
| return hashes | ||
|
|
||
|
|
||
| def get_kat_binary(level): | ||
| suffix = ".exe" if sys.platform == "win32" else "" | ||
| return Path("test/build") / f"mlkem{level}" / "bin" / f"gen_KAT{level}{suffix}" | ||
|
|
||
|
|
||
| def run_kat_single(scheme_name, level, ref_hash): | ||
| binary = get_kat_binary(level) | ||
|
|
||
| if not binary.exists(): | ||
| err(f"Binary not found: {binary}") | ||
| return False | ||
|
|
||
| cmd = exec_prefix + [str(binary)] | ||
| result = subprocess.run(cmd, capture_output=True) | ||
|
|
||
| if result.returncode != 0: | ||
| err("FAIL!") | ||
| err(f"{cmd} failed with error code {result.returncode}") | ||
| err(result.stderr.decode("utf-8", errors="replace")) | ||
| return False | ||
|
|
||
| computed = hashlib.sha256(result.stdout).hexdigest() | ||
|
|
||
| if computed == ref_hash: | ||
| info(f"META.yml {scheme_name} kat-sha256: OK") | ||
| return True | ||
| else: | ||
| err(f"META.yml {scheme_name} kat-sha256: FAIL ({ref_hash} != {computed})") | ||
| return False | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--scheme", | ||
| choices=["512", "768", "1024"], | ||
| help="Run KAT for only one parameter set (default: all)", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| all_schemes = [("ML-KEM-512", 512), ("ML-KEM-768", 768), ("ML-KEM-1024", 1024)] | ||
| if args.scheme is not None: | ||
| schemes = [s for s in all_schemes if str(s[1]) == args.scheme] | ||
| else: | ||
| schemes = all_schemes | ||
|
|
||
| ref_hashes = read_meta_hashes() | ||
|
|
||
| failed = False | ||
| for scheme_name, level in schemes: | ||
| if scheme_name not in ref_hashes: | ||
| err(f"META.yml: no kat-sha256 entry for {scheme_name}") | ||
| failed = True | ||
| continue | ||
| if not run_kat_single(scheme_name, level, ref_hashes[scheme_name]): | ||
| failed = True | ||
|
|
||
| if failed: | ||
| sys.exit(1) | ||
|
|
||
| info("ALL GOOD!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of introducing a second way of running KAT tests.
@hanno-becker, would it be an option to also migrate the other KAT tests to use a Python wrapper?