gonll/algex
GitHub: gonll/algex
基于GF(2⁸)上Berlekamp-Massey算法与LFSR编码的代数压缩器,专门处理统计压缩器因高熵而无法识别的线性递归结构数据。
Stars: 0 | Forks: 0
# pade-compress
**GF(2⁸) 线性递归的代数模型提取器 + 残差编码器。**
在 GF(2⁸) 上运行 Berlekamp-Massey / Padé 逼近,以提取生成字节流的最短 LFSR —— 然后仅存储模型和稀疏 XOR 残差。适用于统计压缩器(gzip、brotli、zstd)从根本上无法识别的数据。
## 这个工具能做到而其他工具做不到的一件事
统计压缩器利用的是符号频率和重复的字节模式。它们无法看到**代数结构**。
GF(2⁸) m 序列具有约 8 bits/byte 的 Shannon 熵 —— 根据任何信息论指标,它都与白噪声无法区分。gzip 会将其视为不可压缩并原样输出。该编解码器能将同样的数据压缩到 **1.2%** —— 达到 **85:1 的压缩比** —— 因为它在正确的领域中进行操作。
| 文件 | gzip | pade-compress | 压缩比 |
|------|------|---------------|-------|
| GF(2⁸) 几何序列 (L=1,完美) | ~100% | **~0.3%** | ~333:1 |
| 混合 LFSR (L=1/2/3 + 8% 噪声) 1 MB | ~99% | **1.1%** | **91:1** |
| 二进制可执行文件 (`/bin/ls`) | ~60% | **19.3%** | 5.1:1 |
| 自然语言文本 | ~35% | 100.1% | — |
| WebP 图像 | ~100% | ~100.5% | — |
适用范围是具有 **GF(2⁸) 线性递归结构** 的数据。在此范围之外,它会快速检测到不匹配并直接退回为透传(passthrough),开销可以忽略不计。
## 先分析 —— 如果合适再压缩
```
npm run analyze ./your-prbs-stream.bin
```
```
File: ./test/gf-structured.bin
Size: 1,048,576 bytes
Entropy: 7.888 bits/byte
Structured: 100.0% algebraic
Avg L: 1.75 (weighted LFSR order)
Verdict: 100% algebraically structured — compresses extremely well (LFSR/PRBS data)
────────────────────────────────────────────────────────────
Segments (11):
+ 0 [ 262,140 B] PRBS-8 m-sequence (maximal-length, perfect) noise 0.0% coeffs [0x03]
+ 262140 [ 262,144 B] L=2 LFSR (exact, period unknown) noise 0.0% coeffs [0x1b,0x4e]
+ 524284 [ 237,249 B] L=3 LFSR (~2.0% noise) noise 2.0% coeffs [0x57,0x2f,0x11]
+ 786433 [ 131,071 B] order 85 (period 85) noise 0.0% coeffs [0x07]
+ 917504 [ 131,072 B] PRBS-8 m-sequence (maximal-length, perfect) noise 0.0% coeffs [0xe3]
```
`--analyze` 会告诉你*存在什么样的代数结构*,即使你不打算压缩。该判定结果驱动路由决策:结构化数据 → 在此压缩;非结构化数据 → 回退使用 zstd/brotli。
## 工作原理
每个字节序列都可以被测试:在 GF(2⁸) 上是否存在短的递推关系
`s[i] = c₁·s[i-1] ⊕ c₂·s[i-2] ⊕ … ⊕ cₗ·s[i-L]`?
**Berlekamp-Massey 算法**能在 O(n²) 时间内找到最短的此类递推关系(最小 LFSR)。如果 LFSR 阶数 L 相对于序列长度 n 较小,则存储 `(L 个系数 + L 个种子字节 + 稀疏残差)` 会远小于原始字节。
### 编码管线
```
Input bytes
│
├─ Adaptive chunking (splits at entropy discontinuities; boundaries refined ±4 bytes)
│
└─ Per chunk — 15 encoding paths tried in order:
├─ 1. Padé [k/L] search (tries offsets 0..32, finds best k + shortest L)
├─ 2. Approx L=1 (brute-forces all 255 GF coefficients for noisy L=1 data)
├─ 3. Approx L=2 (voting over GF quadruples, covers up to ~28% byte noise)
├─ 4. Approx L=3 (voting over quintuple pairs, covers up to ~23% byte noise)
├─ 5. Approx L=4,5 (sub-sequence BM voting for higher-order LFSRs)
├─ 6. Affine L=1 (y[n] = c·y[n-1] ⊕ b via shift normalisation to pure L=1)
├─ 7. Cyclic (exact period detection for lookup tables and repeating patterns)
├─ 8-10. Delta transforms (XOR-diff, ADD-diff, XOR-2nd-diff → re-run paths 1-7)
│ Gated: only attempted when entropy is high AND algebraicity score is low.
├─ 11-13. Interleave m=2,3,4 (split byte lanes → encode independently → merge)
│ Gated: BM pre-screen on each lane to avoid spending time on random data.
├─ 14. Bit-plane decomposition (each of 8 bit planes encoded independently)
│ Gated: BM pre-screen per plane; useful for ADC/DAC samples and firmware images.
└─ 15. Raw passthrough (if no representation is smaller)
```
每条近似路径(2-5)在找到 LFSR 多项式后会应用**种子去噪(seed denoising)**:对每个种子字节遍历所有 256 个候选值,并选择能使总残差最小的值,从而在 O(L×256×N) 时间内消除系统性的初始窗口误差。
**双重前置门控(Dual pre-gate)** 控制转换路径 8-14 是否运行:
- 门控 1(熵):如果数据在统计上已经可以被压缩(H < 原始数据的 60%),则跳过 —— 例如文本、标头、已压缩的字节。
- 门控 2(代数性):如果数据过于随机(通过滑动 16 字节窗口的 BM 复杂度波动来衡量),则跳过。高波动 = 加密/噪声 → 转换无济于事。
只有同时具有高熵和代数结构的数据才会到达路径 8-14。这正是能从基于转换的编码中受益的那类数据。
### 传输格式 (v4)
```
File header: [4] magic "PAD4" [4] originalSize [4] chunkCount
Raw chunk: [1] kind=0 [4] dataLen [N] data [4] CRC32
LFSR chunk: [1] kind=1 [4] origLen [1] prefixLen [P] prefix
[2] lfsrLen L [L] coefficients [L] seed bytes
[1] residual flag: 0=plain sparse 1=deflate-raw 2=brotli
[payload] [4] CRC32
Cyclic chunk: [1] kind=2 [4] origLen [2] period P [P] cycle_bytes [4] CRC32
Delta chunk: [1] kind=3 [4] origLen [1] deltaId [4] innerLen [inner] [4] CRC32
Affine chunk: [1] kind=4 [4] origLen [1] k [4] innerLen [inner] [4] CRC32
Interleave: [1] kind=5 [4] origLen [1] m
m × { [4] laneLen [lane bytes] } [4] CRC32
Bitplane: [1] kind=6 [4] origLen [1] planeCount (always 8)
8 × { [4] planeLen [plane bytes] } [4] CRC32
EOF sentinel: [1] 0xFE
XDNI index: [4] "XDNI" [4] chunkCount [N×8] entries [4] indexOffset
Each entry: [4] chunkOffset [4] origLen
```
残差是预测字节与实际字节的 XOR。完美的递推会产生空残差。对于带有噪声的数据,残差被稀疏编码为经 delta 压缩的位置-值对,然后可选择进行 deflate/brotli 压缩 —— 哪个最小就采用哪个结果。
## 谁会处理这类数据
该编解码器面向处理使用移位寄存器逻辑数据源的工程师。对于这些有效载荷,它能实现任何通用工具都无法比拟的压缩效果 —— 因为**熵并不是衡量可压缩性的正确指标**:
- **硬件测试工程师** —— 用于 PCIe、USB、SONET 和以太网信号完整性测试的 PRBS(伪随机二进制序列)流是由 LFSR 生成的。`--analyze` 能瞬间识别出生成多项式和噪声级别。
- **嵌入式 / 固件团队** —— flash 中的 bootloader 映像、CRC 查找表、DSP 系数表。对于二进制可执行文件,已能实现 19% 的压缩率,而 gzip 约为 60%。
- **电信 / 网络** —— 光纤链路中的线路加扰器使用基于 LFSR 的 XOR 加扰;解扰后的有效载荷带有 GF 结构。
- **汽车 / 航空航天** —— FlexRay 和 MIL-STD-1553 使用 LFSR 成帧;来自移位寄存器硬件的遥测数据。
- **安全 / 密码分析** —— `--analyze` 可以检测“随机”流是否具有意外低的 LFSR 复杂度,这是弱 PRNG 的危险信号。
## 预构建可执行文件
无需 Node.js。从 [GitHub Releases](https://github.com/gonll/algex/releases) 页面下载适用于您平台的压缩包:
| 平台 | 压缩包 |
|---|---|
| Windows x64 | `pade-compress-windows-x64.zip` |
| Linux x64 | `pade-compress-linux-x64.tar.gz` |
| macOS ARM64 | `pade-compress-macos-arm64.tar.gz` |
每个压缩包包含**必须保持在同一目录下**的两个文件:
- `pade-compress`(Windows 上为 `pade-compress.exe`)—— 独立可执行文件
- `pade_compress_addon.node` —— 原生 GF/BM 库,在运行时作为附属组件加载
```
# Windows
.\pade-compress.exe analyze your-prbs-stream.bin
# Linux / macOS
./pade-compress analyze your-prbs-stream.bin
```
版本在每次推送 `v*` 标签时由 GitHub Actions 在所有三个平台上自动构建。
## 安装 (Node.js / npm)
```
npm install pade-compress
```
此软件包包含一个原生 C 插件(GF(2⁸) 算术和 Berlekamp-Massey 核心)。`npm install` 会自动编译它。您需要:
- **macOS**:Xcode Command Line Tools —— `xcode-select --install`
- **Linux**:`build-essential` —— `sudo apt install build-essential` (Debian/Ubuntu) 或等效软件包
- **Windows**:[windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) 或带有 C++ 工作负载的 Visual Studio
需要 Node.js ≥ 18。
## 使用方法
```
# 检测代数结构(不执行压缩)
npx tsx src/cli.ts analyze
# 压缩
npx tsx src/cli.ts compress
# 解压
npx tsx src/cli.ts decompress
标签:Berlekamp-Massey算法, DNS 反向解析, GF(2^8)有限域, LFSR, MITM代理, m序列, Padé逼近, PRBS伪随机二进制序列, 代数压缩, 信息熵, 信道编码, 固件压缩, 密码学, 异或残差编码, 手动系统调用, 数据压缩, 无统计学结构数据压缩, 流密码, 算法库, 纠错码, 线性反馈移位寄存器, 编码理论, 自动化攻击, 通信有效载荷