-
Notifications
You must be signed in to change notification settings - Fork 1
/
giftTb.py
302 lines (232 loc) · 9.34 KB
/
giftTb.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from pathlib import Path
import random
from random import randint
import sys
import os
import inspect
import itertools
from cocotb.handle import HierarchyObject
from functools import partial
from pprint import pprint
import cocotb
pprint = partial(pprint, sort_dicts=False)
SCRIPT_DIR = os.path.realpath(os.path.dirname(
inspect.getfile(inspect.currentframe())))
COCOLIGHT_DIR = os.path.dirname(SCRIPT_DIR)
sys.path.append(COCOLIGHT_DIR)
sys.path.append(SCRIPT_DIR)
# seed = random.randrange(sys.maxsize)
# random = random.Random(seed)
# print("Python random seed is:", seed)
try:
from .cocolight.lwc_api import LwcCffi, LwcAead, LwcHash
from .cocolight import LwcRefCheckerTb
except:
from cocolight.lwc_api import LwcCffi, LwcAead, LwcHash
from cocolight import LwcRefCheckerTb
class Cref(LwcCffi, LwcAead, LwcHash):
""" Python wrapper for C-Reference implementation """
def __init__(self, aead_algorithm, hash_algorithm=None, root_cref_dir=Path(SCRIPT_DIR) / 'cref') -> None:
Cref.aead_algorithm = aead_algorithm
Cref.hash_algorithm = hash_algorithm
Cref.root_cref_dir = root_cref_dir
LwcCffi.__init__(self, force_recompile=True)
ref = Cref(aead_algorithm='giftcofb128v1')
block_bits = dict(AD=128, PT=128)
block_bits['XT'] = block_bits['CT'] = block_bits['PT']
XT_BS = block_bits['PT'] // 8
AD_BS = block_bits['AD'] // 8
HM_BS = block_bits.get('HM',0) // 8
def short_sizes(bs):
return [0, 1, 3, bs - 1, bs, bs + 1, 2 * bs - 1, 2 * bs, 2 * bs + 1]
class RefCheckerTb(LwcRefCheckerTb):
def __init__(self, dut: HierarchyObject, debug=False, max_in_stalls=0, max_out_stalls=0, min_out_stalls=None) -> None:
LwcRefCheckerTb.__init__(self, dut, ref, debug=debug, max_in_stalls=max_in_stalls,
max_out_stalls=max_out_stalls, min_out_stalls=min_out_stalls, supports_hash=ref.hash_algorithm is not None)
@cocotb.test()
async def debug_enc(dut: HierarchyObject):
debug = False
max_in_stalls = 0
min_out_stalls = 0
max_out_stalls = 0
tb = RefCheckerTb(dut, debug=debug, max_in_stalls=max_in_stalls,
max_out_stalls=max_out_stalls, min_out_stalls=min_out_stalls)
await tb.start()
await tb.xenc_test(ad_size=1, pt_size=3)
await tb.xenc_test(ad_size=1, pt_size=3)
await tb.xenc_test(ad_size=1, pt_size=0)
await tb.xenc_test(ad_size=0, pt_size=1)
await tb.xenc_test(ad_size=1, pt_size=1)
await tb.xenc_test(ad_size=1, pt_size=4)
await tb.xenc_test(ad_size=0, pt_size=15)
await tb.xenc_test(ad_size=1, pt_size=4)
await tb.xenc_test(ad_size=0, pt_size=XT_BS)
await tb.xenc_test(ad_size=AD_BS, pt_size=0)
await tb.xenc_test(ad_size=0, pt_size=4)
await tb.xenc_test(ad_size=9, pt_size=9)
await tb.xenc_test(ad_size=4, pt_size=24)
await tb.xenc_test(ad_size=3, pt_size=2)
await tb.xenc_test(ad_size=0, pt_size=2)
await tb.xenc_test(ad_size=0, pt_size=0)
await tb.xenc_test(ad_size=44, pt_size=32)
await tb.xenc_test(ad_size=45, pt_size=0)
await tb.xenc_test(ad_size=44, pt_size=0)
await tb.xenc_test(ad_size=0, pt_size=45)
await tb.xenc_test(ad_size=65, pt_size=65)
await tb.xenc_test(ad_size=1, pt_size=0)
await tb.xenc_test(ad_size=AD_BS + 1, pt_size=2*XT_BS + 1)
await tb.xenc_test(ad_size=0, pt_size=XT_BS + 1)
await tb.xenc_test(ad_size=0, pt_size=0)
await tb.xenc_test(ad_size=AD_BS+1, pt_size=XT_BS+1)
await tb.launch_monitors()
await tb.launch_drivers()
await tb.join_drivers(20000)
await tb.join_monitors(20000)
@cocotb.test()
async def debug_dec(dut: HierarchyObject):
debug = True
max_stalls = 10
tb = RefCheckerTb(dut, debug=debug, max_in_stalls=max_stalls,
max_out_stalls=max_stalls)
await tb.start()
await tb.xdec_test(ad_size=4, ct_size=4)
await tb.xdec_test(ad_size=4, ct_size=0)
await tb.xdec_test(ad_size=0, ct_size=4)
await tb.xdec_test(ad_size=0, ct_size=0)
await tb.xdec_test(ad_size=45, ct_size=0)
await tb.xdec_test(ad_size=44, ct_size=0)
await tb.xdec_test(ad_size=1, ct_size=3)
await tb.xdec_test(ad_size=65, ct_size=65)
await tb.launch_monitors()
await tb.launch_drivers()
await tb.join_drivers(20000)
await tb.join_monitors(20000)
@cocotb.test()
async def debug_hash(dut: HierarchyObject):
debug = True
max_stalls = 0
tb = RefCheckerTb(dut, debug=debug, max_in_stalls=max_stalls,
max_out_stalls=max_stalls)
if not tb.supports_hash:
return
await tb.start()
# await tb.xhash_test(15)
# await tb.xhash_test(16)
await tb.xhash_test(32)
# await tb.xhash_test(99)
await tb.launch_monitors()
await tb.launch_drivers()
await tb.join_drivers(10000)
await tb.join_monitors(10000)
@cocotb.test()
async def blanket_test_simple(dut: HierarchyObject):
debug = os.environ.get('DEBUG', False)
try:
debug = bool(int(debug))
except:
debug = bool(debug)
print(f'DEBUG={debug}')
max_in_stalls = 2
max_out_stalls = 12
min_out_stalls = None
tb = RefCheckerTb(dut, debug=debug, max_in_stalls=max_in_stalls,
max_out_stalls=max_out_stalls, min_out_stalls=min_out_stalls)
await tb.start()
for ad_size, xt_size in itertools.product(short_sizes(AD_BS), short_sizes(XT_BS)):
await tb.xdec_test(ad_size=ad_size, ct_size=xt_size)
print(f"ad_size={ad_size} pt_size={xt_size}")
await tb.xenc_test(ad_size=ad_size, pt_size=xt_size)
# if tb.supports_hash:
# await tb.xhash_test(xt_size)
await tb.xdec_test(ad_size=1536, ct_size=0)
await tb.xenc_test(ad_size=1536, pt_size=0)
await tb.xdec_test(ad_size=0, ct_size=1536)
await tb.xenc_test(ad_size=0, pt_size=1536)
await tb.xdec_test(ad_size=0, ct_size=1535)
await tb.launch_monitors()
await tb.launch_drivers()
await tb.join_drivers()
await tb.join_monitors()
@cocotb.test()
async def randomized_tests(dut: HierarchyObject):
debug = os.environ.get('DEBUG', False)
# debug = True
try:
debug = bool(int(debug))
except:
debug = bool(debug)
print(f'DEBUG={debug}')
tb = RefCheckerTb(dut, debug=debug, max_in_stalls=5, max_out_stalls=5)
ad_sizes = [0, 1, 15, 16, 17, 23, 24, 25, 31, 32, 33, 43, 44, 45, 47, 48, 49, 61, 64, 65, 67, 3*AD_BS - 1 ] + \
[randint(2, 500) for _ in range(30)] + short_sizes(AD_BS)
ad_sizes = list(set(ad_sizes)) # unique
random.shuffle(ad_sizes)
xt_sizes = ad_sizes[:] + short_sizes(XT_BS)
random.shuffle(xt_sizes)
await tb.start()
for ad_size, xt_size in itertools.product(ad_sizes, xt_sizes):
op = randint(0, 2 if tb.supports_hash else 1)
if (op == 0):
await tb.xenc_test(ad_size=ad_size, pt_size=xt_size)
elif (op == 1):
await tb.xdec_test(ad_size=ad_size, ct_size=xt_size)
else:
await tb.xhash_test(hm_size=xt_size)
await tb.launch_monitors()
await tb.launch_drivers()
await tb.join_drivers()
await tb.join_monitors()
@cocotb.test()
async def measure_timings(dut: HierarchyObject, supportsHash=False):
debug = False
max_stalls = 0 # for timing measurements
tb = RefCheckerTb(
dut, debug=debug, max_in_stalls=max_stalls, max_out_stalls=max_stalls)
await tb.start()
all_results = {}
sizes = [16, 64, 1536]
for op in ['enc', 'dec']:
results = {}
bt = 'AD'
for sz in sizes:
cycles = await tb.measure_op(dict(op=op, ad_size=sz, xt_size=0))
results[f'{bt} {sz}'] = cycles
for x in [4, 5]:
cycles = await tb.measure_op(dict(op=op, ad_size=x*block_bits[bt]//8, xt_size=0))
results[f'{bt} {x}BS'] = cycles
results[f'{bt} Long'] = results[f'{bt} 5BS'] - results[f'{bt} 4BS']
bt = 'XT'
for sz in sizes:
cycles = await tb.measure_op(dict(op=op, ad_size=0, xt_size=sz))
results[f'{bt} {sz}'] = cycles
for x in [4, 5]:
cycles = await tb.measure_op(dict(op=op, ad_size=0, xt_size=x*block_bits[bt]//8))
results[f'{bt} {x}BS'] = cycles
results[f'{bt} Long'] = results[f'{bt} 5BS'] - results[f'{bt} 4BS']
bt = 'AD+XT'
for sz in sizes:
cycles = await tb.measure_op(dict(op=op, ad_size=sz, xt_size=sz))
# print(f'{op} PT={sz} AD=0: {cycles}')
results[f'{bt} {sz}'] = cycles
for x in [4, 5]:
cycles = await tb.measure_op(dict(op=op, ad_size=x*block_bits['AD']//8, xt_size=x*block_bits['XT']//8))
# print(f'{op} PT={sz} AD=0: {cycles}')
results[f'{bt} {x}BS'] = cycles
results[f'{bt} Long'] = results[f'{bt} 5BS'] - results[f'{bt} 4BS']
all_results[op] = results
if supportsHash:
results = {}
op = 'hash'
bt = 'HM'
for sz in sizes:
cycles = await tb.measure_op(dict(op=op, hm_size=sz))
# print(f'hash HM={sz}: {cycles}')
results[f'{bt} {sz}'] = cycles
for x in [4, 5]:
cycles = await tb.measure_op(dict(op=op, hm_size=x*block_bits[bt]))
results[f'{bt} {x}BS'] = cycles
results[f'{bt} Long'] = results[f'{bt} 5BS'] - results[f'{bt} 4BS']
all_results[op] = results
pprint(all_results)
if __name__ == "__main__":
print("should be run as a cocotb module")