Showing
3 changed files
with
141 additions
and
0 deletions
PC.py
0 → 100644
1 | +import hashlib | ||
2 | + | ||
3 | +class PCProver: | ||
4 | + def __init__(self, g=5, p=2333, s=15): | ||
5 | + self.g = g | ||
6 | + self.q = 2*p + 1 | ||
7 | + self.s = s | ||
8 | + self.h = pow(g, s, self.q) | ||
9 | + def commit(self, M, r): | ||
10 | + m = int(hashlib.sha256(M.encode()).hexdigest() ,16) | ||
11 | + c = pow(self.g, m, self.q) * pow(self.h, r, self.q) % self.q | ||
12 | + return (Message(M, r), Commitment(c)) | ||
13 | + def getParams(self): | ||
14 | + return PCParameters(self.q, self.g, self.h) | ||
15 | + | ||
16 | +class Message: | ||
17 | + def __init__(self, M, r): | ||
18 | + self.M = M | ||
19 | + self.r = r | ||
20 | + def __repr__(self): | ||
21 | + return """M | {} | ||
22 | +r | {}""".format(self.M, self.r) | ||
23 | + | ||
24 | +class Commitment: | ||
25 | + def __init__(self, c): | ||
26 | + self.c = c | ||
27 | + def __repr__(self): | ||
28 | + return """c | {}""".format(self.c) | ||
29 | + | ||
30 | +class PCParameters: | ||
31 | + def __init__(self, q, g, h): | ||
32 | + self.q = q | ||
33 | + self.g = g | ||
34 | + self.h = h | ||
35 | + | ||
36 | +class PCVerifier: | ||
37 | + def __init__(self, params): | ||
38 | + self.c = [] | ||
39 | + self.m = [] | ||
40 | + self.r = [] | ||
41 | + self.q = params.q | ||
42 | + self.g = params.g | ||
43 | + self.h = params.h | ||
44 | + def __repr__(self): | ||
45 | + returnStr = """Commitments | {} | ||
46 | +Messages | {} | ||
47 | +q | {} | ||
48 | +g | {} | ||
49 | +h | {}""".format(len(self.c), len(self.m), self.q, self.g, self.h) | ||
50 | + return returnStr | ||
51 | + def add(self, dataObject): | ||
52 | + if(type(dataObject) is Commitment): | ||
53 | + self.c.append(dataObject.c) | ||
54 | + if(type(dataObject) is Message): | ||
55 | + m = int(hashlib.sha256(dataObject.M.encode()).hexdigest() ,16) | ||
56 | + self.m.append(m) | ||
57 | + self.r.append(dataObject.r) | ||
58 | + def productCommitments(self): | ||
59 | + pi = 1 | ||
60 | + for ci in self.c: | ||
61 | + pi *= ci | ||
62 | + return pi % self.q | ||
63 | + def verify(self): | ||
64 | + c = self.productCommitments() | ||
65 | + m_sum = 0 | ||
66 | + r_sum = 0 | ||
67 | + for i in range(len(self.m)): | ||
68 | + m_sum += self.m[i] | ||
69 | + r_sum += self.r[i] | ||
70 | + cv = pow(self.g, m_sum, self.q) * pow(self.h, r_sum, self.q) % self.q | ||
71 | + return c == cv | ||
72 | + |
README.md
0 → 100644
1 | +# Pedersen Commitment | ||
2 | +Project includes python implementation of Pedersen Commitment Scheme. | ||
3 | +# How to use | ||
4 | +example.py contains code examples to perform the RAPPOR algorithm. | ||
5 | +### Creating Commitments | ||
6 | +``` | ||
7 | +from PC import PCProver, PCVerifier | ||
8 | +``` | ||
9 | +Initialise the parameters. | ||
10 | +``` | ||
11 | +pc = PCProver(self, g=5, p=2333, s=15) | ||
12 | +params = pc.getParams() | ||
13 | +verifier = PCVerifier(params) | ||
14 | +``` | ||
15 | +Create commitments with a message and random integer. | ||
16 | +``` | ||
17 | +m1, c1 = pc.commit("Hello!", 7) | ||
18 | +m2, c2 = pc.commit("Goodbye!", 9) | ||
19 | +``` | ||
20 | +Send the commitments first. | ||
21 | +``` | ||
22 | +verifier.add(c1) | ||
23 | +verifier.add(c2) | ||
24 | +``` | ||
25 | +When the prover wants to reveal messages, they can be sent to the verifier and finally confirms the messages. | ||
26 | +``` | ||
27 | +verifier.add(m1) | ||
28 | +verifier.add(m2) | ||
29 | +print(verifier.verify()) | ||
30 | +``` | ||
31 | +# Parameters | ||
32 | +**Name** | **Description** | **Rules** | ||
33 | +| --- | --- | --- | | ||
34 | +g | Generator value. | Must be a positive integer between 1 and q - 1. | ||
35 | +p | Prime number that will produce q = 2p + 1. | Must be a prime. | ||
36 | +s | Secret value used to create h = g^s mod q. | Must be a positive integer between 1 and q - 1. | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
example.py
0 → 100644
1 | +from PC import PCProver, PCVerifier | ||
2 | + | ||
3 | +#Set up the params | ||
4 | +pc = PCProver() | ||
5 | +params = pc.getParams() | ||
6 | +verifier = PCVerifier(params) | ||
7 | + | ||
8 | +#Dictionary of messages and their respective "random" integers | ||
9 | +exchange = [{"M":"Hello!", "r":7}, | ||
10 | + {"M":"Goodbye!", "r":9}] | ||
11 | + | ||
12 | +commitments = [] | ||
13 | +messages = [] | ||
14 | + | ||
15 | +#Create commitments and keep the results | ||
16 | +for i in range(len(exchange)): | ||
17 | + ex = exchange[i] | ||
18 | + (m, c) = pc.commit(ex["M"], ex["r"]) | ||
19 | + commitments.append(c) | ||
20 | + messages.append(m) | ||
21 | + | ||
22 | +#Send commitments to the verifier | ||
23 | +for c in commitments: | ||
24 | + verifier.add(c) | ||
25 | + | ||
26 | +#Interim period before prover wants to reveal message | ||
27 | + | ||
28 | +#Send messages to the verifier | ||
29 | +for m in messages: | ||
30 | + verifier.add(m) | ||
31 | + | ||
32 | +#Verifier computes the challenges with the messages and compares them with the commitments | ||
33 | +print(verifier.verify()) |
-
Please register or login to post a comment