Expand description
Zeckendorf compression and decompression library
This library provides functionality for compressing and decompressing data using the Zeckendorf algorithm.
The Zeckendorf algorithm is a way to represent numbers as a sum of non-consecutive Fibonacci numbers. If we first interpret the input data as a big integer, we can then represent the integer as a sum of non-consecutive Fibonacci numbers. Sometimes this results in a more compact representation of the data, but it is not guaranteed. Learn more about the Zeckendorf Theorem in the Zeckendorf’s Theorem Wikipedia article.
§⚠️ 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.
This library is also available as a WebAssembly module for use in web browsers. Available functions are marked with the #[wasm_bindgen] attribute. The WebAssembly module can be built 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.
§Command-Line Tools
This library includes two command-line tools for compressing and decompressing data. They can be installed globally via:
cargo install zeck --features cli_tools(from crates.io)cargo install --git https://github.com/pRizz/zeckendorf --features cli_tools zeck(from GitHub)
After installation, zeck-compress and zeck-decompress will be available in your PATH.
The compression tool automatically uses .zbe extension for big-endian compression and .zle
extension for little-endian compression. The decompression tool automatically detects endianness
from these file extensions.
§zeck-compress
Compresses data using the Zeckendorf representation algorithm. Supports reading from files or stdin,
writing to files or stdout, and choosing between big-endian, little-endian, or automatic best compression.
Automatically adds the appropriate file extension (.zbe or .zle) based on the endianness used.
When using --endian best, if neither compression method produces a smaller output, the tool will
exit with an error showing compression statistics. When writing to a file, the output filename is
printed to stdout. Verbose statistics are shown by default and include descriptive compression ratio messages.
# Compress a file (output filename automatically created from input with extension)
zeck-compress input.bin
# Creates input.bin.zbe or input.bin.zle depending on which endianness was used
# Compress with best endianness (statistics shown by default)
zeck-compress input.bin --endian best
# Compress from stdin to stdout
cat input.bin | zeck-compress§zeck-decompress
Decompresses data that was compressed using the Zeckendorf representation algorithm. Supports reading
from files or stdin, writing to files or stdout. Automatically detects endianness from file extension
(.zbe for big-endian, .zle for little-endian), but allows manual override with the --endian flag.
# Decompress a file (endianness detected from .zbe extension, output filename automatically created)
zeck-decompress input.zbe
# Automatically uses big-endian decompression, creates output file "input"
# Decompress to a specific output file
zeck-decompress input.zbe -o output.bin
# Automatically uses big-endian decompression
# Override automatic detection
zeck-decompress input.zbe --endian little -o output.bin
# Decompress from stdin to stdout (--endian is required)
cat input.zbe | zeck-decompress --endian bigRe-exports§
pub use zeck_file_format::ZeckFile;pub use zeck_file_format::ZeckFormatError;pub use zeck_file_format::compress::compress_zeck_be;pub use zeck_file_format::compress::compress_zeck_best;pub use zeck_file_format::compress::compress_zeck_le;pub use zeck_file_format::decompress::decompress_zeck_file;pub use zeck_file_format::file::deserialize_zeck_file;
Modules§
- zeck_
file_ format - .zeck file format module
Enums§
- Padless
Compression Result - Result of attempting padless compression by interpreting the input data as both big endian and little endian big integers.
Constants§
- PHI
- Golden ratio constant.
This constant is in the rust standard library as
std::f64::consts::PHI, but only available on nightly. - PHI_
SQUARED - Phi squared constant. This also equals the golden ratio plus one.
- SKIP_
BIT - Bit flag indicating that an effective Fibonacci index (EFI) should be skipped in the Zeckendorf representation.
- USE_BIT
- Bit flag indicating that an effective Fibonacci index (EFI) should be used in the Zeckendorf representation.
Statics§
- FAST_
DOUBLING_ FIBONACCI_ BIGUINT_ CACHE - Sparse cache for fast doubling Fibonacci algorithm
Functions§
- all_
ones_ zeckendorf_ to_ biguint - Creates an “all ones Zeckendorf number”, or AOZN, by creating an Effective Zeckendorf Bits Ascending (EZBA)
with
nconsecutive ones, then converting it to aBigUint. - bit_
count_ for_ number - Returns the number of bits required to represent the given number. Returns 0 if the number is less than or equal to 0.
- efi_
to_ fi - Effective Fibonacci Index to Fibonacci Index: FI(efi) === efi + 2, where efi is the Effective Fibonacci Index
- efi_
to_ fi_ biguint - Effective Fibonacci Index to Fibonacci Index: FI(efi) === efi + 2, where efi is the Effective Fibonacci Index
- efi_
to_ fi_ ref - Effective Fibonacci Index to Fibonacci Index: FI(efi) === efi + 2, where efi is the Effective Fibonacci Index
- ezba_
from_ ezld - ezba is Effective Zeckendorf Bits Ascending ; ezld is Effective Zeckendorf List Descending
- ezba_
to_ ezla - Converts a vector of bits (0s and 1s) from an ezba (Effective Zeckendorf Bits Ascending) into a vector of effective Fibonacci indices, the Effective Zeckendorf List Ascending.
- ezl_
to_ zl - Converts an Effective Zeckendorf List to a Zeckendorf List. It does not matter if the list is ascending or descending; it retains the directionality of the original list.
- fast_
doubling_ fibonacci_ biguint - fibonacci(x) is equal to 0 if x is 0; 1 if x is 1; else return fibonacci(x - 1) + fibonacci(x - 2) fi stands for Fibonacci Index
- fi_
to_ efi - Fibonacci Index to Effective Fibonacci Index: EFI(fi) === fi - 2, where fi is the Fibonacci Index
- fi_
to_ efi_ biguint - Fibonacci Index to Effective Fibonacci Index: EFI(fi) === fi - 2, where fi is the Fibonacci Index
- fi_
to_ efi_ ref - Fibonacci Index to Effective Fibonacci Index: EFI(fi) === fi - 2, where fi is the Fibonacci Index
- highest_
one_ bit - Returns a
u64value with only the most significant set bit of n preserved. - memoized_
effective_ fibonacci - The memoized Fibonacci function taking an Effective Fibonacci Index as input.
- memoized_
fast_ doubling_ fibonacci_ biguint - fibonacci(x) is equal to 0 if x is 0; 1 if x is 1; else return fibonacci(x - 1) + fibonacci(x - 2) fi stands for Fibonacci Index
- memoized_
slow_ fibonacci_ biguint_ iterative - fibonacci(x) is equal to 0 if x is 0; 1 if x is 1; else return fibonacci(x - 1) + fibonacci(x - 2) fi stands for Fibonacci Index
- memoized_
slow_ fibonacci_ recursive - fibonacci(x) is equal to 0 if x is 0; 1 if x is 1; else return fibonacci(x - 1) + fibonacci(x - 2)
- memoized_
zeckendorf_ list_ descending_ for_ biguint - A descending Zeckendorf list is a sorted list of unique Fibonacci indices, in descending order, that sum to the given number.
- memoized_
zeckendorf_ list_ descending_ for_ integer - A descending Zeckendorf list is a sorted list of unique Fibonacci indices, in descending order, that sum to the given number.
- pack_
ezba_ bits_ to_ bytes - Packs a slice of bits (0s and 1s) from an ezba (Effective Zeckendorf Bits Ascending) into bytes.
- padless_
zeckendorf_ compress_ be_ dangerous - Compresses a slice of bytes using the Padless Zeckendorf Compression algorithm.
- padless_
zeckendorf_ compress_ best_ dangerous - Attempts to compress the input data using both big endian and little endian interpretations, and returns the best result.
- padless_
zeckendorf_ compress_ le_ dangerous - Compresses a slice of bytes using the Padless Zeckendorf Compression algorithm.
- padless_
zeckendorf_ decompress_ be_ dangerous - Decompresses a slice of bytes compressed using the Zeckendorf algorithm, assuming the original data was compressed using the big endian bytes interpretation.
- padless_
zeckendorf_ decompress_ le_ dangerous - Decompresses a slice of bytes compressed using the Zeckendorf algorithm, assuming the original data was compressed using the little endian bytes interpretation.
- slow_
fibonacci_ biguint_ iterative - fibonacci(x) is equal to 0 if x is 0; 1 if x is 1; else return fibonacci(x - 1) + fibonacci(x - 2) fi stands for Fibonacci Index
- unpack_
bytes_ to_ ezba_ bits - Unpacks a vector of bytes into a vector of bits (0s and 1s) from an ezba (Effective Zeckendorf Bits Ascending).
- zl_
to_ biguint - Converts a Zeckendorf List to a
BigUint. - zl_
to_ ezl - An Effective Zeckendorf List (EZL) has a lowest EFI of 0, which is an FI of 2. This is because it doesn’t make sense for the lists to contain FIs 0 or 1 because 0 can never be added to a number and will therefore never be in a Zeckendorf List and an FI of 1 is equivalent to an FI of 2 which has a Fibonacci value of 1 so let’s just use FI starting at 2, which is an EFI of 0. It does not matter if the list is ascending or descending; it retains the directionality of the original list.