Commit bee1340d8005aeb7d8a4ab8367f98ddc1c10d135

Authored by Brice Colombier
0 parents
Exists in master

Initial commit

Showing 8 changed files with 307 additions and 0 deletions

... ... @@ -0,0 +1 @@
  1 +*.pyc
0 2 \ No newline at end of file
heuristic_approximated_current_flow_betweenness.py View file @ bee1340
... ... @@ -0,0 +1,80 @@
  1 +import collections as col
  2 +import numpy as np
  3 +import networkx as nx
  4 +
  5 +def heuristic_approximated_current_flow_betweenness(g_nx, key_size):
  6 + try:
  7 + print("Trying option 1 (fastest)")
  8 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  9 + normalized = False,
  10 + solver = "full",
  11 + dtype = np.float32,
  12 + epsilon = 1.0).items(), key=lambda t: t[1])))[-key_size:]]
  13 + except:
  14 + try:
  15 + print("Trying option 1 (fastest)")
  16 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  17 + normalized = False,
  18 + solver = "full",
  19 + dtype = np.float32,
  20 + epsilon = 5.0).items(), key=lambda t: t[1])))[-key_size:]]
  21 + except:
  22 + try:
  23 + print("Trying option 1 (fastest)")
  24 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  25 + normalized = False,
  26 + solver = "full",
  27 + dtype = np.float32,
  28 + epsilon = 10.0).items(), key=lambda t: t[1])))[-key_size:]]
  29 + except:
  30 + try:
  31 + print("Trying option 2 (average)")
  32 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  33 + normalized = False,
  34 + solver = "lu",
  35 + dtype = np.float32,
  36 + epsilon = 1.0).items(), key=lambda t: t[1])))[-key_size:]]
  37 + except:
  38 + try:
  39 + print("Trying option 2 (average)")
  40 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  41 + normalized = False,
  42 + solver = "lu",
  43 + dtype = np.float32,
  44 + epsilon = 5.0).items(), key=lambda t: t[1])))[-key_size:]]
  45 + except:
  46 + try:
  47 + print("Trying option 2 (average)")
  48 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  49 + normalized = False,
  50 + solver = "lu",
  51 + dtype = np.float32,
  52 + epsilon = 10.0).items(), key=lambda t: t[1])))[-key_size:]]
  53 + except:
  54 + try:
  55 + print("Trying option 3 (slowest)")
  56 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  57 + normalized = False,
  58 + solver = "cg",
  59 + dtype = np.float32,
  60 + epsilon = 1.0).items(), key=lambda t: t[1])))[-key_size:]]
  61 + except:
  62 + try:
  63 + print("Trying option 3 (slowest)")
  64 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  65 + normalized = False,
  66 + solver = "cg",
  67 + dtype = np.float32,
  68 + epsilon = 5.0).items(), key=lambda t: t[1])))[-key_size:]]
  69 + except:
  70 + try:
  71 + print("Trying option 3 (slowest)")
  72 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
  73 + normalized = False,
  74 + solver = "cg",
  75 + dtype = np.float32,
  76 + epsilon = 10.0).items(), key=lambda t: t[1])))[-key_size:]]
  77 + except:
  78 + print("Failed !")
  79 + return
  80 + return list_nodes_to_mask
0 81 \ No newline at end of file
heuristic_current_flow_betweenness.py View file @ bee1340
... ... @@ -0,0 +1,17 @@
  1 +import collections as col
  2 +import numpy as np
  3 +import networkx as nx
  4 +
  5 +def heuristic_current_flow_betweenness(g_nx, key_size):
  6 +
  7 + try:
  8 + print("Trying option 1 (fastest)")
  9 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_betweenness_centrality(g_nx, normalized = False, solver = "full").items(), key=lambda t: t[1])))[-key_size:]]
  10 + except:
  11 + try:
  12 + print("Trying option 2 (average)")
  13 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_betweenness_centrality(g_nx, normalized = False, solver = "cg").items(), key=lambda t: t[1])))[-key_size:]]
  14 + except:
  15 + print("Failed !")
  16 + return
  17 + return list_nodes_to_mask
