: barrett-mu ( n size -- mu )
#! Calculates Barrett's reduction parameter mu
#! size = word size in bits (8, 16, 32, 64, ...)
- over log2 1+ over / 2 * >r 2 swap ^ r> ^ swap / floor ;
+ ! over log2 1+ over / 2 * >r 2 swap ^ r> ^ swap / floor ;
+ [
+ [ log2 1+ ] [ / 2 * ] bi*
+ ] [
+ 2^ rot ^ swap /i
+ ] 2bi ;
+
: 2seq>seq ( seq1 seq2 -- seq )
#! { aceg } { bdfh } -> { abcdefgh }
- swap ! error?
[ 2array flip concat ] keep like ;
: mod-nth ( n seq -- elt )
#! 5 "abcd" -> b
- [ length mod ] keep nth ;
+ [ length mod ] [ nth ] bi ;
[ "g[\u00000b:\eM\u0000dfN\u000012Hr\u0000dal/c+\u0000fe\u0000d9W\u0000e9" ] [ 16 11 <string> "Hi There" >byte-array byte-array>sha1-hmac >string ] unit-test
[ "\u0000ef\u0000fc\u0000dfj\u0000e5\u0000eb/\u0000a2\u0000d2t\u000016\u0000d5\u0000f1\u000084\u0000df\u00009c%\u00009a|y" ] [ "Jefe" "what do ya want for nothing?" >byte-array byte-array>sha1-hmac >string ] unit-test
[ "\u0000d70YM\u000016~5\u0000d5\u000095o\u0000d8\0=\r\u0000b3\u0000d3\u0000f4m\u0000c7\u0000bb" ] [ 16 HEX: aa <string> 50 HEX: dd <repetition> >byte-array byte-array>sha1-hmac >string ] unit-test
-
: byte-array>sha1-hmac ( K string -- hmac )
binary <byte-reader> stream>sha1-hmac ;
-
: stream>md5-hmac ( K stream -- hmac )
[ init-hmac md5-hmac ] with-stream ;
USING: kernel io io.binary io.files io.streams.byte-array math
math.functions math.parser namespaces splitting strings
sequences crypto.common byte-arrays locals sequences.private
-io.encodings.binary symbols ;
+io.encodings.binary symbols math.bitfields.lib ;
IN: crypto.md5
<PRIVATE
: F ( X Y Z -- FXYZ )
#! F(X,Y,Z) = XY v not(X) Z
- pick bitnot bitand >r bitand r> bitor ;
+ pick bitnot bitand [ bitand ] [ bitor ] bi* ;
: G ( X Y Z -- GXYZ )
#! G(X,Y,Z) = XZ v Y not(Z)
- dup bitnot rot bitand >r bitand r> bitor ;
+ dup bitnot rot bitand [ bitand ] [ bitor ] bi* ;
: H ( X Y Z -- HXYZ )
#! H(X,Y,Z) = X xor Y xor Z
USING: math.miller-rabin kernel math math.functions namespaces
-sequences ;
+sequences accessors ;
IN: crypto.rsa
! The private key is the only secret.
public-key <rsa> ;
: rsa-encrypt ( message rsa -- encrypted )
- [ rsa-public-key ] keep rsa-modulus ^mod ;
+ [ public-key>> ] [ modulus>> ] bi ^mod ;
: rsa-decrypt ( encrypted rsa -- message )
- [ rsa-private-key ] keep rsa-modulus ^mod ;
\ No newline at end of file
+ [ private-key>> ] [ modulus>> ] bi ^mod ;
+++ /dev/null
-USING: kernel math test namespaces crypto ;
-
-[ 0 ] [ 1 0 0 bitroll ] unit-test
-[ 1 ] [ 1 0 1 bitroll ] unit-test
-[ 1 ] [ 1 1 1 bitroll ] unit-test
-[ 1 ] [ 1 0 2 bitroll ] unit-test
-[ 1 ] [ 1 0 1 bitroll ] unit-test
-[ 1 ] [ 1 20 2 bitroll ] unit-test
-[ 1 ] [ 1 8 8 bitroll ] unit-test
-[ 1 ] [ 1 -8 8 bitroll ] unit-test
-[ 1 ] [ 1 -32 8 bitroll ] unit-test
-[ 128 ] [ 1 -1 8 bitroll ] unit-test
-[ 8 ] [ 1 3 32 bitroll ] unit-test
-
-
USING: kernel math threads system ;
IN: crypto.timing
-: with-timing ( ... quot n -- )
+: with-timing ( quot n -- )
#! force the quotation to execute in, at minimum, n milliseconds
- millis 2slip millis - + sleep ;
-
+ millis 2slip millis - + sleep ; inline
USING: crypto.common kernel math sequences ;
IN: crypto.xor
-TUPLE: no-xor-key ;
+ERROR: no-xor-key ;
-: xor-crypt ( key seq -- seq )
- over empty? [ no-xor-key construct-empty throw ] when
+: xor-crypt ( key seq -- seq' )
+ over empty? [ no-xor-key ] when
dup length rot [ mod-nth bitxor ] curry 2map ;