GOST 28147-89 is a standard symmetric encryption based on a Feistel network.



The structure of the algorithm consists of three levels:

   encryption modes - simple replacement, application range, imposing a range of feedback and authentication code generation;
   cycles - 32-З, 32-Р and 16-З, is a repetition of the main step;
   main step, a function that takes a 64-bit block of text and one of the eight 32-bit encryption key elements, and uses the replacement table (8x16 matrix of 4-bit values), and returns encrypted block.


Task

Implement the main step of this encryption algorithm.




namespace eval ::GOST {
    proc tcl::mathfunc::k {a b} {
	variable ::GOST::replacementTable
	lindex $replacementTable $a $b
    }
 
    proc mainStep {textBlock idx key} {
	variable replacementTable
	lassign [lindex $textBlock $idx] N0 N1
	set S [expr {($N0 + $key) & 0xFFFFFFFF}]
	set newS 0
	for {set i 0} {$i < 4} {incr i} {
	    set cell [expr {$S >> ($i * 8) & 0xFF}]
	    incr newS [expr {
		(k($i*2, $cell%15) + k($i*2+1, $cell/16) * 16) << ($i * 8)
	    }]
	}
	set S [expr {((($newS << 11) + ($newS >> 21)) & 0xFFFFFFFF) ^ $N1}]
	lset textBlock $idx [list $S $N0]
	return $textBlock
    }
}