Commit graph

2 commits

Author SHA1 Message Date
DeFiDude
166083aba7 Add bz2 compression for Resource transfer + 3 compression tests
Vendor libbzip2 (1.0.8, BSD license) for ESP32-compatible bz2
compression/decompression. This matches Python's bz2.compress()
and Rust's bzip2 crate for cross-platform Resource transfers.

C++ wrapper (Compression/BZ2.h):
- bz2_compress() — compress with blockSize100k=9
- bz2_decompress() — decompress with progressive buffer sizing
- try_compress() — compress only if smaller than original

Resource integration:
- OutboundResource::init() now compresses with bz2 if beneficial
- InboundResource::assemble() decompresses if compressed flag set
- ResourceFlags.compressed bit tracks compression state

3 new tests (test_resource_transfer):
- testBz2CompressDecompress — roundtrip verification
- testTryCompress — beneficial vs non-beneficial compression
- testBz2CrossPlatformFormat — BZh magic bytes match Python/Rust

Total: 137 tests, 136 pass (1 pre-existing allocator).
2026-03-24 17:15:42 -06:00
DeFiDude
798512e8f7 Implement Resource transfer: advertisement, map hash, chunking, assembly
Complete rewrite of Resource.h/cpp with full protocol implementation:

ResourceAdvertisement: msgpack map pack/unpack with 11 string keys
  (t,d,n,h,r,o,i,l,q,f,m) matching Python/Rust wire format.

OutboundResource (sender): compress → encrypt → chunk → hashmap → advertise.
  Splits encrypted blob into SDU-sized parts (263 bytes), computes
  SHA256(chunk||random_hash)[:4] map hashes, handles receiver requests.

InboundResource (receiver): accept advertisement → receive parts by map hash
  → assemble → decrypt → decompress. Sends initial request, generates proof.

Helper functions: get_map_hash(), compute_resource_hash(),
  compute_expected_proof() — all match Python/Rust implementations.

8 new tests (test_resource_transfer): advertisement roundtrip, map hash,
  resource hash, proof, flag encoding, nil/non-nil request_id.

Legacy Resource class preserved for Link.cpp compatibility.
2026-03-24 16:48:55 -06:00