Skip to content

Instantly share code, notes, and snippets.

@avescodes
Created November 12, 2009 00:55
Show Gist options
  • Save avescodes/232485 to your computer and use it in GitHub Desktop.
Save avescodes/232485 to your computer and use it in GitHub Desktop.
SBox = Hash[ (0..15).to_a.zip( %w[E 2 1 3 D 9 0 6 F 4 5 A 8 C 7 B].map {|n| n.to_i(16)}) ]
# Find all pairs x and x* in delta(x')
def delta_x(xor)
(0..15).map {|x|[x, (0..15).select { |x_star| x ^ x_star == xor }[0]] }
end
# Let's also make iterating over every index [x,y] easier...
class Array
def each_xy
(0...self.size).each {|x| (0...self[x].size).each {|y| yield x,y }}
end
end
if __FILE__ == $0
ddt = Array.new(16) { Array.new(16,0) }
ddt.each_xy do |a,b|
delta_x(a).each {|x,x_st| ddt[a][b] += 1 if (SBox[x] ^ SBox[x_st] == b)}
end
# Table now completed... That was easy.
printf(" " + "%3d"*16+"\n", *((0..15).to_a))
printf(" " + "---"*16+"\n")
ddt.each_with_index do |row,i|
printf("%2d |"+"%3d"*16+"\n",i, *row )
end
end
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# ------------------------------------------------
# 0 | 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 1 | 0 0 2 0 4 0 2 0 0 0 0 2 4 0 0 2
# 2 | 0 2 0 0 0 0 0 2 0 0 2 0 0 2 2 6
# 3 | 0 2 0 4 0 2 0 0 0 2 0 4 0 2 0 0
# 4 | 0 4 2 2 0 2 0 2 2 0 0 2 0 0 0 0
# 5 | 0 0 0 4 0 0 0 4 0 0 0 0 2 2 2 2
# 6 | 0 0 0 0 2 0 2 0 2 0 2 0 2 2 2 2
# 7 | 0 0 4 2 2 0 0 0 4 2 0 0 0 0 2 0
# 8 | 0 2 0 0 2 4 2 2 0 2 0 0 0 2 0 0
# 9 | 0 6 0 0 0 0 2 0 0 0 2 4 0 2 0 0
# 10 | 0 0 2 0 0 0 0 2 4 0 4 2 0 0 2 0
# 11 | 0 0 0 0 2 2 2 2 0 0 0 0 4 0 4 0
# 12 | 0 0 2 0 0 2 4 0 2 0 0 0 2 2 2 0
# 13 | 0 0 2 2 2 0 2 0 0 2 6 0 0 0 0 0
# 14 | 0 0 2 2 0 0 0 0 2 6 0 0 0 0 0 4
# 15 | 0 0 0 0 2 4 0 2 0 2 0 2 2 2 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment