Skip to content

Commit 214d338

Browse files
Remove doctest example from pedersen_commitment to fix test failure
1 parent e4bcd77 commit 214d338

File tree

2 files changed

+49
-57
lines changed

2 files changed

+49
-57
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from pydatastructs.graphs.graph import Graph
1212
from pydatastructs.linear_data_structures.algorithms import merge_sort_parallel
1313
from pydatastructs import PriorityQueue
14+
import hashlib
15+
import secrets
1416

1517
__all__ = [
1618
'breadth_first_search',
@@ -753,6 +755,52 @@ def shortest_paths(graph: Graph, algorithm: str,
753755
"finding shortest paths in graphs."%(algorithm))
754756
return getattr(algorithms, func)(graph, source, target)
755757

758+
def pedersen_commitment(graph, g, h, p, q, include_weights=True):
759+
"""
760+
Returns a Pedersen commitment for the given graph.
761+
762+
This function creates a cryptographic commitment of the graph's structure.
763+
The commitment hides node and edge information but allows later verification
764+
by revealing the original graph and blinding factor.
765+
766+
Parameters
767+
----------
768+
graph : Graph
769+
The PyDataStructs graph object to commit.
770+
771+
g : int
772+
A generator of a subgroup of order q (g^q ≡ 1 mod p).
773+
774+
h : int
775+
A second, independent generator of the same subgroup.
776+
777+
p : int
778+
A large prime modulus (≥1024 bits) such that q divides p - 1.
779+
780+
q : int
781+
A prime number representing the subgroup order (≥160 bits).
782+
783+
include_weights : bool, optional
784+
Whether to include edge weights in the graph serialization. Default is True.
785+
Notes
786+
-----
787+
- The blinding factor `r` must be kept private.
788+
- Changing even a single edge or vertex will yield a different commitment.
789+
"""
790+
if p.bit_length() < 1024:
791+
raise ValueError("p must be a 1024-bit prime or larger.")
792+
if q.bit_length() < 160:
793+
raise ValueError("q must be a 160-bit prime or larger.")
794+
if (p - 1) % q != 0:
795+
raise ValueError("q must divide (p - 1).")
796+
if pow(g, q, p) != 1 or pow(h, q, p) != 1:
797+
raise ValueError("g and h must be generators of a subgroup of order q.")
798+
data = graph.serialize_graph(graph, include_weights)
799+
m = int(hashlib.sha256(data.encode()).hexdigest(), 16) % q
800+
r = secrets.randbelow(q)
801+
commitment = (pow(g, m, p) * pow(h, r, p)) % p
802+
return commitment, r
803+
756804
def _bellman_ford_adjacency_list(graph: Graph, source: str, target: str) -> tuple:
757805
distances, predecessor = {}, {}
758806

pydatastructs/graphs/graph.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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
]
9842
import copy
9943
import time

0 commit comments

Comments
 (0)