-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular_pattern_unif_test.py
99 lines (76 loc) · 3.12 KB
/
regular_pattern_unif_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 9 13:11:41 2023
@author: Mieszko Ferens
Script to run simulation for evaluating uniformity of Binary-coded with
Padding (BP) CRP subsets.
"""
import argparse
import pandas as pd
from pathlib import Path
import numpy as np
from pypuf.simulation import XORArbiterPUF
class ChallengeResponseSet():
def __init__(self, n, challenges, responses):
self.challenge_length = n
self.challenges = challenges
self.responses = np.expand_dims(
np.expand_dims(responses,axis=1),axis=1)
def create_binary_code_challenges(n, N):
n_bits = 16
lsb = np.arange(2**n_bits, dtype=np.uint8).reshape(-1,1)
msb = lsb.copy()
msb.sort(axis=0)
lsb = np.unpackbits(lsb, axis=1)[:,-8:].copy()
msb = np.unpackbits(msb, axis=1)[:,-8:].copy()
challenges = 2*np.concatenate((msb,lsb), axis=1, dtype=np.int8) - 1
for i in range(int(np.sqrt(n/n_bits))):
challenges = np.insert(
challenges, range(1,((2**i)*n_bits)+1), -1, axis=1)
shift = challenges.copy()
for i in range(1, int(n/n_bits)):
challenges = np.append(challenges, np.roll(shift, i, axis=1), axis=0)
_ , idx = np.unique(challenges, return_index=True, axis=0)
challenges = challenges[np.sort(idx)]
assert N <= len(challenges), (
"Not enough CRPs have been generated. The limit is 2^18 - 3 CRPs.")
challenges = challenges[:N]
return challenges
def main():
# Set-up logging
import logging
logging.basicConfig(level=logging.DEBUG)
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("--outdir", type=str, default="./Results/")
parser.add_argument("--seed", type=int, default=0, help="Random seed.")
parser.add_argument("--n-bits", type=int, default=64,
help="Challenge length in bits.")
parser.add_argument("--k", type=int, default=1,
help="The number of parallel APUF in the XOR PUF.")
parser.add_argument("--n-CRPs", type=int, default=2**16,
help="Number of CRPs to be generated.")
args = parser.parse_args()
# Generate the PUF
puf = XORArbiterPUF(args.n_bits, args.k, args.seed)
# Generate the challenges
challenges = create_binary_code_challenges(n=args.n_bits, N=args.n_CRPs)
# Get responses
responses = puf.eval(challenges)
# Calculate uniformity of responses
uniformity = responses.mean()
# Log data into csv format
data = pd.DataFrame({"seed": [args.seed],
"n_bits": [args.n_bits],
"k": [args.k],
"n_CRPs": [args.n_CRPs],
"uniformity": [uniformity]})
filepath = Path(args.outdir + "out_unif_regular_pattern_" + str(args.k) +
"XOR.csv")
if(filepath.is_file()):
data.to_csv(filepath, header=False, index=False, mode='a')
else:
filepath.parent.mkdir(parents=True, exist_ok=True)
data.to_csv(filepath, header=True, index=False, mode='a')
if(__name__ == "__main__"):
main()