merkler

package
v0.0.0-...-05bc493 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const StubNodeIndex = 0

Variables

This section is empty.

Functions

func AdjustStackSequenceIndexByUnused

func AdjustStackSequenceIndexByUnused(index uint) uint

func StackSequenceUnused

func StackSequenceUnused(count uint) uint8

func UnbalancedBitCount

func UnbalancedBitCount(index uint, bitCount uint8) uint8

Returns a level at which the unbalanced node will connect to the rightmost branch. It trims from left all consequent 1's starting from (bitCount) and returns a number of remaining bits. Doesn't panic, but result is meaningless when (maxPairLevel) is not in [0, 64] or (index) >= 1<<(maxPairLevel)

Types

type ForkingCalculator

type ForkingCalculator struct {
	StackedCalculator
}

func NewForkingCalculator

func NewForkingCalculator(digester cryptkit.PairDigester, unbalancedStub cryptkit.Digest) ForkingCalculator

func (*ForkingCalculator) ForkCalculator

func (p *ForkingCalculator) ForkCalculator() ForkingCalculator

func (*ForkingCalculator) ForkSequence

func (p *ForkingCalculator) ForkSequence() cryptkit.ForkingDigester

type PathBuilder

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

func NewPathBuilder

func NewPathBuilder(leafCount uint, stubbed bool) PathBuilder

func (PathBuilder) WalkFor

func (v PathBuilder) WalkFor(index uint, nodeFn PathEntryFunc)

For the given (index) WalkFor will call (nodeFn) for each level of tree that has to be included into a merkle-proof, starting from leafs. The (nodeFn) is called with a relevant index of a hash value in the merkler log, and with flags about the value - leaf-or-node and right-or-left.

NB! When stub is used, then (nodeFn) is called as (StubNodeIndex, false, _) as StackCalculator can't produce a node value at index StubNodeIndex. NB! The (nodeFn) is not called for the requested leaf itself.

type PathEntryFunc

type PathEntryFunc func(index uint, isLeaf, isRight bool)

Is called in a sequence of merkle-proof items, starting from leafs. (index) is an index of a relevant entry of StackCalculator hashing log. (isLeaf) indicates is this should be a leaf value or a node value. (isRight) indicates position of this entry for a hashing operation. Either left or right.

NB! Leaf index is [0, leafCount-1], node index is [1, leafCount+N], node index = 0 means stub value.

type StackedCalculator

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

A calculator to do streaming calculation of Merkle-tree by using provided PairDigester. The resulting merkle will have same depth for all branches except for the rightmost branch.

When unbalancedStub == nil, then FinishSequence() will create the rightmost branch by recursively applying the same rule - all sub-branches will have same depth except for the rightmost sub-branch.

When unbalancedStub != nil, then FinishSequence() will create a perfect tree by using unbalancedStub once per level when a value for the rightmost sub-branch is missing.

When AddNext() was never called then FinishSequence() will return a non-nil unbalancedStub otherwise will panic.

Complexity (n - a number of added hashes):

  • AddNext() is O(1), it does only upto 2 calls to PairDigester.DigestPair()
  • FinishSequence() is O(log n), it does k*log(n) calls to PairDigester.DigestPair() where k ~ 1 when unbalancedStub == nil and k ~ 2 otherwise
  • ForkSequence() is O(log n), but only copies memory
  • Memory is O(log n)

func NewStackedCalculator

func NewStackedCalculator(digester cryptkit.PairDigester, unbalancedStub cryptkit.Digest, traceFn TraceFunc) StackedCalculator

Creates a sequence digester that does merkle tree calculation. When (unbalancedStub) is empty, then the resulting merkle-tree will be closed without stubbing, otherwise with per-level stubbing with (unbalancedStub) on the right side. When (traceFn) is not nil, then StackedCalculator will call it each time when a leaf or node is added.

NB! For every AddNext() call the (traceFn) is always called twice - first time with (leafX, true) value, and then (nodeX, false), where nodeX can be nil. And during FinishSequence() - the (traceFn) can be called N < 2*log(leafCount) times with (nodeX, false), where nodeX can not be nil.

func (*StackedCalculator) AddNext

func (p *StackedCalculator) AddNext(addDigest longbits.FoldableReader)

func (*StackedCalculator) Count

func (p *StackedCalculator) Count() int

func (*StackedCalculator) FinishSequence

func (p *StackedCalculator) FinishSequence() cryptkit.Digest

func (*StackedCalculator) GetDigestMethod

func (p *StackedCalculator) GetDigestMethod() cryptkit.DigestMethod

func (*StackedCalculator) GetDigestSize

func (p *StackedCalculator) GetDigestSize() int

type TraceFunc

type TraceFunc func(v longbits.FoldableReader, isLeaf bool)

Jump to

Keyboard shortcuts

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