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)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-51f6c09ae358> in <module>()
----> 1 filtered_ranked_texts = rm_duplicate_ciphertexts3(ranked_texts, 10)

C:\Users\Paul Brookes\Documents\GitHub\subcipher\functions2.py in rm_duplicate_ciphertexts3(ciphertexts, number_retained)
     90     filtered_ciphertexts = []
     91     for i, x in enumerate(ciphertexts):
---> 92         map = x.tolist()
     93         if map not in added:
     94             number_added += 1

AttributeError: 'Indices' object has no attribute 'tolist'

In [23]:
len(metric_results)


Out[23]:
181440

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)])


no king of the isles had ever needed a hand
qjnpwqhnjknifenwdgednfcyneuesnqeeyeyncnfcqy
map.py:22: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if np.all(ciphertext.rates[0] == None):
map.py:24: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if np.all(natural_sample.rates[0] == None):

In [4]:
len(decryption_attempt)


Out[4]:
181440

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


ts urtl si woe rngen oah emed teeheh a oath
[ 0  1 15  3  7  9  2  6  4 10  8 17 16 23 22 26  5 11 18 12 24 14 13 21 20
 25 19]

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]:
(3, 5)

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)


no wing of the isles had ever needed a hand
[ 0  1  2  3  4  5  6  7  8  9 10 11 14 23 12 26 16 17 13 22 24 21 15 18 20
 25 19]
-0.0114242902507

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)


no wing of the ildel has ever neeses a hans
[ 0  1  2  3  4  5  6 11  8  9  7 10 14 23 12 26 16 17 13 22 24 21 15 18 20
 25 19]
-0.010345812444

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])


1
-0.0110863323333
-0.0102638565802
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-19-8047908cbc46> in <module>()
      2 print pair_metric3(original,natural_text)
      3 print pair_metric3(group.ciphertexts[0],natural_text)
----> 4 print pair_metric3(group.ciphertexts[1],natural_text)
      5 print freq_attempt.map
      6 repeat_indices(message.rates[0])

IndexError: list index out of range

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)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-3cf846ca732b> in <module>()
      1 for i in range(25):
      2     group.proliferate(d_swap_maps,1)
----> 3     group.rank(triplet_metric,1)
      4 print group.ciphertexts[0].text_out()
      5 print group.ciphertexts[0].map_record

C:\Users\Paul Brookes\Documents\GitHub\subcipher\indices_group.py in rank(self, metric_function, number_retained)
     17         metric_results = np.zeros(len(self.ciphertexts))
     18         for i, text in enumerate(self.ciphertexts):
---> 19             metric = metric_function(text, self.natural_text)
     20             metric_results[i] = metric
     21         ranking = np.argsort(metric_results)

C:\Users\Paul Brookes\Documents\GitHub\subcipher\functions2.pyc in triplet_metric(ciphertext, natural_text)
     40 def triplet_metric(ciphertext, natural_text):
     41     ciphertext.triplet_frequencies()
---> 42     difference = abs(natural_text.rates[2] - ciphertext.rates[2])
     43     difference = np.absolute(difference) + 1e-10
     44     metric = -np.sum(1/difference)

TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

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)


the wooren watghtowey was the tallest thind this sire om the countains yisind twentk meet afove the fiddest sentinels anr solriey bines in the suyyounrind woors 
-0.0112277546073
-0.0109555000068

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)


to wath on lie arger isp eved teepep s istp
[ 0  1 11  7  4  3  2  9  5 10 19 17 20 18 14 26  6  8 23 22 25 21 15 24 12
 16 13]
-0.00903424200113

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)


the oiiren outghtioew ous the tullest thand thas sare im the kifntuans wasand toenty meet ucive the caddest sentanels unr silraew banes an the sfwwifnrand oiirs 
[ 0  1  2 13  5  3  6  7  8 15  9 11 17 16 22  4 14 10 18 20 12 21 19 26 23
 25 24]
-0.0106195897286

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]:
[-2, -1, 0, 1, 2, 3, 4]

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


the woonei watchtowed was the tarrest thuig thus sune of the politauis dusuig tweity feet abome the buggest seituiers ain sornued quies ui the slddolinuig woons 
[ 0  1  2  3  4 13  5  7  8 10  6  9 12 11 19 15 16 17 18 25 20 14 23 26 22
 21 24]

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


the woomer witchtowel wis the tinnest tharp thas same of the bourtiars lasarp twerty feet igode the gappest sertarens irm sonmael vares ar the sullourmarp wooms 
[ 0  1  2  5  4  3  9  7  8 11 14  6 12 13 20 15 16 19 18 21 17 10 23 26 25
 24 22]

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


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-cb41c1e673e0> in <module>()
----> 1 natural_text.group_frequencies(4)
      2 for i in range(1):
      3     group.proliferate(d_swap_maps,10)
      4     group.rank(triplet_metric,10)
      5 print group.ciphertexts[0].text_out()

C:\Users\Paul Brookes\Documents\GitHub\subcipher\indices.pyc in group_frequencies(self, number)
     73             counts[indices] += 1
     74         rates = counts/(len(self.text_indices)+1-number)
---> 75         self.rates[number-1] = rates
     76         return None
     77 

IndexError: list assignment index out of range

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)


10000 loops, best of 3: 125 µs per loop

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]:
array([2, 8], dtype=int64)

In [127]:
rates2 = original.rates[0]
print rates2


[ 0.1552795   0.11180124  0.10559006  0.03726708  0.0621118   0.07453416
  0.08074534  0.08074534  0.04968944  0.0310559   0.03726708  0.02484472
  0.00621118  0.01863354  0.00621118  0.03726708  0.01242236  0.0310559
  0.00621118  0.00621118  0.01242236  0.00621118  0.          0.          0.
  0.          0.        ]

In [130]:
rates[1] = 1.5

In [97]:
repeated_elements


Out[97]:
[array([ 3, 10, 15], dtype=int64),
 array([6, 7], dtype=int64),
 array([ 9, 17], dtype=int64),
 array([12, 14, 18, 19, 21], dtype=int64),
 array([16, 20], dtype=int64)]

In [131]:
repeat_indices(rates)


Out[131]:
[array([0, 1], dtype=int64)]

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]:
array([ 1,  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])

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


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-62ee3e009425> in <module>()
----> 1 from ciphertext import Ciphertext

ImportError: No module named ciphertext

In [52]:
import random

In [55]:
i.shape


Out[55]:
(27L, 27L)

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)


100 loops, best of 3: 12 ms per loop

In [12]:
%%timeit
b = [np.dot(kl[x,:,:],g) for x in range(kl.shape[0])]


1000 loops, best of 3: 1.43 ms per loop

In [233]:
%%timeit
a = np.dot(km,g)


100 loops, best of 3: 13.4 ms per loop

In [234]:
%%timeit
b = [np.dot(km[x,:,:],g) for x in range(kl.shape[0])]


1000 loops, best of 3: 1.55 ms per loop

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]


100 loops, best of 3: 3.52 ms per loop

In [7]:
g


Out[7]:
array([[ 0.44276747,  0.44066084],
       [ 0.71265887,  0.15427131]])

In [62]:
random.shuffle(i)

In [8]:
for x in km:
    print x


[[ 0.76585478  0.58817315]
 [ 0.21885781  0.91437784]]
[[ 0.83987738  0.64150724]
 [ 0.07810611  0.19571165]]
[[ 0.62682537  0.19537886]
 [ 0.88531907  0.69661736]]

In [78]:
a = np.array([[1,2],[3,4]])

In [79]:
np.dot(a,np.arange(2))


Out[79]:
array([2, 4])

In [ ]: