more refactoring on sha2
parent
5b70d3ccce
commit
0dd2aa643a
|
@ -2,12 +2,13 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel splitting grouping math sequences namespaces make
|
USING: kernel splitting grouping math sequences namespaces make
|
||||||
io.binary math.bitwise checksums checksums.common
|
io.binary math.bitwise checksums checksums.common
|
||||||
sbufs strings combinators.smart math.ranges fry combinators ;
|
sbufs strings combinators.smart math.ranges fry combinators
|
||||||
|
accessors ;
|
||||||
IN: checksums.sha2
|
IN: checksums.sha2
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
SYMBOLS: vars K H process-M word-size block-size ;
|
SYMBOLS: H word-size block-size ;
|
||||||
|
|
||||||
CONSTANT: a 0
|
CONSTANT: a 0
|
||||||
CONSTANT: b 1
|
CONSTANT: b 1
|
||||||
|
@ -58,25 +59,6 @@ CONSTANT: K-256
|
||||||
[ -10 shift ] tri
|
[ -10 shift ] tri
|
||||||
] [ bitxor ] reduce-outputs ; inline
|
] [ bitxor ] reduce-outputs ; inline
|
||||||
|
|
||||||
: process-M-256 ( n seq -- )
|
|
||||||
{
|
|
||||||
[ [ 16 - ] dip nth ]
|
|
||||||
[ [ 15 - ] dip nth s0-256 ]
|
|
||||||
[ [ 7 - ] dip nth ]
|
|
||||||
[ [ 2 - ] dip nth s1-256 w+ w+ w+ ]
|
|
||||||
[ ]
|
|
||||||
} 2cleave set-nth ; inline
|
|
||||||
|
|
||||||
: prepare-message-schedule ( seq -- w-seq )
|
|
||||||
word-size get group [ be> ] map block-size get 0 pad-tail
|
|
||||||
16 64 [a,b) over '[ _ process-M-256 ] each ;
|
|
||||||
|
|
||||||
: ch ( x y z -- x' )
|
|
||||||
[ bitxor bitand ] keep bitxor ;
|
|
||||||
|
|
||||||
: maj ( x y z -- x' )
|
|
||||||
[ [ bitand ] [ bitor ] 2bi ] dip bitand bitor ;
|
|
||||||
|
|
||||||
: S0-256 ( x -- x' )
|
: S0-256 ( x -- x' )
|
||||||
[
|
[
|
||||||
[ -2 bitroll-32 ]
|
[ -2 bitroll-32 ]
|
||||||
|
@ -91,21 +73,42 @@ CONSTANT: K-256
|
||||||
[ -25 bitroll-32 ] tri
|
[ -25 bitroll-32 ] tri
|
||||||
] [ bitxor ] reduce-outputs ; inline
|
] [ bitxor ] reduce-outputs ; inline
|
||||||
|
|
||||||
: slice3 ( n seq -- a b c ) [ dup 3 + ] dip <slice> first3 ; inline
|
: process-M-256 ( n seq -- )
|
||||||
|
{
|
||||||
|
[ [ 16 - ] dip nth ]
|
||||||
|
[ [ 15 - ] dip nth s0-256 ]
|
||||||
|
[ [ 7 - ] dip nth ]
|
||||||
|
[ [ 2 - ] dip nth s1-256 w+ w+ w+ ]
|
||||||
|
[ ]
|
||||||
|
} 2cleave set-nth ; inline
|
||||||
|
|
||||||
: T1 ( W n -- T1 )
|
: 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 )
|
||||||
|
word-size get <sliced-groups> [ be> ] map block-size get 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
|
[ swap nth ] keep
|
||||||
K get nth +
|
K-256 nth +
|
||||||
e vars get slice3 ch +
|
] dip
|
||||||
e vars get nth S1-256 +
|
[ e swap slice3 ch w+ ]
|
||||||
h vars get nth w+ ;
|
[ e swap nth S1-256 w+ ]
|
||||||
|
[ h swap nth w+ ] tri ;
|
||||||
|
|
||||||
: T2 ( -- T2 )
|
: T2 ( H -- T2 )
|
||||||
a vars get nth S0-256
|
[ a swap nth S0-256 ]
|
||||||
a vars get slice3 maj w+ ;
|
[ a swap slice3 maj w+ ] bi ;
|
||||||
|
|
||||||
: update-vars ( T1 T2 -- )
|
: update-H ( T1 T2 H -- )
|
||||||
vars get
|
|
||||||
h g pick exchange
|
h g pick exchange
|
||||||
g f pick exchange
|
g f pick exchange
|
||||||
f e pick exchange
|
f e pick exchange
|
||||||
|
@ -115,28 +118,35 @@ CONSTANT: K-256
|
||||||
b a pick exchange
|
b a pick exchange
|
||||||
[ w+ a ] dip set-nth ;
|
[ w+ a ] dip set-nth ;
|
||||||
|
|
||||||
: process-chunk ( M -- )
|
: process-chunk ( M block-size H-cloned -- )
|
||||||
H get clone vars set
|
[
|
||||||
prepare-message-schedule block-size get [
|
'[
|
||||||
T1 T2 update-vars
|
_
|
||||||
] with each vars get H get [ w+ ] 2map H set ;
|
[ T1 ]
|
||||||
|
[ T2 ]
|
||||||
|
[ update-H ] tri
|
||||||
|
] with each
|
||||||
|
] keep H get [ w+ ] 2map H set ;
|
||||||
|
|
||||||
: seq>byte-array ( n seq -- string )
|
: pad-initial-bytes ( string -- padded-string )
|
||||||
[ swap '[ _ >be % ] each ] B{ } make ;
|
dup [
|
||||||
|
|
||||||
: preprocess-plaintext ( string big-endian? -- padded-string )
|
|
||||||
#! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
|
|
||||||
[ >sbuf ] dip over [
|
|
||||||
HEX: 80 ,
|
HEX: 80 ,
|
||||||
dup length HEX: 3f bitand
|
length
|
||||||
calculate-pad-length 0 <string> %
|
[ HEX: 3f bitand calculate-pad-length 0 <string> % ]
|
||||||
length 3 shift 8 rot [ >be ] [ >le ] if %
|
[ 3 shift 8 >be % ] bi
|
||||||
] "" make over push-all ;
|
] "" make append ;
|
||||||
|
|
||||||
|
: seq>byte-array ( seq n -- string )
|
||||||
|
'[ _ >be ] map B{ } join ;
|
||||||
|
|
||||||
: byte-array>sha2 ( byte-array -- string )
|
: byte-array>sha2 ( byte-array -- string )
|
||||||
t preprocess-plaintext
|
pad-initial-bytes
|
||||||
block-size get group [ process-chunk ] each
|
block-size get <sliced-groups>
|
||||||
4 H get seq>byte-array ;
|
[
|
||||||
|
prepare-message-schedule
|
||||||
|
block-size get H get clone process-chunk
|
||||||
|
] each
|
||||||
|
H get 4 seq>byte-array ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -146,9 +156,9 @@ INSTANCE: sha-256 checksum
|
||||||
|
|
||||||
M: sha-256 checksum-bytes
|
M: sha-256 checksum-bytes
|
||||||
drop [
|
drop [
|
||||||
K-256 K set
|
|
||||||
initial-H-256 H set
|
initial-H-256 H set
|
||||||
4 word-size set
|
4 word-size set
|
||||||
64 block-size set
|
64 block-size set
|
||||||
byte-array>sha2
|
byte-array>sha2
|
||||||
|
|
||||||
] with-scope ;
|
] with-scope ;
|
||||||
|
|
Loading…
Reference in New Issue