-
Notifications
You must be signed in to change notification settings - Fork 4
/
blum_whitener_2.py
73 lines (60 loc) · 1.78 KB
/
blum_whitener_2.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
#!/usr/bin/env python
import random
import sys
# Tuple of (state number, output value, destination state,
# destination probability, other destination state)
states = [0,(1, 0, 2, 0.4, 1),(2, 1, 3, 0.1, 1),(3,1,4,0.3,2),(4,0,3,0.2,4)]
state = 1
out_value = 0
out_string = ""
index = 0
count = 0
ic = 2**16 # input count
# State bits can be L, 0, 1, H or T.
# Initiatize all states to L
state_bits = ['L' for x in states]
i = 1
for j in xrange(ic):
# Step 1
s = ""
for st in state_bits:
s = s+str(st)
if (state_bits[i] == 'H') or (state_bits[i] == 'T'):
if (state_bits[i] == 'H'):
out_value = out_value + (0x01 << index)
index += 1
elif (state_bits[i] == 'T'):
index += 1
state_bits[i] = 'L'
count += 1
if index == 8:
out_string += "%02x" % out_value
out_value = 0
index = 0
if len(out_string) == 64:
print out_string
out_string = ""
# Step 2
# Run the Markov Chain one step to get 1 or 0
(s,ov,dest1,dest_prob1,dest2) = states[i]
if random.SystemRandom().random() < dest_prob1:
next_state = dest1
else:
next_state = dest2
(s,ov,dest1,dest_prob2,dest2) = states[next_state]
exit_bit = ov
# Step 3
s = str(state_bits[i])
if state_bits[i] == 'L':
state_bits[i] = exit_bit
elif (state_bits[i] == 0) or (state_bits[i] == 1):
if exit_bit == state_bits[i]:
state_bits[i] = 'L'
else:
if state_bits[i] == 1 and exit_bit == 0:
state_bits[i] = 'H'
else:
state_bits[i] = 'T'
# Step 4
i = next_state
print >> sys.stderr, "Bits in:",ic,", Bits out:",count," Ratio:",(float(count)/ic)