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

    } 2cleave set-nth ; inline

: ch ( x y z -- x' )
    [ bitxor bitand ] keep bitxor ;

: maj ( x y z -- x' )
    [ [ bitand ] [ bitor ] 2bi ] dip bitand bitor ;

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

: slice3 ( n seq -- a b c )
    [ dup 3 + ] dip <slice> first3 ; inline

: T1 ( W n H -- T1 )
    [
        [ swap nth ] keep
        sha2 get K>> nth +
    ] dip
    [ e swap slice3 ch w+ ]
    [ e swap nth S1-256 w+ ]
    [ h swap nth w+ ] tri ;

: T2 ( H -- T2 )
    [ a swap nth S0-256 ]

        K-256 >>K
        initial-H-256 >>H
        4 >>word-size
        64 >>block-size ;

M: sha-256 checksum-bytes
    drop <sha-256-state> byte-array>sha2 ;
