Commit 25170877 authored by Luc Libralesso's avatar Luc Libralesso
Browse files

update cryptodag + add graphviz_atomic visualization

parent c3528057
......@@ -138,5 +138,16 @@ class CryptoDag
@nodes = nodes
end
## returns operators used by the cryptodag
# @returns [Vec<CryptoOperator>]
def operators()
return @nodes.map{|n| n.operators}.reduce([], :+)
end
## returns a list of atoms used by the cryptodag
# @returns [Vec<CryptoAtom=Variable>]
def atoms()
return operators().map{|op| op.inputs.flatten + op.outputs.flatten}.uniq.flatten
end
end
\ No newline at end of file
#!/usr/bin/ruby
require_relative "../../cryptodag.rb"
require_relative "../../cryptosystems/midori128/midori128.rb"
require_relative "../../writers/graphviz_atomic.rb"
require "minitest/autorun"
# require "minitest/color"
require "pry"
class TestWriterGraphviz < Minitest::Unit::TestCase
def test_a()
dag = Midori128_Dag.new(nb_rounds=3)
# p get_graphviz(dag)
writefile_graphviz(dag, "test.dot")
assert_equal(true,true)
end
end
\ No newline at end of file
require_relative '../cryptodag.rb'
require_relative '../operators/elementary.rb'
def get_shorthand_op_classname(c)
case c.class.name
when "XorOperator"
return "+"
when "SOperator"
return "S"
when "EqualityOperator"
return "="
when "MixColumnsOperator"
return "MC"
else
# p c.class
return "?"
end
end
def get_graphviz(dag)
res = ""
res += "digraph {\n"
# digraph options
res += "\tgraph [rankdir=LR];\n"
res += "\tnode [fontname=\"helvetica\"];\n"
res += "\n"
# display operators
op_to_id = {}
id_to_op = {}
operators = dag.operators()
id = 0
operators.each do |op|
op_to_id[op] = id
id_to_op[id] = op
res += "\top_#{id} [shape=circle,label=\"#{get_shorthand_op_classname(op)}\"];\n"
id += 1
end
# display atoms
atom_to_id = {}
id_to_atom = {}
id = 0
atoms = dag.atoms()
atoms.each do |atom|
if !atom_to_id.key?(atom)
atom_to_id[atom] = id
id_to_atom[id] = atom
res += "\tatom_#{id} [shape=box,label=\"#{atom.name}\"];\n"
id += 1
end
end
# display links between operators and atoms
operators.each do |op|
op.inputs.each do |atom|
res += "\tatom_#{atom_to_id[atom]} -> op_#{op_to_id[op]};\n"
end
op.outputs.each do |atom|
res += "\top_#{op_to_id[op]} -> atom_#{atom_to_id[atom]};\n"
end
end
# p operators.map{|op|op.class.name}
# # display vertices
# vertices.each() do |e|
# case e.class.name
# when "InputNode"
# res += "\t#{e.name}[color=pink];\n"
# when "ConstantNode"
# res += "\t#{e.name}[color=palegreen];\n"
# when "OutputNode"
# res += "\t#{e.name}[color=skyblue];\n"
# else
# res += "\t#{e.name};\n"
# end
# end
# res += "\n"
# # display arcs
# vertices.each() do |v|
# v.inputs.each() do |u|
# res += "\t" + u.name() + " -> " + v.name() + "\n"
# end
# end
res += "}\n"
end
def writefile_graphviz(dag, filename)
file = File.open(filename, "w")
file.puts(get_graphviz(dag))
file.close
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment