| 1 | package md5x16 |
| 2 | |
| 3 | import ( |
| 4 | "crypto/md5" |
| 5 | "encoding/hex" |
| 6 | "math/rand" |
| 7 | "testing" |
| 8 | "testing/quick" |
| 9 | |
| 10 | "golang.org/x/sys/cpu" |
| 11 | ) |
| 12 | |
| 13 | func RequireISA(t *testing.T) { |
| 14 | t.Helper() |
| 15 | if !cpu.X86.HasAVX512F { |
| 16 | t.Skip("requires AVX512F instruction set") |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestVectors(t *testing.T) { |
| 21 | RequireISA(t) |
| 22 | |
| 23 | cases := []struct { |
| 24 | Data string |
| 25 | HexDigest string |
| 26 | }{ |
| 27 | {"", "d41d8cd98f00b204e9800998ecf8427e"}, |
| 28 | {"The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6"}, |
| 29 | {"The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0"}, |
| 30 | } |
| 31 | for _, c := range cases { |
| 32 | digest := Single(t, []byte(c.Data)) |
| 33 | got := hex.EncodeToString(digest[:]) |
| 34 | if got != c.HexDigest { |
| 35 | t.Errorf("Sum(%#v) = %s; expect %s", c.Data, got, c.HexDigest) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestCmp(t *testing.T) { |
| 41 | RequireISA(t) |
| 42 | |
| 43 | sum := func(data []byte) [Size]byte { return Single(t, data) } |
| 44 | if err := quick.CheckEqual(sum, md5.Sum, nil); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestLengths(t *testing.T) { |
| 50 | RequireISA(t) |
| 51 | |
| 52 | const max = BlockSize << 6 |
| 53 | data := make([]byte, max) |
| 54 | rand.Read(data) |
| 55 | |
| 56 | for n := 0; n <= max; n++ { |
| 57 | got := Single(t, data[:n]) |
| 58 | expect := md5.Sum(data[:n]) |
| 59 | if got != expect { |
| 60 | t.Fatalf("failed on length %d", n) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Single hashes a single data buffer in all 16 lanes and returns the result, |
| 66 | // after asserting that all lanes are the same. |
| 67 | func Single(t *testing.T, d []byte) [Size]byte { |
| 68 | // Place the same data in every lane. |
| 69 | var data [Lanes][]byte |
| 70 | for l := range data { |
| 71 | data[l] = d |
| 72 | } |
| 73 | |
| 74 | if err := Validate(data); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | |
| 78 | // Hash and check the lanes are the same. |
| 79 | digest := Sum(data) |
| 80 | for l := range data { |
| 81 | if digest[0] != digest[l] { |
| 82 | t.Logf("lane %02d: %x", 0, digest[0]) |
| 83 | t.Logf("lane %02d: %x", l, digest[l]) |
| 84 | t.Fatal("lane mismatch") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return digest[0] |
| 89 | } |
| 90 | |
| 91 | func TestActiveLanes(t *testing.T) { |
| 92 | RequireISA(t) |
| 93 | |
| 94 | const trials = 1 << 10 |
| 95 | const maxlen = BlockSize << 6 |
| 96 | for trial := 0; trial < trials; trial++ { |
| 97 | // Pick active lanes. |
| 98 | lanes := 1 + rand.Intn(Lanes-1) |
| 99 | active := rand.Perm(Lanes)[:lanes] |
| 100 | |
| 101 | // Fill active lanes with random data. |
| 102 | n := rand.Intn(maxlen) |
| 103 | buffer := make([]byte, lanes*n) |
| 104 | rand.Read(buffer) |
| 105 | |
| 106 | var data [Lanes][]byte |
| 107 | for i, l := range active { |
| 108 | data[l] = buffer[i*n : (i+1)*n] |
| 109 | } |
| 110 | |
| 111 | // Hash. |
| 112 | digest := Sum(data) |
| 113 | |
| 114 | // Verify correct result in active lanes. |
| 115 | for _, l := range active { |
| 116 | expect := md5.Sum(data[l]) |
| 117 | if digest[l] != expect { |
| 118 | t.Fatalf("lane %02d: mismatch", l) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Verify other lanes are zero. |
| 123 | isactive := map[int]bool{} |
| 124 | for _, l := range active { |
| 125 | isactive[l] = true |
| 126 | } |
| 127 | for l := 0; l < Lanes; l++ { |
| 128 | if !isactive[l] { |
| 129 | var zero [Size]byte |
| 130 | if digest[l] != zero { |
| 131 | t.Fatalf("inactive lane %d is non-zero", l) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |