venumML.graphs.venum_graph

class Graph:

Represents a Venum encrypted graph that supports encryption and decryption of the adjacency matrix for privacy-preserving computations.

Attributes
  • use_hashing (bool): Indicates whether to use hashing for node identifiers.
  • nodes (dict): Maps hashed node identifiers to integer indices.
  • directed (bool): Specifies if the graph is directed.
  • adjacency_matrix (np.ndarray): Encrypted adjacency matrix representing edge weights.
  • boolean_matrix (np.ndarray): Encrypted adjacency matrix representing the presence of edges.
  • inverse_outbound (np.ndarray): Encrypted array of inverse outbound degree counts for each node.
  • boolean_outbound (np.ndarray): Encrypted boolean array indicating nodes with outbound edges.
Graph(ctx, directed=True, use_hashing=True)

Initialises an encrypted graph with optional hashing for node identifiers.

Parameters
  • ctx (EncryptionContext): The encryption context used to initialise matrices.
  • directed (bool, optional, default=True): Whether the graph is directed.
  • use_hashing (bool, optional, default=True): Whether to use SHA-256 hashing for node identifiers.
@staticmethod
def hash_node(node):

Hashes a node using SHA-256.

Parameters
  • node (any): The node identifier.
Returns
  • str: The SHA-256 hexadecimal digest of the node.
def is_sha256_hash(self, value):

Checks if the given value is a SHA-256 hash.

Parameters
  • value (str): The value to check.
Returns
  • bool: True if the value is a valid SHA-256 hash, False otherwise.
def add_node(self, ctx, node):

Adds a node to the graph after hashing and ensures the adjacency matrix is resized.

Parameters
  • ctx (EncryptionContext): The encryption context used to encrypt values.
  • node (any): The node identifier.
def add_edge(self, ctx, from_node, to_node, weight=1):

Adds a weighted edge between two nodes.

Parameters
  • ctx (EncryptionContext): The encryption context used to encrypt values.
  • from_node (any): The starting node of the edge.
  • to_node (any): The ending node of the edge.
  • weight (float, optional, default=1): The weight of the edge.
def remove_edge(self, from_node, to_node):

Removes an edge between two nodes by setting the matrix value to 0.

Parameters
  • from_node (any): The starting node of the edge.
  • to_node (any): The ending node of the edge.
def get_edge_weight(self, from_node, to_node):

Returns the weight of an edge between two nodes.

Parameters
  • from_node (any): The starting node of the edge.
  • to_node (any): The ending node of the edge.
Returns
  • float or int: The weight of the edge, or 0 if no edge exists.
def get_adjacency_matrix(self):

Retrieves the encrypted adjacency matrix.

Returns
  • np.ndarray: The current encrypted adjacency matrix.
def get_boolean_matrix(self):

Retrieves the encrypted boolean adjacency matrix.

Returns
  • np.ndarray: The current encrypted boolean adjacency matrix.
def get_node_degree(self, node):

Calculates the degree of a node.

Parameters
  • node (any): The node identifier.
Returns
  • int: The degree of the node.
def encrypt_networkx(ctx, nx_graph, use_hashing=True):

Converts a NetworkX graph to an encrypted custom Graph.

Parameters
  • ctx (EncryptionContext): The encryption context used to encrypt node and edge data.
  • nx_graph (networkx.Graph or networkx.DiGraph): The NetworkX graph to convert.
  • use_hashing (bool, optional, default=True): Whether to hash node identifiers.
Returns
  • Graph: An encrypted custom Graph with nodes, edges, and outbound counts encrypted.
def df_to_encrypted_graph( ctx, df, from_col, to_col, weight_col=None, use_hashing=True, directed=True):

Creates an encrypted custom Graph from a pandas DataFrame.

Parameters
  • ctx (EncryptionContext): The encryption context used to encrypt node and edge data.
  • df (pd.DataFrame): DataFrame containing the edge data.
  • from_col (str): Column name representing the source node.
  • to_col (str): Column name representing the target node.
  • weight_col (str): Column name representing the edge weight.
  • use_hashing (bool, optional, default=True): Whether to hash node identifiers.
Returns
  • Graph: An encrypted custom Graph with nodes, edges, and outbound counts encrypted.
def decrypt_graph(ctx, graph):

Decrypts an encrypted adjacency matrix of an encrypted Graph.

Parameters
  • ctx (EncryptionContext): The encryption context used to decrypt values.
  • custom_graph (Graph): The encrypted Graph to decrypt.
Returns
  • networkx.Graph or networkx.DiGraph: A decrypted NetworkX graph with nodes and edges restored.
def pagerank(ctx, encrypted_graph, damping_factor=0.85, iters=20):

Computes PageRank scores for an encrypted Graph using the Power Method.

Parameters
  • ctx (EncryptionContext): The encryption context used to handle encrypted values.
  • encrypted_graph (Graph): The encrypted custom Graph on which to compute PageRank.
  • damping_factor (float, optional, default=0.85): The probability of following an edge; 1 - damping_factor is the teleport probability.
  • iters (int, optional, default=20): The number of iterations for the Power Method.
Returns
  • dict: A dictionary mapping node identifiers to encrypted PageRank scores.
def decrypt_pagerank(ctx, encrypted_pagerank):

Decrypts encrypted PageRank scores.

Parameters
  • ctx (EncryptionContext): The encryption context used to decrypt values.
  • encrypted_pagerank (dict): A dictionary mapping node identifiers to encrypted PageRank scores.
Returns
  • dict: A dictionary mapping node identifiers to decrypted PageRank scores.