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

Commit 95e83cf

Browse files
committed
move siphash into internal/crypto
1 parent f9b668b commit 95e83cf

File tree

4 files changed

+120
-66
lines changed

4 files changed

+120
-66
lines changed

internal/crypto/siphash.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package crypto
2+
3+
type SipHasher struct {
4+
v0 uint64
5+
v1 uint64
6+
v2 uint64
7+
v3 uint64
8+
}
9+
10+
func NewSipHasher(v0, v1, v2, v3 uint64) *SipHasher {
11+
hasher := &SipHasher{
12+
v0: v0,
13+
v1: v1,
14+
v2: v2,
15+
v3: v3,
16+
}
17+
18+
return hasher
19+
}
20+
21+
func (h *SipHasher) XorLanes() uint64 {
22+
return h.v0 ^ h.v1 ^ h.v2 ^ h.v3
23+
}
24+
25+
func (h *SipHasher) SipRound() {
26+
h.v0 += h.v1
27+
h.v1 = h.v1<<13 | h.v1>>51
28+
h.v1 ^= h.v0
29+
h.v0 = h.v0<<32 | h.v0>>32
30+
31+
h.v2 += h.v3
32+
h.v3 = h.v3<<16 | h.v3>>48
33+
h.v3 ^= h.v2
34+
35+
h.v0 += h.v3
36+
h.v3 = h.v3<<21 | h.v3>>43
37+
h.v3 ^= h.v0
38+
39+
h.v2 += h.v1
40+
h.v1 = h.v1<<17 | h.v1>>47
41+
h.v1 ^= h.v2
42+
h.v2 = h.v2<<32 | h.v2>>32
43+
}
44+
45+
func (h *SipHasher) Hash24(nonce uint64) {
46+
h.v3 ^= nonce
47+
h.SipRound()
48+
h.SipRound()
49+
h.v0 ^= nonce
50+
h.v2 ^= 0xff
51+
h.SipRound()
52+
h.SipRound()
53+
h.SipRound()
54+
h.SipRound()
55+
}

internal/crypto/siphash_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package crypto
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHash24(t *testing.T) {
8+
tests := []struct {
9+
v0 uint64
10+
v1 uint64
11+
v2 uint64
12+
v3 uint64
13+
nonce uint64
14+
result uint64
15+
}{
16+
{
17+
v0: 1,
18+
v1: 2,
19+
v2: 3,
20+
v3: 4,
21+
nonce: 10,
22+
result: 928382149599306901,
23+
},
24+
{
25+
v0: 1,
26+
v1: 2,
27+
v2: 3,
28+
v3: 4,
29+
nonce: 111,
30+
result: 10524991083049122233,
31+
},
32+
{
33+
v0: 9,
34+
v1: 7,
35+
v2: 6,
36+
v3: 7,
37+
nonce: 12,
38+
result: 1305683875471634734,
39+
},
40+
{
41+
v0: 9,
42+
v1: 7,
43+
v2: 6,
44+
v3: 7,
45+
nonce: 10,
46+
result: 11589833042187638814,
47+
},
48+
}
49+
50+
for i, tt := range tests {
51+
hasher := NewSipHasher(tt.v0, tt.v1, tt.v2, tt.v3)
52+
hasher.Hash24(tt.nonce)
53+
result := hasher.XorLanes()
54+
55+
if result != tt.result {
56+
t.Errorf("failed on %d: result mismatch: have %x, want %x", i, result, tt.result)
57+
}
58+
}
59+
}

octopus/octopus.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ func octopus(hash []byte, nonce, datasetSize uint64, lookup func(index uint32) [
9090

9191
warpID := nonce / powWarpSize
9292
for i := 0; i < powWarpSize; i++ {
93-
hasher := NewSipHasher(v0, v1, v2, v3)
94-
hasher.hash24(warpID*powWarpSize + uint64(i))
93+
hasher := crypto.NewSipHasher(v0, v1, v2, v3)
94+
hasher.Hash24(warpID*powWarpSize + uint64(i))
9595
for j := 0; j < powDataPerThread; j++ {
96-
hasher.sipRound()
97-
d[(j*powWarpSize + i)] = uint32((hasher.xorLanes() & math.MaxUint32) % powMod)
96+
hasher.SipRound()
97+
d[(j*powWarpSize + i)] = uint32((hasher.XorLanes() & math.MaxUint32) % powMod)
9898
}
9999
}
100100

@@ -130,8 +130,6 @@ func octopus(hash []byte, nonce, datasetSize uint64, lookup func(index uint32) [
130130
}
131131

132132
halfMix := make([]byte, nodeBytes)
133-
compressBytes := make([]byte, 32)
134-
135133
copy(halfMix, hash)
136134
binary.LittleEndian.PutUint64(halfMix[len(hash):], result)
137135
halfMix = crypto.Keccak512(halfMix[:len(hash)+8])
@@ -159,9 +157,7 @@ func octopus(hash []byte, nonce, datasetSize uint64, lookup func(index uint32) [
159157
}
160158
}
161159

162-
compressWords := convutil.BytesToUint32Array(compressBytes, binary.LittleEndian)
163-
// @TODO: compressWords := make([]uint32, 8)
164-
160+
compressWords := make([]uint32, 8)
165161
for i := 0; i < 8; i++ {
166162
w := i * 4
167163
w2 := (8 + i) * 4
@@ -179,7 +175,7 @@ func octopus(hash []byte, nonce, datasetSize uint64, lookup func(index uint32) [
179175
compressWords[i] = reduction*crypto.FnvPrime ^ reduction2
180176
}
181177

182-
compressBytes = convutil.Uint32ArrayToBytes(compressWords, binary.LittleEndian)
178+
compressBytes := convutil.Uint32ArrayToBytes(compressWords, binary.LittleEndian)
183179
digest := crypto.Keccak256(append(mix[:nodeBytes], compressBytes...))
184180

185181
return digest

octopus/siphash.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)