ISAAC is a cryptographically secure pseudo-random number generator (CSPRNG) and stream cipher. It was developed by Bob Jenkins from 1993 (http://burtleburtle.net/bob/rand/isaac.html) and placed in the Public Domain. ISAAC is fast - especially when optimised - and portable to most architectures in nearly all programming and scripting languages.
It is also simple and succinct, using as it does just two 256-word arrays for its state.

ISAAC stands for "Indirection, Shift, Accumulate, Add, and Count" which are the principal bitwise operations employed.
To date - and that's after more than 20 years of existence - ISAAC has not been broken (unless GCHQ or NSA did it, but they wouldn't be telling).
ISAAC thus deserves a lot more attention than it has hitherto received and it would be salutary to see it more universally implemented.



Task

Translate ISAAC's reference C or Pascal code into your language of choice.

The RNG should then be seeded with the string "this is my secret key" and
finally the message "a Top Secret secret" should be encrypted on that key.
Your program's output cipher-text will be a string of hexadecimal digits.

Optional: Include a decryption check by re-initializing ISAAC and performing
the same encryption pass on the cipher-text.

Please use the C or Pascal as a reference guide to these operations.

Two encryption schemes are possible:
(1) XOR (Vernam) or
(2) Caesar-shift mod 95 (Vigenère).
XOR is the simplest; C-shifting offers greater security.

You may choose either scheme, or both, but please specify which you used.
Here are the alternative sample outputs for checking purposes:

Message: a Top Secret secret
Key    : this is my secret key
XOR    : 1C0636190B1260233B35125F1E1D0E2F4C5422
MOD    : 734270227D36772A783B4F2A5F206266236978
XOR dcr: a Top Secret secret
MOD dcr: a Top Secret secret

No official seeding method for ISAAC has been published, but for this task
we may as well just inject the bytes of our key into the randrsl array,
padding with zeroes before mixing, like so:

// zeroise mm array
FOR i:= 0 TO 255 DO mm[i]:=0;
// check seed's highest array element
m := High(seed);
// inject the seed
FOR i:= 0 TO 255 DO BEGIN
	// in case seed[] has less than 256 elements.
	IF i>m THEN randrsl[i]:=0  
		ELSE randrsl[i]:=seed[i];
END;
// initialize ISAAC with seed
RandInit(true);

ISAAC can of course also be initialized with a single 32-bit unsigned integer in the manner of traditional RNGs, and indeed used as such for research and gaming purposes.
But building a strong and simple ISAAC-based stream cipher - replacing the irreparably broken RC4 - is our goal here: ISAAC's intended purpose.




package require Tcl 8.6
 
oo::class create ISAAC {
    variable aa bb cc mm randrsl randcnt
 
    constructor {seed} {
	namespace eval tcl {
	    namespace eval mathfunc {
		proc mm {idx} {
		    upvar 1 mm list
		    lindex $list [expr {$idx % [llength $list]}]
		}
		proc clamp {value} {
		    expr {$value & 0xFFFFFFFF}
		}
	    }
	}
	proc mix1 {i v} {
	    upvar 1 a a
	    lset a $i [expr {clamp([lindex $a $i] ^ $v)}]
	    lset a [set idx [expr {($i+3)%8}]] \
		[expr {clamp([lindex $a $idx] + [lindex $a $i])}]
	    lset a [set idx [expr {($i+1)%8}]] \
		[expr {clamp([lindex $a $idx] + [lindex $a [expr {($i+2)%8}]])}]
	}
 
	binary scan $seed[string repeat \u0000 256] c256 randrsl
	set mm [lrepeat 256 0]
	set randcnt [set aa [set bb [set cc 0]]]
 
	set a [lrepeat 8 0x9e3779b9]
	foreach i {1 2 3 4} {
	    mix1 0 [expr {[lindex $a 1] << 11}]
	    mix1 1 [expr {[lindex $a 2] >> 2}]
	    mix1 2 [expr {[lindex $a 3] << 8}]
	    mix1 3 [expr {[lindex $a 4] >> 16}]
	    mix1 4 [expr {[lindex $a 5] << 10}]
	    mix1 5 [expr {[lindex $a 6] >> 4}]
	    mix1 6 [expr {[lindex $a 7] << 8}]
	    mix1 7 [expr {[lindex $a 0] >> 9}]
	}
	for {set i 0} {$i < 256} {incr i 8} {
	    set a [lmap av $a bv [lrange $randrsl $i [expr {$i+7}]] {
		expr {clamp($av + $bv)}
	    }]
	    mix1 0 [expr {[lindex $a 1] << 11}]
	    mix1 1 [expr {[lindex $a 2] >> 2}]
	    mix1 2 [expr {[lindex $a 3] << 8}]
	    mix1 3 [expr {[lindex $a 4] >> 16}]
	    mix1 4 [expr {[lindex $a 5] << 10}]
	    mix1 5 [expr {[lindex $a 6] >> 4}]
	    mix1 6 [expr {[lindex $a 7] << 8}]
	    mix1 7 [expr {[lindex $a 0] >> 9}]
	    for {set j 0} {$j < 8} {incr j} {
		lset mm [expr {$i+$j}] [lindex $a $j]
	    }
	}
	for {set i 0} {$i < 256} {incr i 8} {
	    set a [lmap av $a bv [lrange $mm $i [expr {$i+7}]] {
		expr {clamp($av + $bv)}
	    }]
	    mix1 0 [expr {[lindex $a 1] << 11}]
	    mix1 1 [expr {[lindex $a 2] >> 2}]
	    mix1 2 [expr {[lindex $a 3] << 8}]
	    mix1 3 [expr {[lindex $a 4] >> 16}]
	    mix1 4 [expr {[lindex $a 5] << 10}]
	    mix1 5 [expr {[lindex $a 6] >> 4}]
	    mix1 6 [expr {[lindex $a 7] << 8}]
	    mix1 7 [expr {[lindex $a 0] >> 9}]
	    for {set j 0} {$j < 8} {incr j} {
		lset mm [expr {$i+$j}] [lindex $a $j]
	    }
	}
	my Step
    }
 
    method Step {} {
	incr bb [incr cc]
	set i -1
	foreach x $mm {
	    set shift [lindex {13 -6 2 -16} [expr {[incr i] % 4}]]
	    set aa [expr {$aa ^ ($shift>0 ? $aa<<$shift : $aa>>-$shift)}]
	    set aa [expr {clamp($aa + mm($i+128))}]
	    set y [expr {clamp(mm($x>>2) + $aa + $bb)}]
	    lset mm $i $y
	    set bb [expr {clamp(mm($y>>10) + $x)}]
	    lset randrsl $i $bb
	}
    }
 
    method random {} {
	set r [lindex $randrsl $randcnt]
	if {[incr randcnt] == 256} {
	    my Step
	    set randcnt 0
	}
	return $r
    }
 
    method RandA {} {
	expr {([my random] % 95) + 32}
    }
    method vernam {msg} {
	binary scan $msg c* b
	for {set i 0} {$i < [llength $b]} {incr i} {
	    lset b $i [expr {[lindex $b $i] & 255 ^ [my RandA]}]
	}
	return [binary encode hex [binary format c* $b]]
    }
}