-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update CONTRIBUTING to use ML-KEM as example for test vectors, as it is the most up-to-date primitive currently. Add the new-vectors-import.py template for parsing test vectors, including a function that generates the JSON file that enumerates the available vectors for the given parameters.
- Loading branch information
1 parent
7fdda2b
commit fb43ae1
Showing
6 changed files
with
142 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,70 @@ | ||
"""Script to import PLACEHOLDER vectors. | ||
.. caution:: | ||
This module is intended for developers of this tool, as it's only used for | ||
testing and packaging, has hard-coded filenames, and uses relative paths. | ||
""" | ||
|
||
import json | ||
from collections import defaultdict | ||
from pathlib import Path | ||
|
||
from crypto_condor.vectors._LCPLACEHOLDER.LCPLACEHOLDER_pb2 import CapPLACEHOLDERVectors | ||
|
||
VECTORS_DIR = Path("crypto_condor/vectors/_LCPLACEHOLDER") | ||
|
||
|
||
def generate_json() -> None: | ||
"""Generates the JSON file indexing the vectors.""" | ||
pb2_dir = VECTORS_DIR / "pb2" | ||
|
||
# This is an example of a single level dictionary. Using defaultdict(list) means | ||
# that we can easily append values to a new key without having to check the | ||
# existence of the key or the list. | ||
vectors: dict[str, list[str]] = defaultdict(list) | ||
|
||
# This is an example of a two-level dict based on ECDH, whose vectors are separated | ||
# by elliptic curve, and then by type of public key. | ||
# | ||
# vectors: dict[str, dict[str, list[str]]] = dict() | ||
|
||
for file in pb2_dir.iterdir(): | ||
cur = CapPLACEHOLDERVectors() | ||
try: | ||
cur.ParseFromString(file.read_bytes()) | ||
except Exception: | ||
print("[ERROR] Failed to read vectors from %s", file) | ||
continue | ||
|
||
# FIXME: parameter is the attribute that categorises the vectors. If you changed | ||
# the name in the proto descriptor, change it here too. | ||
vectors[cur.parameter].append(str(file.name)) | ||
|
||
# Otherwise, here is the equivalent for the two-level example: we do have to | ||
# check whether the first key is present, but we can still use defaultdict for | ||
# the second level. | ||
# | ||
# if cur.curve not in vectors: | ||
# vectors[cur.curve] = defaultdict(list) | ||
# vectors[cur.curve][cur.public_type].append(str(file.name)) | ||
|
||
out = Path("crypto_condor/vectors/_LCPLACEHOLDER/LCPLACEHOLDER.json") | ||
with out.open("w") as fp: | ||
json.dump(vectors, fp, indent=2) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Ensure that the output directory exists. | ||
pb2_dir = VECTORS_DIR / "pb2" | ||
pb2_dir.mkdir(0o755, parents=False, exist_ok=True) | ||
|
||
# Define the placeholder that Make uses to compile only when necessary. | ||
imported_marker = VECTORS_DIR / "LCPLACEHOLDER.imported" | ||
|
||
try: | ||
# FIXME: import vectors here and generate JSON at the end | ||
generate_json() | ||
except Exception: | ||
imported_marker.unlink(missing_ok=True) | ||
else: | ||
imported_marker.touch() |
This file contains 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