From f96c69dc77a5d2a8cd601295b5513f560e11a6a1 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Mon, 23 Nov 2026 10:24:44 +0800 Subject: [PATCH] mldsa: fix asm FP-width vet warnings in the avx2 field kernels asmdecl flagged three references in field_amd64.s: * nttMatRowVecMulAVX2 read the 8-byte int argument with MOVL, i.e. only its low 32 bits. Load it with MOVQ instead; the body still does 32-bit arithmetic on it. * polyInfinityNormAVX2 and polyInfinityNormSignedAVX2 stored their uint32 result with VMOVD, which asmdecl sizes as 8 bytes from the opcode suffix. The store itself was correct; split it into VMOVD+MOVL so the width matches and vet stays quiet. Also guard a non-positive len in nttMatRowVecMulAVX2: the accumulate counter used to underflow to 0xFFFFFFFF. A test pins both the guard and the unchanged normal path. --- mldsa/field_amd64.s | 12 +++++++++--- mldsa/field_amd64_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/mldsa/field_amd64.s b/mldsa/field_amd64.s index e6a7575c..c276b3fe 100644 --- a/mldsa/field_amd64.s +++ b/mldsa/field_amd64.s @@ -556,7 +556,10 @@ TEXT ·nttMatRowVecMulAVX2(SB), NOSPLIT, $0-32 MOVQ dst+0(FP), DI MOVQ vec+8(FP), SI MOVQ matRow+16(FP), DX - MOVL len+24(FP), CX + MOVQ len+24(FP), CX + + TESTL CX, CX + JLE mvmDone // len <= 0: nothing to accumulate VPBROADCASTD qNegInvConst, QNegInv VPBROADCASTD qConst, Q @@ -636,6 +639,7 @@ mvmWrite: DECL R8 JNZ mvmChunkLoop +mvmDone: VZEROUPPER RET @@ -720,7 +724,8 @@ polyInfinityNormLoop: VPMAXSD X0, X7, X7 VPSHUFD $0xB1, X7, X0 VPMAXSD X0, X7, X7 - VMOVD X7, ret+8(FP) + VMOVD X7, AX // 32-bit lane; store width must match the uint32 result + MOVL AX, ret+8(FP) VZEROUPPER RET @@ -746,7 +751,8 @@ polyInfinityNormSignedLoop: VPMAXSD X0, X7, X7 VPSHUFD $0xB1, X7, X0 VPMAXSD X0, X7, X7 - VMOVD X7, ret+8(FP) + VMOVD X7, AX // 32-bit lane; store width must match the uint32 result + MOVL AX, ret+8(FP) VZEROUPPER RET diff --git a/mldsa/field_amd64_test.go b/mldsa/field_amd64_test.go index 55374825..969a33ed 100644 --- a/mldsa/field_amd64_test.go +++ b/mldsa/field_amd64_test.go @@ -200,6 +200,35 @@ func TestPolyInfinityNormSignedAVX2(t *testing.T) { } } +func TestNttMatRowVecMulAVX2NonPositiveLen(t *testing.T) { + if !useAVX2 { + t.Skip("AVX2 is not available") + } + + vec := nttElement(randomRingElement()) + matRow := nttElement(randomRingElement()) + + // A non-positive length must leave dst untouched instead of underflowing + // the accumulate counter into a near-infinite loop. + for _, length := range []int{0, -1} { + dst := nttElement(randomRingElement()) + want := dst + nttMatRowVecMulAVX2(&dst, &vec, &matRow, length) + if dst != want { + t.Fatalf("nttMatRowVecMulAVX2(len=%d) modified dst", length) + } + } + + // The guard must not disturb the normal path. + got := nttElement(randomRingElement()) + want := nttElement(randomRingElement()) + nttMatRowVecMulAVX2(&got, &vec, &matRow, 1) + nttMatRowVecMulGeneric(&want, &vec, &matRow, 1) + if got != want { + t.Fatal("nttMatRowVecMulAVX2(len=1) mismatch with generic") + } +} + func TestDecomposeSubToR0Gamma32AVX2(t *testing.T) { if !useAVX2 { t.Skip("AVX2 is not available")