This commit is contained in:
xenia 2021-04-25 05:16:33 -04:00
parent 42e0cf6a0e
commit 9876dcff1f
1 changed files with 20 additions and 2 deletions

View File

@ -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)