crc32

    Portable IEEE CRC-32 with streaming, checksum combination, and SIMD processing

    crc32
    checksum
    simd
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    58 minutes ago
    Downloads
    2

    No dependencies

    #justjavac/crc32

    IEEE CRC-32 with streaming updates and checksum combination, licensed under MIT. See README.md for installation, the full API, and backend details.

    ///|
    test "hash and stream" {
    assert_eq(@crc32.hash(b"123456789"), 0xCBF43926U)
    assert_eq(@crc32.hash(b"123456789", simd=true), 0xCBF43926U)
    let state = @crc32.Hasher::new()
    state.update(b"1234")
    state.update(b"56789")
    assert_eq(state.finalize(), 0xCBF43926U)
    }

    ///|
    test "combine blocks" {
    let joined = @crc32.combine(@crc32.hash(b"hello"), @crc32.hash(b" world"), 6)
    assert_eq(joined, @crc32.hash(b"hello world"))
    }

    Hasher

    type Hasher

    Maintains a mutable, incremental CRC-32/ISO-HDLC checksum and byte count.

    This is the reflected IEEE CRC used by PNG, ZIP, and gzip, with polynomial 0x04C11DB7 (reflected 0xEDB88320) and initial/final XOR 0xFFFFFFFF. The finalized checksum of empty input is zero. The internal fields are private; use the constructors and methods to manage the state.

    MoonBit values of this type have reference semantics: assigning a hasher to another variable shares its mutable state. Use copy() to fork an independent computation. finalize() observes the current checksum and allows subsequent updates; it does not consume or reset the hasher.

    The represented length must fit in UInt64. Updates and combinations panic before changing the state if that length would overflow. Constructors accept simd=true to request experimental vector processing on native and wasm; other backends retain the scalar implementation. On native, the default uses vector loads for views of at least 64 bytes.

    test {
    let hasher = @crc32.Hasher::new()
    hasher.update(b"1234")
    hasher.update(b"56789")
    assert_eq(hasher.finalize(), 0xCBF43926U)
    assert_eq(hasher.total_len(), 9UL)
    }

    Hasher::combine

    fn Hasher::combine(self : Hasher, other : Hasher) -> Unit

    Appends a separately computed suffix state without reading its bytes.

    other must represent the subsequent byte sequence, computed from standard initialization with its complete, accurate length. This state's checksum and count become those of the concatenation; other is unchanged unless it is the same mutable object as self, in which case the sequence is doubled. A suffix with zero length is ignored, even if it has a nonzero checksum. Panics before mutation if the combined count would exceed 2^64 - 1 bytes.

    test {
    let prefix = @crc32.Hasher::new()
    prefix.update(b"hello")
    let suffix = @crc32.Hasher::new()
    suffix.update(b" world")
    prefix.combine(suffix)
    assert_eq(prefix.finalize(), @crc32.hash(b"hello world"))
    assert_eq(prefix.total_len(), 11UL)
    assert_eq(suffix.total_len(), 6UL)
    }

    Hasher::copy

    fn Hasher::copy(self : Hasher) -> Hasher

    Returns an independent mutable copy of the current checksum and byte count.

    Updating or resetting either hasher leaves the other unchanged. This is the explicit alternative to assignment, which aliases the same MoonBit object. Copying reads no input bytes and does not change this hasher. The copy retains the constructor's SIMD selection.

    test {
    let prefix = @crc32.Hasher::new()
    prefix.update(b"hello")
    let branch = prefix.copy()
    branch.update(b" world")
    assert_eq(prefix.finalize(), @crc32.hash(b"hello"))
    assert_eq(branch.finalize(), @crc32.hash(b"hello world"))
    }

    Hasher::finalize

    fn Hasher::finalize(self : Hasher) -> UInt

    Returns the current finalized IEEE CRC-32 checksum without changing the state.

    A fresh or reset state returns zero. Repeated calls return the same value until an update or combination occurs. Unlike Rust's consuming crc32fast::Hasher::finalize, this method leaves the MoonBit reference usable for further updates, copying, combination, or reset.

    Hasher::new

    fn Hasher::new(simd? : Bool) -> Hasher

    Creates a fresh incremental computation with checksum zero and length zero.

    The state uses the standard IEEE initialization. Construction allocates only the hasher itself; reusable algorithm tables are initialized once. Updating the returned state and calling finalize() computes the same result as hash() over the concatenated updates. Each update applies the same vector thresholds as hash().

    Hasher::new_with_initial

    fn Hasher::new_with_initial(initial : UInt, simd? : Bool) -> Hasher

    Resumes computation from a finalized checksum with an unknown prefix length.

    initial uses the same convention as hash() and update(). The length counter starts at zero because the prefix length is unknown; subsequent updates still compute the correct continued checksum.

    Use new_with_initial_len() when this state will be supplied as the suffix to combine(), which requires its complete represented byte count. simd selects the same implementation policy as new().

    Hasher::new_with_initial_len

    fn Hasher::new_with_initial_len(initial : UInt, amount : UInt64, simd? : Bool) -> Hasher

    Resumes computation from a finalized checksum and its represented byte count.

    initial is the existing finalized checksum and amount is the number of bytes it represents. The caller must supply an accurate count for later combination. No bytes are read and the relationship between the count and checksum cannot be validated. Zero amount is valid, including for a custom initial checksum. Subsequent updates add to this count without wrapping. simd selects the same implementation policy as new(). The supplied count is accepted in its full UInt64 range.

    test {
    let state = @crc32.Hasher::new_with_initial_len(@crc32.hash(b"hello"), 5)
    state.update(b" world")
    assert_eq(state.finalize(), @crc32.hash(b"hello world"))
    assert_eq(state.total_len(), 11UL)
    }

    Hasher::reset

    fn Hasher::reset(self : Hasher) -> Unit

    Restores the standard empty state, setting both checksum and length to zero.

    Discards a supplied initial checksum and supplied initial length as well as all subsequent updates. The same mutable hasher can then be reused for a separate byte sequence; references sharing this state observe the reset. The implementation policy selected by the constructor is retained.

    Hasher::total_len

    fn Hasher::total_len(self : Hasher) -> UInt64

    Returns the number of bytes represented by this hasher without changing it.

    Includes the constructor's supplied length, all updates, and the lengths of combined suffix states. new_with_initial() starts at zero because its prefix length is unknown. reset() sets the count back to zero.

    Hasher::update

    fn Hasher::update(self : Hasher, data : BytesView) -> Unit

    Appends data to this hasher's represented byte sequence.

    Updates the checksum and adds data.length() to the byte count. Empty input is a no-op. Reads the input view without retaining, copying, or changing it. Different chunk boundaries produce the same final checksum. Panics before mutation if the total length would exceed 2^64 - 1 bytes.

    combine

    fn combine(first : UInt, second : UInt, second_len : UInt64) -> UInt

    Combines the finalized IEEE checksums of two consecutive byte sequences.

    first is the finalized prefix checksum, including a checksum continued from a custom initial state. second must be the suffix checksum computed with the standard zero initial checksum. second_len must be the exact suffix length in bytes. The result equals the continued checksum of their concatenation, without needing either byte sequence.

    A zero second_len returns first, regardless of second. Every UInt64 length is supported, with logarithmic work and no allocation per call. Passing a checksum or length that does not describe the suffix produces a checksum for that supplied state; the function cannot validate the bytes.

    test {
    let joined = @crc32.combine(@crc32.hash(b"hello"), @crc32.hash(b" world"), 6)
    assert_eq(joined, @crc32.hash(b"hello world"))
    }

    hash

    fn hash(data : BytesView, simd? : Bool) -> UInt

    Computes the standard IEEE CRC-32 checksum of data in one call.

    The result is a finalized 32-bit unsigned checksum, with zero for empty input. data is a read-only byte view; passing Bytes or a subview avoids copying the input. The input is interpreted as bytes, without text encoding or host-endian conversion. Backend selection does not change the result. simd=false selects scalar processing. The native default uses vector loads for input of at least 64 bytes. On Wasm simd=true uses vectors from 16 bytes onward. Wasm GC and JS always use scalar code.

    test {
    assert_eq(@crc32.hash(b"123456789"), 0xCBF43926U)
    assert_eq(@crc32.hash(b""), 0U)
    }

    update

    fn update(initial : UInt, data : BytesView, simd? : Bool) -> UInt

    Extends a finalized IEEE CRC-32 checksum with additional bytes.

    initial is a previous finalized checksum, not the inverted internal CRC register. Pass zero to start a standard computation. An empty data leaves initial unchanged; otherwise the result represents the preceding bytes followed by data. The input view is read without copying or mutation.

    Unlike a Hasher, this function tracks no byte count. Use it when a checksum alone is enough, or use Hasher for later checksum combination. simd selects the same backend policy as hash().

    test {
    let prefix = @crc32.hash(b"1234")
    assert_eq(@crc32.update(prefix, b"56789"), 0xCBF43926U)
    }