It is difficult, perhaps impossible, to learn where the generator state resides in the too concise synopsis of RC4 function given here.
We expand here.
- (define r (fileVal "RC4"))
-
r is an immutable value that accesses the functionality as follows:
- (define s (r seed))
-
s abstracts (hides) a new mutable RC4 generator state initialized by seed which is an ASCII string, in the sense of a Scheme string.
s is a function that takes a scheme symbol as an argument.
s returns a generator, sequential invocations of which mutate the generator s to produce and return a random value.
- (define bs (s 'nb))
-
(bs n) yields the next pseudo random integer with 8n bits and s is thereby mutated.
- (define byteg (s 'sb))
-
then (byteg) yields the next 8 bit integer and mutates s.
- (define rn ((s 'rbi) n))
-
Successive invocations of rn produce a new random number 0≤x<n and mutate s.
In each case the mutable state resides in s.
The RC4 crypto plan is to produce the key-stream for seed s as follows:
(define t ((fileVal "RC4") 'sb s)).
The jth invocation of this value produces the jth character from the stream.
(list (t) (t) (t) … ) is then the key-stream.
Some advocate discarding the first 1000 stream values.
Here are my development notes for the code.