Skip to content

Commit

Permalink
removed skein, added removed repo metro (go-metro), module support
Browse files Browse the repository at this point in the history
  • Loading branch information
tildeleb committed Mar 20, 2021
1 parent 07375b5 commit be19204
Show file tree
Hide file tree
Showing 14 changed files with 525 additions and 1,261 deletions.
73 changes: 35 additions & 38 deletions hashf/hashf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ package hashf
import (
"crypto/sha1"
"fmt"
"hash"
"hash/adler32"
"time"
"unsafe"

farm "github.com/dgryski/go-farm"
metro "github.com/dgryski/go-metro"
"github.com/jzelinskie/whirlpool"
"github.com/minio/blake2b-simd"
"hash"
"hash/adler32"
"leb.io/aeshash"
"leb.io/hashes/skein"
"leb.io/hashland/crapwow"
"leb.io/hashland/gomap"
"leb.io/hashland/jenkins"
"leb.io/hashland/keccak"
"leb.io/hashland/keccakpg"
"leb.io/hashland/mahash"
"leb.io/hashland/metro"
"leb.io/hashland/murmur3"
"leb.io/hashland/nhash"
"leb.io/hashland/nullhash"
"leb.io/hashland/sbox"
"leb.io/hashland/siphash"
"leb.io/hashland/siphashpg"
"leb.io/hashland/skein"
"leb.io/hashland/spooky"
"time"
"unsafe"
)

// interfaces
Expand Down Expand Up @@ -247,14 +248,12 @@ func Hashf(k []byte, seed uint64) uint64 {
}
*/

var t uint64
var l uint64
var b uint32
var c uint32

