Table of Contents

MerkleBlockComputation Class

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
MerkleBlockComputation.cs

Represents the result of one block-mode Merkle computation: the tree's root, the shape of the input it was taken over, and the ordered leaf hashes an authentication path is built from.

public sealed class MerkleBlockComputation
Inheritance
MerkleBlockComputation
Inherited Members
Extension Methods

Remarks

This type exists so that a caller which has just streamed a large input past itself receives both the root and the leaf hashes from the same pass. Generating an authentication path needs the leaf hashes; without them the caller would have to stream the input a second time.

The leaf hashes cost leafCount × hashLength bytes to retain - for a 512 MiB input at one-mebibyte blocks that is 512 hashes, or 16 KiB. A caller that needs only the root and not a path should use ComputeRootOfBlocks(Stream, int, MerkleTreeDiagnostics?, CancellationToken), which folds the tree as it reads and never holds more than a logarithmic number of hashes.

Properties

BlockCount

Gets the number of blocks the input divided into, which is also the number of leaves.

public long BlockCount { get; }

Property Value

long

BlockCount(long, int) over InputLength and BlockSize; zero for an empty input.

BlockSize

Gets the block size the input was divided into leaves by.

public int BlockSize { get; }

Property Value

int

The block size in bytes.

InputLength

Gets the total number of input bytes the tree was computed over.

public long InputLength { get; }

Property Value

long

The input length in bytes; zero for an empty input.

LeafHashes

Gets the ordered leaf hashes, one per block.

public IReadOnlyList<byte[]> LeafHashes { get; }

Property Value

IReadOnlyList<byte[]>

The leaf hashes in block order, which is empty for a zero-length input because such an input has zero blocks rather than one empty block.

Remarks

Pass this list to AuthenticationPath(IReadOnlyList<byte[]>, long) to produce an authentication path without re-reading the input.

Root

Gets the tree's root hash - the unbound Merkle Tree Hash, not a length-bound root.

public byte[] Root { get; }

Property Value

byte[]

The root, which is the hash of zero bytes when InputLength is zero, and the sole leaf's hash when the input produced exactly one block.

Remarks

To publish a commitment that also pins the input's length, pass this value and InputLength to BindRoot(ReadOnlySpan<byte>, long).

Methods

BlockLength(long)

Returns the length of the specified block of this computation's input, which is shorter than BlockSize only for the final block of an input whose length is not a whole multiple of it.

public int BlockLength(long blockIndex)

Parameters

blockIndex long

The zero-based index of the block.

Returns

int

The length, in bytes, of the block.

Remarks

BlockLength(long, long, int) returns zero for a block beyond the input; this member knows the input it belongs to and rejects such a block instead.

Exceptions

ArgumentOutOfRangeException

blockIndex is negative or not less than BlockCount.

BlockOffset(long)

Returns the byte offset at which the specified block of this computation's input begins.

public long BlockOffset(long blockIndex)

Parameters

blockIndex long

The zero-based index of the block.

Returns

long

The offset, in bytes, from the start of the input.

Remarks

BlockOffset(long, int) is pure arithmetic over any index; this member knows the input it belongs to and rejects a block that does not exist.

Exceptions

ArgumentOutOfRangeException

blockIndex is negative or not less than BlockCount.

Applies to

ProductVersions
.NET8, 10