Amarula.Protocol.AppState.LTHash (amarula v0.1.0)

View Source

LT Hash — the summation hash that keeps app-state integrity across mutations. Ported from Baileys' pre-WASM pure-JS impl (src/Utils/lt-hash.ts before the rust bridge). Adding/removing a mutation gives the same hash as if the whole sequence had been applied in order, so patches can be verified incrementally.

The hash is 128 bytes = 64 little-endian unsigned 16-bit words. Each mutation mac is expanded with HKDF(mac, 128, salt="", info="WhatsApp Patch Integrity") into the same 64-word space and added/subtracted pointwise mod 2^16 (wraparound).

subtract_then_add/3 is the operation app-state uses: drop the macs of removed records, add the macs of new ones.

Summary

Functions

Add each mac in macs to hash (pointwise, with wraparound).

Subtract each mac in macs from hash.

Subtract the subtract macs, then add the add macs.

A fresh, all-zero 128-byte hash.

Functions

add(hash, macs)

@spec add(binary(), [binary()]) :: binary()

Add each mac in macs to hash (pointwise, with wraparound).

subtract(hash, macs)

@spec subtract(binary(), [binary()]) :: binary()

Subtract each mac in macs from hash.

subtract_then_add(hash, subtract, add)

@spec subtract_then_add(binary(), [binary()], [binary()]) :: binary()

Subtract the subtract macs, then add the add macs.

zero()

@spec zero() :: binary()

A fresh, all-zero 128-byte hash.