In 2014 — after receiving dozens of unsolicited emails from the International Journal of Advanced Computer Technology1 (IJACT) — scientists David Mazières and Eddie Kohler submitted a paper titled "Get me off Your Fucking Mailing List2".

Get me off your fucking mailing list.
Image from the paper David Mazières and Eddie Kohler submitted to the International Journal of Advanced Computer Technology in 2014. Much to their surprise, the manuscript was accepted for publication.

They pretty much fell off their chair3 when the news came in the manuscript had been accepted for publication. However, that acceptance bolstered the authors' contention that IJACT is a predatory journal, an indiscriminate but superficially scholarly publication that subsists on editorial fees. Mazières said:

They told us to add some more recent references and do a bit of reformatting. But otherwise they said its suitability for the journal was excellent.

The authors didn't pursue it and so far also haven't received any confirmation yet that the journal took them off their mailing list.

Assignment

An essential step in the bitflip cipher involves converting messages consisting of a sequence of symbols (letters, numbers, punctuation marks and spaces) into bitstrings consisting only of a sequence of bits: zero (0) or one (1). To do so, the cipher uses a key that indicates what bitstring corresponds to each symbol. The key is given as a text file whose lines consists of a symbol followed by a tab and the corresponding bitstring. For example, with this key (key.01.txt4)

R	010
C	1100
A	10
B	00
D	1101

the message ABRACADABRA can be converted into the bitstring

A B R  A C   A D   A B R  A
1000010101100101101100001010

To convert this long bitstring back into the original message, we seem to run into the problem that the bitstrings for the symbols have different lengths. As a result, it is unclear how to divide the longer bitstring into shorter bitstrings that correspond to symbols. However, the key is composed in such a way that no bitstring for a symbol is a prefix of a bitstring for any of the other symbols.

A string $$s$$ is a prefix of another string $$t$$ if string $$t$$ begins with string $$s$$.

As a consequence, the long bitstring has exactly one prefix that is a bitstring for a symbol. In the example, it is the prefix 10 that corresponds to symbol A. If the prefix is then removed from the long string, the process can be repeated with the remaining bitstring until it has been fully processed. This is how we can convert the long bitstring step by step to the symbols of the message ABRACADABRA:

long bitstring prefix symbol
1000010101100101101100001010 10 A
00010101100101101100001010 00 B
010101100101101100001010 010 R
101100101101100001010 10 A
1100101101100001010 1100 C
101101100001010 10 A
1101100001010 1101 D
100001010 10 A
0001010 00 B
01010 010 R
10 10 A

The bitflip cipher itself was named after an operation on bitstrings: a bitflip converts a bitstring into a new bitstring by replacing all zeros (0) with ones (1), and all ones (1) with zeros (0). For example, a bitflip converts the bitstring 010110 into the bitstring 101001.

The bitflip cipher only works with symmetric keys: keys where for each bitstring corresponding to a symbol, its bitflip also corresponds to a symbol. However, the key we used as an example above is asymmetric: bitstring 10 corresponds to symbol A, but the bitflip of that bitstring 01 does not correspond to any symbol. We will therefore use this symmetric key (key.02.txt5)

a	011011111
T	0110111101
s	100100000
g	10011
n	01100
e	110
 	010
f	001
o	1000
m	111
y	0110110
k	10010001
c	01101110
r	1001001
l	011010
.	1001000010
G	100101
t	000
i	101
u	0111

to explain how the message

Get me off your fucking mailing list.

is encoded with the bitflip cipher. First, we use the key to convert the message into its corresponding bitstring:

1001011100000101111100101000001001010011011010000111100100101000101110110111010010001101011001001101011101101111110101101010101100100110100110101011001000000001001000010

We then perform a bitflip on the obtained bitstring:

0110100011111010000011010111110110101100100101111000011011010111010001001000101101110010100110110010100010010000001010010101010011011001011001010100110111111110110111101

Finally, we use the key to convert the bitstring back into a message of symbols:

lfmitfiueeiruoyieokc gnits G gniG amT

This is the step that will not necessarily succeed with asymmetric keys, but will always succeed with symmetric keys. The message obtained in this way is the ciphertext for the original message. Decoding a ciphertext back into the original message is very simple: decoding works exactly the same as encoding.

Your task:

Example

