@@ -36,64 +36,8 @@ def serialize_graph(graph, include_weights=True):
3636 else :
3737 edges = sorted (str (k ) for k in graph .edge_weights )
3838 return str (vertices ) + str (edges )
39- def pedersen_commitment (graph , g , h , p , q , include_weights = True ):
40- """
41- Returns a Pedersen commitment for the given graph.
42-
43- This function creates a cryptographic commitment of the graph's structure.
44- The commitment hides node and edge information but allows later verification
45- by revealing the original graph and blinding factor.
46-
47- Parameters
48- ----------
49- graph : Graph
50- The PyDataStructs graph object to commit.
51-
52- g : int
53- A generator of a subgroup of order q (g^q ≡ 1 mod p).
54-
55- h : int
56- A second, independent generator of the same subgroup.
57-
58- p : int
59- A large prime modulus (≥1024 bits) such that q divides p - 1.
60-
61- q : int
62- A prime number representing the subgroup order (≥160 bits).
63-
64- include_weights : bool, optional
65- Whether to include edge weights in the graph serialization. Default is True.
66- Toy Example
67- -----------
68- >>> g = Graph(implementation='adjacency_list')
69- >>> g.add_edge('A', 'B', 5)
70- >>> p = 208351617316091241234326746312124448251235562226470491514186331217050270460481
71- >>> q = 233970423115425145524320034830162017933
72- >>> commitment, r = pedersen_commitment(g, g=5, h=7, p=p, q=q)
73- >>> print(commitment)
74- 98392819481230984098123
75-
76- Notes
77- -----
78- - The blinding factor `r` must be kept private.
79- - Changing even a single edge or vertex will yield a different commitment.
80-
81- """
82- if p .bit_length () < 1024 :
83- raise ValueError ("p must be a 1024-bit prime or larger." )
84- if q .bit_length () < 160 :
85- raise ValueError ("q must be a 160-bit prime or larger." )
86- if (p - 1 ) % q != 0 :
87- raise ValueError ("q must divide (p - 1)." )
88- if pow (g , q , p ) != 1 or pow (h , q , p ) != 1 :
89- raise ValueError ("g and h must be generators of a subgroup of order q." )
90- data = serialize_graph (graph , include_weights )
91- m = int (hashlib .sha256 (data .encode ()).hexdigest (), 16 ) % q
92- r = secrets .randbelow (q )
93- commitment = (pow (g , m , p ) * pow (h , r , p )) % p
94- return commitment , r
9539__all__ = [
96- 'Graph' , 'pedersen_commitment'
40+ 'Graph'
9741]
9842import copy
9943import time
0 commit comments