chainhash

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2024 License: ISC Imports: 9 Imported by: 0

README

chainhash

Build Status ISC License GoDoc

chainhash provides a generic hash type and associated functions that allows the specific hash algorithm to be abstracted.

Installation and Updating

$ go get -u github.com/btcsuite/btcd/chaincfg/chainhash

GPG Verification Key

All official release tags are signed by Conformal so users can ensure the code has not been tampered with and is coming from the btcsuite developers. To verify the signature perform the following:

  • Download the public key from the Conformal website at https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt

  • Import the public key into your GPG keyring:

    gpg --import GIT-GPG-KEY-conformal.txt
    
  • Verify the release tag with the following command where TAG_NAME is a placeholder for the specific tag:

    git tag -v TAG_NAME
    

License

Package chainhash is licensed under the copyfree ISC License.

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

View Source
const HashSize = 32

HashSize of array used to store hashes. See Hash.

View Source
const MaxHashStringSize = HashSize * 2

MaxHashStringSize is the maximum length of a Hash hash string.

Variables

View Source
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 */

)
View Source
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

func Decode(dst *Hash, src string) error

Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.

func DoubleHashB

func DoubleHashB(b []byte) []byte

DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.

func DoubleHashRaw

func DoubleHashRaw(h hash.Hash) []byte

DoubleHashRaw calculates hash(hash(h)) and returns the resulting bytes.

func HashB

func HashB(b []byte) []byte

HashB calculates hash(b) and returns the resulting bytes.

func PackedHashesToUint64

func PackedHashesToUint64(hashes []Hash) []uint64

PackedHashesToUint64 returns the uint64s in the packed hashes as a slice of uint64s.

Types

type Hash

type Hash [HashSize]byte

Hash is used in several of the bitcoin messages and common structures. It typically represents the double sha256 of data.

func DoubleHashH

func DoubleHashH(b []byte) Hash

DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a Hash.

func HashH

func HashH(b []byte) Hash

HashH calculates hash(b) and returns the resulting bytes as a Hash.

func NewHash

func NewHash(newHash []byte) (*Hash, error)

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

func NewHashFromStr(hash string) (*Hash, error)

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

func TaggedHash(tag []byte, msgs ...[]byte) *Hash

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

func TaggedHash512_256(tag []byte, serialize func(io.Writer)) *Hash

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

func Uint64sToPackedHashes(ints []uint64) []Hash

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

func (hash *Hash) CloneBytes() []byte

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) IsEqual

func (hash *Hash) IsEqual(target *Hash) bool

IsEqual returns true if target is the same as hash.

func (Hash) MarshalJSON

func (hash Hash) MarshalJSON() ([]byte, error)

MarshalJSON serialises the hash as a JSON appropriate string value.

func (*Hash) SetBytes

func (hash *Hash) SetBytes(newHash []byte) error

SetBytes sets the bytes which represent the hash. An error is returned if the number of bytes passed in is not HashSize.

func (Hash) String

func (hash Hash) String() string

String returns the Hash as the hexadecimal string of the byte-reversed hash.

func (*Hash) UnmarshalJSON

func (hash *Hash) UnmarshalJSON(input []byte) error

UnmarshalJSON parses the hash with JSON appropriate string value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL