diff --git a/sca/ascad/attack.py b/sca/ascad/attack.py index fd217b7..d539562 100644 --- a/sca/ascad/attack.py +++ b/sca/ascad/attack.py @@ -91,8 +91,26 @@ def main(db): model[:, i] = AES_SBOX[model[:, i] ^ pt_v] ^ mask_v model = numpy_popcount.popcount(model).astype("double") - input_matrix = numpy.vstack((model, traces.transpose())) - coefs = numpy.corrcoef(input_matrix)[0:256, 256:] + # O - (n,t) array of n traces with t samples each + # P - (n,m) array of n predictions for each of the m candidates + # returns an (m,t) correaltion matrix of m traces t samples each + def corr_submatrix(O, P): + O -= numpy.mean(O, axis=0) + P -= numpy.mean(P, axis=0) + + numerator = numpy.einsum("nm,nt->mt", P, O, optimize='optimal') + denominator = numpy.sqrt(numpy.outer((P * P).sum(axis=0), (O * O).sum(axis=0))) + + return numerator / denominator + + # boring, slow, uses too much RAM + # input_matrix = numpy.vstack((model, traces.transpose())) + # coefs = numpy.corrcoef(input_matrix)[0:256, 256:] + + # uwu + coefs = corr_submatrix(traces, model.transpose()) + + # plot absmax coefs = numpy.abs(coefs) max_by_key = numpy.max(coefs, axis=1) plt.plot(max_by_key)