Semantic Encryption
How a language model can compress a message and turn encrypted bytes back into readable text.
What if I could send you an encrypted message that looked like the opening line of a story, and tell you that it contained a secret message? You could read the sentence, but its meaning wouldn’t tell you what I’d sent, there’s no hidden spacing or capital letter coding either, The information would be in which words appeared in what order, not in what they said.
That was the idea behind Semantic Encryption. The name is somewhat misleading: the language model doesn’t provide the encryption, and it doesn’t preserve the meaning of the original in the new sentence. It actually gives us something more useful here: a reversible way to move between text and bits… but how?
Turning a Predictor into a Compressor
Ask a language model to finish “Peanut butter and…” and it’ll probably give jelly a better chance than taxes. Under the hood, it’s not making one random guess. It’s actually assigning probabilities to every single possible next token! A token might be a whole word, a piece of one, or even just punctuation, the tokenization of text is not important as far as we’re concerned here.
The other day Youtube decided to bless me with this video from 3Blue1Brown on arithmetic coding, it teaches an interesting trick: instead of picking a token at random, use those probabilities to encode the token you already know is there, in a way, we’re making a deterministic sampling algorithm for LLMs.
Start with the interval [0, 1). Split it into many slices per possible next token, in a fixed order, with widths equal to their probabilities (since the sum of all probable tokens add up to 1). Keep the slice for the actual first token. Feed that token back into the model, split the remaining slice using the new probabilities, and keep the slice for the second token. Repeat. A predictable token takes up most of the remaining interval, so it costs little extra precision. An unlikely token gets a tiny slice, so it costs more bits to represent, hence we achieve higher compression for likely sentences, and lower compression, for unlikely ones.
but How can the reader tell coffee from the beginning of coffee please? when should we stop sampling more tokens? we solve this by giving end of message its own slice. Suppose coffee occupies [1/2, 3/4) and, within that slice, the end symbol occupies [5/8, 3/4). The fraction 11/16 lands first in coffee, then in end. The reader returns just coffee. Adding please would mean taking a different branch after coffee.
The compressor reserves one count in each frequency table for this end symbol. It isn’t a word, punctuation, or even the model’s end-of-sequence token: it’s an extra symbol belonging only to the arithmetic coder. The sender writes it after the last plaintext token, even for an empty message, then packs the arithmetic code into bytes. The receiver can stop at that symbol without knowing the token count.
The [0, 1) interval is a useful mental picture, but in reality we can’t use Python floats for this, since python float may not have enough precision bits to represent our full message. An actual arithmetic coder uses an ever growing large integer, and re-scales the interval as it goes, python integers can be infinitely long, so we never run out of precision bits. Both sides need to reproduce the same tables at every step, down to the rounding and token order. If they disagree about where a slice begins (or where it ends), they can recover different tokens from the same bits.
The animation uses small probability tables so you can follow the numbers by eye. Watch for two endings: one tells the reader when the plaintext is complete; the other tells it where the sealed bytes end. Neither marker appears as a word in the story.
Two messages, one lighthouse
I encrypted Bring the map. and Hide the key. with the same demo key and LFM3-1.7B-Base on CUDA (the CUDA part is important, because even changing something as trivial as the implementation of the LLM runtime algorithm, can change the order of operations, and introduce errors). Then, to get a story rather than whatever the model might write from a blank page, I gave both runs the same opening paragraph as context:
1
2
3
4
5
context = (
"Every winter the lighthouse keeper told us the same story. "
"It always began on the night the lantern went out.\n\n"
)
key = bytes(range(32)) # Public demo key, not for private messages.
Cover for Bring the map.
It was the night of last September.
Then he began telling us a long story about an old girl named Amara who lost her eyesight years ago. She was a woman of mystery, said to have lived in the lighthouse for decades, tipped over into the ocean with no one coming close enough.
For days before
Cover for Hide the key.
The winter before that had also been dark. When we went to visit the keeeper he would go to his bed and, after getting up, tell us that he had lost his lanterns. He would then begin his story.
“Years and years ago,”
He would say, “there came this fierce storm. A great flood came into Bay Harbor, and I
As you can see, Neither is a paraphrase of the secret, and they look completely natural, A cover message ends as soon as it can carry the complete information, even if the story is not finished, there could be a version that actually generates complete stories, but this is not what I’ve done here.
The context paragraph isn’t part of either cover. Think of it as the preceding page of a book both readers already have: it changes the LLM’s probabilities of next words, without changing the plaintext being compressed. The receiver must supply the exact same key and context separately (including those two final newlines!) to reconstruct the original message.
What goes into the cover?
The encryption code derives a key from the shared 32-byte secret using HKDF-SHA256, then seals the compressed bytes with AES-SIV. The domain label and exact context are authenticated alongside the message. The renderer gets the sealed output, but it also needs a way to tell the receiver where that output ends.
To solve this, we introduce a byte-level rule: copy each nonzero byte as it is, then append 00 00, if the original sealed message has a 00 byte, write it as 00 01, so we don’t mistake it with the end marker, For example, imagine the sealed output ends like this:
1
2
3
Sealed tail: … 73 00 A4
Framed tail: … 73 00 01 A4 00 00
data end
When reading, replace every 00 01 with 00 and keep going. whenever we encounter 00 00, the ciphertext is complete. All of this happens in the bytes the words represent; we don’t need to add a discrete footer to the cover messages!
The first cover choices therefore depend on sealed data, a shared context can naturally produce some of the same words. AES-SIV is also deterministic, meaning with the same message, key, context and model environment, the entire cover message repeats!
Beyond the Code: The Lighthouse Keeper Has No Map
So where in that lighthouse story did I hide the map? I didn’t give it to Amara, and “last September” isn’t a date you need to look up. In fact, the model writing the story never gets to read Bring the map. By the time it gets a say in the matter, those words have already been compressed and encrypted.
What it does get to do is suggest how the story might continue. Remember the slices from the animation? The encrypted bits mark out a patch of the number line. Of the plausible next tokens that the receiver can reproduce, the renderer picks the one whose slice overlaps the most with that patch. Each word leaves fewer possibilities open for the next one. Read the same words with the same model and context, and you can retrace those choices until the bytes are pinned down.
My favorite line in the whole experiment is For days before. As the ending of a story, it’s terrible. For days before what? But as a way to send Bring the map., it’s a perfectly good place to stop. The reader who wants to know what happened to Amara or decipher the message is missing half a sentence, and is left with a cliffhanger! The reader with the key isn’t missing a thing.
If you remember Pixi, my chatbot with a thing for pancakes, you can probably guess which page of the cookbook I turned to next. Could I send Bring the map. as a pancake recipe? I kept the lighthouse example’s demo key and changed just the context:
1
2
3
4
5
context = (
"My grandmother never measured the flour. "
"This is how she taught me to make buttermilk pancakes on a Sunday morning."
"\n\nMethod:\n1."
)
The 1. is mine. Everything after it, starting with the space before Take, is the generated cover:
1
2
3
4
5
Take the flour and put it into the bowl. Add the eggs and mix it up until well combined.
- I want to make sure that the mixture is well mixed, not too dry and not so loose that the buttermilk will make the pancake too dense. If the mixing is still loose after two or three minutes,
I add some of the flour.
2. Stir
I fed those lines back into the notebook’s decryptor with the same key and recipe context. Out came Bring the map., period and all. I’d owe Pixi the rest of the recipe: you can’t make much of a pancake if the instructions stop at 2. Stir. But if you’re looking for the map, breakfast is ready.