]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/internal/bigmod: add comparison test for addMulVVW
authorCherry Mui <cherryyz@google.com>
Thu, 14 Nov 2024 23:40:42 +0000 (18:40 -0500)
committerCherry Mui <cherryyz@google.com>
Fri, 15 Nov 2024 17:18:22 +0000 (17:18 +0000)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
src/crypto/internal/bigmod/nat_test.go

index 7a956e3a57d30036160ea3edbf12a3ec3211cd95..79b143ab0202561f454e878732d115e7ca6fc32f 100644 (file)
@@ -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)
+                       }
+               })
+       }
+}