In this interactive session we assume the text files key.01.txt6 and key.02.txt7 are located in the current directory. These are the two keys we also used as examples in the description of this assignment.

>>> key = read_key('key.01.txt8')
>>> key['A']
'10'
>>> key['R']
'010'
>>> key['C']
'1100'
>>> symbols2bits('ABRACADABRA', key)
'1000010101100101101100001010'
>>> bits2symbols('1000010101100101101100001010', key)
'ABRACADABRA'
>>> flip('1000010101100101101100001010')
'0111101010011010010011110101'
>>> issymmetric(key)
False
>>> encode('ABRACADABRA', key)
Traceback (most recent call last):
AssertionError: asymmetric key

>>> key = read_key('key.02.txt9')
>>> key['G']
'100101'
>>> key['e']
'110'
>>> key[' ']
'010'
>>> key['.']
'1001000010'
>>> symbols2bits('Get me off your fucking mailing list.', key)
'1001011100000101111100101000001001010011011010000111100100101000101110110111010010001101011001001101011101101111110101101010101100100110100110101011001000000001001000010'
>>> bits2symbols('1001011100000101111100101000001001010011011010000111100100101000101110110111010010001101011001001101011101101111110101101010101100100110100110101011001000000001001000010', key)
'Get me off your fucking mailing list.'
>>> flip('1001011100000101111100101000001001010011011010000111100100101000101110110111010010001101011001001101011101101111110101101010101100100110100110101011001000000001001000010')
'0110100011111010000011010111110110101100100101111000011011010111010001001000101101110010100110110010100010010000001010010101010011011001011001010100110111111110110111101'
>>> issymmetric(key)
True
>>> encode('Get me off your fucking mailing list.', key)
'lfmitfiueeiruoyieokc gnits G gniG amT'
>>> decode('lfmitfiueeiruoyieokc gnits G gniG amT', key)
'Get me off your fucking mailing list.'

Epilogue

In 2011, M.V. Berry et al. published "Can apparent superluminal neutrino speeds be explained as a quantum weak measurement?10" in Journal of Physics A: Mathematical and Theoretical11. Here's the abstract:

Probably not.

In 1978, John C. Doyle published "Guaranteed margins for LQG regulators12" in IEEE Transactions on Automatic Control13. The abstract reads as:

There are none.

In 1962, botanist R. Moran published a note in the journal Madroño14 recounting his collection of a bush rue on a mountaintop in Baja California. The note's title was "Cneoridium dumosum (Nuttall) Hooker f. Collected March 26, 1960, at an Elevation of About 1450 Meters on Cerro Quemazón, 15 Miles South of Bahía de Los Angeles, Baja California, México, Apparently for a Southeastward Range Extension of Some 140 Miles15". Here's the full text of the note:

I got it there and then.

This is followed by a 28-line acknowledgment section in which Moran thanks the person who has reviewed the text, his college professors, and the person who has mailed the manuscript to the journal.

It is certainly also worth reading through the full text of "The Unsuccessful Self-Treatment of a Case of 'Writer's Block'16", an article published by D. Upper in 1974 in Journal of Applied Behavior Analysis17.

Epilogue

In 1962, a burnt golf ball arrived at the botanic gardens at Kew18, in southwest London. The head of mycology, R.W.G. Dennis, may have rolled his eyes: The office had received another burnt golf ball 10 years earlier, which the submitter had claimed to be a "rare fungal species." In that case the staff had got as far as trying to collect spores before they'd realized the hoax.

Twice provoked, Dennis responded in good humor. He published an article titled "A Remarkable New Genus of Phalloid in Lancashire and East Africa19", formally nominating it as a new species of fungus, Golfballia ambusta20, and describing the specimens as

small, hard but elastic balls used in certain tribal rites of the Caledonians, which take place all season in enclosed paddocks with partially mown grass

When a third burnt golf ball arrived in 1971, it was accepted into the collection, where all three balls now reside.

That creates a sort of Dadaist dilemma in mycology. By accepting the specimens and publishing a description, Dennis had arguably honored them as a genuine species. The precise definition of a fungus has varied somewhat over time; in publishing his article, Dennis may have been satirically questioning criteria that could accept a nonliving golf ball as a species. But what's the solution? Some specialists have argued that fungi should be defined as "microorganisms studied by mycologists." But in that case, points out mycologist Nathan Smith21, we should be asking, "Who is a mycologist?"