hash.go raw

   1  package lattice
   2  
   3  import (
   4  	"encoding/binary"
   5  	"math/bits"
   6  
   7  	"github.com/zeebo/xxh3"
   8  )
   9  
  10  const (
  11  	fnvOffset64 uint64 = 14695981039346656037
  12  	fnvPrime64  uint64 = 1099511628211
  13  )
  14  
  15  // HashBytes is iskra/hash.mx HashBytes: FNV-1a truncated to 56 bits. This, not
  16  // SipHash, is what the word, segment and bigram keys in existing .iskr files
  17  // were made with.
  18  func HashBytes(data []byte) uint64 {
  19  	h := fnvOffset64
  20  	for _, c := range data {
  21  		h ^= uint64(c)
  22  		h *= fnvPrime64
  23  	}
  24  	return h >> 8
  25  }
  26  
  27  // HashKey is iskradb/lattice.mx HashKey: the low 64 bits of SipHash-2-4-128
  28  // under moxie's default key, ported from moxie's src/crypto/siphash.
  29  //
  30  // It is not the low half of standard SipHash-2-4: moxie's Sum128 XORs 0xee into
  31  // v1 at initialization and 0xee into v2 before the first finalization. Any
  32  // port that reaches for a siphash library gets different keys.
  33  func HashKey(data []byte) Key {
  34  	lo, _ := sipSum128(0x736f6d6570736575, 0x646f72616e646f6d, data)
  35  	return Key(lo)
  36  }
  37  
  38  func sipSum128(k0, k1 uint64, p []byte) (lo, hi uint64) {
  39  	v0 := k0 ^ 0x736f6d6570736575
  40  	v1 := k1 ^ 0x646f72616e646f6d
  41  	v2 := k0 ^ 0x6c7967656e657261
  42  	v3 := k1 ^ 0x7465646279746573
  43  	v1 ^= 0xee
  44  
  45  	length := uint64(len(p))
  46  	blocks := len(p) / 8
  47  	for i := 0; i < blocks; i++ {
  48  		m := binary.LittleEndian.Uint64(p[i*8:])
  49  		v3 ^= m
  50  		v0, v1, v2, v3 = sipRound(v0, v1, v2, v3)
  51  		v0, v1, v2, v3 = sipRound(v0, v1, v2, v3)
  52  		v0 ^= m
  53  	}
  54  	last := length << 56
  55  	for i, c := range p[blocks*8:] {
  56  		last |= uint64(c) << (uint(i) * 8)
  57  	}
  58  	v3 ^= last
  59  	v0, v1, v2, v3 = sipRound(v0, v1, v2, v3)
  60  	v0, v1, v2, v3 = sipRound(v0, v1, v2, v3)
  61  	v0 ^= last
  62  
  63  	v2 ^= 0xee
  64  	for i := 0; i < 4; i++ {
  65  		v0, v1, v2, v3 = sipRound(v0, v1, v2, v3)
  66  	}
  67  	lo = v0 ^ v1 ^ v2 ^ v3
  68  
  69  	v1 ^= 0xdd
  70  	for i := 0; i < 4; i++ {
  71  		v0, v1, v2, v3 = sipRound(v0, v1, v2, v3)
  72  	}
  73  	hi = v0 ^ v1 ^ v2 ^ v3
  74  	return
  75  }
  76  
  77  func sipRound(v0, v1, v2, v3 uint64) (r0, r1, r2, r3 uint64) {
  78  	v0 += v1
  79  	v1 = bits.RotateLeft64(v1, 13)
  80  	v1 ^= v0
  81  	v0 = bits.RotateLeft64(v0, 32)
  82  	v2 += v3
  83  	v3 = bits.RotateLeft64(v3, 16)
  84  	v3 ^= v2
  85  	v0 += v3
  86  	v3 = bits.RotateLeft64(v3, 21)
  87  	v3 ^= v0
  88  	v2 += v1
  89  	v1 = bits.RotateLeft64(v1, 17)
  90  	v1 ^= v2
  91  	v2 = bits.RotateLeft64(v2, 32)
  92  	return v0, v1, v2, v3
  93  }
  94  
  95  // HashWord is the go-ref word hash: xxh3 truncated to 56 bits. No moxie code
  96  // ever produced these keys, so nothing on disk was keyed with it.
  97  func HashWord(normalized []byte) uint64 {
  98  	return xxh3.Hash(normalized) >> 8
  99  }
 100