func Hashf(k []byte, seed uint64) (h uint64) {
/*
_, ok := HashFunctions[Hf2]
if !ok {
fmt.Printf("%q not found\n", Hf2)
panic("hashf")
}
*/
switch Hf2 {
case "perfecthash":
fmt.Printf("k=%v\n", k)
Expand Down Expand Up @@ -293,13 +292,13 @@ func Hashf(k []byte, seed uint64) (h uint64) {
case "MaHash8v64":
h = mahash.MaHash8v64(k)
case "j364":
c, b := jenkins.Jenkins364(k, len(k), uint32(seed), uint32(seed))
c, b = jenkins.Jenkins364(k, len(k), uint32(seed), uint32(seed))
h = uint64(b)<<32 | uint64(c)
case "j332c":
c, _ := jenkins.Jenkins364(k, len(k), uint32(seed), uint32(seed))
c, _ = jenkins.Jenkins364(k, len(k), uint32(seed), uint32(seed))
h = uint64(c)
case "j332b":
_, b := jenkins.Jenkins364(k, len(k), uint32(seed), uint32(seed))
_, b = jenkins.Jenkins364(k, len(k), uint32(seed), uint32(seed))
h = uint64(b)
case "j232":
h = uint64(jenkins.Hash232(k, uint32(seed)))
Expand All @@ -308,11 +307,13 @@ func Hashf(k []byte, seed uint64) (h uint64) {
case "j264l":
h = uint64(jenkins.Hash264(k, seed) & 0xFFFFFFFF)
case "j264h":
t := jenkins.Hash264(k, seed)
h = uint64((t >> 32) & 0xFFFFFFFF)
h = jenkins.Hash264(k, seed)
h = uint64((h >> 32) & 0xFFFFFFFF)
//h = uint64((t >> 32) & 0xFFFFFFFF)
case "j264xor":
t := jenkins.Hash264(k, seed)
h = uint64(uint32(t&0xFFFFFFFF) ^ uint32((t>>32)&0xFFFFFFFF))
h = jenkins.Hash264(k, seed)
h = uint64(uint32(h&0xFFFFFFFF) ^ uint32((h>>32)&0xFFFFFFFF))
//h = uint64(uint32(t&0xFFFFFFFF) ^ uint32((t>>32)&0xFFFFFFFF))
case "spooky32":
h = uint64(spooky.Hash32(k, uint32(seed)))
case "spooky64":
Expand All @@ -322,13 +323,12 @@ func Hashf(k []byte, seed uint64) (h uint64) {
case "spooky128l":
_, h = spooky.Hash128(k, seed)
case "spooky128xor":
t, l := spooky.Hash128(k, seed)
t, l = spooky.Hash128(k, seed)
h = t ^ l
case "murmur332":
m332.Reset()
m332.Write(k)
t := m332.Sum32()
h = uint64(t)
h = uint64(m332.Sum32())
case "murmur364":
m364.Reset()
m364.Write(k)
Expand All @@ -339,16 +339,15 @@ func Hashf(k []byte, seed uint64) (h uint64) {
sipSeedSet(seed)
h, _ = siphashpg.Siphash(k, seeds, siphashpg.Crounds, siphashpg.Drounds, false)
case "FarmHash32":
t := farm.Fingerprint32(k)
h = uint64(t & 0xFFFFFFFF)
h = uint64(farm.Fingerprint32(k) & 0xFFFFFFFF)
case "FarmHash64":
h = farm.Fingerprint64(k)
case "FarmHash128-high":
h, _ = farm.Fingerprint128(k)
case "FarmHash128-low":
_, h = farm.Fingerprint128(k)
case "FarmHash128-xor":
t, l := farm.Fingerprint128(k)
t, l = farm.Fingerprint128(k)
h = t ^ l
case "MetroHash64-1":
h = metro.Hash64_1(k, uint32(seed))
Expand All @@ -359,14 +358,14 @@ func Hashf(k []byte, seed uint64) (h uint64) {
case "MetroHash128-1l":
_, h = metro.Hash128_1(k, uint32(seed))
case "MetroHash128-1xor":
t, l := metro.Hash128_1(k, uint32(seed))
t, l = metro.Hash128_1(k, uint32(seed))
h = t ^ l
case "MetroHash128-2h":
h, _ = metro.Hash128_2(k, uint32(seed))
case "MetroHash128-2l":
_, h = metro.Hash128_2(k, uint32(seed))
case "MetroHash128-2xor":
t, l := metro.Hash128_2(k, uint32(seed))
t, l = metro.Hash128_2(k, uint32(seed))
h = t ^ l

/*
Expand Down Expand Up @@ -414,24 +413,22 @@ func Hashf(k []byte, seed uint64) (h uint64) {
k643.Reset()
k643.Write(k)
fp8 = k643.Sum(fp8)
//fmt.Printf("keccak160xor: fp=%v\n", fp)
//fmt.Printf("keccak160xor: fp=%v\n", fp8)
h = uint64(fp8[0])<<56 | uint64(fp8[1])<<48 | uint64(fp8[2])<<40 | uint64(fp8[3])<<32 | uint64(fp8[4])<<24 | uint64(fp8[5])<<16 | uint64(fp8[6])<<8 | uint64(fp8[7])<<0
case "keccakpg644":
fp := make([]byte, 8, 8)
fp = fp[0:0]
fp8 = fp8[0:0]
k644.Reset()
k644.Write(k)
fp = k644.Sum(fp)
//fmt.Printf("keccak160xor: fp=%v\n", fp)
h = uint64(fp[0])<<56 | uint64(fp[1])<<48 | uint64(fp[2])<<40 | uint64(fp[3])<<32 | uint64(fp[4])<<24 | uint64(fp[5])<<16 | uint64(fp[6])<<8 | uint64(fp[7])<<0
fp8 = k644.Sum(fp8)
//fmt.Printf("keccak160xor: fp=%v\n", fp8)
h = uint64(fp8[0])<<56 | uint64(fp8[1])<<48 | uint64(fp8[2])<<40 | uint64(fp8[3])<<32 | uint64(fp8[4])<<24 | uint64(fp8[5])<<16 | uint64(fp8[6])<<8 | uint64(fp8[7])<<0
case "keccakpg648":
fp := make([]byte, 8, 8)
fp = fp[0:0]
fp8 = fp8[0:0]
k648.Reset()
k648.Write(k)
fp = k648.Sum(fp)
fp8 = k648.Sum(fp8)
//fmt.Printf("keccak160xor: fp=%v\n", fp)
h = uint64(fp[0])<<56 | uint64(fp[1])<<48 | uint64(fp[2])<<40 | uint64(fp[3])<<32 | uint64(fp[4])<<24 | uint64(fp[5])<<16 | uint64(fp[6])<<8 | uint64(fp[7])<<0
h = uint64(fp8[0])<<56 | uint64(fp8[1])<<48 | uint64(fp8[2])<<40 | uint64(fp8[3])<<32 | uint64(fp8[4])<<24 | uint64(fp8[5])<<16 | uint64(fp8[6])<<8 | uint64(fp8[7])<<0
case "keccakpg160":
fp32 = fp32[0:0]
k160.Reset()
Expand Down
5 changes: 3 additions & 2 deletions hashtable/hashtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ func (ht *HashTable) Insert(ka []byte) {
h := hashf.Hashf(k, ht.Seed) // jenkins.Hash232(k, 0)
if ht.prime {
idx = h % ht.Size
//fmt.Printf("idx=%d\n", idx)
} else {
idx = h & ht.SizeMask
}
//fmt.Printf("index=%d\n", idx)
//fmt.Printf("k=%d, idx=%d, ht.Size=%d, h=%#016x\n", btoi(k), idx, ht.Size, h)
cnt := 0
pass := 0

Expand Down Expand Up @@ -182,7 +183,7 @@ func (ht *HashTable) Insert(ka []byte) {
break
}
if ht.oa {
//fmt.Printf("Insert: col idx=%d, len=%d, hash=0x%08x, key=%q\n", idx, len(ht.Buckets[idx]), h, ht.Buckets[idx][0].Key)
//fmt.Printf("Insert: OA col idx=%d, len=%d, hash=0x%08x, key=%q\n", idx, len(ht.Buckets[idx]), h, ht.Buckets[idx][0].Key)
if cnt == 0 {
ht.Probes++
} else {
Expand Down
53 changes: 42 additions & 11 deletions ht.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"leb.io/hashland/keccakpg" // remove
"leb.io/hashland/nullhash" // remove
"leb.io/hashland/siphash" // remove
"leb.io/hashland/spooky"
)

func ReadFile(file string, cb func(line string)) int {
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestH(file string, lines int, hf2 string) (ht *HashTable) {
// integers 0 to n
func TestI(file string, lines int, hf2 string) (ht *HashTable) {
//r = rand.New(rand.NewSource(int64(bseed)))
//fmt.Printf("ni=%d\n", *ni)
fmt.Printf("n=%d, ni=%d\n", *n, *ni)
bs := make([]byte, 4, 4)
if *dru {
*n = -*n
Expand Down Expand Up @@ -286,6 +287,31 @@ func TestL(file string, lines int, hf2 string) (ht *HashTable) {
return
}

// hash function mod test
func TestM(file string, lines int, hf2 string) (ht *HashTable) {
//r = rand.New(rand.NewSource(int64(bseed)))
fmt.Printf("n=%d, ni=%d\n", *n, *ni)
bs := make([]byte, 4, 4)
if *dru {
*n = -*n
}
hist := make([]int, *n)
ht = NewHashTable(*n, seed, *extra, *pd, *oa, *prime) // ??? @@@
mod := uint64(*n)
start := time.Now()
for i := 0; i < *ni; i++ {
bs[0], bs[1], bs[2], bs[3] = byte(i), byte(i>>8), byte(i>>16), byte(i>>24)
h := Hashf(bs, ht.Seed)
hist[h%mod]++
//fmt.Printf("i=%d, 0x%08x\n", i, i)
}
stop := time.Now()
ht.Dur = tdiff(start, stop)
fmt.Printf("expected=%d\n", *ni / *n)
fmt.Printf("hist=%v\n", hist)
return
}

func unhex(c byte) uint8 {
switch {
case '0' <= c && c <= '9':
Expand Down Expand Up @@ -481,7 +507,11 @@ func benchmark32s(n int) {
case 4:
start = time.Now()
for i := 0; i < n; i++ {
_ = aeshash.Hash(bs, 0)
//_ = aeshash.Hash(bs, 0)
//_, _ = jenkins.Jenkins364(bs, 0, 0, 0)
//_ = jenkins.Hash232(bs, 0)
//_, _ = jenkins.Jenkins364(bs, 0, 0, 0)
_ = spooky.Hash64(bs, 0)
// sha1160.Reset()
// sha1160.Write(bs)
// fp20 = fp20[0:0]
Expand All @@ -492,9 +522,6 @@ func benchmark32s(n int) {
//k224.Write(bs)
//_ = k224.Sum(nil)
//bs[0], bs[1], bs[2], bs[3] = byte(i), byte(i>>8), byte(i>>16), byte(i>>24)
//_, _ = jenkins.Jenkins364(bs, 0, 0, 0)
//_ = jenkins.Hash232(bs, 0)
//_, _ = jenkins.Jenkins364(bs, 0, 0, 0)
//_ = gomap.Hash64(bs, 0)
//_ = nhf64.Hash64S(bs, 0) // chnage below too
//nh.Reset()
Expand All @@ -511,18 +538,19 @@ func benchmark32s(n int) {
default:
start = time.Now()
for i := 0; i < n; i++ {
//_ = aeshash.Hash(bs, 0)
//_, _ = jenkins.Jenkins364(bs, 0, 0, 0)
_ = spooky.Hash64(bs, 0)
// sha1160.Reset()
// sha1160.Write(bs)
// fp20 = fp20[0:0]
// fp20 = sha1160.Sum(fp20)
// _ = uint64(fp20[0])<<56 | uint64(fp20[1])<<48 | uint64(fp20[2])<<40 | uint64(fp20[3])<<32 |
// uint64(fp20[4])<<24 | uint64(fp20[5])<<16 | uint64(fp20[6])<<8 | uint64(fp20[7])<<0
_ = aeshash.Hash(bs, 0)
//k224.Reset()
//k224.Write(bs)
//_ = k224.Sum(nil)
//bs[0], bs[1], bs[2], bs[3], bs[4], bs[5], bs[6], bs[7] = byte(i), byte(i>>8), byte(i>>16), byte(i>>24), byte(i>>32), byte(i>>40), byte(i>>48), byte(i>>56)
//_, _ = jenkins.Jenkins364(bs, 0, 0, 0)
//_ = aeshash.Hash(bs, 0)
//_ = gomap.Hash64(bs, 0)
//_ = siphash.Hash(0, 0, bs)
Expand Down Expand Up @@ -672,6 +700,7 @@ var Tests = []Test{
{"TestJ", &J, TestJ, "one bit keys (does not read file)"},
{"TestK", &K, TestK, "read file of keys and print hashes"},
{"TestL", &L, TestL, "all possible 3 byte keys"},
{"TestL", &M, TestM, "hash function mod test"},
}

func runTestsWithFileAndHashes(file string, lines int, hf []string) {
Expand Down Expand Up @@ -716,7 +745,7 @@ var file = flag.String("file", "", "words to read")
var lines = flag.Int("lines", 0, "number of lines to read in file")
var hf = flag.String("hf", "all", "hash function")
var extra = flag.Int("e", 1, "extra bis in table size")
var prime = flag.Bool("p", false, "table size is primes and use mod")
var prime = flag.Bool("p", false, "table size is prime, use mod")
var all = flag.Bool("a", false, "run all tests")
var pd = flag.Bool("pd", false, "print duplicate hashes")
var oa = flag.Bool("oa", false, "open addressing (no buckets)")
Expand Down Expand Up @@ -760,17 +789,18 @@ var I = flag.Bool("I", false, "test I")
var J = flag.Bool("J", false, "test J")
var K = flag.Bool("K", false, "test K")
var L = flag.Bool("L", false, "test L")
var M = flag.Bool("M", false, "test M")
var S = flag.Bool("S", false, "test S")

var letters = []string{"abcdefgh", "efghijkl", "ijklmnop", "mnopqrst", "qrstuvwx", "uvwxyz01"} // 262144 words
var TestPointers = []**bool{&A, &B, &C, &D, &E, &F, &G, &H, &I, &J, &K, &L}
var TestPointers = []**bool{&A, &B, &C, &D, &E, &F, &G, &H, &I, &J, &K, &L, &M}

func allTestsOn() {
*A, *B, *C, *D, *E, *F, *G, *H, *I, *J, *L = true, true, true, true, true, true, true, true, true, true, true
*A, *B, *C, *D, *E, *F, *G, *H, *I, *J, *L, *M = true, true, true, true, true, true, true, true, true, true, true, true
}

func allTestsOff() {
*A, *B, *C, *D, *E, *F, *G, *H, *I, *J, *L = false, false, false, false, false, false, false, false, false, false, false
*A, *B, *C, *D, *E, *F, *G, *H, *I, *J, *L, *M = false, false, false, false, false, false, false, false, false, false, false, false
}

func main() {
Expand Down Expand Up @@ -900,6 +930,7 @@ func main() {
allTestsOff()
*I, *J = true, true
}
fmt.Printf("here\n")
if *hf == "all" {
runTestsWithFileAndHashes("", *lines, TestHashFunctions)
} else {
Expand Down
Loading

0 comments on commit be19204

Please sign in to comment.