-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA-256-PROBABILITIES.py
162 lines (147 loc) · 8.72 KB
/
SHA-256-PROBABILITIES.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#SHA-256 basic, last 3 operation functions altered to handle probabilities instead of binaries (0.56=56% chance of being 1, 44% change of being 0)
#Sadly it all tends towards 0.5, as in with random inputs it will all be 0.5 less than a quarter of the way through.
#Sadly, could not generate hash collisions using this method. Its predictive powers are 0% better than random :(
#main function, takes array of size 512 of numbers 0<=x<=1
def generate_hash(inp):
if len(inp)!=512:
return 'BAD_LENGTH'
input=[]
for x in range(512):
input.append(inp[x])
for x in range(512):
if input[x]<0 or input[x]>1:
return 'BAD_VALUE_AT_INDEX: '+str(x)
K = [[0,1,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1,1,1,0,0,1,1,0,0,0],[0,1,1,1,0,0,0,1,0,0,1,1,0,1,1,1,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1],
[1,0,1,1,0,1,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1],[1,1,1,0,1,0,0,1,1,0,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0,1,0,0,1,0,1],
[0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,0,1,1,0,0,0,0,1,0,0,1,0,1,1,0,1,1],[0,1,0,1,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1,1,1,1,1,1,1,1,0,1,0,1,0,0,1,1,1,0,1,0]
h4 = [0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,1,0,0,1,1,1,1,1,1,1]
h5 = [1,0,0,1,1,0,1,1,0,0,0,0,0,1,0,1,0,1,1,0,1,0,0,0,1,0,0,0,1,1,0,0]
h6 = [0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,1,1,0,0,1,1,0,1,0,1,0,1,1]
h7 = [0,1,0,1,1,0,1,1,1,1,1,0,0,0,0,0,1,1,0,0,1,1,0,1,0,0,0,1,1,0,0,1]
# Prepare message schedule
W = []
for i in range(0, 64):
if i <= 15:
W.append(input[i*32:(i*32)+32])
else:
s0=xor(xor(rightRotate(W[i-15],7),rightRotate(W[i-15],18)),rightShift(W[i-15],3))
s1=xor(xor(rightRotate(W[i-2],17),rightRotate(W[i-2],19)),rightShift(W[i-2],10))
W.append(plus(plus(plus(W[i-16],s0),W[i-7]),s1))
a = h0
b = h1
c = h2
d = h3
e = h4
f = h5
g = h6
h = h7
for i in range(64):
S1=xor(xor(rightRotate(e,6),rightRotate(e,11)),rightRotate(e,25))
ch=xor(ander(e,f),ander(noter(e),g))
temp1=plus(plus(plus(plus(h,S1),ch),K[i]),W[i])
S0=xor(xor(rightRotate(a,2),rightRotate(a,13)),rightRotate(a,22))
maj=xor(xor(ander(a,b),ander(a,c)),ander(b,c))
temp2=plus(S0,maj)
h = g
g = f
f = e
e = plus(d,temp1)
d = c
c = b
b = a
a = plus(temp1,temp2)
h0 = plus(h0,a)
h1 = plus(h1,b)
h2 = plus(h2,c)
h3 = plus(h3,d)
h4 = plus(h4,e)
h5 = plus(h5,f)
h6 = plus(h6,g)
h7 = plus(h7,h)
return h0+h1+h2+h3+h4+h5+h6+h7
#basic rotation
def rightRotate(arr,dist):
ret=[]
for x in range (32):
ret.append(arr[(x-dist)%32])
return ret
#shift over, leave 0's in wake
def rightShift(arr,dist):
ret=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for x in range (dist,32):
ret[x]=arr[x-dist]
return ret
#invert every value
def noter(arr):
ret=[]
for x in range (32):
ret.append(1-arr[x])
return ret
#apply probabilistic xor to every pair of bits
def xor(arr1,arr2):
ret=[]
for x in range (32):
ret.append((arr1[x]*(1-arr2[x]))+((1-arr1[x])*arr2[x]))
return ret
#apply probabilistic and to every pair of bits
def ander(arr1,arr2):
ret=[]
for x in range (32):
ret.append(arr1[x]*arr2[x])
return ret
#binary probabilistic addition, discard overflow
def plus(arr1,arr2):
ret=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
carry=0
for x in range(31, -1, -1):
r = carry+arr1[x]+arr2[x] #P(ret[x]=1) is based on arr1[x], arr2[x], carry. This gets P(exactly 1/2/3 of them are "1")
P1=(carry*(1-arr1[x])*(1-arr2[x]))+((1-carry)*arr1[x]*(1-arr2[x]))+((1-carry)*(1-arr1[x])*arr2[x])
P2=(carry*arr1[x]*(1-arr2[x]))+(carry*(1-arr1[x])*arr2[x])+((1-carry)*arr1[x]*arr2[x])
P3=carry*arr1[x]*arr2[x]
ret[x] = P1+P3
carry = P2+P3
return ret
if __name__ == "__main__":
hello_world=[0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,1,0,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,0,1,0,0]
hello_world.append(1)
while(len(hello_world)<512-8):
hello_world.append(0)
hello_world+=[0,1,0,1,1,0,0,0]
print(generate_hash(hello_world))
hold=[]
for x in range(512):
hold.append(random.random())
print(hash(hold))