forked from asonnino/coconut-chainspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_petition.py
More file actions
294 lines (207 loc) · 7.84 KB
/
Copy pathrun_petition.py
File metadata and controls
294 lines (207 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# system
import requests
import json
import pprint
# chainspace
from chainspacecontract.examples.utils import *
import chainspacecontract as csc
# petlib
from petlib.bn import Bn
# coconut
import coconut.utils as cu
import coconut.scheme as cs
# petitions contracts
from contracts import petition
from contracts.petition import contract as petition_contract
from contracts.utils import *
debug = 0
pp = pprint.PrettyPrinter(indent=4)
# Petition Parameters
petition_UUID = Bn(1234) # petition unique id (needed for crypto) - A BigNumber from Open SSL
options = ['YES', 'NO']
# petition owner parameters
# G = Group from the elliptic curve (default is openssl 713, NIST p224
# g = Generator for the group - this is a point on the ellptic curve (ECPt)
# hs =
# o = The order of the group
(G, g, hs, o) = pet_setup()
# Do some interesting things with EcPts:
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
# https://stackoverflow.com/questions/5181320/under-what-circumstances-are-rmul-called
# Petlib overloads the multiplier operator "*" to allow multiplication of elliptic curve points by scalars
# which basically is like saying P * 3 == P + P + P
def debug_ec_point(ec_point):
x, y = ec_point.get_affine()
print("x = " + str(x))
print("y = " + str(y))
def str_ec_pt(ec_pt):
x, y = ec_pt.get_affine()
return "EcPt(x = " + str(x) + ", y = " + str(y) + ")"
print("Type of g : " + str(type(g)))
print("Add g to itself:")
debug_ec_point(g.pt_add(g))
print("2 * g:")
debug_ec_point(2 * g)
print("d.pt_double:")
debug_ec_point(g.pt_double())
# Example of el-gammal standard protocol:
#
# hide message m using alpha^k and beta^k where
# alpha is the primitive root of of a large prime and k is a random integer
# beta is equal to alpha^a where a is a secret known only to the reciever (receivers private key)
# primitive roots: http://mathworld.wolfram.com/PrimitiveRoot.html
# e.g. primitive roots of prim 13 are 2, 6, 7, 11
# Brooke chooses:
p = 13
alpha = 6
a = 3
# Brooke computes:
beta = pow(alpha, a)
# alpha, beta and p are public
# Abbie chooses:
k = 3
# Abbie wants to send the message m
m = 21
# Abbie computes:
alpha_k = pow(alpha, k)
beta_k_m = pow(beta, k) * m
print("\nEl-Gammal without elliptic curves...")
print("plaintext message(m) : " + str(m))
print("encrypted message: ")
print("alpha_k: " + str(alpha_k))
print("beta_k_m: " + str(beta_k_m))
# Abbie sends alpha_k and beta_k to brooke who can decrypt:
# See https://cse.unl.edu/~ssamal/crypto/EEECC.pdf
# (alpha_k ^ -a) * (beta_k * m) = m
# Brooke computes:
alpha_k_minus_a = pow(alpha_k, -a)
m_decrypted = int(alpha_k_minus_a * beta_k_m)
print("alpha_k_minus_a : " + str(alpha_k_minus_a))
print("decrypted message: " + str(m_decrypted))
print("-- end el-gammal\n")
# Set up the petition
# Select a cryptographically secure random number less than the order for each of the owners
print("Petition Setup:")
# sk = secret key
# vk = verification key
t_owners = 2 # threshold number of owners
n_owners = 3 # total number of owners
v = [o.random() for _ in range(0, t_owners)]
print("threshold seeds for secret keys (v): " + str(v))
sk_owners = [cu.poly_eval(v, i) % o for i in range(1, n_owners + 1)]
print("\nSecret Keys of the petition owners: ")
for i in range(n_owners):
print("sk[" + str(i) + "] : " + str(sk_owners[i]))
pk_owner = [xi * g for xi in sk_owners]
print("\nPublic keys of the petition owners: ")
for i in range(n_owners):
print("pk[" + str(i) + "] : " + str_ec_pt(pk_owner[i]))
l = cu.lagrange_basis(range(1, t_owners + 1), o, 0)
aggr_pk_owner = cu.ec_sum([l[i] * pk_owner[i] for i in range(t_owners)])
print("\nAggregate public key for owners: " + str_ec_pt(aggr_pk_owner))
# coconut parameters
print("Coconut setup")
t, n = 4, 5 # threshold and total number of authorities
bp_params = cs.setup() # bp system's parameters
(sk, vk) = cs.ttp_keygen(bp_params, t, n) # authorities keys
print("\nSecret keys of the authorities (Credential issuers):")
for i in range(n):
print("sk_auth[" + str(i) + "] : " + str(sk[i]))
print("\nVerification keys of the authorities (Credential issuers):")
for i in range(n):
print("pk_auth[" + str(i) + "] : " + str(vk[i]))
aggr_vk = cs.agg_key(bp_params, vk, threshold=True)
print("\nAggregated Verification Key: " + str(aggr_vk))
def pp_json(json_str):
pp.pprint(json.loads(json_str))
def pp_object(obj):
pp.pprint(obj)
def post_transaction(transaction, path):
transaction = csc.transaction_inline_objects(transaction)
print("Posting transaction to " + path)
if debug == 1:
pp_object(transaction)
response = requests.post(
'http://127.0.0.1:5000/'
+ petition_contract.contract_name
+ path,
json=transaction)
return response
print("\n======== EXECUTING PETITION =========\n")
def sign_petition_crypto():
global d, gamma, private_m, Lambda, ski, sigs_tilde, sigma_tilde, sigs, sigma
# some crypto to get the credentials
# ------------------------------------
# This can be done with zencode "I create my new credential keypair"
# Keypair for signer
(d, gamma) = cs.elgamal_keygen(bp_params)
private_m = [d] # array containing the private attributes, in this case the private key
Lambda = cs.prepare_blind_sign(bp_params, gamma, private_m) # signer prepares a blind signature request from their private key
# This would be done by the authority
sigs_tilde = [cs.blind_sign(bp_params, ski, gamma, Lambda) for ski in sk] # blind sign from each authority
# back with the signer, unblind all the signatures, using the private key
sigs = [cs.unblind(bp_params, sigma_tilde, d) for sigma_tilde in sigs_tilde]
# aggregate all the credentials
sigma = cs.agg_cred(bp_params, sigs)
return (sigma, d)
with petition_contract.test_service():
print("\npetition.init()")
init_transaction = petition.init()
token = init_transaction['transaction']['outputs'][0]
post_transaction(init_transaction, "/init")
create_transaction = petition.create_petition(
(token,),
None,
None,
petition_UUID,
options,
sk_owners[0], # private key of the owner for signing
aggr_pk_owner, # public key of the owner
t_owners,
n_owners,
aggr_vk # aggregated verifier key
)
print("\npetition.create_petition()")
post_transaction(create_transaction, "/create_petition")
old_petition = create_transaction['transaction']['outputs'][1]
old_list = create_transaction['transaction']['outputs'][2]
for i in range(3):
print("\npetition.sign() [" + str(i) + "]")
(sigma, d) = sign_petition_crypto()
print("\nAggregated credentials (sigma): " + str(sigma))
# ------------------------------------
print("\npetition.sign()")
sign_transaction = petition.sign(
(old_petition, old_list),
None,
None,
d,
sigma,
aggr_vk,
1
)
post_transaction(sign_transaction, "/sign")
old_petition = sign_transaction['transaction']['outputs'][0]
old_list = sign_transaction['transaction']['outputs'][1]
# tally
for i in range(t_owners):
tally_transaction = petition.tally(
(old_petition,),
None,
None,
sk_owners[i],
i,
t_owners
)
post_transaction(tally_transaction, "/tally")
old_petition = tally_transaction['transaction']['outputs'][0]
# read
read_transaction = petition.read(
None,
(old_petition,),
None
)
post_transaction(read_transaction, "/read")
print("\n\n==================== OUTCOME ====================\n")
print('OUTCOME: ', json.loads(read_transaction['transaction']['returns'][0]))
print("\n===================================================\n\n")