Created
April 16, 2018 00:04
-
-
Save back-seat-driver/82572f6b36e9cb16f58716a7ce9f858e to your computer and use it in GitHub Desktop.
S-Box Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#forward_build | |
set_0=['00000','00101','01011','01010', | |
'10110','10111','10001','10100', | |
'01101','01000','01110','01111', | |
'00011','00010','01100','01001', | |
'11010','11101','10011','10000', | |
'11100','11111','11001','11110', | |
'00110','00001','00111','00100', | |
'11000','11011','10101','10010'] | |
#forward_search | |
set_1=['00000','00001','00011','00010', | |
'00110','00111','00101','00100', | |
'01100','01101','01111','01110', | |
'01010','01011','01001','01000', | |
'11000','11001','11011','11010', | |
'11110','11111','11101','11100', | |
'10100','10101','10111','10110', | |
'10010','10011','10001','10000'] | |
#reverse_build <---forward_search | |
#reverse_search <---forward_build | |
def bin_n_bit(dec,n): | |
return(str(format(dec,'0'+n+'b'))) | |
def rc_con(sub_set): | |
to_return=[] | |
for i in range(len(sub_set[0])): | |
insert='' | |
for x in range(len(sub_set)): | |
insert+=sub_set[x][i] | |
to_return.append(insert) | |
return(to_return) | |
def sub_string_star(a_input,to_pop): | |
#Where to_pop is the number of end lanes to be trimmed, and a_input is | |
#generated sbox set mapping. Will be used in furture for sbox proof. | |
to_return=[] | |
for i in range(len(a_input)): | |
to_return.append(a_input[i][0:5-to_pop]+'*'*(to_pop)) | |
return(to_return) | |
def reverse_set_test_return(forward_build_canidate,forward_search_canidate): | |
#forward_build_canidate=set_0 | |
#forward_search_canidate=set_1 | |
main=[] | |
for i in range(1,5): | |
search_set=sub_string_star(forward_build_canidate,i) | |
insert_0=[] | |
for x in range(32): | |
to_hold=bin_n_bit(x,'5')[0:5-i]+'*'*i | |
insert_1=[] | |
for z in range(len(search_set)): | |
if search_set[z]==to_hold: | |
insert_1.append(forward_search_canidate[z]) | |
insert_0.append(insert_1) | |
main.append(insert_0) | |
#to_print(main) | |
return(main) | |
def forward_set_test_return(forward_build_canidate): | |
main=[] | |
for i in range(1,5): | |
search_set=sub_string_star(forward_build_canidate,i) | |
insert_0=[] | |
for x in range(32): | |
to_hold=bin_n_bit(x,'5')[0:5-i]+'*'*i | |
insert_1=[] | |
for z in range(len(search_set)): | |
if search_set[z]==to_hold: | |
insert_1.append(to_hold) | |
insert_0.append(insert_1) | |
main.append(insert_0) | |
return(main) | |
def probability_return(possible_value_set): | |
a_index_set=rc_con(possible_value_set) | |
dem=len(a_index_set[0]) | |
to_return=[] | |
for i in range(len(a_index_set)): | |
x=a_index_set[i].count('1')#Position zero '1' probability | |
y=a_index_set[i].count('0')#Position one '0' probability | |
x_0=float(x)/float(dem) | |
y_0=float(y)/float(dem) | |
to_return.append([x_0,y_0,i]) | |
return(to_return) | |
''' | |
test_0 | |
''' | |
def test_0(): | |
to_return=[] | |
var_0=forward_set_test_return(set_0) | |
for i in range(len(var_0)): | |
insert=[] | |
for x in range(len(var_0[i])): | |
insert.append(probability_return(var_0[i][x])) | |
to_return.append(insert) | |
for i in range(len(to_return)): | |
for x in range(len(to_return[i])): | |
print(to_return[i][x]) | |
print('b') | |
''' | |
test_1 | |
''' | |
def test_1(): | |
#Note that based on printed sets, | |
#the Order should be | |
to_return=[] | |
var_0=reverse_set_test_return(set_0,set_1) | |
for i in range(len(var_0)): | |
insert=[] | |
for x in range(len(var_0[i])): | |
insert.append(probability_return(var_0[i][x])) | |
to_return.append(insert) | |
for i in range(len(to_return)): | |
for x in range(len(to_return[i])): | |
print(to_return[i][x]) | |
print('b') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment