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")