package crypto import ( "git.mleku.dev/mleku/dendrite/pkg/ratio" "git.mleku.dev/mleku/dendrite/pkg/state" ) // ShadowElement wraps an encrypted hexagram token as a lattice element. // Each element carries phase-derived permutation and projection data // that entangles the message with the binary/decimal shadow channel. type ShadowElement struct { Index int // position in the token stream Encrypted state.Hexagram // phase-rotated hexagram (6-bit) ProjVertex uint8 // derived from phase denominator mod 8 ProjKey uint8 // derived from |phase numerator| mod 8 ProjPath uint16 // index in token stream PermIdx uint8 // S_3 index derived from phase TypeTag string // constraint layer tag } func (e ShadowElement) Type() string { return e.TypeTag } func (e ShadowElement) Value() any { return e.Encrypted } func (e ShadowElement) Permutation() uint8 { return e.PermIdx } func (e ShadowElement) ProjectionVertex() uint8 { return e.ProjVertex } func (e ShadowElement) ProjectionKey() uint8 { return e.ProjKey } func (e ShadowElement) ProjectionPath() uint16 { return e.ProjPath } // ShadowCiphertext is a message encrypted via the binary/decimal shadow // channel. The plaintext is hexagram-encoded, phase-permuted, and // crystallized into a lattice. type ShadowCiphertext struct { Sites []SiteMark // bonding pattern after grow + dissolve Noise []NoiseSample // dissolution events (error term) Params Params // crypto parameters used Basis *Basis // public key basis Nonce Hamadryad // unique per message EpochDec int // epoch decimal exponent EpochBin int // epoch binary exponent TokenCount int // number of hexagram tokens OrigLen int // original plaintext byte count } // ShadowCompressed is the output of shadow compression: Huffman-coded // hexagram stream with auxiliary data encoded in the permutation // pair shadow channel. type ShadowCompressed struct { // Primary is the Huffman-coded hexagram bitstream. Primary []byte // Shadow is the packed permutation pair indices (6 bits each). // Each index selects one of 36 S_3×S_3 pairs; deviation from // the phase-determined "home" pair encodes auxiliary bits. Shadow []byte // FreqTable holds hexagram symbol frequencies for Huffman tree // reconstruction. 64 entries (one per hexagram value). FreqTable [64]uint32 // Epoch parameters for phase schedule reconstruction. EpochDec int EpochBin int // OrigLen is the original data length in bytes. OrigLen int // TokenCount is the number of hexagram tokens. TokenCount int // ShadowPayloadLen is the length of auxiliary data embedded // in the shadow channel. ShadowPayloadLen int // ContentHash is the Hamadryad hash of the original data // for integrity verification. ContentHash Hamadryad } // CompressRatio returns the compression ratio as an exact rational // (compressed size / original size). func (sc *ShadowCompressed) CompressRatio() ratio.Ratio { if sc.OrigLen == 0 { return ratio.Zero } compressed := int64(len(sc.Primary) + len(sc.Shadow) + shadowCompressedHeaderSize) return ratio.New(compressed, int64(sc.OrigLen)) } // shadowCompressedHeaderSize is the fixed overhead of the wire format. // Magic(4) + OrigLen(4) + TokenCount(4) + EpochDec(2) + EpochBin(2) // + PrimaryLen(4) + ShadowLen(4) + ShadowPayloadLen(4) // + FreqTable(256) + ContentHash(56) = 340 bytes. const shadowCompressedHeaderSize = 340