Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Commit 6be808b

Browse files
committed
standardize sipnode and sipblock
1 parent 9ded801 commit 6be808b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

internal/crypto/siphash.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,66 @@ func (h *SipHasher) Hash24(nonce uint64) {
5353
h.SipRound()
5454
h.SipRound()
5555
}
56+
57+
func (h *SipHasher) Hash48(nonce uint64) {
58+
h.v3 ^= nonce
59+
h.SipRound()
60+
h.SipRound()
61+
h.SipRound()
62+
h.SipRound()
63+
h.v0 ^= nonce
64+
h.v2 ^= 0xff
65+
h.SipRound()
66+
h.SipRound()
67+
h.SipRound()
68+
h.SipRound()
69+
h.SipRound()
70+
h.SipRound()
71+
h.SipRound()
72+
h.SipRound()
73+
}
74+
75+
type SipNodeFunc func(uint64, [4]uint64, uint64, uint64) uint64
76+
77+
func SipNode24(edgeMask uint64, siphashKeys [4]uint64, edge, uorv uint64) uint64 {
78+
hasher := NewSipHasher(siphashKeys[0], siphashKeys[1], siphashKeys[2], siphashKeys[3])
79+
hasher.Hash24(2*edge + uorv)
80+
value := hasher.XorLanes()
81+
82+
return value & edgeMask
83+
}
84+
85+
func SipNode24Legacy(edgeMask uint64, siphashKeys [4]uint64, edge, uorv uint64) uint64 {
86+
hasher := NewSipHasher(siphashKeys[0], siphashKeys[1], siphashKeys[2], siphashKeys[3])
87+
hasher.Hash24(2*edge + uorv)
88+
89+
value := hasher.XorLanes()
90+
value = value<<17 | value>>47
91+
92+
return value & edgeMask
93+
}
94+
95+
type SipBlockFunc func([4]uint64, uint64) uint64
96+
97+
func SipBlock48(siphashKeys [4]uint64, edge uint64) uint64 {
98+
const edgeBlockBits uint64 = 6
99+
const edgeBlockSize uint64 = (1 << edgeBlockBits)
100+
const edgeBlockMask uint64 = (edgeBlockSize - 1)
101+
102+
hasher := NewSipHasher(siphashKeys[0], siphashKeys[1], siphashKeys[2], siphashKeys[3])
103+
block := make([]uint64, edgeBlockSize)
104+
edge0 := edge & ^edgeBlockMask
105+
106+
var i uint64
107+
for i = 0; i < edgeBlockSize; i++ {
108+
hasher.Hash48(edge0 + uint64(i))
109+
block[i] = hasher.XorLanes()
110+
}
111+
112+
last := block[edgeBlockMask]
113+
for i = 0; i < edgeBlockMask; i++ {
114+
block[i] ^= last
115+
}
116+
117+
return block[edge&edgeBlockMask]
118+
}

0 commit comments

Comments
 (0)