-
Notifications
You must be signed in to change notification settings - Fork 183
ML-DSA: import and enable x86_64 assembly backend from mldsa-native #3195
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
base: main
Are you sure you want to change the base?
Changes from all commits
f3b147e
1c4f4d2
8ff3089
f304e50
4596c20
0ed5f6e
3f72ba0
0de2fe7
7931498
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| name: mldsa-native | ||
| source: pq-code-package/mldsa-native.git | ||
| branch: main | ||
| commit: b61e84f0c73d4ed612ffcaea4282a9d682de3f46 | ||
| imported-at: 2026-01-16T13:12:01-0800 | ||
| branch: 1b47ba602b3220fb06380840fd516dde4243122e | ||
| commit: 1b47ba602b3220fb06380840fd516dde4243122e | ||
| imported-at: 2026-05-14T03:52:22+0000 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,22 +72,46 @@ popd | |
|
|
||
| echo "Pull source code from remote repository..." | ||
|
|
||
| # Copy mldsa-native source tree -- C source only (no native backends for now) | ||
| # Copy mldsa-native source tree -- C source | ||
| mkdir $SRC | ||
| cp $TMP/mldsa/src/* $SRC | ||
| # Copy only files (not subdirectories like native/ and fips202/) | ||
| find $TMP/mldsa/src -maxdepth 1 -type f -exec cp {} $SRC \; | ||
|
|
||
| # Copy x86_64 backend | ||
| # We import all assembly (.S) files and shared headers/constants from the | ||
| # upstream x86_64 backend. The AVX2 C-intrinsic .c files (rej_uniform, | ||
| # decompose, use_hint, chknorm, polyz_unpack) are excluded — their includes | ||
| # are stripped from the BCM below. | ||
| # | ||
| # The upstream meta.h advertises both assembly and C-intrinsic operations. | ||
| # Rather than modify it, we keep a hand-maintained replacement in | ||
| # ../mldsa_x86_64_meta.h (referenced via MLD_CONFIG_ARITH_BACKEND_FILE) that | ||
| # declares only the assembly-backed subset. Upstream meta.h is not copied. | ||
| mkdir -p $SRC/native/x86_64/src | ||
| cp $TMP/mldsa/src/native/api.h $SRC/native | ||
| cp $TMP/mldsa/src/native/x86_64/src/arith_native_x86_64.h $SRC/native/x86_64/src | ||
| cp $TMP/mldsa/src/native/x86_64/src/consts.h $SRC/native/x86_64/src | ||
| cp $TMP/mldsa/src/native/x86_64/src/consts.c $SRC/native/x86_64/src | ||
| # NOTE: all imported .S files must have verified proofs in s2n-bignum. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If new .S functions are converted upstream without a hol-light proof at the same time, this import script will pick them up. This could be awkward if we run imports before the proofs land for new functions. We could add a manual check, but its clunky. anything we add will be temporary, as over time we will add all .S functions, as all will have proofs. Upstream I'm writing the proof at the same time as the conversion, so this won't be an issue for upcoming uniform_rej for example. |
||
| cp $TMP/mldsa/src/native/x86_64/src/*.S $SRC/native/x86_64/src | ||
|
|
||
| # We use the custom `mldsa_native_config.h`, so can remove the default one | ||
| rm $SRC/config.h | ||
| rm -f $SRC/config.h | ||
|
|
||
| # Copy formatting file | ||
| cp $TMP/.clang-format $SRC | ||
|
|
||
| # ================================================================ | ||
| # Process mldsa_native_bcm.c | ||
| # ================================================================ | ||
|
|
||
| # Copy and statically simplify BCM file | ||
| # The static simplification is not necessary, but improves readability | ||
| # by removing directives related to the FIPS-202 backend that we provide | ||
| # via our own glue layer. | ||
| unifdef -DMLD_CONFIG_FIPS202_CUSTOM_HEADER \ | ||
| -UMLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 \ | ||
| -UMLD_SYS_AARCH64 \ | ||
| $TMP/mldsa/mldsa_native.c \ | ||
| > $SRC/mldsa_native_bcm.c | ||
|
|
||
|
|
@@ -110,6 +134,40 @@ cp $TMP/mldsa/mldsa_native.h $SRC | |
| echo "Fixup include paths" | ||
| sed "${SED_I[@]}" 's/#include "src\/\([^"]*\)"/#include "\1"/' $SRC/mldsa_native_bcm.c | ||
|
|
||
| # Drop #include directives for the C-intrinsic .c files we did not import. | ||
| # Only consts.c (shared with the assembly backend) is kept. | ||
| echo "Strip C-intrinsic includes from mldsa_native_bcm.c" | ||
| BCM=$SRC/mldsa_native_bcm.c | ||
| sed "${SED_I[@]}" '/^#include "native\/x86_64\/src\/[^"]*\.c"/{/consts\.c/!d;}' "$BCM" | ||
|
|
||
| # ================================================================ | ||
| # Fixup x86_64 assembly backend to use s2n-bignum macros | ||
| # ================================================================ | ||
|
|
||
| echo "Fixup x86_64 assembly backend to use s2n-bignum macros" | ||
| for file in $SRC/native/x86_64/src/*.S; do | ||
| echo "Processing $file" | ||
| tmp_file=$(mktemp) | ||
|
|
||
| backend_define="MLD_ARITH_BACKEND_X86_64_DEFAULT" | ||
|
|
||
| # Flatten multiline preprocessor directives, then process with unifdef | ||
| sed -e ':a' -e 'N' -e '$!ba' -e 's/\\\n/ /g' "$file" | \ | ||
| unifdef -D$backend_define -UMLD_CONFIG_MULTILEVEL_NO_SHARED -DMLD_CONFIG_MULTILEVEL_WITH_SHARED > "$tmp_file" | ||
| mv "$tmp_file" "$file" | ||
|
|
||
| # Replace common.h include and assembly macros | ||
| s2n_header="_internal_s2n_bignum_x86_att.h" | ||
| sed "${SED_I[@]}" "s/#include \"\.\.\/\.\.\/\.\.\/common\.h\"/#include \"$s2n_header\"/" "$file" | ||
|
|
||
| func_name=$(grep -o '\.global MLD_ASM_NAMESPACE(\([^)]*\))' "$file" | sed 's/\.global MLD_ASM_NAMESPACE(\([^)]*\))/\1/') | ||
| if [ -n "$func_name" ]; then | ||
| sed "${SED_I[@]}" "s/\.global MLD_ASM_NAMESPACE($func_name)/ S2N_BN_SYM_VISIBILITY_DIRECTIVE(mldsa_$func_name)\n S2N_BN_SYM_PRIVACY_DIRECTIVE(mldsa_$func_name)/" "$file" | ||
| sed "${SED_I[@]}" "s/MLD_ASM_FN_SYMBOL($func_name)/S2N_BN_SYMBOL(mldsa_$func_name):/" "$file" | ||
| sed "${SED_I[@]}" "s/MLD_ASM_FN_SIZE($func_name)/S2N_BN_SIZE_DIRECTIVE(mldsa_$func_name)/" "$file" | ||
| fi | ||
| done | ||
|
Comment on lines
+147
to
+169
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid this complexity if we can. I'll do some experiments on the x-native side to see if we can get rid of it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more difficult than I thought. Let's stick with this for now. |
||
|
|
||
| echo "Remove temporary artifacts ..." | ||
| rm -rf $TMP | ||
|
|
||
|
|
||
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.
For my benefit, what's the practical difference between the before/after here?
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.
The old
cp $TMP/mldsa/src/* $SRCwould also copy subdirectories (native/, fips202/) via shell glob expansion. Switching tofind -maxdepth 1 -type fensures only top-level source files are copied, while the x86_64 backend files are selectively copied below.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.
No, I don't think
cpcopies subdirectories -- onlycp -rwould do.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.
You're right —
cpwithout-rwon't copy subdirectories, it'll just error/skip them. Thefind -maxdepth 1 -type favoids the error thatcp src/*would produce when it encounters subdirectories (native/, fips202/) in the glob expansion. Updated the comment to reflect this.