{ "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 }