         K-256 >>K
         initial-H-256 >>H
         4 >>word-size
-        64 >>block-size ; 
+        64 >>block-size ;
 
 M: sha-256 checksum-bytes
-    drop
-    <sha-256-state> sha2 [
-        byte-array>sha2
-    ] with-variable ;
+    drop <sha-256-state> byte-array>sha2 ;

USING: kernel splitting grouping math sequences namespaces make
io.binary math.bitwise checksums checksums.common
sbufs strings combinators.smart math.ranges fry combinators
accessors ;
IN: checksums.sha2

<PRIVATE

    d c pick exchange
    c b pick exchange
    b a pick exchange
    [ w+ a ] dip set-nth ; inline

: prepare-message-schedule ( seq sha2 -- w-seq )
    [ word-size>> <sliced-groups> [ be> ] map ]
    [ block-size>> 0 pad-tail 16 64 [a,b) over '[ _ process-M-256 ] each ] bi ;

:: process-chunk ( M block-size cloned-H sha2 -- )
    block-size [
        M cloned-H sha2 T1
        cloned-H T2
        cloned-H update-H 
    ] each
    cloned-H sha2 H>> [ w+ ] 2map sha2 (>>H) ;

:: byte-array>sha2 ( bytes state -- string )
    bytes pad-initial-bytes
    state block-size>> <sliced-groups>
    [
        state prepare-message-schedule
        state [ block-size>> ] [ H>> clone ] bi state process-chunk
    ] each
    state H>> 4 seq>byte-array ;

PRIVATE>

