From a9afea969aa63e0706d193b2fdca11cfd7d65e3c Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Thu, 14 Nov 2024 18:40:42 -0500 Subject: [PATCH] crypto/internal/bigmod: add comparison test for addMulVVW Sized addMulVVW (addMulVVW1024 etc.) have architecture-specific implementations on a number of architectures. Add a test checking that they match the generic implementation. Change-Id: I574f00ad7cd27d4e1bf008561023f713876244f8 Reviewed-on: https://go-review.googlesource.com/c/go/+/628256 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Filippo Valsorda --- src/crypto/internal/bigmod/nat_test.go | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/crypto/internal/bigmod/nat_test.go b/src/crypto/internal/bigmod/nat_test.go index 7a956e3a57..79b143ab02 100644 --- a/src/crypto/internal/bigmod/nat_test.go +++ b/src/crypto/internal/bigmod/nat_test.go @@ -10,6 +10,7 @@ import ( "math/bits" "math/rand" "reflect" + "slices" "strings" "testing" "testing/quick" @@ -478,3 +479,40 @@ func TestNewModFromBigZero(t *testing.T) { t.Errorf("NewModulusFromBig(2) got %q, want %q", err, expected) } } + +func makeTestValue(nbits int) []uint { + n := nbits / _W + x := make([]uint, n) + for i := range n { + x[i]-- + } + return x +} + +func TestAddMulVVWSized(t *testing.T) { + // Sized addMulVVW have architecture-specific implementations on + // a number of architectures. Test that they match the generic + // implementation. + tests := []struct { + n int + f func(z, x *uint, y uint) uint + }{ + {1024, addMulVVW1024}, + {1536, addMulVVW1536}, + {2048, addMulVVW2048}, + } + for _, test := range tests { + t.Run(fmt.Sprint(test.n), func(t *testing.T) { + x := makeTestValue(test.n) + z := makeTestValue(test.n) + z2 := slices.Clone(z) + var y uint + y-- + c := addMulVVW(z, x, y) + c2 := test.f(&z2[0], &x[0], y) + if !slices.Equal(z, z2) || c != c2 { + t.Errorf("%016X, %016X != %016X, %016X", z, c, z2, c2) + } + }) + } +} -- 2.48.1