import numpy as np
from pylab import *
import sys, subprocess

def test(method, length):
	p = subprocess.Popen(['./aes %d %d' % (method, length)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
	stdout, stderr = p.communicate()
	return float(stdout.decode().split(' ')[1].split('s')[0])

def draw(length, a128, a192, a256):
	length = length[7:]
	a128 = a128[7:]
	a192 = a192[7:]
	a256 = a256[7:]
	figure()
	p = subplot(111)
	p.plot(length, a128, '-o', label='AES-128-CBC')
	p.plot(length, a192, '-o', label='AES-192-CBC')
	p.plot(length, a256, '-o', label='AES-256-CBC')
	p.set_xlabel('Length')
	p.set_ylabel('Time')
	# p.loglog()
	p.legend()
	tight_layout()
	savefig('AES.png')

if __name__ == '__main__':
	np.set_printoptions(suppress=True, precision=6)
	lengths = 2 ** np.arange(10, 27)
	draw(lengths, 
		np.array([0.000042, 0.00007,  0.000135, 0.000597, 0.001008, 0.001583, 0.002078, 0.003434, 0.006661, 0.012918, 0.025619, 0.051024, 0.101693, 0.203135, 0.406457, 0.814705, 1.644701]),
		np.array([0.000044, 0.000163, 0.000178, 0.000722, 0.000846, 0.001475, 0.002863, 0.003979, 0.007735, 0.015403, 0.030612, 0.061112, 0.121336, 0.24296, 0.485726, 0.971193, 1.942015]),
		np.array([0.000062, 0.000218, 0.000311, 0.000866, 0.000926, 0.002804, 0.003353, 0.005029, 0.009039, 0.017751, 0.035523, 0.070484, 0.140922, 0.281624, 0.563629, 1.126613, 2.255])
		)
	lengths = [16384, 10485760]
	print(lengths)
	cnt = int(sys.argv[-1])
	for method in [128, 192, 256]:
		result = []
		for length in lengths:
			s = 0
			for i in range(cnt):
				s += test(method, length)
			s /= cnt
			result.append(s)
		print(method, np.array(result))

# [    1024     2048     4096     8192    16384    32768    65536   131072
#    262144   524288  1048576  2097152  4194304  8388608 16777216 33554432
#  67108864]
# 128 [0.000042 0.00007  0.000135 0.000597 0.001008 0.001583 0.002078 0.003434
#  0.006661 0.012918 0.025619 0.051024 0.101693 0.203135 0.406457 0.814705
#  1.644701]
# 192 [0.000044 0.000163 0.000178 0.000722 0.000846 0.001475 0.002863 0.003979
#  0.007735 0.015403 0.030612 0.061112 0.121336 0.24296  0.485726 0.971193
#  1.942015]
# 256 [0.000062 0.000218 0.000311 0.000866 0.000926 0.002804 0.003353 0.005029
#  0.009039 0.017751 0.035523 0.070484 0.140922 0.281624 0.563629 1.126613
#  2.255   ]

# [    1024     2048     4096     8192    16384    32768    65536   131072
#    262144   524288  1048576  2097152  4194304  8388608 16777216 33554432
#  67108864]
# 128 [0.000069 0.000105 0.000124 0.000206 0.000409 0.000813 0.001624 0.003248
#  0.006487 0.012965 0.025928 0.051656 0.102907 0.205018 0.410802 0.820433
#  1.637919]
# 192 [0.000063 0.000135 0.000288 0.000585 0.001161 0.001802 0.002463 0.003996
#  0.007819 0.01534  0.030624 0.061172 0.122678 0.245187 0.489606 0.979193
#  1.96162 ]
# 256 [0.000036 0.000072 0.000142 0.000282 0.000565 0.001127 0.002254 0.004504
#  0.008996 0.017995 0.035923 0.071466 0.142658 0.284698 0.569603 1.139418
#  2.276553]

import numpy as np
from pylab import *
import sys, subprocess

def test(method, length):
	p = subprocess.Popen(['./aes %d %d' % (method, length)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
	stdout, stderr = p.communicate()
	return float(stdout.decode().split(' ')[1].split('s')[0])

def draw(length, a128, a192, a256):
	length = length[7:]
	a128 = a128[7:]
	a192 = a192[7:]
	a256 = a256[7:]
	figure()
	p = subplot(111)
	p.plot(length, a128, '-o', label='AES-128-CBC')
	p.plot(length, a192, '-o', label='AES-192-CBC')
	p.plot(length, a256, '-o', label='AES-256-CBC')
	p.set_xlabel('Length')
	p.set_ylabel('Time')
	# p.loglog()
	p.legend()
	tight_layout()
	savefig('AES.png')

if __name__ == '__main__':
	np.set_printoptions(suppress=True, precision=6)
	lengths = 2 ** np.arange(10, 27)
	draw(lengths, 
		np.array([0.000042, 0.00007,  0.000135, 0.000597, 0.001008, 0.001583, 0.002078, 0.003434, 0.006661, 0.012918, 0.025619, 0.051024, 0.101693, 0.203135, 0.406457, 0.814705, 1.644701]),
		np.array([0.000044, 0.000163, 0.000178, 0.000722, 0.000846, 0.001475, 0.002863, 0.003979, 0.007735, 0.015403, 0.030612, 0.061112, 0.121336, 0.24296, 0.485726, 0.971193, 1.942015]),
		np.array([0.000062, 0.000218, 0.000311, 0.000866, 0.000926, 0.002804, 0.003353, 0.005029, 0.009039, 0.017751, 0.035523, 0.070484, 0.140922, 0.281624, 0.563629, 1.126613, 2.255])
		)
	lengths = [16384, 10485760]
	print(lengths)
	cnt = int(sys.argv[-1])
	for method in [128, 192, 256]:
		result = []
		for length in lengths:
			s = 0
			for i in range(cnt):
				s += test(method, length)
			s /= cnt
			result.append(s)
		print(method, np.array(result))

# [    1024     2048     4096     8192    16384    32768    65536   131072
#    262144   524288  1048576  2097152  4194304  8388608 16777216 33554432
#  67108864]
# 128 [0.000042 0.00007  0.000135 0.000597 0.001008 0.001583 0.002078 0.003434
#  0.006661 0.012918 0.025619 0.051024 0.101693 0.203135 0.406457 0.814705
#  1.644701]
# 192 [0.000044 0.000163 0.000178 0.000722 0.000846 0.001475 0.002863 0.003979
#  0.007735 0.015403 0.030612 0.061112 0.121336 0.24296  0.485726 0.971193
#  1.942015]
# 256 [0.000062 0.000218 0.000311 0.000866 0.000926 0.002804 0.003353 0.005029
#  0.009039 0.017751 0.035523 0.070484 0.140922 0.281624 0.563629 1.126613
#  2.255   ]

# [    1024     2048     4096     8192    16384    32768    65536   131072
#    262144   524288  1048576  2097152  4194304  8388608 16777216 33554432
#  67108864]
# 128 [0.000069 0.000105 0.000124 0.000206 0.000409 0.000813 0.001624 0.003248
#  0.006487 0.012965 0.025928 0.051656 0.102907 0.205018 0.410802 0.820433
#  1.637919]
# 192 [0.000063 0.000135 0.000288 0.000585 0.001161 0.001802 0.002463 0.003996
#  0.007819 0.01534  0.030624 0.061172 0.122678 0.245187 0.489606 0.979193
#  1.96162 ]
# 256 [0.000036 0.000072 0.000142 0.000282 0.000565 0.001127 0.002254 0.004504
#  0.008996 0.017995 0.035923 0.071466 0.142658 0.284698 0.569603 1.139418
#  2.276553]

# p.loglog() # [    1024     2048     4096     8192    16384    32768    65536   131072 #    262144   524288  1048576  2097152  4194304  8388608 16777216 33554432 #  67108864] # 128 [0.000042 0.00007  0.000135 0.000597 0.001008 0.001583 0.002078 0.003434 #  0.006661 0.012918 0.025619 0.051024 0.101693 0.203135 0.406457 0.814705 #  1.644701] # 192 [0.000044 0.000163 0.000178 0.000722 0.000846 0.001475 0.002863 0.003979 #  0.007735 0.015403 0.030612 0.061112 0.121336 0.24296  0.485726 0.971193 #  1.942015] # 256 [0.000062 0.000218 0.000311 0.000866 0.000926 0.002804 0.003353 0.005029 #  0.009039 0.017751 0.035523 0.070484 0.140922 0.281624 0.563629 1.126613 #  2.255   ] # [    1024     2048     4096     8192    16384    32768    65536   131072 #    262144   524288  1048576  2097152  4194304  8388608 16777216 33554432 #  67108864] # 128 [0.000069 0.000105 0.000124 0.000206 0.000409 0.000813 0.001624 0.003248 #  0.006487 0.012965 0.025928 0.051656 0.102907 0.205018 0.410802 0.820433 #  1.637919] # 192 [0.000063 0.000135 0.000288 0.000585 0.001161 0.001802 0.002463 0.003996 #  0.007819 0.01534  0.030624 0.061172 0.122678 0.245187 0.489606 0.979193 #  1.96162 ] # 256 [0.000036 0.000072 0.000142 0.000282 0.000565 0.001127 0.002254 0.004504 #  0.008996 0.017995 0.035923 0.071466 0.142658 0.284698 0.569603 1.139418 #  2.276553]