Skip to content

Commit

Permalink
Replace math/rand with crypto/rand in cuckoo (#62)
Browse files Browse the repository at this point in the history
* Seed math/rand with crypto/rand for performance

Co-authored-by: Jaskaran Veer Singh <[email protected]>
  • Loading branch information
jvsg and Jaskaran Veer Singh authored Dec 15, 2021
1 parent b236d33 commit ff23743
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/cuckoo/cuckoo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cuckoo

import (
"bytes"
crand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand"

Expand Down Expand Up @@ -79,10 +81,23 @@ type Cuckoo struct {
}

// NewCuckoo instantiates a Cuckoo struct with a bucket of size Factor * size,
// and a CuckooHasher for the 3-way cuckoo hashing.
// seeds math/rand with a random seed, returns a CuckooHasher for the 3-way
// cuckoo hashing.
func NewCuckoo(size uint64, seeds [Nhash][]byte) *Cuckoo {
cuckooHasher := NewCuckooHasher(size, seeds)

// get randombyte from crypto/rand
var rb [8]byte
if _, err := crand.Read(rb[:]); err != nil {
panic(err)
}

// WARNING: math/rand is not concurrency-safe
// replace with crypto/rand if that's what you want

// seed math/rand with crypto/rand
rand.Seed(int64(binary.LittleEndian.Uint64(rb[:])))

return &Cuckoo{
// extra element is "keeper" to which the bucketLookup can be directed
// when there is no element present in the bucket.
Expand Down Expand Up @@ -181,7 +196,8 @@ func (c *Cuckoo) tryAdd(idx uint64, bucketIndices [Nhash]uint64, ignore bool, ex
func (c *Cuckoo) tryGreedyAdd(idx uint64, bucketIndices [Nhash]uint64) (homeLessItem uint64, added bool) {
for i := 1; i < ReInsertLimit; i++ {
// select a random slot to be evicted
evictedHIdx := rand.Int31n(Nhash)
// replace me with crypto/rand for concurrent safety
evictedHIdx := rand.Intn(Nhash)
evictedBIdx := bucketIndices[evictedHIdx]
evictedIdx := c.bucketLookup[evictedBIdx]
// insert the item in the evicted slot
Expand Down

0 comments on commit ff23743

Please sign in to comment.