TRNG done

This commit is contained in:
Hladu357 2024-12-09 18:00:42 +01:00
parent f15cc5c838
commit 667efc212c
4 changed files with 624 additions and 22 deletions

View File

@ -0,0 +1,96 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 73,
"id": "5a87ea11-1c00-4097-a5b3-aa56cc33b7fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[44 44 45 ... 43 44 44]\n",
" [44 45 45 ... 45 45 45]\n",
" [44 45 45 ... 44 44 44]\n",
" ...\n",
" [45 45 45 ... 46 45 46]\n",
" [44 44 45 ... 45 45 45]\n",
" [45 46 45 ... 45 45 46]]\n",
"(128, 750000)\n",
"[59867 59631 64874 55734 51531 64641 63078 56084 62793 54154 50676 62981\n",
" 55136 62640 58096 48365 62697 51194 54140 57243 59968 63117 45133 61542\n",
" 59420 59012 61305 63438 58405 57121 62630 56219 54454 50854 65426 51115\n",
" 47308 42314 54576 63390 62825 51672 51684 60670 51151 49354 51373 61212\n",
" 61200 49430 64257 58178 63377 62001 57011 64714 64740 54700 56772 63292\n",
" 57779 59093 61834 61733]\n",
"True\n",
"\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"TRNG_PAIR_CNT = 64\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" # reading info file - length of trace, sampling frequency (not necessary to know in our case), random value generated by the TRNG\n",
" with open(\"data_info.txt\", \"r\") as fin:\n",
" tracelen = int(fin.readline())\n",
" fs = int(fin.readline())\n",
" trng_val = fin.readline()\n",
"\n",
" traces = np.fromfile(\"data.bin\", dtype='uint8') # reading traces for individual ROs\n",
" traces = np.reshape(traces, (traces.size//tracelen, tracelen)) # reshape of matrix, each row contains the trace for one RO\n",
"\n",
" traces_bin = traces > 128 # conversion of waveforms to rectangles - everything below threshold is 0, otherwise 1 (they are boolean values actually)\n",
" print(traces)\n",
" print(traces_bin.shape)\n",
" rising_edges = np.logical_not(traces_bin[:,:-1]) & traces_bin[:,1:] # finding rising edges, each rising edge is represented by True\n",
"\n",
" cnt = np.count_nonzero(rising_edges, axis=1) # count the number of rising edges in rows\n",
" \n",
" # cnt is now a 1D vector\n",
" cnt = cnt.reshape(TRNG_PAIR_CNT,2).min(axis=1) # Reshape of the count array into matrix, where each row contains 2 values - the number of rising edges for two ROs in a pair. Then we select the smaller value.\n",
" \n",
" cnt_sel = cnt & 0b00000011 # select only the two least significant bits\n",
"\n",
" estimate = ''.join([np.binary_repr(x, width=2) for x in cnt_sel]) # binary representation of the values (the last 2 bits) and joining them into one string\n",
" estimate = '{0:0>32x}'.format(int(estimate, 2))\n",
" # from data_info, output of the RNG in FPGA\n",
" print(trng_val[:-1] == estimate)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f8447e8-39c3-4706-902e-3970459ce515",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

15
lab04-06_AES_PC/tabulka Normal file
View File

@ -0,0 +1,15 @@
100 000 cycles:
implementation: OpenSSL Naive (4) With macro (5a) With T-Box (5b) Intrinsics (6)
total time: 6.74264ms 23.6295ms 23.494ms 8.70669ms 1.18704ms
avg time: 67.4264ns 236.295ns 234.94ns 87.0669ns 11.8704ns
avg CPU cycles: 255 896 891 330 45
1 000 000 cycles:
implementation: OpenSSL Naive (4) With macro (5a) With T-Box (5b) Intrinsics (6)
total time: 65.397ms 23.6295ms 233.56ms 88.0696ms 11.8984ms
avg time: 65.397ns 236.295ns 233.56ns 88.0696ns 11.8984ns
avg CPU cycles: 248 896 885 334 45

File diff suppressed because one or more lines are too long

394
lab09_PUF/puf.ipynb Normal file
View File

@ -0,0 +1,394 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"executionInfo": {
"elapsed": 212,
"status": "ok",
"timestamp": 1733157570419,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "qIH9E-egIPJk"
},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"\n",
"if not os.path.exists(\"chip1.bin\"):\n",
" !wget https://users.fit.cvut.cz/bucekj/SRAM_data.zip\n",
" !unzip SRAM_data.zip"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"executionInfo": {
"elapsed": 340,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "c6CaoHF7IVcf"
},
"outputs": [],
"source": [
"MEASUREMENTS = 1000\n",
"MEM_SIZE = 512*8\n",
"CHIPS = 10\n",
"\n",
"memories = np.array([\n",
" np.unpackbits(\n",
" np.fromfile(f\"chip{i+1}.bin\", dtype='uint8')\n",
" ).reshape(MEASUREMENTS, MEM_SIZE)\n",
" for i in range(CHIPS)\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 8,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "IVARKdgpMOgI",
"outputId": "1eb61f33-4c4d-46a4-aed1-ae467881f4de"
},
"outputs": [
{
"data": {
"text/plain": [
"(10, 1000, 4096)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memories.shape\n",
"# cip, mereni, bit"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "Iba9M10hIZql"
},
"outputs": [],
"source": [
"# Create reference responses\n",
"avgs = memories.mean(axis=1, keepdims=True)\n",
"refs = avgs>0.5 # reference responses"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "0elUoltMIbZW"
},
"outputs": [],
"source": [
"xors = refs ^ memories # HDintra\n",
" # finish HDintra calculation (look at the definition)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "2orRDbEnIdKh"
},
"outputs": [],
"source": [
"idxs = np.triu_indices(CHIPS, 1)\n",
"pairwise_xors = refs[idxs[0]] ^ refs[idxs[1]]\n",
"inter = pairwise_xors.mean()\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "M4maPUbhIfF2"
},
"outputs": [],
"source": [
"# Create masks\n",
"e = 0.05 # error rate into the mask (5%)\n",
"masks = (avgs > (1-e)) | (avgs < e)\n",
"masks = masks[:,0,:]\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "JQaOfv7Jin6S"
},
"outputs": [],
"source": [
"# Save masks into file\n",
"np.savetxt('masks.txt', masks.astype(int), fmt='%d', delimiter='')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "qsDh1_3DImCW"
},
"outputs": [],
"source": [
"# Create masked responses (1024 bits long)\n",
"mask_idx = np.array([np.nonzero(masks[i])[0][:1024] for i in range(CHIPS)])\n",
"memories_masked = np.array([memories[i][:,mask_idx[i]] for i in range(CHIPS)])\n",
"refs_masked = np.array([refs[i][:,mask_idx[i]] for i in range(CHIPS)])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "IhsqiP0WIqTe"
},
"outputs": [],
"source": [
"# Compute HDintra of masked PUF responses\n",
"masked_xors = refs_masked ^ memories_masked # HDintra\n",
" # finish HDintra calculation (look at the definition)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1733157570996,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "S8omYwr1It2_"
},
"outputs": [],
"source": [
"# Compute HDinter of masked PUF responses\n",
"idxs = np.triu_indices(CHIPS, 1)\n",
"pairwise_xors = refs_masked[idxs[0]] ^ refs_masked[idxs[1]]\n",
"masked_inter = pairwise_xors.mean()\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 297,
"status": "ok",
"timestamp": 1733157721196,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "jB0oHd-Mjkbx",
"outputId": "75f811c4-4a3f-420a-8bb0-3a9b54bb94a9"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vzdalenosti jednotlivych cipu: [0.03431421 0.02955688 0.0316062 0.03096167 0.0320459 0.02866528\n",
" 0.0269895 0.03107129 0.03059814 0.02904199]\n",
"\n",
"HDintra prumerny: 0.030485107421875\n",
"HDinter: 0.43573133680555554\n",
"\n",
"vyhovujicich bitu je 0.8744384765625 %\n",
"\n",
"Masked vzdalenosti jednotlivych cipu: [0.00191406 0.0017832 0.00191211 0.00174609 0.00198633 0.00169141\n",
" 0.0015 0.00189844 0.00163672 0.00171484]\n",
"\n",
"Masked HDintra prumerny: 0.0017783203125\n",
"Masked HDinter: 0.4957682291666667\n"
]
}
],
"source": [
"# Print a summary of results (HDintra, HDinter - for both unmasked and masked PUF responses)\n",
"print(\"Vzdalenosti jednotlivych cipu:\", xors.mean(axis=1).mean(axis=1))\n",
"print()\n",
"\n",
"print(\"HDintra prumerny:\", xors.mean())\n",
"print(\"HDinter: \", inter)\n",
"print()\n",
"\n",
"print(\"vyhovujicich bitu je \", masks.mean(), \"%\")\n",
"print()\n",
"\n",
"print(\"Masked vzdalenosti jednotlivych cipu: \", masked_xors.mean(axis=1).mean(axis=1))\n",
"print()\n",
"\n",
"print(\"Masked HDintra prumerny:\", masked_xors.mean())\n",
"\n",
"print(\"Masked HDinter: \", masked_inter)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"executionInfo": {
"elapsed": 2,
"status": "ok",
"timestamp": 1733157571185,
"user": {
"displayName": "Ondřej Hladůvka",
"userId": "07082375608847075000"
},
"user_tz": -60
},
"id": "ZFVMMBQ8PFBc"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": [
{
"file_id": "https://gist.github.com/kodytfil/ca2be4dfc2413f2335451994308fb3c5#file-puf_template-ipynb",
"timestamp": 1733152912568
}
]
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 4
}