Recent Releases

2012/07/15

Working with generators - John Nash's Cipherer

Generators and lazy sequences are relatively new addition in Gauche,
and I keep looking for small exercises I can practice using them.
Here's one I did several months ago.

I stumbled on John Nash's 1955 letter to NSA
discussing how encryption scheme should be designed, accompanied
with a concrete design of cipherer/decipherer.
It is basically a state machine that reads bits and write out
moified bits, and the configuration of the state machine is the "key"
of encryption. The nature of the machine can be implemented
naturally as a generator converter---it takes a generator that feeds
input information, and returns a generator that yields
encrypted/decrypted information.

make-permuter is a common function that implements a
bit stream (takes 1 bit, and returns 1 bit) with internal
states. make-encipherer and make-decipherer return
procedures that take an input bit generator and return
an output bit generator.

To run this "machine", you want some convenience functions
that converts between text and bit generators
(Note: Recently I fixed reverse-bits->generator; you need
git HEAD of Gauche at this moment to run this example):

And a couple of examples. First one takes a string,
encripts it by E, immediately decripts by D, and
converts back to string to show it. The second one reads
the text from current input port, encripts and decripts it
and echo back.