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+ # ================================================================
116+ # Process mldsa_native_bcm.c
117+ # ================================================================
118+
85119# Copy and statically simplify BCM file
86120# The static simplification is not necessary, but improves readability
87121# by removing directives related to the FIPS-202 backend that we provide
88122# via our own glue layer.
89123unifdef -DMLD_CONFIG_FIPS202_CUSTOM_HEADER \
90124 -UMLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 \
125+ -UMLD_SYS_AARCH64 \
91126 $TMP /mldsa/mldsa_native.c \
92127 > $SRC /mldsa_native_bcm.c
93128
@@ -110,6 +145,51 @@ cp $TMP/mldsa/mldsa_native.h $SRC
110145echo " Fixup include paths"
111146sed " ${SED_I[@]} " ' s/#include "src\/\([^"]*\)"/#include "\1"/' $SRC /mldsa_native_bcm.c
112147
148+ # Drop #include directives for the C-intrinsic .c files we did not import.
149+ # Only consts.c (shared with the assembly backend) needs to be compiled.
150+ echo " Strip C-intrinsic includes from mldsa_native_bcm.c"
151+ BCM=$SRC /mldsa_native_bcm.c
152+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_caddq_avx2\.c"/d' " $BCM "
153+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_chknorm_avx2\.c"/d' " $BCM "
154+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_decompose_32_avx2\.c"/d' " $BCM "
155+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_decompose_88_avx2\.c"/d' " $BCM "
156+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_use_hint_32_avx2\.c"/d' " $BCM "
157+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/poly_use_hint_88_avx2\.c"/d' " $BCM "
158+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/polyz_unpack_17_avx2\.c"/d' " $BCM "
159+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/polyz_unpack_19_avx2\.c"/d' " $BCM "
160+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_avx2\.c"/d' " $BCM "
161+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_eta2_avx2\.c"/d' " $BCM "
162+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_eta4_avx2\.c"/d' " $BCM "
163+ sed " ${SED_I[@]} " ' /^#include "native\/x86_64\/src\/rej_uniform_table\.c"/d' " $BCM "
164+
165+ # ================================================================
166+ # Fixup x86_64 assembly backend to use s2n-bignum macros
167+ # ================================================================
168+
169+ echo " Fixup x86_64 assembly backend to use s2n-bignum macros"
170+ for file in $SRC /native/x86_64/src/* .S; do
171+ echo " Processing $file "
172+ tmp_file=$( mktemp)
173+
174+ backend_define=" MLD_ARITH_BACKEND_X86_64_DEFAULT"
175+
176+ # Flatten multiline preprocessor directives, then process with unifdef
177+ sed -e ' :a' -e ' N' -e ' $!ba' -e ' s/\\\n/ /g' " $file " | \
178+ unifdef -D$backend_define -UMLD_CONFIG_MULTILEVEL_NO_SHARED -DMLD_CONFIG_MULTILEVEL_WITH_SHARED > " $tmp_file "
179+ mv " $tmp_file " " $file "
180+
181+ # Replace common.h include and assembly macros
182+ s2n_header=" _internal_s2n_bignum_x86_att.h"
183+ sed " ${SED_I[@]} " " s/#include \" \.\.\/\.\.\/\.\.\/common\.h\" /#include \" $s2n_header \" /" " $file "
184+
185+ func_name=$( grep -o ' \.global MLD_ASM_NAMESPACE(\([^)]*\))' " $file " | sed ' s/\.global MLD_ASM_NAMESPACE(\([^)]*\))/\1/' )
186+ if [ -n " $func_name " ]; then
187+ 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 "
188+ sed " ${SED_I[@]} " " s/MLD_ASM_FN_SYMBOL($func_name )/S2N_BN_SYMBOL(mldsa_$func_name ):/" " $file "
189+ sed " ${SED_I[@]} " " s/MLD_ASM_FN_SIZE($func_name )/S2N_BN_SIZE_DIRECTIVE(mldsa_$func_name )/" " $file "
190+ fi
191+ done
192+
113193echo " Remove temporary artifacts ..."
114194rm -rf $TMP
115195
0 commit comments