From e90dc4231fc50facfba6d015b06a3f73d384cb1e Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Mon, 28 May 2018 19:46:51 -0700 Subject: [PATCH] [lbry] chaincfg: implement LBRY PoW Hash --- chaincfg/chainhash/hashfuncs.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/chaincfg/chainhash/hashfuncs.go b/chaincfg/chainhash/hashfuncs.go index bf74f73c..194c60e3 100644 --- a/chaincfg/chainhash/hashfuncs.go +++ b/chaincfg/chainhash/hashfuncs.go @@ -5,7 +5,12 @@ package chainhash -import "crypto/sha256" +import ( + "crypto/sha256" + "crypto/sha512" + + "golang.org/x/crypto/ripemd160" +) // HashB calculates hash(b) and returns the resulting bytes. func HashB(b []byte) []byte { @@ -31,3 +36,26 @@ func DoubleHashH(b []byte) Hash { first := sha256.Sum256(b) return Hash(sha256.Sum256(first[:])) } + +// LbryPoWHashH calculates returns the PoW Hash. +// +// doubled := SHA256(SHA256(b)) +// expanded := SHA512(doubled) +// left := RIPEMD160(expanded[0:32]) +// right := RIPEMD160(expanded[32:64]) +// result := SHA256(SHA256(left||right)) +func LbryPoWHashH(b []byte) Hash { + doubled := DoubleHashB(b) + expanded := sha512.Sum512(doubled) + + r := ripemd160.New() + r.Reset() + r.Write(expanded[:sha256.Size]) + left := r.Sum(nil) + + r.Reset() + r.Write(expanded[sha256.Size:]) + + combined := r.Sum(left) + return DoubleHashH(combined) +}