aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-03-05 23:38:34 +0800
committerFelix Lange <fjl@users.noreply.github.com>2017-03-09 22:50:14 +0800
commit3b00a77de57ab2737a7887521c192ce004c721e3 (patch)
tree74f9abad1af8fec472c2d5cb5f4ab424e44f9b1f /crypto
parent288700c4d884321b7de7119b596c6e052fa33b27 (diff)
downloaddexon-3b00a77de57ab2737a7887521c192ce004c721e3.tar.gz
dexon-3b00a77de57ab2737a7887521c192ce004c721e3.tar.zst
dexon-3b00a77de57ab2737a7887521c192ce004c721e3.zip
crypto, pow: add pure Go implementation of ethash
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.go48
-rw-r--r--crypto/sha3/hashes.go3
-rw-r--r--crypto/sha3/keccakf_amd64.s2
3 files changed, 51 insertions, 2 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index ecc3be3ce..a60a4617e 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -37,6 +37,11 @@ var (
secp256k1_halfN = new(big.Int).Div(secp256k1_N, big.NewInt(2))
)
+// Hasher is a repetitive hasher allowing the same hash data structures to be
+// reused between hash runs instead of requiring new ones to be created.
+type Hasher func(data []byte) []byte
+
+// Keccak256 calculates and returns the Keccak256 hash of the input data.
func Keccak256(data ...[]byte) []byte {
d := sha3.NewKeccak256()
for _, b := range data {
@@ -45,6 +50,8 @@ func Keccak256(data ...[]byte) []byte {
return d.Sum(nil)
}
+// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
+// converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) (h common.Hash) {
d := sha3.NewKeccak256()
for _, b := range data {
@@ -54,6 +61,47 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
return h
}
+// Keccak256Hasher creates a repetitive Keccak256 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 Keccak256Hasher() Hasher {
+ hasher := sha3.NewKeccak256()
+
+ return func(data []byte) []byte {
+ hasher.Write(data)
+ result := hasher.Sum(nil)
+ hasher.Reset()
+ return result
+ }
+}
+
+// Keccak512 calculates and returns the Keccak512 hash of the input data.
+func Keccak512(data ...[]byte) []byte {
+ d := sha3.NewKeccak512()
+ for _, b := range data {
+ d.Write(b)
+ }
+ return d.Sum(nil)
+}
+
+// Keccak512Hasher creates a repetitive Keccak512 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 Keccak512Hasher() Hasher {
+ hasher := sha3.NewKeccak512()
+
+ return func(data []byte) []byte {
+ hasher.Write(data)
+ result := hasher.Sum(nil)
+ hasher.Reset()
+ return result
+ }
+}
+
// Deprecated: For backward compatibility as other packages depend on these
func Sha3Hash(data ...[]byte) common.Hash { return Keccak256Hash(data...) }
diff --git a/crypto/sha3/hashes.go b/crypto/sha3/hashes.go
index 669cd5543..fa0d7b436 100644
--- a/crypto/sha3/hashes.go
+++ b/crypto/sha3/hashes.go
@@ -15,6 +15,9 @@ import (
// NewKeccak256 creates a new Keccak-256 hash.
func NewKeccak256() hash.Hash { return &state{rate: 136, outputLen: 32, dsbyte: 0x01} }
+// NewKeccak512 creates a new Keccak-512 hash.
+func NewKeccak512() hash.Hash { return &state{rate: 72, outputLen: 64, dsbyte: 0x01} }
+
// New224 creates a new SHA3-224 hash.
// Its generic security strength is 224 bits against preimage attacks,
// and 112 bits against collision attacks.
diff --git a/crypto/sha3/keccakf_amd64.s b/crypto/sha3/keccakf_amd64.s
index a35335178..f88533acc 100644
--- a/crypto/sha3/keccakf_amd64.s
+++ b/crypto/sha3/keccakf_amd64.s
@@ -322,7 +322,6 @@
// func keccakF1600(state *[25]uint64)
TEXT ·keccakF1600(SB), 0, $200-8
MOVQ state+0(FP), rpState
- SUBQ $(8*25), SP
// Convert the user state into an internal state
NOTQ _be(rpState)
@@ -388,5 +387,4 @@ TEXT ·keccakF1600(SB), 0, $200-8
NOTQ _mi(rpState)
NOTQ _sa(rpState)
- ADDQ $(8*25), SP
RET