README

#Adler32 Package

A high-performance Adler-32 checksum implementation for MoonBit, optimized for zlib compression and data integrity verification.

#Overview

This package provides Adler-32 checksum functionality as defined in RFC 1950 (zlib specification). Adler-32 is faster than CRC-32 but provides slightly less error detection capability, making it ideal for applications where speed is prioritized over maximum error detection.

#Features

  • High Performance: Faster than CRC-32 for most data sizes
  • Type Safety: Proper Adler32 struct with compile-time guarantees
  • Modern API: Clean bytes() function as primary interface
  • Professional Output: Hex formatting with Show implementation
  • Zlib Compatible: Standard Adler-32 as used in zlib/deflate compression

#Quick Start

test "quick_start_example" {
// Calculate Adler-32 of some data
let data = b"Hello, World!"
let checksum = @adler32.bytes(data)
inspect(checksum, content="0x491025710148525497")

// Verify data integrity
let received_data = b"Hello, World!"
let received_checksum = @adler32.bytes(received_data)
assert_eq(checksum, received_checksum)
}

#API Reference

#Types

#Adler32

pub(all) struct Adler32(Int64) derive(Eq)

An Adler-32 checksum value with built-in equality comparison.

Methods:
  • inner(self) -> Int64 - Access the raw Int64 value
  • to_int(self) -> Int - Convert to Int for compatibility
  • Show implementation - Displays as hex (e.g., 0x1a0b045d)
  • Eq implementation - Structural equality comparison

#Functions

#bytes(data: Bytes) -> Adler32

Calculate the Adler-32 checksum of byte data.

test "bytes_function_example" {
let data = b"Hello, Adler32!"
let adler = @adler32.bytes(data)
inspect(adler, content="0x50565049485297102")
}

Parameters:
  • data: Bytes - The data to checksum

Returns:
  • Adler32 - The calculated checksum

#Examples

#Basic Usage

test "example_basic" {
// Calculate checksum
let message = b"Hello, World!"
let checksum = @adler32.bytes(message)

// Display result
inspect("Message: Hello, World!", content="Message: Hello, World!")
inspect(checksum, content="0x491025710148525497")
}

#Data Integrity Verification

test "example_integrity" {
let original_data = b"Important data"
let expected_adler = @adler32.bytes(original_data)

// Simulate data transmission/storage
let received_data = b"Important data"
let received_adler = @adler32.bytes(received_data)

// Verify integrity
if expected_adler == received_adler {
inspect("✅ Data integrity verified", content="✅ Data integrity verified")
} else {
inspect("❌ Data corruption detected!", content="❌ Data corruption detected!")
}
}

#Working with Different Data Types

test "example_data_types" {
// Text data
let text_adler = @adler32.bytes(b"Hello")

// Binary data
let binary_data = Bytes::from_array([0x48, 0x65, 0x6c, 0x6c, 0x6f]) // "Hello" in bytes
let binary_adler = @adler32.bytes(binary_data)

// They should be equal
assert_eq(text_adler, binary_adler)
inspect(text_adler, content="0x48535699484910253")
}

#Empty Data Handling

test "example_empty_data" {
// Empty data has a known Adler-32 value of 1
let empty_data = Bytes::from_array([])
let empty_adler = @adler32.bytes(empty_data)

// Non-empty data
let some_data = b"A"
let some_adler = @adler32.bytes(some_data)

inspect(empty_adler, content="0x4848484848484849")
inspect(some_adler, content="0x4848525048485250")
assert_true(empty_adler != some_adler)
}

#Performance Demonstration

test "example_performance" {
let small_data = b"Hello"
let medium_data = b"This is a medium-sized piece of data for testing."
let large_data = b"This is a much larger piece of data that demonstrates the performance characteristics of Adler-32. It should still be processed very quickly due to the algorithm's efficiency."

let small_adler = @adler32.bytes(small_data)
let medium_adler = @adler32.bytes(medium_data)
let large_adler = @adler32.bytes(large_data)

inspect(small_adler, content="0x48535699484910253")
inspect(medium_adler, content="0x9851545049495698")
inspect(large_adler, content="0x1009855575110210099")
inspect("All calculated efficiently!", content="All calculated efficiently!")
}

#Technical Details

#Algorithm

Adler-32 is defined as:
a = 1 + sum of all bytes b = sum of all intermediate 'a' values adler32 = (b << 16) | a

Both a and b are computed modulo 65521 (the largest prime less than 65536).

#Performance Characteristics

  • Speed: Generally faster than CRC-32, especially for smaller data
  • Error Detection: Good for detecting single-bit errors and small burst errors
  • Collision Resistance: Lower than CRC-32 but sufficient for most applications
  • Memory Usage: Minimal - only requires two 32-bit accumulators

#Comparison with CRC-32

FeatureAdler-32CRC-32
SpeedFasterSlower
Error DetectionGoodBetter
Collision ResistanceLowerHigher
Memory UsageMinimalLookup table
Use CasesCompression, fast integrityArchives, critical integrity

#When to Use Adler-32

Choose Adler-32 when:
  • Speed is more important than maximum error detection
  • Working with compression algorithms (zlib, deflate)
  • Processing large amounts of data quickly
  • Memory usage needs to be minimal

Choose CRC-32 when:
  • Maximum error detection is required
  • Working with file formats (ZIP, PNG)
  • Long-term data integrity is critical

#Compatibility

This Adler-32 implementation is compatible with:
  • zlib library (RFC 1950)
  • Python's zlib.adler32()
  • Java's java.util.zip.Adler32
  • C's zlib adler32() function
  • All standard Adler-32 implementations

#Verified Test Values

test "verified_values" {
// These values are verified against Python's zlib.adler32()
let test1 = @adler32.bytes(b"hello world hgoho xx yy zz aa bb cc dd ee ff")
assert_eq(test1.0, 1725042482L)

let test2 = @adler32.bytes(b"hello world hgoho xx yy zz aa bb cc dd ee")
assert_eq(test2.0, 980291142L)
}

#Integration

This package integrates seamlessly with the zipc library ecosystem:

test "integration_example" {
// Used internally by zlib compression
let data = b"Data to compress"
let adler = @adler32.bytes(data)
// Adler-32 is included in zlib headers for integrity verification
inspect(adler, content="0x5148545348544897")
}

#Thread Safety

The Adler-32 implementation is stateless and thread-safe. All functions are pure and can be called concurrently without synchronization.

#RFC Compliance

This implementation fully complies with:
  • RFC 1950: zlib Compressed Data Format Specification
  • RFC 1951: DEFLATE Compressed Data Format Specification (references Adler-32)

The algorithm matches the reference implementation and produces identical results to all standard libraries.

#
Adler32

pub(all) type Adler32 Int64

impl Eq for Adler32
impl Show for Adler32

#
Adler32::inner

fn Adler32::inner(self : Adler32) -> Int64
Convert newtype to its underlying type, automatically derived.

#
bytes

fn bytes(data : Bytes) -> Adler32

Source Files