factor/contrib/crypto/common.factor

36 lines
1.2 KiB
Factor
Raw Normal View History

2005-10-16 15:11:50 -04:00
IN: crypto-internals
USING: kernel io strings sequences namespaces math parser ;
: w+ ( int -- int ) + HEX: ffffffff bitand ; inline
: nth-int ( string n -- int ) 2 shift dup 4 + rot <slice> le> ; inline
: nth-int-be ( string n -- int ) 2 shift dup 4 + rot <slice> be> ; inline
: float-sin ( int -- int ) sin abs 4294967296 * >bignum ; inline
: update ( num var -- ) [ w+ ] change ; inline
: update-old-new ( old new -- )
2005-09-10 15:53:17 -04:00
[ get >r get r> ] 2keep >r >r w+ dup r> set r> set ; inline
2006-08-10 00:27:21 -04:00
: calculate-pad-length ( length -- pad-length )
dup 56 < 55 119 ? swap - ;
! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
2006-08-10 00:27:21 -04:00
: preprocess-plaintext ( string big-endian? -- padded-string )
2006-09-07 16:15:41 -04:00
>r >sbuf r> over [
HEX: 80 ,
2006-08-10 00:27:21 -04:00
dup length HEX: 3f bitand calculate-pad-length 0 <string> %
2006-09-07 16:15:41 -04:00
length 3 shift 8 rot [ >be ] [ >le ] if %
] "" make dupd nappend ;
: shift-mod ( n s w -- n ) >r shift r> 1 swap shift 1 - bitand ; inline
2005-10-16 15:11:50 -04:00
IN: crypto
: bitroll ( n s w -- n )
#! Roll n by s bits to the left, wrapping around after
#! w bits.
[ 1 - bitand ] keep
over 0 < [ [ + ] keep ] when
[ shift-mod ] 3keep
[ - ] keep shift-mod bitor ; inline
: hex-string ( str -- str ) [ [ >hex 2 48 pad-left % ] each ] "" make ;