-USING: math.miller-rabin kernel math math.functions namespaces
-sequences ;
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: math.primes kernel math math.functions namespaces
+sequences accessors ;
IN: crypto.rsa
! The private key is the only secret.
<PRIVATE
-: public-key 65537 ; inline
+CONSTANT: public-key 65537
: rsa-primes ( numbits -- p q )
- 2/ 2 unique-primes first2 ;
+ 2/ 2 swap unique-primes first2 ;
: modulus-phi ( numbits -- n phi )
#! Loop until phi is not divisible by the public key.
dup rsa-primes [ * ] 2keep
- [ 1- ] bi@ *
+ [ 1 - ] bi@ *
dup public-key gcd nip 1 = [
rot drop
] [
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 ;