Skip to content

Reformat JSON test vector files, verify formatting in CI - #183

Merged
cpu merged 4 commits into
C2SP:mainfrom
cpu:cpu-json-format
Oct 7, 2025
Merged

cpu merged 4 commits into
C2SP:mainfrom
cpu:cpu-json-format

Conversation

@cpu

@cpu cpu commented Oct 2, 2025

Copy link
Copy Markdown
Member

Adds a simple tools/reformat_json.py Python script to ensure consistent and uniform formatting, and to switch away from the less conventional use of " : " separators. We use ensure_ascii=False to preserve unicode content in comments (e.g. 2⁻¹⁶ in mlkem_512_test.json). CI is updated to run this script and fail if a diff is produced to prevent regression.

The script avoids formatting aes_ff1_*.json files for the reasons outlined in this comment. The delimiters in this file are fixed manually with sed in a separate commit, and we'll have to be careful to avoid regressions since it's excluded from CI checks. It seems unlikely these files will change anytime soon, so let's not get hung up on this while we can do better for the majority of files.

Python is used here because unfortunately Go makes it quite difficult to accomplish the same thing without reordering fields. I'm personally of the mindset that sorting the field order would make this easier and our primary consumers are machines and not humans, but an alpha sort has the disadvantage of moving fields like tcId that are currently up-front into a more or less random position. For now let's avoid this complication and use Python.

Updates #162

@cpu cpu self-assigned this Oct 2, 2025
cpu added 4 commits October 2, 2025 14:02
For now we use Python since the original files were generated with the
Python `json` module and its convenient for reformatting w/o affecting
field order. In the future we may revisit this.

The `aes_ff1_*` vector files are skipped for now since they have
properties that make automated reformatting more challenging.
Using `./tools/reformat_json.py`.
Using:
`sed -i 's/" : /": /g' testvectors_v1/aes_ff1_*.json`
By running `tools/reformat_json.py` and verifying no diff is produced.

@davidben davidben left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Verified the formatting by spot-checking a few and then confirming that json.dumps(json.load(...)) before and after this PR produced the same output. (Am assuming that the GHA workflow will confirm that the formatting itself matches expectations.)

@cpu

cpu commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

Thanks for the reviews 🙇

@cpu
cpu merged commit 91a9db9 into C2SP:main Oct 7, 2025
1 check passed
@cpu
cpu deleted the cpu-json-format branch October 7, 2025 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants