Commit 4bffb6b5 authored by Luc Libralesso's avatar Luc Libralesso
Browse files

add abstraction writer

parent 25170877
......@@ -147,7 +147,7 @@ class CryptoDag
## 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
return operators().map{|op| op.inputs.flatten + op.outputs.flatten}.flatten.uniq
end
end
\ No newline at end of file
#!/usr/bin/ruby
require_relative "../../cryptodag.rb"
require_relative "../../cryptosystems/midori128/midori128.rb"
require_relative "../../writers/abstract_constraints_atomic.rb"
require_relative "../../backends/minizinc"
require "minitest/autorun"
# require "minitest/color"
require "pry"
class TestWriterGraphviz < Minitest::Unit::TestCase
def test_a()
dag = Midori128_Dag.new(nb_rounds=3)
model = get_abstract_constraint_model(dag)
file = File.open("test.mzn", "w")
file.puts(Minizinc.new.generate_code(model))
file.close
assert_equal(true,true)
end
end
\ No newline at end of file
require_relative '../cryptodag.rb'
require_relative '../operators/elementary.rb'
require_relative '../constraints/all.rb'
def get_abstract_constraint_model(dag)
atoms = dag.atoms()
operators = dag.operators()
# create model
model = Model.new()
variable_dict = {} # Atom -> Variable
# add atoms as booleans
atoms.each do |atom|
var = Variable.new(atom.name, 0..1, atom.value)
variable_dict[atom] = var
model.add_variables(var)
end
# add constraints
operators.each do |op|
case op.class.name
when "XorOperator"
model.add_constraints(Diff1.new(
*op.inputs.map {|atom| variable_dict[atom]},
*op.outputs.map {|atom| variable_dict[atom]}
))
when "SOperator"
model.add_constraints(Equality.new(
*op.inputs.map {|atom| variable_dict[atom]},
*op.outputs.map {|atom| variable_dict[atom]}
))
when "EqualityOperator"
model.add_constraints(Equality.new(
*op.inputs.map {|atom| variable_dict[atom]},
*op.outputs.map {|atom| variable_dict[atom]}
))
when "MixColumnsOperator"
model.add_constraints(IncludedIn.new(
Sum.new(
*op.inputs.map {|atom| variable_dict[atom]},
*op.outputs.map {|atom| variable_dict[atom]}
),
Set[0,4,5,6,7,8]
))
model.add_constraints(Equivalence.new(
Equals.new(Sum.new(*op.inputs.map {|atom| variable_dict[atom]}), 0),
Equals.new(Sum.new(*op.outputs.map {|atom| variable_dict[atom]}), 0)
))
else
# p c.class
raise "gen_abstract_constraint_model: unknown operator: #{op.class.name}"
end
end
# add objective function
s_operators = operators.filter {|op| op.class <= SOperator}
obj = Variable.new("obj", 1..20)
model.add_variables(obj)
model.add_constraints(
Equality.new(
obj,
Sum.new(*s_operators.map{|op| variable_dict[op.outputs[0]]})
)
)
model.add_instructions(Instruction.new("solve minimize obj"))
return model
end
\ No newline at end of file
......@@ -58,29 +58,6 @@ def get_graphviz(dag)
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
......
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