heuristic_current_flow_closeness.py View file @ bee1340
... ... @@ -0,0 +1,26 @@
  1 +import collections as col
  2 +import networkx as nx
  3 +import numpy as np
  4 +
  5 +def heuristic_current_flow_closeness(g_nx, key_size):
  6 + try:
  7 + print("Trying option 1 (fastest)")
  8 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
  9 + solver = "full",
  10 + dtype = np.float32).items(), key=lambda t: t[1])))[-key_size:]]
  11 + except:
  12 + try:
  13 + print("Trying option 2 (average)")
  14 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
  15 + solver = "lu",
  16 + dtype = np.float32).items(), key=lambda t: t[1])))[-key_size:]]
  17 + except:
  18 + try:
  19 + print("Trying option 3 (slowest)")
  20 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
  21 + solver = "cg",
  22 + dtype = np.float32).items(), key=lambda t: t[1])))[-key_size:]]
  23 + except:
  24 + print("Failed !")
  25 + return
  26 + return list_nodes_to_mask
isolate_largest_cluster.py View file @ bee1340
... ... @@ -0,0 +1,16 @@
  1 +import igraph
  2 +import networkx as nx
  3 +import os
  4 +
  5 +def isolate_largest_cluster(g):
  6 + if len(g.clusters(mode="WEAK")) > 1:
  7 + largest_cluster = g.clusters(mode="WEAK").sizes().index(max(g.clusters(mode="WEAK").sizes()))
  8 + g.clusters(mode="WEAK").subgraph(largest_cluster).simplify().write_gml("saved_graph.gml")
  9 + g_nx = nx.read_gml("saved_graph.gml").to_undirected()
  10 + os.remove("saved_graph.gml")
  11 + else :
  12 + g_nx = nx.Graph(g.get_edgelist())
  13 + g.simplify().write_gml("saved_graph.gml")
  14 + g_nx = nx.read_gml("saved_graph.gml").to_undirected()
  15 + os.remove("saved_graph.gml")
  16 + return g_nx
0 17 \ No newline at end of file
... ... @@ -0,0 +1,85 @@
  1 +# -*- coding: utf-8 -*-
  2 +"""
  3 +Author : Brice Colombier
  4 +Affiliation: Laboratoire Hubert Curien, UMR CNRS 5516
  5 + University of Lyon
  6 + 18 rue du Professeur Benoit Lauras
  7 + 42000 Saint-Etienne - France
  8 +Contact : b.colombier@univ-st-etienne.fr
  9 +
  10 +Title : For test
  11 +Project : Graph-based nodes selection for logic masking
  12 +
  13 +File : test.py
  14 +Last update: 2015-03-17
  15 +"""
  16 +
  17 +import sys
  18 +sys.path.append("../Designs/Demonstrator/Parsers/")
  19 +
  20 +import build_bench
  21 +import build_blif
  22 +import build_edif
  23 +import build_slif
  24 +import build_verilog_rtl
  25 +import build_verilog_struct
  26 +import build_vhd_rtl
  27 +import build_vhd_struct
  28 +import build_xilinx
  29 +
  30 +import random
  31 +import networkx as nx
  32 +import igraph
  33 +import collections as col
  34 +import isolate_largest_cluster
  35 +import modify_nodes_mask
  36 +
  37 +import heuristic_current_flow_closeness
  38 +import heuristic_approximated_current_flow_betweenness
  39 +import heuristic_current_flow_betweenness
  40 +
  41 +import numpy as np
  42 +
  43 +def masking(g, name, prim_in, prim_out, nodes, overhead, heuristic):
  44 +
  45 + h = g.copy() #Perform deep copy
  46 + key_size = int(len(nodes)*overhead)
  47 +
  48 + if heuristic == "random":
  49 + list_nodes_to_mask = random.sample(nodes, key_size)
  50 + elif heuristic == "current_flow_closeness":
  51 + g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
  52 + list_nodes_to_mask = heuristic_current_flow_closeness.heuristic_current_flow_closeness(g_nx, key_size)
  53 + elif heuristic == "approximated_current_flow_betweenness":
  54 + g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
  55 + list_nodes_to_mask = heuristic_approximated_current_flow_betweenness.heuristic_approximated_current_flow_betweenness(g_nx, key_size)
  56 + elif heuristic == "closeness":
  57 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.evcent(directed = False), nodes))[-key_size:]]
  58 + elif heuristic == "betweenness":
  59 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), nodes))[-key_size:]]
  60 + elif heuristic == "current_flow_betweenness":
  61 + g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
  62 + list_nodes_to_mask = heuristic_current_flow_betweenness.heuristic_current_flow_betweenness(g_nx, key_size)
  63 + else:
  64 + raise ValueError("Unknown heuristic")
  65 +
  66 + masking_key = []
  67 + for i in list_nodes_to_mask:
  68 + masking_key.append(random.randint(0, 1))
  69 + h = modify_nodes_mask.modify_nodes_mask(h, list_nodes_to_mask, masking_key)
  70 +
  71 + return h, masking_key
  72 +
  73 +if __name__ == "__main__":
  74 + for name in ["c3540"]:
  75 + overhead = 0.01
  76 + for heuristic in ["random",
  77 + "current_flow_closeness",
  78 + "approximated_current_flow_betweenness",
  79 + "closeness",
  80 + "betweenness",
  81 + "current_flow_betweenness"]:
  82 + g, prim_in, prim_out, nodes = build_bench.build(name)
  83 + print heuristic
  84 + _, b = masking(g, name, prim_in, prim_out, nodes, overhead, heuristic)
  85 + print b
