2 from typing
import List
5 __all__
= ['expand', 'encrypt', 'decrypt']
8 # I’ve used List[int], but it amuses me to contemplate Tuple[(int,) * 30] too.
10 def rotate_left(lhs
: int, rhs
: int, bits
: int) -> int:
11 return (lhs
<< rhs
) |
(lhs
>> (bits
- rhs
))
14 def rotate_right(lhs
: int, rhs
: int, bits
: int) -> int:
15 return (lhs
<< (bits
- rhs
)) |
(lhs
>> rhs
)
18 def expand(key
: int) -> List
[int]:
22 x
= (key
>> 64) & mask
24 for i
in range(0, 29):
25 x
= ((rotate_right(x
, 9, 64) + y
) ^ i
) & mask
26 y
= (rotate_left(y
, 2, 64) & mask
) ^ x
32 def encrypt(k
: List
[int], n
: int, plain_text
: int) -> int:
35 y
= (plain_text
>> n
) & mask
38 x
= ((rotate_right(x
, 9, n
) + y
) ^ ki
) & mask
39 y
= (rotate_left(y
, 2, n
) ^ x
) & mask
44 def decrypt(k
: List
[int], n
: int, cipher_text
: int) -> int:
46 x
= cipher_text
& mask
47 y
= (cipher_text
>> n
) & mask
50 y
= rotate_right(x ^ y
, 2, n
) & mask
;
51 x
= rotate_left(((x ^ ki
) - y
) & mask
, 9, n
) & mask
;