Skip to content

Commit 290079e

Browse files
committed
update from atlas
1 parent 8fb691d commit 290079e

16 files changed

+803
-2
lines changed

17-futures/crypto/arcfour-timings.txt

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
workers|time
2+
4|4.96
3+
3|5.40
4+
2|8.35
5+
1|11.25
6+
1|11.17
7+
2|8.45
8+
3|6.08
9+
4|5.83
10+
4|6.22
11+
3|7.33
12+
2|9.48
13+
1|11.86
14+
1|11.72
15+
2|9.22
16+
3|6.74
17+
4|6.37
18+
4|4.94
19+
3|5.51
20+
2|8.25
21+
1|11.47
22+
1|12.90
23+
2|8.94
24+
3|6.44
25+
4|5.90
26+
4|5.94
27+
3|6.46
28+
2|9.10
29+
1|11.66
30+
1|11.48
31+
2|9.08
32+
3|6.31
33+
4|5.99
34+
4|5.02
35+
3|5.46
36+
2|8.26
37+
1|11.18
38+
1|11.23
39+
2|8.52
40+
3|5.64
41+
4|5.39
42+
4|5.53
43+
3|6.07
44+
2|8.66
45+
1|11.42
46+
1|11.34
47+
2|8.44
48+
3|5.88
49+
4|5.57
50+
4|4.93
51+
3|5.47
52+
2|8.65
53+
1|11.23
54+
1|11.12
55+
2|7.83
56+
3|5.81
57+
4|5.45
58+
4|5.54
59+
3|6.09
60+
2|8.84
61+
1|11.45
62+
1|11.25
63+
2|8.32
64+
3|6.02
65+
4|5.74

17-futures/crypto/arcfour.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""RC4 compatible algorithm"""
2+
3+
def arcfour(key, in_bytes, loops=20):
4+
5+
kbox = bytearray(256) # create key box
6+
for i, car in enumerate(key): # copy key and vector
7+
kbox[i] = car
8+
j = len(key)
9+
for i in range(j, 256): # repeat until full
10+
kbox[i] = kbox[i-j]
11+
12+
# [1] initialize sbox
13+
sbox = bytearray(range(256))
14+
15+
# repeat sbox mixing loop, as recommened in CipherSaber-2
16+
# http://ciphersaber.gurus.com/faq.html#cs2
17+
j = 0
18+
for k in range(loops):
19+
for i in range(256):
20+
j = (j + sbox[i] + kbox[i]) % 256
21+
sbox[i], sbox[j] = sbox[j], sbox[i]
22+
23+
# main loop
24+
i = 0
25+
j = 0
26+
out_bytes = bytearray()
27+
28+
for car in in_bytes:
29+
i = (i + 1) % 256
30+
# [2] shuffle sbox
31+
j = (j + sbox[i]) % 256
32+
sbox[i], sbox[j] = sbox[j], sbox[i]
33+
# [3] compute t
34+
t = (sbox[i] + sbox[j]) % 256
35+
k = sbox[t]
36+
car = car ^ k
37+
out_bytes.append(car)
38+
39+
return out_bytes
40+
41+
42+
def test():
43+
from time import time
44+
clear = bytearray(b'1234567890' * 100000)
45+
t0 = time()
46+
cipher = arcfour(b'key', clear)
47+
print('elapsed time: %.2fs' % (time() - t0))
48+
result = arcfour(b'key', cipher)
49+
assert result == clear, '%r != %r' % (result, clear)
50+
print('elapsed time: %.2fs' % (time() - t0))
51+
print('OK')
52+
53+
54+
if __name__ == '__main__':
55+
test()

17-futures/crypto/arcfour_futures.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import time
3+
from concurrent import futures
4+
from random import randrange
5+
from arcfour import arcfour
6+
7+
JOBS = 12
8+
SIZE = 2**18
9+
10+
KEY = b"'Twas brillig, and the slithy toves\nDid gyre"
11+
STATUS = '{} workers, elapsed time: {:.2f}s'
12+
13+
14+
def arcfour_test(size, key):
15+
in_text = bytearray(randrange(256) for i in range(size))
16+
cypher_text = arcfour(key, in_text)
17+
out_text = arcfour(key, cypher_text)
18+
assert in_text == out_text, 'Failed arcfour_test'
19+
return size
20+
21+
22+
def main(workers=None):
23+
if workers:
24+
workers = int(workers)
25+
t0 = time.time()
26+
27+
with futures.ProcessPoolExecutor(workers) as executor:
28+
actual_workers = executor._max_workers
29+
to_do = []
30+
for i in range(JOBS, 0, -1):
31+
size = SIZE + int(SIZE / JOBS * (i - JOBS/2))
32+
job = executor.submit(arcfour_test, size, KEY)
33+
to_do.append(job)
34+
35+
for future in futures.as_completed(to_do):
36+
res = future.result()
37+
print('{:.1f} KB'.format(res/2**10))
38+
39+
print(STATUS.format(actual_workers, time.time() - t0))
40+
41+
if __name__ == '__main__':
42+
if len(sys.argv) == 2:
43+
workers = int(sys.argv[1])
44+
else:
45+
workers = None
46+
main(workers)

17-futures/crypto/arcfour_test.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env python3
2+
3+
from arcfour import arcfour
4+
5+
'''
6+
Source of the test vectors:
7+
A Stream Cipher Encryption Algorithm "Arcfour"
8+
http://tools.ietf.org/html/draft-kaukonen-cipher-arcfour-03
9+
'''
10+
11+
TEST_VECTORS = [
12+
('CRYPTLIB', {
13+
'Plain Text' : (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
14+
'Key' : (0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF),
15+
'Cipher Text' : (0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79),
16+
}
17+
),
18+
('COMMERCE', {
19+
'Plain Text' : (0xdc, 0xee, 0x4c, 0xf9, 0x2c),
20+
'Key' : (0x61, 0x8a, 0x63, 0xd2, 0xfb),
21+
'Cipher Text' : (0xf1, 0x38, 0x29, 0xc9, 0xde),
22+
}
23+
),
24+
('SSH ARCFOUR', {
25+
'Plain Text' : (
26+
0x52, 0x75, 0x69, 0x73, 0x6c, 0x69, 0x6e, 0x6e,
27+
0x75, 0x6e, 0x20, 0x6c, 0x61, 0x75, 0x6c, 0x75,
28+
0x20, 0x6b, 0x6f, 0x72, 0x76, 0x69, 0x73, 0x73,
29+
0x73, 0x61, 0x6e, 0x69, 0x2c, 0x20, 0x74, 0xe4,
30+
0x68, 0x6b, 0xe4, 0x70, 0xe4, 0x69, 0x64, 0x65,
31+
0x6e, 0x20, 0x70, 0xe4, 0xe4, 0x6c, 0x6c, 0xe4,
32+
0x20, 0x74, 0xe4, 0x79, 0x73, 0x69, 0x6b, 0x75,
33+
0x75, 0x2e, 0x20, 0x4b, 0x65, 0x73, 0xe4, 0x79,
34+
0xf6, 0x6e, 0x20, 0x6f, 0x6e, 0x20, 0x6f, 0x6e,
35+
0x6e, 0x69, 0x20, 0x6f, 0x6d, 0x61, 0x6e, 0x61,
36+
0x6e, 0x69, 0x2c, 0x20, 0x6b, 0x61, 0x73, 0x6b,
37+
0x69, 0x73, 0x61, 0x76, 0x75, 0x75, 0x6e, 0x20,
38+
0x6c, 0x61, 0x61, 0x6b, 0x73, 0x6f, 0x74, 0x20,
39+
0x76, 0x65, 0x72, 0x68, 0x6f, 0x75, 0x75, 0x2e,
40+
0x20, 0x45, 0x6e, 0x20, 0x6d, 0x61, 0x20, 0x69,
41+
0x6c, 0x6f, 0x69, 0x74, 0x73, 0x65, 0x2c, 0x20,
42+
0x73, 0x75, 0x72, 0x65, 0x20, 0x68, 0x75, 0x6f,
43+
0x6b, 0x61, 0x61, 0x2c, 0x20, 0x6d, 0x75, 0x74,
44+
0x74, 0x61, 0x20, 0x6d, 0x65, 0x74, 0x73, 0xe4,
45+
0x6e, 0x20, 0x74, 0x75, 0x6d, 0x6d, 0x75, 0x75,
46+
0x73, 0x20, 0x6d, 0x75, 0x6c, 0x6c, 0x65, 0x20,
47+
0x74, 0x75, 0x6f, 0x6b, 0x61, 0x61, 0x2e, 0x20,
48+
0x50, 0x75, 0x75, 0x6e, 0x74, 0x6f, 0x20, 0x70,
49+
0x69, 0x6c, 0x76, 0x65, 0x6e, 0x2c, 0x20, 0x6d,
50+
0x69, 0x20, 0x68, 0x75, 0x6b, 0x6b, 0x75, 0x75,
51+
0x2c, 0x20, 0x73, 0x69, 0x69, 0x6e, 0x74, 0x6f,
52+
0x20, 0x76, 0x61, 0x72, 0x61, 0x6e, 0x20, 0x74,
53+
0x75, 0x75, 0x6c, 0x69, 0x73, 0x65, 0x6e, 0x2c,
54+
0x20, 0x6d, 0x69, 0x20, 0x6e, 0x75, 0x6b, 0x6b,
55+
0x75, 0x75, 0x2e, 0x20, 0x54, 0x75, 0x6f, 0x6b,
56+
0x73, 0x75, 0x74, 0x20, 0x76, 0x61, 0x6e, 0x61,
57+
0x6d, 0x6f, 0x6e, 0x20, 0x6a, 0x61, 0x20, 0x76,
58+
0x61, 0x72, 0x6a, 0x6f, 0x74, 0x20, 0x76, 0x65,
59+
0x65, 0x6e, 0x2c, 0x20, 0x6e, 0x69, 0x69, 0x73,
60+
0x74, 0xe4, 0x20, 0x73, 0x79, 0x64, 0xe4, 0x6d,
61+
0x65, 0x6e, 0x69, 0x20, 0x6c, 0x61, 0x75, 0x6c,
62+
0x75, 0x6e, 0x20, 0x74, 0x65, 0x65, 0x6e, 0x2e,
63+
0x20, 0x2d, 0x20, 0x45, 0x69, 0x6e, 0x6f, 0x20,
64+
0x4c, 0x65, 0x69, 0x6e, 0x6f),
65+
'Key' : (
66+
0x29, 0x04, 0x19, 0x72, 0xfb, 0x42, 0xba, 0x5f,
67+
0xc7, 0x12, 0x77, 0x12, 0xf1, 0x38, 0x29, 0xc9),
68+
'Cipher Text' : (
69+
0x35, 0x81, 0x86, 0x99, 0x90, 0x01, 0xe6, 0xb5,
70+
0xda, 0xf0, 0x5e, 0xce, 0xeb, 0x7e, 0xee, 0x21,
71+
0xe0, 0x68, 0x9c, 0x1f, 0x00, 0xee, 0xa8, 0x1f,
72+
0x7d, 0xd2, 0xca, 0xae, 0xe1, 0xd2, 0x76, 0x3e,
73+
0x68, 0xaf, 0x0e, 0xad, 0x33, 0xd6, 0x6c, 0x26,
74+
0x8b, 0xc9, 0x46, 0xc4, 0x84, 0xfb, 0xe9, 0x4c,
75+
0x5f, 0x5e, 0x0b, 0x86, 0xa5, 0x92, 0x79, 0xe4,
76+
0xf8, 0x24, 0xe7, 0xa6, 0x40, 0xbd, 0x22, 0x32,
77+
0x10, 0xb0, 0xa6, 0x11, 0x60, 0xb7, 0xbc, 0xe9,
78+
0x86, 0xea, 0x65, 0x68, 0x80, 0x03, 0x59, 0x6b,
79+
0x63, 0x0a, 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 0x73,
101+
0x68, 0xc4, 0xad, 0x6a, 0xc1, 0x86, 0xd0, 0x82,
102+
0x45, 0xb2, 0x63, 0xa2, 0x66, 0x6d, 0x1f, 0x6c,
103+
0x54, 0x20, 0xf1, 0x59, 0x9d, 0xfd, 0x9f, 0x43,
104+
0x89, 0x21, 0xc2, 0xf5, 0xa4, 0x63, 0x93, 0x8c,
105+
0xe0, 0x98, 0x22, 0x65, 0xee, 0xf7, 0x01, 0x79,
106+
0xbc, 0x55, 0x3f, 0x33, 0x9e, 0xb1, 0xa4, 0xc1,
107+
0xaf, 0x5f, 0x6a, 0x54, 0x7f),
108+
}
109+
),
110+
]
111+
112+
for name, vectors in TEST_VECTORS:
113+
print(name, end='')
114+
plain = bytearray(vectors['Plain Text'])
115+
cipher = bytearray(vectors['Cipher Text'])
116+
key = bytearray(vectors['Key'])
117+
assert cipher == arcfour(key, plain, loops=1)
118+
assert plain == arcfour(key, cipher, loops=1)
119+
print(' --> OK')

17-futures/crypto/sha-timings.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
workers|time
2+
4|8.88
3+
3|11.14
4+
2|13.66
5+
1|22.80
6+
1|25.42
7+
2|16.37
8+
3|12.09
9+
4|11.06
10+
4|11.40
11+
3|11.51
12+
2|15.20
13+
1|24.18
14+
1|22.09
15+
2|12.48
16+
3|10.78
17+
4|10.48
18+
4|8.48
19+
3|10.07
20+
2|12.42
21+
1|20.24
22+
1|20.31
23+
2|11.39
24+
3|10.88
25+
4|10.44
26+
4|10.43
27+
3|11.11
28+
2|12.39
29+
1|20.69
30+
1|20.53
31+
2|11.80
32+
3|11.01
33+
4|10.52
34+
4|11.50
35+
3|14.45
36+
2|16.95
37+
1|24.77
38+
1|22.71
39+
2|18.35
40+
3|12.66
41+
4|12.20
42+
4|12.37
43+
3|13.37
44+
2|19.30
45+
1|24.30
46+
1|23.93
47+
2|18.51
48+
3|13.88
49+
4|12.97
50+

17-futures/crypto/sha_futures.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
import time
3+
import hashlib
4+
from concurrent import futures
5+
from random import randrange
6+
7+
JOBS = 12
8+
SIZE = 2**20
9+
STATUS = '{} workers, elapsed time: {:.2f}s'
10+
11+
12+
def sha(size):
13+
data = bytearray(randrange(256) for i in range(size))
14+
algo = hashlib.new('sha256')
15+
algo.update(data)
16+
return algo.hexdigest()
17+
18+
19+
def main(workers=None):
20+
if workers:
21+
workers = int(workers)
22+
t0 = time.time()
23+
24+
with futures.ProcessPoolExecutor(workers) as executor:
25+
actual_workers = executor._max_workers
26+
to_do = (executor.submit(sha, SIZE) for i in range(JOBS))
27+
for future in futures.as_completed(to_do):
28+
res = future.result()
29+
print(res)
30+
31+
print(STATUS.format(actual_workers, time.time() - t0))
32+
33+
if __name__ == '__main__':
34+
if len(sys.argv) == 2:
35+
workers = int(sys.argv[1])
36+
else:
37+
workers = None
38+
main(workers)

0 commit comments

Comments
 (0)