nupp.checksum

Numeric checksums, for noticing that bytes got corrupted.

One call for a whole string, or a context you feed in pieces.

const checksum = nupp.checksum

print(checksum.value("crc32c", "hello"))

with running = checksum.newChecksum("crc32") do
    running:write("hel")
    running:write("lo")
    print(running:value())
end

value is a snapshot rather than a finish, so a context keeps going after one is taken. A context is an io.Writer, so reader:transferTo(running) checksums a whole stream without holding it. These say nothing about whether bytes were changed on purpose: for that reach for nupp.mac.

Built-in algorithms#

Name Width Parameters
adler32 32 Adler-32
crc32 32 Reflected IEEE CRC-32
crc32c 32 Reflected Castagnoli CRC-32C
crc64-ecma 64 ECMA-182, non-reflected, zero initialization and xor
crc64nvme 64 Reflected CRC-64/NVME

crc32, crc32c, and crc64nvme use the lowercase names from Smithy's httpChecksum trait. crc64-ecma is ECMA-182, a different algorithm from crc64nvme. The CRC implementations use crc-fast, which selects hardware acceleration when the CPU supports it.

A snapshot is a number, so whatever writes one to the wire picks its byte order.

A host may supply its own catalog through nupp.checksum.spi, selected during setup before this module loads. Its entries override the built-ins of the same name and may add others, and the ones it does not name stay available. The catalog is read once, at load, so no lookup or update resolves a service.

Submodules

ModuleDescription
nupp.checksum.spiImplementation interfaces for nupp.checksum algorithm catalogs.

Module contents

Types

TypeKindDescription
AlgorithmrecordChecksum metadata and a factory for independent state.
ChecksuminterfaceOwned incremental checksum state with non-consuming numeric reads.

Functions

FunctionKindDescription
algorithmfunctionRequires a checksum algorithm descriptor.
algorithmsfunctionLists available canonical checksum names in ascending order.
lookupfunctionFinds a checksum algorithm without creating incremental state.
newChecksumfunctionCreates independent owned checksum state.
valuefunctionComputes a checksum; its bit width belongs to the algorithm descriptor.

Types#

Algorithmrecord#

record Algorithm ...

Checksum metadata and a factory for independent state.

Members

NameKindDescription
namefieldCanonical algorithm name.
widthfieldNumber of meaningful checksum bits, from one through 64.
implementationfield
newChecksummethodCreates independent owned state for incremental writes and snapshots.

namefield#

name: string
@readonly

Canonical algorithm name.

widthfield#

width: integer
@readonly

Number of meaningful checksum bits, from one through 64.

implementationfield#

implementation: provider.Algorithm
@private

newChecksummethod#

newChecksum: function newChecksum(self): Checksum

Creates independent owned state for incremental writes and snapshots.

Arguments
NameTypeDescription
selfany
Returns
TypeDescription
Checksum

Checksuminterface#

interface Checksum is Writer ...

Owned incremental checksum state with non-consuming numeric reads.

Members

NameKindDescription
closemethodConsumes this context and releases its provider state without suspension.
algorithmmethodReturns the canonical algorithm name.
widthmethodReturns the number of meaningful bits in each snapshot.
writemethodAbsorbs raw bytes while retaining no input views.
writeSpanmethodAbsorbs a borrowed byte view without retaining it.
flushmethodNothing is buffered behind a checksum, so this only returns true.
valuemethodReturns an unsigned snapshot without consuming this context.

closemethod#

close: @nosuspend function(takes self: Checksum): nil

Consumes this context and releases its provider state without suspension.

Arguments
NameTypeDescription
takes selfChecksum
Returns
TypeDescription
nil

algorithmmethod#

algorithm: function(self: Checksum): string

Returns the canonical algorithm name.

Arguments
NameTypeDescription
selfChecksum
Returns
TypeDescription
string

widthmethod#

width: function(self: Checksum): integer

Returns the number of meaningful bits in each snapshot.

Arguments
NameTypeDescription
selfChecksum
Returns
TypeDescription
integer

writemethod#

write: function(exclusive self: Checksum, bytes: string): (boolean, string?)

Absorbs raw bytes while retaining no input views.

Arguments
NameTypeDescription
exclusive selfChecksum
bytesstring
Returns
TypeDescription
boolean

true, since a checksum takes every byte it is given

string?

writeSpanmethod#

writeSpan: function(exclusive self: Checksum, borrows bytes: span.ByteSpan): (integer?, string?)

Absorbs a borrowed byte view without retaining it.

Arguments
NameTypeDescription
exclusive selfChecksum
borrows bytesspan.ByteSpan
Returns
TypeDescription
integer?

how many bytes it absorbed, which is all of them

string?

flushmethod#

flush: function(self: Checksum): (boolean, string?)

Nothing is buffered behind a checksum, so this only returns true.

Arguments
NameTypeDescription
selfChecksum
Returns
TypeDescription
boolean
string?

valuemethod#

value: function(self: Checksum): uint64

Returns an unsigned snapshot without consuming this context.

Arguments
NameTypeDescription
selfChecksum
Returns
TypeDescription
uint64

Functions#

algorithmfunction#

function algorithm(name: string): Algorithm

Requires a checksum algorithm descriptor.

Arguments

NameTypeDescription
namestring

the canonical checksum algorithm name

Returns

TypeDescription
Algorithm

the selected descriptor

Raises

TypeCondition
string

when the algorithm is unavailable

algorithmsfunction#

function algorithms(): {string}

Lists available canonical checksum names in ascending order.

Returns

TypeDescription
{string}

the available names

lookupfunction#

function lookup(name: string): Algorithm?

Finds a checksum algorithm without creating incremental state.

Arguments

NameTypeDescription
namestring

the canonical checksum algorithm name

Returns

TypeDescription
Algorithm?

the descriptor, or nil when unavailable

newChecksumfunction#

function newChecksum(name: string): Checksum

Creates independent owned checksum state.

Arguments

NameTypeDescription
namestring

the canonical checksum algorithm name

Returns

TypeDescription
Checksum

an unfinished checksum context

valuefunction#

function value(name: string, bytes: string): uint64

Computes a checksum; its bit width belongs to the algorithm descriptor.

Arguments

NameTypeDescription
namestring

the canonical checksum algorithm name

bytesstring

the message bytes

Returns

TypeDescription
uint64

the unsigned checksum value