]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/optimize_shift.rb
new and more compact aes
[avr-crypto-lib.git] / host / optimize_shift.rb
1 #!/usr/bin/ruby 
2 # shavs_test.rb
3 =begin
4     This file is part of the AVR-Crypto-Lib.
5     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
6
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 =end
20
21 shift_values = [ 5, 36, 13, 58, 26, 53, 11, 59, 
22                 56, 28, 46, 44, 20, 35, 42, 50,
23                
24                             38, 48, 34, 26, 33, 39, 29, 33, 
25                 30, 20, 14, 12, 49, 27, 26, 51,
26                 50, 43, 15, 58,  8, 41, 11, 39,
27                 53, 31, 27,  7, 42, 14,  9, 35,
28                
29                             55, 25, 33, 34, 28, 17, 58, 47,
30                 43, 25,  8, 43,  7,  6,  7, 49,
31                 37, 46, 18, 25, 47, 18, 32, 27,
32                 40, 13, 57, 60, 48, 25, 45, 58,
33                 16, 14, 21, 44, 51, 43, 19, 37,
34                 22, 13, 12,  9,  9, 42, 18, 48,
35                 38, 52, 32, 59, 35, 40,  2, 53,
36                 12, 57, 54, 34, 41, 15, 56, 56 ]
37
38 def transform_shift(value)
39   byteshift = (value+3)/8
40   singleshift = value%8
41   if singleshift>4
42  #   byteshift += 1
43         singleshift -= 8
44   end
45   return [singleshift, byteshift]
46 end
47
48 def transform_singleshift(value)
49   if(value>=0)
50     return value
51   end
52   return 0x08+(value*-1)
53 end
54
55 bs_hist = Hash.new
56 bs_hist.default = 0
57 ss_hist = Hash.new
58 ss_hist.default = 0
59 shift_values.each{|v|
60   
61   a = transform_shift(v)
62   printf("%2d = %2d * 8 %+2d\n", v, a[1], a[0])
63   bs_hist[a[1]] += 1
64   ss_hist[a[0]] += 1
65 }
66
67 puts("byteshift histogram:")
68 for i in 0..7
69   printf("%d: %4d\n", i, bs_hist[i])
70  end
71
72 puts("singleshift histogram:")
73 for i in -3..4
74   printf("%+d: %4d\n", i, ss_hist[i])
75  end
76
77 puts "\ntransformed:"
78 (0..shift_values.length-1).each{|i|
79   puts " for 256 bit:" if i==0
80   puts " for 512 bit:" if i==16
81   puts " for 1024 bit:" if i==16+32
82   
83   a = transform_shift(shift_values[i])
84   a[0] = transform_singleshift(a[0])
85   printf("0x%01x%01x, ", a[1], a[0])
86   puts("") if (i%8==7)
87 }
88
89
90 puts "\ntransformed (decryption):"
91 (0..shift_values.length-1).each{|i|
92   puts " for 256 bit:" if i==0
93   puts " for 512 bit:" if i==16
94   puts " for 1024 bit:" if i==16+32
95   
96   a = transform_shift(shift_values[(i/8)*8+7-(i%8)])
97   a[0] = transform_singleshift(a[0])
98   printf("0x%01x%01x, ", a[1], a[0])
99   puts("") if (i%8==7)
100 }