Documentation ¶
Overview ¶
Package chainhash provides abstracted hash functionality.
This package provides a generic hash type and associated functions that allows the specific hash algorithm to be abstracted.
Index ¶
- Constants
- Variables
- func Decode(dst *Hash, src string) error
- func DoubleHashB(b []byte) []byte
- func DoubleHashRaw(h hash.Hash) []byte
- func HashB(b []byte) []byte
- func PackedHashesToUint64(hashes []Hash) []uint64
- type Hash
- func DoubleHashH(b []byte) Hash
- func HashH(b []byte) Hash
- func NewHash(newHash []byte) (*Hash, error)
- func NewHashFromStr(hash string) (*Hash, error)
- func TaggedHash(tag []byte, msgs ...[]byte) *Hash
- func TaggedHash512_256(tag []byte, serialize func(io.Writer)) *Hash
- func Uint64sToPackedHashes(ints []uint64) []Hash
Constants ¶
const HashSize = 32
HashSize of array used to store hashes. See Hash.
const MaxHashStringSize = HashSize * 2
MaxHashStringSize is the maximum length of a Hash hash string.
Variables ¶
var ( // TagBIP0340Challenge is the BIP-0340 tag for challenges. TagBIP0340Challenge = []byte("BIP0340/challenge") // TagBIP0340Aux is the BIP-0340 tag for aux data. TagBIP0340Aux = []byte("BIP0340/aux") // TagBIP0340Nonce is the BIP-0340 tag for nonces. TagBIP0340Nonce = []byte("BIP0340/nonce") // TagTapSighash is the tag used by BIP 341 to generate the sighash // flags. TagTapSighash = []byte("TapSighash") // TagTagTapLeaf is the message tag prefix used to compute the hash // digest of a tapscript leaf. TagTapLeaf = []byte("TapLeaf") // TagTapBranch is the message tag prefix used to compute the // hash digest of two tap leaves into a taproot branch node. TagTapBranch = []byte("TapBranch") // TagTapTweak is the message tag prefix used to compute the hash tweak // used to enable a public key to commit to the taproot branch root // for the witness program. TagTapTweak = []byte("TapTweak") // TagUtreexoV1 is the tag used by utreexo v1 serialized hashes to // generate the leaf hashes to be committed into the accumulator. TagUtreexoV1 = []byte("UtreexoV1") // UTREEXO_TAG_V1 is the version tag to be prepended to the leafhash. It's just the sha512 hash of the string // `UtreexoV1` represented as a vector of [u8] ([85 116 114 101 101 120 111 86 49]). // The same tag is "5574726565786f5631" as a hex string. UTREEXO_TAG_V1 = [64]byte{ 0x5b, 0x83, 0x2d, 0xb8, 0xca, 0x26, 0xc2, 0x5b, 0xe1, 0xc5, 0x42, 0xd6, 0xcc, 0xed, 0xdd, 0xa8, 0xc1, 0x45, 0x61, 0x5c, 0xff, 0x5c, 0x35, 0x72, 0x7f, 0xb3, 0x46, 0x26, 0x10, 0x80, 0x7e, 0x20, 0xae, 0x53, 0x4d, 0xc3, 0xf6, 0x42, 0x99, 0x19, 0x99, 0x31, 0x77, 0x2e, 0x03, 0x78, 0x7d, 0x18, 0x15, 0x6e, 0xb3, 0x15, 0x1e, 0x0e, 0xd1, 0xb3, 0x09, 0x8b, 0xdc, 0x84, 0x45, 0x86, 0x18, 0x85, } // UTREEXO_TAG_V1_APPEND is just UTREEXO_TAG_V1 doubled so it can be appeneded directly to a hash digest. UTREEXO_TAG_V1_APPEND = [128]byte{}/* 128 elements not displayed */ )
var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)
ErrHashStrSize describes an error that indicates the caller specified a hash string that has too many characters.
Functions ¶
func Decode ¶
Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.
func DoubleHashB ¶
DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
func DoubleHashRaw ¶
DoubleHashRaw calculates hash(hash(h)) and returns the resulting bytes.
func PackedHashesToUint64 ¶
PackedHashesToUint64 returns the uint64s in the packed hashes as a slice of uint64s.
Types ¶
type Hash ¶
Hash is used in several of the bitcoin messages and common structures. It typically represents the double sha256 of data.
func DoubleHashH ¶
DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a Hash.
func NewHash ¶
NewHash returns a new Hash from a byte slice. An error is returned if the number of bytes passed in is not HashSize.
func NewHashFromStr ¶
NewHashFromStr creates a Hash from a hash string. The string should be the hexadecimal string of a byte-reversed hash, but any missing characters result in zero padding at the end of the Hash.
func TaggedHash ¶
TaggedHash implements the tagged hash scheme described in BIP-340. We use sha-256 to bind a message hash to a specific context using a tag: sha256(sha256(tag) || sha256(tag) || msg).
func TaggedHash512_256 ¶
TaggedHash512_256 implements a tagged hash scheme for utreexo leaves. We use sha-512_256 to bind a message hash to a specific context using a tag: sha512_256(sha512(tag) || sha512(tag) || leafdata).
func Uint64sToPackedHashes ¶
Uint64sToPackedHashes packs the passed in uint64s into the 32 byte hashes. 4 uint64s are packed into each 32 byte hash and if there's leftovers, it's filled with maxuint64.
func (*Hash) CloneBytes ¶
CloneBytes returns a copy of the bytes which represent the hash as a byte slice.
NOTE: It is generally cheaper to just slice the hash directly thereby reusing the same bytes rather than calling this method.
func (Hash) MarshalJSON ¶
MarshalJSON serialises the hash as a JSON appropriate string value.
func (*Hash) SetBytes ¶
SetBytes sets the bytes which represent the hash. An error is returned if the number of bytes passed in is not HashSize.
func (*Hash) UnmarshalJSON ¶
UnmarshalJSON parses the hash with JSON appropriate string value.