-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbenchmark.py
75 lines (60 loc) · 1.92 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
import time
import random
import string
import argparse
import ffx
from ffx import FFXInteger
from six.moves import range
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--radix", type=int, default=2)
parser.add_argument("--tweaksize", type=int, default=8)
parser.add_argument("--messagesize", type=int, default=32)
parser.add_argument("--trials", type=int, default=10, required=False)
args = parser.parse_args()
radix = args.radix
tweaksize = args.tweaksize
messagesize = args.messagesize
trials = args.trials
keysize = 128
K = random.randint(0, 2 ** keysize - 1)
K = FFXInteger(K, radix=2, blocksize=keysize)
print(
'RADIX={}, TWEAKSIZE={}, MESSAGESIZE={}, KEY={:x}'.format(
radix,
tweaksize,
messagesize,
K.to_int()
)
)
ffx_obj = ffx.new(K.to_bytes(), radix)
for i in range(1, trials):
T = random.randint(0, radix ** tweaksize - 1)
T = FFXInteger(T, radix=radix, blocksize=tweaksize)
M1 = random.randint(0, radix ** messagesize - 1)
M1 = FFXInteger(M1, radix=radix, blocksize=messagesize)
start = time.time()
C = ffx_obj.encrypt(T, M1)
encrypt_cost = time.time() - start
encrypt_cost *= 1000.0
start = time.time()
M2 = ffx_obj.decrypt(T, C)
decrypt_cost = time.time() - start
decrypt_cost *= 1000.0
assert M1 == M2
print(
'test #{} SUCCESS: (encrypt_cost={} ms, decrypt_cost={} ms, tweak={}, plaintext={}, ciphertext={})'.format(
i,
round(encrypt_cost, 1),
round(decrypt_cost, 1),
T,
M1,
C,
)
)
if __name__ == "__main__":
main()