Skip to content

Commit 3b00a77

Browse files
karalabefjl
authored andcommitted
crypto, pow: add pure Go implementation of ethash
1 parent 288700c commit 3b00a77

6 files changed

Lines changed: 1929 additions & 2 deletions

File tree

‎crypto/crypto.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ var (
3737
secp256k1_halfN = new(big.Int).Div(secp256k1_N, big.NewInt(2))
3838
)
3939

40+
// Hasher is a repetitive hasher allowing the same hash data structures to be
41+
// reused between hash runs instead of requiring new ones to be created.
42+
type Hasher func(data []byte) []byte
43+
44+
// Keccak256 calculates and returns the Keccak256 hash of the input data.
4045
func Keccak256(data ...[]byte) []byte {
4146
d := sha3.NewKeccak256()
4247
for _, b := range data {
@@ -45,6 +50,8 @@ func Keccak256(data ...[]byte) []byte {
4550
return d.Sum(nil)
4651
}
4752

53+
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
54+
// converting it to an internal Hash data structure.
4855
func Keccak256Hash(data ...[]byte) (h common.Hash) {
4956
d := sha3.NewKeccak256()
5057
for _, b := range data {
@@ -54,6 +61,47 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
5461
return h
5562
}
5663

64+
// Keccak256Hasher creates a repetitive Keccak256 hasher, allowing the same hash
65+
// data structures to be reused between hash runs instead of requiring new ones
66+
// to be created.
67+
//
68+
// The returned function is not thread safe!
69+
func Keccak256Hasher() Hasher {
70+
hasher := sha3.NewKeccak256()
71+
72+
return func(data []byte) []byte {
73+
hasher.Write(data)
74+
result := hasher.Sum(nil)
75+
hasher.Reset()
76+
return result
77+
}
78+
}
79+
80+
// Keccak512 calculates and returns the Keccak512 hash of the input data.
81+
func Keccak512(data ...[]byte) []byte {
82+
d := sha3.NewKeccak512()
83+
for _, b := range data {
84+
d.Write(b)
85+
}
86+
return d.Sum(nil)
87+
}
88+
89+
// Keccak512Hasher creates a repetitive Keccak512 hasher, allowing the same hash
90+
// data structures to be reused between hash runs instead of requiring new ones
91+
// to be created.
92+
//
93+
// The returned function is not thread safe!
94+
func Keccak512Hasher() Hasher {
95+
hasher := sha3.NewKeccak512()
96+
97+
return func(data []byte) []byte {
98+
hasher.Write(data)
99+
result := hasher.Sum(nil)
100+
hasher.Reset()
101+
return result
102+
}
103+
}
104+
57105
// Deprecated: For backward compatibility as other packages depend on these
58106
func Sha3Hash(data ...[]byte) common.Hash { return Keccak256Hash(data...) }
59107

‎crypto/sha3/hashes.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import (
1515
// NewKeccak256 creates a new Keccak-256 hash.
1616
func NewKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} }
1717

18+
// NewKeccak512 creates a new Keccak-512 hash.
19+
func NewKeccak512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x01} }
20+
1821
// New224 creates a new SHA3-224 hash.
1922
// Its generic security strength is 224 bits against preimage attacks,
2023
// and 112 bits against collision attacks.

‎crypto/sha3/keccakf_amd64.s‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@
322322
// func keccakF1600(state *[25]uint64)
323323
TEXT ·keccakF1600(SB), 0, $200-8
324324
MOVQ state+0(FP), rpState
325-
SUBQ $(8*25), SP
326325

327326
// Convert the user state into an internal state
328327
NOTQ _be(rpState)
@@ -388,5 +387,4 @@ TEXT ·keccakF1600(SB), 0, $200-8
388387
NOTQ _mi(rpState)
389388
NOTQ _sa(rpState)
390389

391-
ADDQ $(8*25), SP
392390
RET

0 commit comments

Comments
 (0)