0 86 \ No newline at end of file
modify_nodes_mask.py View file @ bee1340
... ... @@ -0,0 +1,76 @@
  1 +# -*- coding: utf-8 -*-
  2 +"""
  3 +Author : Brice Colombier
  4 +Affiliation: Laboratoire Hubert Curien, UMR CNRS 5516
  5 + University of Lyon
  6 + 18 rue du Professeur Benoit Lauras
  7 + 42000 Saint-Etienne - France
  8 +Contact : b.colombier@univ-st-etienne.fr
  9 +
  10 +Title : Modify nodes mask
  11 +Project : Graph-based nodes selection for logic masking
  12 +
  13 +File : modify_nodes_mask.py
  14 +Last update: 2015-04-09
  15 +"""
  16 +
  17 +def modify_nodes_mask(self, list_nodes_to_mask, masking_key):
  18 +
  19 + if len(list_nodes_to_mask) != len(masking_key):
  20 + raise ValueError("The key length does not match the number of nodes to mask !")
  21 +
  22 + mask = {0:"xor", 1:"xnor"}
  23 + #Handle vertices
  24 + for i in list_nodes_to_mask:
  25 + attributes = self.vs.find(i).attributes()
  26 + attributes["name"] = "K"+i
  27 + attributes["label"] = attributes["name"]
  28 + self.add_vertex(name = attributes["name"])
  29 + for j in attributes:
  30 + self.vs[len(self.vs)-1][j] = attributes[j]
  31 + self.vs[len(self.vs)-1]["color"] = "orange"
  32 + self.vs[len(self.vs)-1]["cat"] = "input"
  33 + attributes["name"] = i+"_mod"
  34 + attributes["label"] = attributes["name"]
  35 + self.add_vertex(name = attributes["name"])
  36 + for j in attributes:
  37 + self.vs[len(self.vs)-1][j] = attributes[j]
  38 + self.vs[len(self.vs)-1]["color"] = "lightblue"
  39 + self.vs[len(self.vs)-1]["cat"] = "mod"
  40 + #Handle edges
  41 + for i in list_nodes_to_mask:
  42 + #Copy outgoing edges to mod node
  43 + for j in self.incident(self.vs.find(i), mode = "OUT"):
  44 + self.add_edge(i+"_mod", self.vs[self.es[j].target]["label"],
  45 + label = self.es[j]["label"],
  46 + width = 5,
  47 + arrow_size = 2,
  48 + label_size = 30,
  49 + color = "#FF0000",
  50 + cat = "mask")
  51 + edges_to_delete = []
  52 + for i in list_nodes_to_mask:
  53 + #Delete copied edges from the original node
  54 + for j in self.incident(self.vs.find(i), mode = "OUT"):
  55 + edges_to_delete.append((i, self.vs[self.es[j].target]["label"]))
  56 + self.delete_edges(edges_to_delete)
  57 + for i in zip(list_nodes_to_mask, masking_key):
  58 + if i[1] in [0, 1]:
  59 + #The node should be forced to 0
  60 + #An AND gate will be used
  61 + self.add_edge("K"+i[0], i[0]+"_mod",
  62 + label = mask[i[1]],
  63 + width = 5,
  64 + arrow_size = 2,
  65 + label_size = 30,
  66 + color = "#FF0000",
  67 + cat = "mask")
  68 + self.add_edge(i[0], i[0]+"_mod",
  69 + label = mask[i[1]],
  70 + width = 5,
  71 + arrow_size = 2,
  72 + label_size = 30,
  73 + color = "#FF0000",
  74 + cat = "mask")
  75 +
  76 + return self
0 77 \ No newline at end of file
... ... @@ -0,0 +1,6 @@
  1 +# Centrality indicators as node selection heuristics for logic masking
  2 +#### Authors: Brice Colombier, Lilian Bossuet, David Hély
  3 +#### Laboratoire Hubert Curien
  4 +#### 18 rue Pr. Benoît Lauras, 42000 Saint-Etienne, FRANCE
  5 +
  6 +Source code associated to this research article.