]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: mix a bit more in arm64 hash function
authorKeith Randall <khr@golang.org>
Wed, 19 Apr 2023 23:41:37 +0000 (16:41 -0700)
committerKeith Randall <khr@google.com>
Thu, 20 Apr 2023 15:45:06 +0000 (15:45 +0000)
We really need 3 mix steps between the data being hashed and the output.
One mix can only spread a 1 bit change to 32 bits. The second mix
can spread to all 128 bits, but the spread is not complete. A third mix
spreads out ~evenly to all 128 bits.

The amd64 version has 3 mix steps.

Fixes #59643

Change-Id: I54ad8686ca42bcffb6d0ec3779d27af682cc96e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/486616
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/runtime/asm_arm64.s
src/runtime/hash_test.go

index ef70970f775715acbfb451b1fcfc129659be66ba..143ea38fbebe7f7d4485011780feb867174539be 100644 (file)
@@ -611,6 +611,7 @@ done:
        AESE    V0.B16, V2.B16
        AESMC   V2.B16, V2.B16
        AESE    V0.B16, V2.B16
+       AESMC   V2.B16, V2.B16
 
        VMOV    V2.D[0], R0
        RET
index d4a2b3f6bb5039cc84228fca9a484cc5f2fc8e07..099bf511295ac0352fd92e6a7839a5c335b3dab2 100644 (file)
@@ -775,8 +775,11 @@ func TestCollisions(t *testing.T) {
                                a[j] = byte(n >> 8)
                                m[uint16(BytesHash(a[:], 0))] = struct{}{}
                        }
-                       if len(m) <= 1<<15 {
-                               t.Errorf("too many collisions i=%d j=%d outputs=%d out of 65536\n", i, j, len(m))
+                       // N balls in N bins, for N=65536
+                       avg := 41427
+                       stdDev := 123
+                       if len(m) < avg-40*stdDev || len(m) > avg+40*stdDev {
+                               t.Errorf("bad number of collisions i=%d j=%d outputs=%d out of 65536\n", i, j, len(m))
                        }
                }
        }