diff --git a/cw/tvla.py b/cw/tvla.py index bf67e107..b3111c12 100755 --- a/cw/tvla.py +++ b/cw/tvla.py @@ -817,12 +817,12 @@ def run_tvla(ctx: typer.Context): if save_to_disk_ttest: if num_steps > 1: log.info("Saving T-test Step") - np.savez('tmp/ttest-step.npy', - ttest_step=ttest_step, - trace_end_vec=trace_end_vec, - rnd_list=rnd_list, - byte_list=byte_list, - single_trace=traces[1]) + np.savez_compressed('tmp/ttest-step.npy', + ttest_step=ttest_step, + trace_end_vec=trace_end_vec, + rnd_list=rnd_list, + byte_list=byte_list, + single_trace=traces[1]) else: log.info("Saving T-test") np.save('tmp/ttest.npy', ttest_trace) diff --git a/test/data/tvla_general/ttest-step-golden.npy.npz b/test/data/tvla_general/ttest-step-golden.npy.npz new file mode 100644 index 00000000..0e0e8ea4 --- /dev/null +++ b/test/data/tvla_general/ttest-step-golden.npy.npz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a8a5464d66ea197c1677ad6d049fc155a453ede90d0853c3a05dc3107d7ba2e +size 456902 diff --git a/test/tvla_test.py b/test/tvla_test.py index 4ab135a9..b24f10df 100644 --- a/test/tvla_test.py +++ b/test/tvla_test.py @@ -28,12 +28,34 @@ def ttest_significant(ttest_trace) -> bool: return np.any(abs_max > threshold) +def ttest_compare_results(expected, received, delta) -> bool: + """ + Determine if the numerical values of two ttest traces match. + + Checks if all nan values are in the same positions in both arrays. + Checks if all numerical values match within precision delta. + """ + nan_match = np.all(np.isnan(expected) == np.isnan(received)) + numbers_match = np.all(np.logical_or(np.isnan(expected), np.abs(expected - received) < delta)) + return nan_match and numbers_match + + def test_general_nonleaking_project(): project_path = TestDataPath('tvla_general/ci_opentitan_simple_kmac.cwp') tvla = TvlaCmd(Args(['--project-file', str(project_path), - '--mode', 'kmac', '--save-to-disk-ttest', 'run-tvla'])).run() - assert not ttest_significant(np.load('tmp/ttest.npy')), ( - f"{tvla} did find significant leakage, which is unexpected") + '--mode', 'kmac', '--save-to-disk-ttest', + '--number-of-steps', '10', 'run-tvla'])).run() + expected_path = TestDataPath('tvla_general/ttest-step-golden.npy.npz') + expected_file = np.load(str(expected_path)) + expected_trace = expected_file['ttest_step'] + received_file = np.load('tmp/ttest-step.npy.npz') + received_trace = received_file['ttest_step'] + # Expected and received traces should be equal within precision delta. + # Small mismatch is possible due to the differences in floating point operations + # on different machines. + delta = 0.001 + assert ttest_compare_results(expected_trace, received_trace, delta), ( + f"{tvla} generated ttest_step values that don't match the expected ones") def test_general_leaking_histogram():