7272
7373echo " Pull source code from remote repository..."
7474
75- # Copy mldsa-native source tree -- C source only (no native backends for now)
75+ # Copy mldsa-native source tree -- C source
7676mkdir $SRC
77- cp $TMP /mldsa/src/* $SRC
77+ # Copy only files (not subdirectories like native/ and fips202/)
78+ find $TMP /mldsa/src -maxdepth 1 -type f -exec cp {} $SRC \;
79+
80+ # Copy x86_64 backend
81+ # We import only the assembly-backed operations (NTT, INTT, nttunpack,
82+ # pointwise, polyvecl_pointwise_acc). The AVX2 C-intrinsic operations
83+ # (rej_uniform, decompose, use_hint, chknorm, caddq, polyz_unpack) are
84+ # intentionally excluded.
85+ #
86+ # The upstream meta.h advertises both assembly and C-intrinsic operations.
87+ # Rather than modify it, we keep a hand-maintained replacement in
88+ # ../mldsa_x86_64_meta.h (referenced via MLD_CONFIG_ARITH_BACKEND_FILE) that
89+ # declares only the assembly-backed subset. Upstream meta.h is not copied.
90+ mkdir -p $SRC /native/x86_64/src
91+ # Backend API and specification assumed by mldsa-native frontend
92+ cp $TMP /mldsa/src/native/api.h $SRC /native
93+ # Backend header -- unused C-intrinsic declarations are harmless and left intact
94+ cp $TMP /mldsa/src/native/x86_64/src/arith_native_x86_64.h $SRC /native/x86_64/src
95+ # Shared constants (zetas table); needed by the assembly kernels
96+ cp $TMP /mldsa/src/native/x86_64/src/consts.h $SRC /native/x86_64/src
97+ cp $TMP /mldsa/src/native/x86_64/src/consts.c $SRC /native/x86_64/src
98+ # Assembly source files for the operations we import (NTT, INTT, nttunpack,
99+ # pointwise, polyvecl_pointwise_acc). Only files with verified proofs are
100+ # included.
101+ cp $TMP /mldsa/src/native/x86_64/src/ntt_avx2_asm.S $SRC /native/x86_64/src
102+ cp $TMP /mldsa/src/native/x86_64/src/intt_avx2_asm.S $SRC /native/x86_64/src
103+ cp $TMP /mldsa/src/native/x86_64/src/nttunpack_avx2_asm.S $SRC /native/x86_64/src
104+ cp $TMP /mldsa/src/native/x86_64/src/pointwise_avx2_asm.S $SRC /native/x86_64/src
105+ cp $TMP /mldsa/src/native/x86_64/src/pointwise_acc_l4_avx2_asm.S $SRC /native/x86_64/src
106+ cp $TMP /mldsa/src/native/x86_64/src/pointwise_acc_l5_avx2_asm.S $SRC /native/x86_64/src
107+ cp $TMP /mldsa/src/native/x86_64/src/pointwise_acc_l7_avx2_asm.S $SRC /native/x86_64/src
78108
79109# We use the custom `mldsa_native_config.h`, so can remove the default one
80- rm $SRC /config.h
110+ rm -f $SRC /config.h
81111
82112# Copy formatting file
83113cp $TMP /.clang-format $SRC
84114
115+ if [[ " $( uname) " == " Darwin" ]]; then
116+ SED_I=(-i " " )
117+ else
118+ SED_I=(-i)
119+ fi
120+
121+ # ================================================================
122+ # Process mldsa_native_bcm.c
123+ # ================================================================
124+
85125# Copy and statically simplify BCM file
86126# The static simplification is not necessary, but improves readability
87127# by removing directives related to the FIPS-202 backend that we provide
@@ -91,12 +131,6 @@ unifdef -DMLD_CONFIG_FIPS202_CUSTOM_HEADER \
91131 $TMP /mldsa/mldsa_native.c \
92132 > $SRC /mldsa_native_bcm.c
93133
94- if [[ " $( uname) " == " Darwin" ]]; then
95- SED_I=(-i " " )
96- else
97- SED_I=(-i)
98- fi
99-
100134# Copy mldsa-native header
101135# This is only needed for access to the various macros defining key sizes.
102136# The function declarations itself are all visible in ml_dsa.c by virtue
@@ -110,6 +144,51 @@ cp $TMP/mldsa/mldsa_native.h $SRC
110144echo " Fixup include paths"
111145sed " ${SED_I[@]} " ' s/#include "src\/\([^"]*\)"/#include "\1"/' $SRC /mldsa_native_bcm.c
112146
147+ # Drop #include directives for the C-intrinsic .c files we did not import.
148+ # Only consts.c (shared with the assembly backend) needs to be compiled.
149+ echo " Strip C-intrinsic includes from mldsa_native_bcm.c"
150+ BCM=$SRC /mldsa_native_bcm.c
151+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_caddq_avx2\.c"/d' " $BCM "
152+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_chknorm_avx2\.c"/d' " $BCM "
153+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_decompose_32_avx2\.c"/d' " $BCM "
154+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_decompose_88_avx2\.c"/d' " $BCM "
155+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_use_hint_32_avx2\.c"/d' " $BCM "
156+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_use_hint_88_avx2\.c"/d' " $BCM "
157+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/polyz_unpack_17_avx2\.c"/d' " $BCM "
158+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/polyz_unpack_19_avx2\.c"/d' " $BCM "
159+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_avx2\.c"/d' " $BCM "
160+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_eta2_avx2\.c"/d' " $BCM "
161+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_eta4_avx2\.c"/d' " $BCM "
162+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_table\.c"/d' " $BCM "
163+
164+ # ================================================================
165+ # Fixup x86_64 assembly backend to use s2n-bignum macros
166+ # ================================================================
167+
168+ echo " Fixup x86_64 assembly backend to use s2n-bignum macros"
169+ for file in $SRC /native/x86_64/src/* .S; do
170+ echo " Processing $file "
171+ tmp_file=$( mktemp)
172+
173+ backend_define=" MLD_ARITH_BACKEND_X86_64_DEFAULT"
174+
175+ # Flatten multiline preprocessor directives, then process with unifdef
176+ sed -e ' :a' -e ' N' -e ' $!ba' -e ' s/\\\n/ /g' " $file " | \
177+ unifdef -D$backend_define -UMLD_CONFIG_MULTILEVEL_NO_SHARED -DMLD_CONFIG_MULTILEVEL_WITH_SHARED > " $tmp_file "
178+ mv " $tmp_file " " $file "
179+
180+ # Replace common.h include and assembly macros
181+ s2n_header=" _internal_s2n_bignum_x86_att.h"
182+ sed " ${SED_I[@]} " " s/#include \" \.\.\/\.\.\/\.\.\/common\.h\" /#include \" $s2n_header \" /" " $file "
183+
184+ func_name=$( grep -o ' \.global MLD_ASM_NAMESPACE(\([^)]*\))' " $file " | sed ' s/\.global MLD_ASM_NAMESPACE(\([^)]*\))/\1/' )
185+ if [ -n " $func_name " ]; then
186+ 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 "
187+ sed " ${SED_I[@]} " " s/MLD_ASM_FN_SYMBOL($func_name )/S2N_BN_SYMBOL(mldsa_$func_name ):/" " $file "
188+ sed " ${SED_I[@]} " " s/MLD_ASM_FN_SIZE($func_name )/S2N_BN_SIZE_DIRECTIVE(mldsa_$func_name )/" " $file "
189+ fi
190+ done
191+
113192echo " Remove temporary artifacts ..."
114193rm -rf $TMP
115194
0 commit comments