Low-level kernel & storage

termbank reference

termbank provides the trusted storage primitives for the proof-DAG stack: shared work budgets, storage error types, content hashing (TERMBANK_FORMAT, fnv1a64, mix64, combine), SHA-256 digests, and the node id TermId. The hash-consed proof-DAG tables, workspaces, and verified watermarks live in the termbank.dag subpackage (DAG storage).

For typed expressions and proofs, start with the API guide.

The public root API of termbank: every name src/termbank/__init__.mojo exports, rendered from the sources' docstrings. The shared contracts are on the termbank page and the DAG storage page.

Ids

The id of a node (T1.1).

TermId

@fieldwise_init
struct TermId(Equatable, Hashable, ImplicitlyCopyable, Writable)
Field Type Description
index UInt32 The node's position in its table.
generation UInt32 The generation component of the id; termbank.dag leaves it 0.

The id of a node: its index in the bank and its generation.

Defined in src/termbank/ids.mojo.

TermId.write_to

def write_to(self, mut writer: Some[Writer])

Write the id as t<index>@<generation>.

Arguments.

  • writer: The writer to write to.

Budgets and errors

Every input-dependent loop charges a Budget (G3); every failing storage call raises a BankError (T1.1).

Budget

Mojo

@fieldwise_init
struct Budget(ImplicitlyCopyable, Writable)
Field Type Description
remaining UInt64 The steps left.

Python

from judgment import Budget

Python constructor:

def __init__(self, remaining)
Field Description
remaining The steps left.

A count of steps that a call may still take.

Defined in src/termbank/budget.mojo.

Budget.charge

Mojo

@always_inline
def charge(mut self, n: UInt64) raises BankError

Python

This overload is available only in Mojo; it is not exposed by the Python client.

Take n steps from the budget.

Arguments.

  • n: The steps to take.

Raises.

  • BankError: LIMIT when n exceeds what remains; remaining is then unchanged.

Budget.write_to

Mojo

def write_to(self, mut writer: Some[Writer])

Python

str(value)

Use str(value) or print(value) in Python.

Write the remaining steps.

Arguments.

  • writer: The writer to write to.

BankError

@fieldwise_init
struct BankError(Copyable, Movable, Writable)
Field Type Description
kind BankErrorKind What went wrong.
message String A description for people; callers match on kind.

The error every failing termbank method raises.

Defined in src/termbank/errors.mojo.

BankError.write_to

def write_to(self, mut writer: Some[Writer])

Write the kind and the message.

Arguments.

  • writer: The writer to write to.

BankErrorKind

@fieldwise_init
struct BankErrorKind(Equatable, ImplicitlyCopyable, Writable)
Field Type Description
code UInt8 The kind's number; compare kinds with ==, never by this number.

What went wrong in a termbank call.

Defined in src/termbank/errors.mojo.

BankErrorKind.CAPACITY

comptime CAPACITY = BankErrorKind(1)

An index, count, offset, or generation would exceed its limit (G4), or a bvar index is too large.

BankErrorKind.LIMIT

comptime LIMIT = BankErrorKind(2)

A budget ran out (G3).

BankErrorKind.ARITY

comptime ARITY = BankErrorKind(3)

An argument count does not fit a head's arity, or an argument position is out of range.

BankErrorKind.UNKNOWN_ID

comptime UNKNOWN_ID = BankErrorKind(4)

An id or row reference is invalid or out of range.

BankErrorKind.HEAD

comptime HEAD = BankErrorKind(5)

register_head received an invalid HeadInfo.

BankErrorKind.PAYLOAD

comptime PAYLOAD = BankErrorKind(6)

A payload does not fit its head's payload kind.

BankErrorKind.REGION

comptime REGION = BankErrorKind(7)

A reserved kind that no termbank operation raises.

BankErrorKind.INTERNAL

comptime INTERNAL = BankErrorKind(8)

A broken termbank invariant, never a property of the input; a bank bug.

BankErrorKind.STORAGE

comptime STORAGE = BankErrorKind(9)

A paged backing, integrity or coordination failure; not invalid syntax.

BankErrorKind.WORKSPACE

comptime WORKSPACE = BankErrorKind(10)

A writer, workspace commit, truncation or child that termbank.dag does not allow.

BankErrorKind.VERDICT

comptime VERDICT = BankErrorKind(11)

A verdict of another bank, out of range, overlapping, or after a refusal.

BankErrorKind.write_to

def write_to(self, mut writer: Some[Writer])

Write the kind's name.

Arguments.

  • writer: The writer to write to.

Hashes and digests

The structural hash primitives, their versioned format (TERMBANK_FORMAT), and SHA-256 hashing (T1.2).

TERMBANK_FORMAT

comptime TERMBANK_FORMAT: UInt32 = 2

The format version of the hash constants and the structural order.

Defined in src/termbank/hash.mojo.

mix64

def mix64(z: UInt64) -> UInt64

Scramble 64 bits with SplitMix64's finalizer.

In the weak_hash test build it returns 0, so that every structural hash and every hash-index probe collides.

Arguments.

  • z: The value to scramble.

Returns. The scrambled value, computed modulo 2⁶⁴.

Defined in src/termbank/hash.mojo.

combine

def combine(h: UInt64, x: UInt64) -> UInt64

Fold a value into a running hash.

Arguments.

  • h: The running hash.
  • x: The value to fold in.

Returns. mix64(h ^ (x + 0x9e3779b97f4a7c15 + (h << 6) + (h >> 2))), modulo 2⁶⁴.

Defined in src/termbank/hash.mojo.

fnv1a64

def fnv1a64(data: Span[UInt8, _]) -> UInt64

Hash bytes with 64-bit FNV-1a.

Arguments.

  • data: The bytes to hash.

Returns. The FNV-1a hash, computed modulo 2⁶⁴.

Defined in src/termbank/hash.mojo.

Sha256

struct Sha256(Copyable, Movable)

Accumulate a SHA-256 digest in fixed working space.

Feed byte spans with update, then obtain the digest with finish. The FIPS 180-4 message domain is fewer than 264 bits (261 bytes) across all updates. Callers enforce their input limits before hashing.

Defined in src/termbank/sha256.mojo.

Sha256.__init__

def __init__(out self)

Initialize the eight chaining words for an empty message (FIPS 180-4 section 5.3.3).

Sha256.update

def update(mut self, data: Span[UInt8, _])

Append bytes to the message without retaining the input span.

Empty updates do nothing. Work is linear in the input length; storage is fixed and independent of message size or chunking.

Arguments.

  • data: The next bytes; total message length must remain below 2**61 bytes.

Sha256.finish

def finish(var self) -> InlineArray[UInt8, 32]

Pad the message and return its SHA-256 digest in network byte order.

Transfer the state with state^.finish(), or finalize an explicit copy with state.copy().finish() to keep appending to the original.

Returns. The 32 digest bytes, most significant byte of each word first.

sha256

def sha256(data: Span[UInt8, _]) -> InlineArray[UInt8, 32]

Hash a byte span with SHA-256 (FIPS 180-4).

Arguments.

  • data: The complete message, fewer than 2**61 bytes.

Returns. The 32 digest bytes in network byte order.

Defined in src/termbank/sha256.mojo.

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub