| Crates.io | zeck |
| lib.rs | zeck |
| version | 3.0.1 |
| created_at | 2026-01-02 20:41:11.143362+00 |
| updated_at | 2026-01-10 13:58:31.459596+00 |
| description | A Rust library for compressing and decompressing data using the Zeckendorf representation algorithm |
| homepage | |
| repository | https://github.com/pRizz/zeckendorf.git |
| max_upload_size | |
| id | 2019171 |
| size | 334,795 |
A Rust library for compressing and decompressing data using the Zeckendorf representation algorithm.
The Zeckendorf algorithm represents numbers as a sum of non-consecutive Fibonacci numbers. This library interprets input data as a big integer (either big-endian or little-endian), converts it to its Zeckendorf representation, and sometimes achieves compression. However, compression is not guaranteed; the algorithm may result in a larger representation depending on the input data. The library can automatically try both endian interpretations and select the one that produces the best compression.
⚠️ Warning: Compressing or decompressing files larger than 10KB (10,000 bytes) is unstable due to time and memory pressure. The library may experience performance issues, excessive memory usage, or failures when processing files exceeding this size.
Command-line tools (zeck-compress and zeck-decompress) are available and can be installed via cargo install zeck. See the Binaries section for usage details.
.zeck file format that automatically preserves original file size and endianness informationnum-bigintThis library is also available as a WebAssembly module for use in web browsers and JavaScript/TypeScript projects. The WebAssembly module can be installed via npm:
npm install zeck
Available functions are marked with the #[wasm_bindgen] attribute. The WebAssembly module can also be built manually using the convenience script at scripts/build_wasm_bundle.sh that builds the WebAssembly module with the wasm-pack tool.
You can see a live demo of the WebAssembly module in action at https://prizz.github.io/zeckendorf-webapp/. The source code for the demo is available at https://github.com/pRizz/zeckendorf-webapp.
Run:
cargo add zeck
Or add this to your Cargo.toml:
[dependencies]
zeck = "2.1.0"
Features:
cli_tools: Enables the zeck-compress and zeck-decompress command-line binaries. This feature includes the clap dependency. Not enabled by default - use --features cli_tools when installing binaries.For CLI tools (when installing binaries):
[dependencies]
zeck = { version = "2.1.0", features = ["cli_tools"] }
Run:
cargo add zeck --git https://github.com/pRizz/zeckendorf
Or add this to your Cargo.toml:
[dependencies]
zeck = { git = "https://github.com/pRizz/zeckendorf" }
For CLI tools:
[dependencies]
zeck = { git = "https://github.com/pRizz/zeckendorf", features = ["cli_tools"] }
Run:
npm install zeck
Or add this to your package.json:
{
"dependencies": {
"zeck": "^2.1.0"
}
}
The .zeck file format automatically handles size preservation and endianness information. This is the recommended approach for most use cases.
use zeck::zeck_file_format::{compress::compress_zeck_be, decompress::decompress_zeck_file};
// Compress data (interpreted as big-endian integer)
let data = vec![12u8];
let zeck_file = compress_zeck_be(&data)?;
// Serialize to bytes for storage
let bytes = zeck_file.to_bytes();
// Later, deserialize and decompress
use zeck::zeck_file_format::file::deserialize_zeck_file;
let zeck_file = deserialize_zeck_file(&bytes)?;
let decompressed = decompress_zeck_file(&zeck_file)?;
assert_eq!(data, decompressed);
use zeck::zeck_file_format::{compress::compress_zeck_le, decompress::decompress_zeck_file};
// Compress data (interpreted as little-endian integer)
let data = vec![12u8];
let zeck_file = compress_zeck_le(&data)?;
// Decompress data
let decompressed = decompress_zeck_file(&zeck_file)?;
assert_eq!(data, decompressed);
use zeck::zeck_file_format::{
compress::{compress_zeck_best, BestCompressionResult},
decompress::decompress_zeck_file,
};
// Try both endian interpretations and get the best result
let data = vec![1, 0];
match compress_zeck_best(&data)? {
BestCompressionResult::BigEndianBest { zeck_file, le_size } => {
// Big-endian produced the best compression
let decompressed = decompress_zeck_file(&zeck_file)?;
assert_eq!(data, decompressed);
}
BestCompressionResult::LittleEndianBest { zeck_file, be_size } => {
// Little-endian produced the best compression
let decompressed = decompress_zeck_file(&zeck_file)?;
assert_eq!(data, decompressed);
}
BestCompressionResult::Neither { be_size, le_size } => {
// Neither method compressed the data (both were larger than original)
println!("Neither method compressed: BE size = {}, LE size = {}", be_size, le_size);
}
}
The padless compression functions strip leading zero bytes and do not preserve original size information. You must manually track the original size if you need to restore leading zeros. These functions are marked as _dangerous to indicate they require careful handling.
⚠️ Important: The padless functions are lower-level and do not preserve leading zero bytes. Use the file format functions above for most use cases.
use zeck::{padless_zeckendorf_compress_be_dangerous, padless_zeckendorf_decompress_be_dangerous};
// Compress data (interpreted as big-endian integer)
let data = vec![12u8];
let compressed = padless_zeckendorf_compress_be_dangerous(&data);
// Decompress data (leading zeros may be lost)
let decompressed = padless_zeckendorf_decompress_be_dangerous(&compressed);
// Note: decompressed may not equal data if data had leading zeros
use zeck::{padless_zeckendorf_compress_le_dangerous, padless_zeckendorf_decompress_le_dangerous};
// Compress data (interpreted as little-endian integer)
let data = vec![12u8];
let compressed = padless_zeckendorf_compress_le_dangerous(&data);
// Decompress data (trailing zeros may be lost)
let decompressed = padless_zeckendorf_decompress_le_dangerous(&compressed);
use zeck::{padless_zeckendorf_compress_best_dangerous, PadlessCompressionResult};
let data = vec![1, 0];
match padless_zeckendorf_compress_best_dangerous(&data) {
PadlessCompressionResult::BigEndianBest { compressed_data, le_size } => {
// Use padless_zeckendorf_decompress_be_dangerous for decompression
}
PadlessCompressionResult::LittleEndianBest { compressed_data, be_size } => {
// Use padless_zeckendorf_decompress_le_dangerous for decompression
}
PadlessCompressionResult::Neither { be_size, le_size } => {
// Neither method compressed the data
}
}
use zeck::memoized_slow_fibonacci_recursive;
// Calculate Fibonacci numbers (for indices up to 93)
let fib_10 = memoized_slow_fibonacci_recursive(10); // Returns 55
// For larger numbers, use BigInt versions
use zeck::fast_doubling_fibonacci_biguint;
let fib_100 = fast_doubling_fibonacci_biguint(100);
// For even better performance with caching, use memoized fast doubling
use zeck::memoized_fast_doubling_fibonacci_biguint;
let fib_1000 = memoized_fast_doubling_fibonacci_biguint(1000);
use zeck::memoized_zeckendorf_list_descending_for_integer;
// Get Zeckendorf representation as a list of Fibonacci indices
let zld = memoized_zeckendorf_list_descending_for_integer(12);
// Returns [6, 4, 2] meaning F(6) + F(4) + F(2) = 8 + 3 + 1 = 12
// For BigInt numbers
use zeck::memoized_zeckendorf_list_descending_for_biguint;
use num_bigint::BigUint;
let zld = memoized_zeckendorf_list_descending_for_biguint(&BigUint::from(12u64));
The library provides various utility functions for working with Fibonacci numbers and Zeckendorf representations:
use zeck::{
bit_count_for_number, // Count bits needed to represent a number
highest_one_bit, // Get the highest set bit
efi_to_fi, fi_to_efi, // Convert between Effective Fibonacci Index and Fibonacci Index
memoized_effective_fibonacci, // Get Fibonacci number from Effective Fibonacci Index
zl_to_ezl, ezl_to_zl, // Convert between Zeckendorf List and Effective Zeckendorf List
all_ones_zeckendorf_to_biguint, // Create "all ones" Zeckendorf numbers
PHI, PHI_SQUARED, // Golden ratio constants
};
The file format functions return Result types with comprehensive error handling:
use zeck::zeck_file_format::{compress::compress_zeck_be, error::ZeckFormatError};
match compress_zeck_be(&data) {
Ok(zeck_file) => {
// Compression succeeded
}
Err(ZeckFormatError::DataSizeTooLarge { size }) => {
// Data size exceeds u64::MAX
}
Err(e) => {
// Handle other errors
eprintln!("Compression error: {}", e);
}
}
Common error types include:
HeaderTooShort: Input data is too short to contain a valid headerUnsupportedVersion: File format version is not supportedReservedFlagsSet: Reserved flags are set (indicating a newer format)CompressionFailed: Compression did not reduce the data sizeDecompressedTooLarge: Decompressed data is larger than expectedDataSizeTooLarge: Data size exceeds the maximum representable sizeThe project includes several utility binaries. The command-line compression tools (zeck-compress and zeck-decompress) can be installed globally via:
cargo install zeck --features cli_tools
cargo install --git https://github.com/pRizz/zeckendorf --features cli_tools zeck
After installation, you can use zeck-compress and zeck-decompress directly from your command line.
⚠️ Warning: Compressing or decompressing files larger than 10KB (10,000 bytes) is unstable due to time and memory pressure. The library may experience performance issues, excessive memory usage, or failures when processing files exceeding this size.
Compresses data using the Zeckendorf representation algorithm. Automatically adds .zeck extension for compressed files.
zeck-compress [INPUT] [-o OUTPUT] [--endian ENDIAN] [-v]
Options:
INPUT: Input file path (optional, reads from stdin if not specified)
-o, --output FILE: Output file path (optional)
.zeck extension appended.zeck extension is automatically added unless the file already ends with .zeck--endian ENDIAN: Endianness to use (big, little, or best). Default: best
big: Use big-endian interpretationlittle: Use little-endian interpretationbest: Try both and use the best result (default)best, if neither method produces compression (both result in larger or equal output), the tool will exit with an error showing compression statistics-v, --verbose: Show compression statistics (default: true, use --no-verbose to disable)Examples:
# Compress a file (output filename automatically created from input with extension)
zeck-compress input.bin
# Creates input.bin.zeck
# Compress with best endianness (statistics shown by default)
zeck-compress input.bin --endian best
# Compress with specific endianness
zeck-compress input.bin --endian big
# Compress to a specific output file
zeck-compress input.bin -o output
# Creates output.zeck
# Compress from stdin to stdout
cat input.bin | zeck-compress
Note: When writing to a file, the output filename is printed to stdout (e.g., "Compressed to: input.bin.zeck"). Verbose statistics are shown by default and include descriptive messages about compression ratios (e.g., "File was compressed by X.XX% (Y bytes -> Z bytes)"). A warning is shown when reading from stdin if no data was piped in.
Decompresses data that was compressed using the Zeckendorf representation algorithm. Automatically detects endianness from the file header.
zeck-decompress [INPUT] [-o OUTPUT] [-v]
Options:
INPUT: Input file path (optional, reads from stdin if not specified)
-o, --output FILE: Output file path (optional)
.zeck extension removed-v, --verbose: Show decompression statistics (default: true, use --no-verbose to disable)Examples:
# Decompress a file (endianness detected from file header, output filename automatically created)
zeck-decompress input.zeck
# Automatically detects endianness from header, creates output file "input"
# Decompress to a specific output file
zeck-decompress input.zeck -o output.bin
# Automatically detects endianness from header
# Decompress from stdin to stdout
cat input.zeck | zeck-decompress
# Automatically detects endianness from header
Note: The endianness used for decompression must match the endianness used during compression. The file header stores which endianness was used, so decompression will automatically use the correct endianness when reading from a file or from stdin.
Additional features:
--no-verbose to disable) and include descriptive messages about compression/decompression ratios--endian best and neither method produces compression)cargo run --release --example playground
A playground/scratchpad for testing library functions.
cargo run --release --bin zeck-generate-data --features development_tools -- <size_in_bytes> [filename]
Generates random test data files in the generated_data/ directory.
Example:
cargo run --release --bin zeck-generate-data --features development_tools -- 1024 my_file.bin
cargo run --release --example generate-statistics
Generates comprehensive compression statistics and plots:
statistics_history/ directoryplots/ directorycargo run --release --example plot
Generates visualization plots of:
cargo bench --bench zeckendorf_bench
Benchmarks compression, decompression, and round-trip performance for various data sizes (4 bytes to 16KB).
cargo bench --bench fibonacci_bench
Compares performance of different Fibonacci calculation algorithms:
Save a new baseline:
cargo bench --bench zeckendorf_bench -- --save-baseline <name>
Compare to an existing baseline:
cargo bench --bench zeckendorf_bench -- --baseline <name>
Every positive integer can be uniquely represented as a sum of non-consecutive Fibonacci numbers. For example:
compress_zeck_best to try both)The library provides functions to compress with either interpretation, or you can use compress_zeck_best to automatically try both and select the one that produces the smallest output.
The .zeck file format includes a 10-byte header:
The header is followed by the compressed data. This format automatically preserves the original file size, allowing proper restoration of leading or trailing zero bytes during decompression.
The library uses "Effective Fibonacci Indices" (EFI) starting from 0, where:
This avoids redundant Fibonacci numbers (F(0)=0 and F(1)=F(2)=1).
*_dangerous) do not preserve leading/trailing zero bytes—use the file format functions for automatic size preservationFor some reason, NPM was showing there were versions of zeck published between 1.0.0 and 1.0.6 from 2024 (we are in 2026), even though I never published them to npm. I don't know how this happened. So I bumped the version to 1.0.7 and was able to successfully publish it to npm. Maybe there was an old package with the same name that was deleted, and NPM is still showing the old versions.
Here is a snippet of the time object from the npm registry JSON (https://registry.npmjs.org/zeck):
"time": {
"created": "2026-01-02T20:19:14.018Z",
"modified": "2026-01-03T17:25:15.940Z",
"1.0.0": "2024-02-21T14:36:36.292Z",
"1.0.1": "2024-02-21T15:26:38.621Z",
"1.0.2": "2024-02-21T15:36:30.258Z",
"1.0.3": "2024-02-21T15:48:07.853Z",
"1.0.4": "2024-02-21T15:48:38.804Z",
"1.0.5": "2024-02-21T16:02:36.339Z",
"1.0.6": "2024-02-21T16:36:36.643Z",
"0.1.0": "2026-01-02T20:19:14.175Z",
"0.2.0": "2026-01-03T17:25:15.702Z"
},
This project is licensed under the MIT License - see the LICENSE.txt file for details.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.