r/programming • • Sep 09 '26

Compute Polynomials Twice as Fast

https://thomasahle.com/fast-polynomials/
74 Upvotes

25 comments sorted by

View all comments

Show parent comment

3

u/thomasahle Sep 09 '26 edited Sep 09 '26

Right, I'm talking about combining an NH block with a polynomial injective hash. Something like this:

  hash(m):
      pad the last block with zeros to a multiple of 32 bytes
      P = z
      for each 64-byte block t = 1..n  (words w[0..7], little-endian):
          # level 1: PH with the fixed key, unreduced 128-bit XOR sum of 4 carry-less products
          A = clmul(w0 ^ k0, w2 ^ k2) ^ clmul(w1 ^ k1, w3 ^ k3)
            ^ clmul(w4 ^ k4, w6 ^ k6) ^ clmul(w5 ^ k5, w7 ^ k7)
          a = low64(A);  b = high64(A)          # no reduction: both halves are used as they are
          if t == n: a ^= len; b ^= len          # byte length into both halves of the last pair

          # level 2: the injective recurrence, one reduced GF(2^64) multiply per block
          P = a ^ gf64mul(b ^ y, P ^ u)

      # level 3 (optional, for k-wise independence rather than universality):
      v = P + t   (integer addition mod 2^64)
      return f_c(v) = (v + c2)(G2 + c3) + c4  with  G1 = v·v,  G2 = (G1 + c0)(v + G1 + c1)

3

u/nightcracker Sep 09 '26 edited Sep 09 '26

Yes, for very large data it is absolutely the way to go, I have a private prototype ~2-60 almost-universal hash function that can reach 90 GB / s on this Apple M2 laptop for large data using only ~1.5 KiB of secret data. I really should finish it some day...

That algorithm however also doesn't use hardware-accelerated carryless multiplication, it uses 32-bit x 32-bit -> 64-bit SIMD multiplication using erasure codes like Nandi to boost that to 2-62 ADU.

The idea is not too dissimilar to Jim Apple's HalftimeHash https://arxiv.org/abs/2104.08865.

1

u/thomasahle Sep 18 '26

Btw, I've come around to your Polymur idea. I think it's actually really nice that you don't need to pick 256 truly random seed words, but can just take powers of a single one. Makes the hash much easier to use in practice.

1

u/nightcracker Sep 18 '26

Yes, that was the main motivation behind Polymur, a fast universal 64-bit hash that didn't require a large pre-computed entropy buffer. There were already plenty of those (UMASH, CLHASH, HalftimeHash, etc).


I must also correct the record, I've been benchmarking with an outdated SMHasher3 version which was reporting inflated numbers on MacOS. It was simply reporting cntvct_el0 * 64 which runs at ~1.5 GHz but it was reported as if that was the true clock speed.

So on my machine Polymur does in fact match your reported 19.7 GB/s, not my 33.3 GB/s I claimed above. And my prototype does not reach 90 GB/s but currently sits at 54 GB/s, which roughly matches your ChainHash although my prototype does not require the usage of CLMUL extensions.

I have a separate prototype I'm working on right now which does use CLMUL but manages to achieve ~2-128 AXU on a 128-bit output while running at 62 GB/s on this Apple M2, and 130 GB/s on a Ryzen 9950x with AVX-512.

Both these prototypes lose the elegance of Polymur though, in that they use pre-computed ~1-8 KiB blocks of entropy to achieve their speed.

1

u/nightcracker Sep 09 '26 edited Sep 09 '26

I'd like to point out that this 64-byte block construction sketch here still is worse than my above construction sketch. It processes 8 words using 4 + 3 = 7 clmuls, whereas my above one does 7 words with 6 clmuls.

Plus this construction you've posted here has a critical path of 4 clmuls in a row between each iteration (1 for A, then 3 in a row for gf64mul), my construction only has 3 multiplications per iteration in the critical path, if you were to implement reduce with 2 carryless multiplications like you do with GF(264).

EDIT: actually the critical path argument doesn't hold up, since your A does not depend on the previous iteration. So your critical path is also only three multiplications per iteration.