In [2]:
import random
import time
import numpy as np
from indices import Indices
from indices_group import Indices_Group
from map import Map
import itertools
from functions2 import quadruplet_metric, pair_metric3, triplet_metric2, pair_metric4, rm_duplicate_ciphertexts3
from functions2 import pair_metric, cycle, cycle_list, rm_duplicate_arrays, cycle_list_gen, pair_metric2, swap, swap_list, triplet_metric, swap_list_gen
In [ ]:
metric_results = np.zeros(len(self.ciphertexts))
for i, text in enumerate(self.ciphertexts):
metric = metric_function(text, self.natural_text)
metric_results[i] = metric
ranking = np.argsort(metric_results)
ranked_texts = [self.ciphertexts[x] for x in ranking]
filtered_ranked_texts = rm_duplicate_ciphertexts(ranked_texts, number_retained)
self.ciphertexts = filtered_ranked_texts
In [5]:
metric_results = np.zeros(len(trial_texts))
for i, text in enumerate(trial_texts):
metric = pair_metric(text, natural_text)
metric_results[i] = metric
ranking = np.argsort(metric_results)
ranked_texts = [trial_texts[x] for x in ranking]
In [6]:
filtered_ranked_texts = rm_duplicate_ciphertexts3(ranked_texts, 10)
In [23]:
len(metric_results)
Out[23]:
In [3]:
with open('message4.txt','r') as source:
message_text = source.read()
message = Indices()
message.text_in(message_text)
original = Indices()
original.text_in(message_text)
print message.text_out()
message.randomise()
print message.text_out()
with open('sample.txt','r') as source:
sample_text = source.read()
natural_text = Indices()
natural_text.text_in(sample_text)
natural_text.group_frequencies(1)
natural_text.pair_frequencies()
message.group_frequencies(1)
freq_attempt = Map()
freq_attempt.freq_key(message, natural_text)
identity = np.arange(27)
decryption_attempt = grand_permute(identity, message.rates[0])
decryption_attempt = [substitute(map,freq_attempt.map) for map in decryption_attempt]
trial_texts = [message.map(map) for map in decryption_attempt]
group = Indices_Group(trial_texts,natural_text)
natural_text.pair_frequencies()
natural_text.triplet_frequencies()
zero_map = Map(np.arange(27))
cycle_maps = zero_map.proliferate([cycle_list_gen(i) for i in range(2,5)]) + [np.arange(27)]
swap_maps = zero_map.proliferate([swap_list_gen(3)])
d_swap_maps = zero_map.proliferate([swap_list_gen(15)])
In [4]:
len(decryption_attempt)
Out[4]:
In [4]:
#initial discrimination
for i in range(1):
group.rank(pair_metric,10)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
In [6]:
backup_group = group
a1 = group
a2 = group
a3 = group
In [50]:
[i, j] = random.sample([1,2,3,4,5],2)
In [51]:
i, j
Out[51]:
In [41]:
for i in range(1):
a1.proliferate(d_swap_maps,1)
a1.rank(quadruplet_metric,1)
print a1.ciphertexts[0].text_out()
print a1.ciphertexts[0].map_record
print pair_metric3(a1.ciphertexts[0],natural_text)
In [39]:
for i in range(5):
a2.proliferate(d_swap_maps,1)
a2.rank(triplet_metric,1)
print a2.ciphertexts[0].text_out()
print a2.ciphertexts[0].map_record
print pair_metric3(a2.ciphertexts[0],natural_text)
In [19]:
print len(group.ciphertexts)
print pair_metric3(original,natural_text)
print pair_metric3(group.ciphertexts[0],natural_text)
print pair_metric3(group.ciphertexts[1],natural_text)
print freq_attempt.map
repeat_indices(message.rates[0])
In [11]:
for i in range(25):
group.proliferate(d_swap_maps,1)
group.rank(triplet_metric,1)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
print pair_metric3(group.ciphertexts[0],natural_text)
In [55]:
my_map = np.arange(27)
my_map[15] = 3
my_map[3] = 15
trial1 = my_trial.map(my_map)
print trial1.text_out()
print pair_metric3(trial1,natural_text)
print pair_metric3(my_trial,natural_text)
a = (swap_maps == my_map)
In [ ]:
In [ ]:
for i in range(10):
group.proliferate(d_swap_maps,20)
group.rank(triplet_metric,20)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
print pair_metric3(group.ciphertexts[0],natural_text)
In [24]:
for i in range(1):
group.proliferate(swap_maps,10)
group.rank(quadruplet_metric,10)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
print pair_metric3(group.ciphertexts[0],natural_text)
In [4]:
#generate Indices object for Indices_Group
with open('message.txt','r') as source:
original_text = source.read()
original = Indices()
original.text_in(original_text)
with open('sample.txt','r') as source:
sample_text = source.read()
natural_text = Indices()
natural_text.text_in(sample_text)
natural_text.group_frequencies(1)
freq_attempt = Map()
decryption_attempts = []
for i in range(100):
with open('message.txt','r') as source:
message_text = source.read()
message = Indices()
message.text_in(message_text)
message.randomise()
freq_attempt.freq_key(message, natural_text)
decryption_attempt = message.map(freq_attempt.map)
decryption_attempts.append(decryption_attempt.map_record)
decryption_attempts = rm_duplicate_arrays(decryption_attempts)
decryption_attempts = [original.map(x) for x in decryption_attempts]
group = Indices_Group(decryption_attempts,natural_text)
In [ ]:
In [30]:
#GENEREATE MAPS
with open('sample.txt','r') as source:
sample_text = source.read()
natural_text = Indices()
natural_text.text_in(sample_text)
natural_text.group_frequencies(1)
freq_attempt = Map()
decryption_attempts = []
for i in range(100):
with open('message.txt','r') as source:
message_text = source.read()
message = Indices()
message.text_in(message_text)
message.randomise()
freq_attempt.freq_key(message, natural_text)
decryption_attempt = message.map(freq_attempt.map)
decryption_attempts.append(decryption_attempt.map_record)
decryption_attempts = rm_duplicate_arrays(decryption_attempts)
In [ ]:
In [ ]:
In [68]:
range(-2,5)
Out[68]:
In [ ]:
In [23]:
natural_text.pair_frequencies()
for i in range(20):
group.proliferate(swap_maps,20)
group.rank(pair_metric,20)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
In [30]:
natural_text.triplet_frequencies()
for i in range(1):
group.proliferate(d_swap_maps,10)
group.rank(triplet_metric,10)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
In [31]:
natural_text.group_frequencies(4)
for i in range(1):
group.proliferate(d_swap_maps,10)
group.rank(triplet_metric,10)
print group.ciphertexts[0].text_out()
print group.ciphertexts[0].map_record
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [3]:
ciphertexts = [sample2 for i in range(1)]
ciphertexts[0].randomise()
natural_sample.pair_frequencies()
group = Indices_Group(ciphertexts, natural_sample)
In [5]:
%%timeit
group.ranking(pair_metric)
In [39]:
import numpy as np
values = np.array([1,2,3,1,2,4,5,6,3,2,1])
searchval = 3
ii = np.where(values == searchval)[0]
In [40]:
ii
Out[40]:
In [127]:
rates2 = original.rates[0]
print rates2
In [130]:
rates[1] = 1.5
In [97]:
repeated_elements
Out[97]:
In [131]:
repeat_indices(rates)
Out[131]:
In [1]:
def repeat_indices(rates):
added = []
indices = []
repeated_elements = []
for x in rates:
if (x not in added and x !=0):
added.append(x)
indices.append(np.where(rates==x)[0])
for x in indices:
if len(x) != 1:
repeated_elements.append(x)
return repeated_elements
def perm_maps(indices):
perms = itertools.permutations(indices)
subs = [np.array([indices,x]) for x in perms]
maps = [sub_to_map(sub) for sub in subs]
return maps
def sub_to_map(sub):
map = np.arange(27)
for i in range(sub[0].size):
map[sub[0][i]] = sub[1][i]
return map
def substitute(map, s_map):
new_map = np.copy(map)
for i, x in enumerate(new_map):
new_map[i] = s_map[x]
return new_map
def permute(old_maps, indices):
shuffle_maps = perm_maps(indices)
permuted_maps = []
for o_map in old_maps:
for s_map in shuffle_maps:
permuted_maps.append(substitute(o_map, s_map))
return permuted_maps
def grand_permute(map,rates):
maps = [map]
indices = repeat_indices(rates)
for index_set in indices:
maps = permute(maps, index_set)
return maps
In [134]:
rates = np.array([1,1,2,3,2,2])
map = np.arange(6)
new_maps = grand_permute(map,rates)
In [126]:
perms = itertools.permutations(indices)
subs = [np.array([indices,x]) for x in perms]
maps = [sub_to_map(sub) for sub in subs]
In [128]:
sub = np.array([[0,1],[0,1]])
sub_to_map(sub)
Out[128]:
In [129]:
def sub_to_map(sub):
map = np.arange(27)
for i in range(sub[0].size):
map[sub[0][i]] = sub[1][i]
return map
In [1]:
from ciphertext import Ciphertext
In [52]:
import random
In [55]:
i.shape
Out[55]:
In [73]:
i = np.array([[random.random() for x in range(27)] for y in range(27)])
j = np.array([[random.random() for x in range(27)] for y in range(27)])
im = np.matrix(i)
jm = np.matrix(j)
In [10]:
import random
import numpy as np
l = 27
r = 300
g = np.array([[random.random() for x in range(l)] for y in range(l)])
#g = np.array([[1,2],[3,4]])
k = np.array([[[random.random() for x in range(l)] for y in range(l)] for z in range(r)])
kl = np.array([g for z in range(r)])
km = np.array([[[random.random() for x in range(l)]for y in range(l)] for z in range(r)])
In [231]:
%%timeit
a = np.dot(kl,g)
In [12]:
%%timeit
b = [np.dot(kl[x,:,:],g) for x in range(kl.shape[0])]
In [233]:
%%timeit
a = np.dot(km,g)
In [234]:
%%timeit
b = [np.dot(km[x,:,:],g) for x in range(kl.shape[0])]
In [2]:
from pair_rates import Pair_Rates
pr = Pair_Rates(g,np.arange(27))
In [11]:
%%timeit
a = [pr.map(x) for x in km]
In [7]:
g
Out[7]:
In [62]:
random.shuffle(i)
In [8]:
for x in km:
print x
In [78]:
a = np.array([[1,2],[3,4]])
In [79]:
np.dot(a,np.arange(2))
Out[79]:
In [ ]: