ethash: Use KeccackFast for cache generation

This commit is contained in:
Paweł Bylica 2018-05-29 15:27:18 +02:00
parent 47cec850c1
commit 8e61afff2d
No known key found for this signature in database
GPG key ID: 7A0C037434FE77EF

View file

@ -18,7 +18,6 @@ package ethash
import ( import (
"encoding/binary" "encoding/binary"
"hash"
"math/big" "math/big"
"reflect" "reflect"
"runtime" "runtime"
@ -94,17 +93,6 @@ func calcDatasetSize(epoch int) uint64 {
// reused between hash runs instead of requiring new ones to be created. // reused between hash runs instead of requiring new ones to be created.
type hasher func(dest []byte, data []byte) type hasher func(dest []byte, data []byte)
// makeHasher creates a repetitive hasher, allowing the same hash data structures
// to be reused between hash runs instead of requiring new ones to be created.
// The returned function is not thread safe!
func makeHasher(h hash.Hash) hasher {
return func(dest []byte, data []byte) {
h.Write(data)
h.Sum(dest[:0])
h.Reset()
}
}
// seedHash is the seed to use for generating a verification cache and the mining // seedHash is the seed to use for generating a verification cache and the mining
// dataset. // dataset.
func seedHash(block uint64) []byte { func seedHash(block uint64) []byte {
@ -112,7 +100,7 @@ func seedHash(block uint64) []byte {
if block < epochLength { if block < epochLength {
return seed return seed
} }
keccak256 := makeHasher(sha3.NewKeccak256()) keccak256 := sha3.KeccakFast256
for i := 0; i < int(block/epochLength); i++ { for i := 0; i < int(block/epochLength); i++ {
keccak256(seed, seed) keccak256(seed, seed)
} }
@ -166,7 +154,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
} }
}() }()
// Create a hasher to reuse between invocations // Create a hasher to reuse between invocations
keccak512 := makeHasher(sha3.NewKeccak512()) keccak512 := sha3.KeccakFast512
// Sequentially produce the initial dataset // Sequentially produce the initial dataset
keccak512(cache, seed) keccak512(cache, seed)
@ -299,7 +287,7 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
defer pend.Done() defer pend.Done()
// Create a hasher to reuse between invocations // Create a hasher to reuse between invocations
keccak512 := makeHasher(sha3.NewKeccak512()) keccak512 := sha3.KeccakFast512
// Calculate the data segment this thread should generate // Calculate the data segment this thread should generate
batch := uint32((size + hashBytes*uint64(threads) - 1) / (hashBytes * uint64(threads))) batch := uint32((size + hashBytes*uint64(threads) - 1) / (hashBytes * uint64(threads)))
@ -373,7 +361,7 @@ func hashimoto(hash []byte, nonce uint64, size uint64, lookup func(index uint32)
// in-memory cache) in order to produce our final value for a particular header // in-memory cache) in order to produce our final value for a particular header
// hash and nonce. // hash and nonce.
func hashimotoLight(size uint64, cache []uint32, hash []byte, nonce uint64) ([]byte, []byte) { func hashimotoLight(size uint64, cache []uint32, hash []byte, nonce uint64) ([]byte, []byte) {
keccak512 := makeHasher(sha3.NewKeccak512()) keccak512 := sha3.KeccakFast512
lookup := func(index uint32) []uint32 { lookup := func(index uint32) []uint32 {
rawData := generateDatasetItem(cache, index, keccak512) rawData := generateDatasetItem(cache, index, keccak512)