mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-23 07:16:47 +00:00

* Replace default hasher (Double-SHA256) with domain seperated blake2b * Replace all hashes with domain seperated blake2b * Update the genesis blocks * Replace OP_HASH256 with OP_BLAKE2B * Fix the merkle tree by appending zeros instead of duplicating the hash when there is 1 branch left * Update tests * Add a payloadHash function * Update gitignore to ignore binaries * Fix a bug in the blake2b opcode
80 lines
2.7 KiB
Go
80 lines
2.7 KiB
Go
package hashes
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/crypto/blake2b"
|
|
)
|
|
|
|
const (
|
|
transcationHashDomain = "TransactionHash"
|
|
transcationIDDomain = "TransactionID"
|
|
transcationSigningDomain = "TransactionSigningHash"
|
|
payloadDomain = "PayloadHash"
|
|
blockDomain = "BlockHash"
|
|
proofOfWorkDomain = "ProofOfWorkHash"
|
|
merkleBranchDomain = "MerkleBranchHash"
|
|
)
|
|
|
|
// NewTransactionHashWriter Returns a new HashWriter used for transaction hashes
|
|
func NewTransactionHashWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(transcationHashDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", transcationHashDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|
|
|
|
// NewTransactionIDWriter Returns a new HashWriter used for transaction IDs
|
|
func NewTransactionIDWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(transcationIDDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", transcationIDDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|
|
|
|
// NewTransactionSigningHashWriter Returns a new HashWriter used for signing on a transaction
|
|
func NewTransactionSigningHashWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(transcationSigningDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", transcationSigningDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|
|
|
|
// NewPayloadHashWriter Returns a new HashWriter used for hashing a transaction payload
|
|
func NewPayloadHashWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(payloadDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", payloadDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|
|
|
|
// NewBlockHashWriter Returns a new HashWriter used for hashing blocks
|
|
func NewBlockHashWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(blockDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", blockDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|
|
|
|
// NewPoWHashWriter Returns a new HashWriter used for the PoW function
|
|
func NewPoWHashWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(proofOfWorkDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", proofOfWorkDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|
|
|
|
// NewMerkleBranchHashWriter Returns a new HashWriter used for a merkle tree branch
|
|
func NewMerkleBranchHashWriter() HashWriter {
|
|
blake, err := blake2b.New256([]byte(merkleBranchDomain))
|
|
if err != nil {
|
|
panic(errors.Wrapf(err, "this should never happen. %s is less than 64 bytes", merkleBranchDomain))
|
|
}
|
|
return HashWriter{blake}
|
|
}
|