-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.py
More file actions
62 lines (55 loc) · 1.32 KB
/
crypto.py
File metadata and controls
62 lines (55 loc) · 1.32 KB
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
'''
Crypto ex sheet 4 number 3.3
'''
n = 4
# write function for sample space generation
def sampleSpaceGeneration(size):
S = []
if size == 0:
raise Exception("N needs to be higher than 0")
if size == 1:
S = ['0','1']
return S
smallerSet = sampleSpaceGeneration(size-1)
for elem in smallerSet:
S.append(elem+'0')
S.append(elem+'1')
return S
#print(sampleSpaceGeneration(n))
def hardCoreBit(s,x):
bit = 0
for i in range(len(s)):
X = int("" + x[i])
S = int("" + s[i])
b = X & S
bit = b ^ bit
return str(bit)
# write a function for goldreich-levin hardcore bit
# build sample space {0,1}^n+1
BiggerSet = sampleSpaceGeneration(n+1)
print("{0,1}^n+1")
print(BiggerSet)
# build sample space s = {0,1}^n
smallSSet = sampleSpaceGeneration(n)
print("s set")
print(smallSSet)
#build distribution X
def buildDistributionX(n):
xleftDistribution = sampleSpaceGeneration(n/2)
X = []
for x in xleftDistribution:
X.append(x+'1'*(int)(n/2))
return X
D = buildDistributionX(n)
print("Distribution x")
print(D)
# build sample space S3
def buildS3(S,D):
NewSet = []
for s in S:
for x in D:
NewSet.append(s+hardCoreBit(s,x))
return NewSet
S3 = buildS3(smallSSet,D)
print(S3)
# count the frequencies