shachain

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HashSize of array used to store hashes.  See ShaHash.
	HashSize = sha256.Size

	// MaxHashStringSize is the maximum length of a ShaHash hash string.
	MaxHashStringSize = HashSize * 2
)

Variables

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

This section is empty.

Types

type Producer

type Producer interface {
	// AtIndex produces a secret by evaluating using the initial seed and a
	// particular index.
	AtIndex(uint64) (*ShaHash, error)

	// Encode writes a binary serialization of the Producer implementation
	// to the passed io.Writer.
	Encode(io.Writer) error
}

Producer is an interface which serves as an abstraction over the data structure responsible for efficiently generating the secrets for a particular index based on a root seed. The generation of secrets should be made in such way that secret store might efficiently store and retrieve the secrets. This is typically implemented as a tree-based PRF.

type RevocationProducer

type RevocationProducer struct {
	// contains filtered or unexported fields
}

RevocationProducer is an implementation of Producer interface using the shachain PRF construct. Starting with a single 32-byte element generated from a CSPRNG, shachain is able to efficiently generate a nearly unbounded number of secrets while maintaining a constant amount of storage. The original description of shachain can be found here: https://github.com/rustyrussell/ccan/blob/master/ccan/crypto/shachain/design.txt with supplementary material here: https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#per-commitment-secret-requirements

func NewRevocationProducer

func NewRevocationProducer(root ShaHash) *RevocationProducer

NewRevocationProducer creates new instance of shachain producer.

func NewRevocationProducerFromBytes

func NewRevocationProducerFromBytes(data []byte) (*RevocationProducer, error)

NewRevocationProducerFromBytes deserializes an instance of a RevocationProducer encoded in the passed byte slice, returning a fully initialized instance of a RevocationProducer.

func (*RevocationProducer) AtIndex

func (p *RevocationProducer) AtIndex(v uint64) (*ShaHash, error)

AtIndex produces a secret by evaluating using the initial seed and a particular index.

NOTE: Part of the Producer interface.

func (*RevocationProducer) Encode

func (p *RevocationProducer) Encode(w io.Writer) error

Encode writes a binary serialization of the Producer implementation to the passed io.Writer.

NOTE: Part of the Producer interface.

type RevocationStore

type RevocationStore struct {
	// contains filtered or unexported fields
}

RevocationStore is a concrete implementation of the Store interface. The revocation store is able to efficiently store N derived shachain elements in a space efficient manner with a space complexity of O(log N). The original description of the storage methodology can be found here: https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#efficient-per-commitment-secret-storage

func NewRevocationStore

func NewRevocationStore() *RevocationStore

NewRevocationStore creates the new shachain store.

func NewRevocationStoreFromBytes

func NewRevocationStoreFromBytes(r io.Reader) (*RevocationStore, error)

NewRevocationStoreFromBytes recreates the initial store state from the given binary shachain store representation.

func (*RevocationStore) AddNextEntry

func (store *RevocationStore) AddNextEntry(hash *ShaHash) error

AddNextEntry attempts to store the given hash within its internal storage in an efficient manner.

NOTE: The hashes derived from the shachain MUST be inserted in the order they're produced by a shachain.Producer.

NOTE: This function is part of the Store interface.

func (*RevocationStore) Encode

func (store *RevocationStore) Encode(w io.Writer) error

Encode writes a binary serialization of the shachain elements currently saved by implementation of shachain.Store to the passed io.Writer.

NOTE: This function is part of the Store interface.

func (*RevocationStore) LookUp

func (store *RevocationStore) LookUp(v uint64) (*ShaHash, error)

LookUp function is used to restore/lookup/fetch the previous secret by its index. If secret which corresponds to given index was not previously placed in store we will not able to derive it and function will fail.

NOTE: This function is part of the Store interface.

type ShaHash

type ShaHash [HashSize]byte

ShaHash represents a sha256 hash that is used when working with shachains.

func NewHash

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

NewHash returns a new ShaHash from a byte slice. An error is returned if the number of bytes passed in is not HashSize.

func NewHashFromStr

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

NewHashFromStr creates a ShaHash from a hexadeimal hash string. Unlike most hashes used in cryptocurrencies, this function does not perform any byte reversals.

func (ShaHash) String

func (hash ShaHash) String() string

String returns the ShaHash as a hexadecimal string.

type Store

type Store interface {
	// LookUp function is used to restore/lookup/fetch the previous secret
	// by its index.
	LookUp(uint64) (*ShaHash, error)

	// AddNextEntry attempts to store the given hash within its internal
	// storage in an efficient manner.
	//
	// NOTE: The hashes derived from the shachain MUST be inserted in the
	// order they're produced by a shachain.Producer.
	AddNextEntry(*ShaHash) error

	// Encode writes a binary serialization of the shachain elements
	// currently saved by implementation of shachain.Store to the passed
	// io.Writer.
	Encode(io.Writer) error
}

Store is an interface which serves as an abstraction over data structure responsible for efficiently storing and restoring hash secrets by given indexes.

Description: The Lightning Network wants a chain of (say 1 million) unguessable 256 bit values; we generate them and send them one at a time to a remote node. We don't want the remote node to have to store all the values, so it's better if they can derive them once they see them.

Jump to

Keyboard shortcuts

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