protocol.py 29.5 KB
Newer Older
root's avatar
root committed
1
"""
Aditya Damodaran's avatar
Aditya Damodaran committed
2
3
4
SPDX-FileCopyrightText: 2021 University of Luxembourg
SPDX-License-Identifier: GPL-3.0-or-later
SPDXVersion: SPDX-2.2
Aditya Damodaran's avatar
Aditya Damodaran committed
5

Aditya Damodaran's avatar
Aditya Damodaran committed
6
7
8
Authors: 
       Aditya Damodaran, aditya.damodaran@uni.lu
       Alfredo Rial, alfredo.rial@uni.lu
root's avatar
root committed
9
"""
Aditya Damodaran's avatar
Aditya Damodaran committed
10

Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
11
12
13
14
import argparse
import time
import random
import sys
root's avatar
root committed
15
from pathlib import Path
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
16
17
18
from collections import namedtuple

from charm.toolbox.pairinggroup import PairingGroup, ZR, G1, G2
root's avatar
root committed
19
from openpyxl import load_workbook, Workbook
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
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
from texttable import Texttable

from uuhd.jsonobjects import (
    ZKWitness,
    ZKInstance,
    SubWitnessRecord,
    SubInstanceRecord,
    dict_from_class,
)
from uuhd.functionalities import (
    FNYM,
    FCRS,
    FREG,
    FZK,
    FZK_RD,
    FZK_PR3,
    WeakReference,
)
from uuhd.primitives import (
    VectorCommitment,
    StructurePreservingSignature,
    PedersenCommitment,
)
from uuhd.measurement_util import get_real_size

Aditya Damodaran's avatar
Aditya Damodaran committed
45
46
47
parser = argparse.ArgumentParser(
    description="Implementation of the protocol described in the paper titled 'Unlinkable Updatable Hiding Databases and Privacy-Preserving Loyalty Programs'"
)
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
48
49
50
51
52
parser.add_argument("size", metavar="N", type=int, help="Database size")

parser.add_argument(
    "-k",
    "--keylength",
Aditya Damodaran's avatar
Aditya Damodaran committed
53
54
55
    metavar="K",
    type=int,
    help="Paillier Encryption key size. (Supported values: 1024, 2048)",
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
56
57
58
59
)
parser.add_argument(
    "-r",
    "--randomise",
Aditya Damodaran's avatar
Aditya Damodaran committed
60
61
62
    action="store_true",
    default=False,
    help="Randomise database state",
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
63
64
65
66
67
68
69
70
71
72
73
)
# parser.add_argument('-w','--writetofile', action='writetofile', help='Paillier


db_size = 100
keylength = 2048

args = parser.parse_args()

if args.keylength == 1024 or args.keylength == 2048:
    keylength = args.keylength
Aditya Damodaran's avatar
Aditya Damodaran committed
74

root's avatar
root committed
75
if args.size != None and int(args.size) > 10 and int(args.size) < 800000:
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
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
    db_size = int(args.size)


# Curve specification
group = PairingGroup("BN256")

# Primitive instantiation
vector_commitment = VectorCommitment(group)
structure_preserving_signature = StructurePreservingSignature()
pedersen_commitment = PedersenCommitment(group)

# Functionality instantiation
f_crs = FCRS()
weak_reference = WeakReference()
f_nym = FNYM(weak_reference)
f_reg = FREG()
f_zk = FZK(f_nym, keylength)

# Execution time measurement vars
t_setup = ""
t_first_update = ""
t_comp_vcom = ""
t_one_entry_update = ""
t_one_entry_read = ""
t_five_entry_read = ""
t_register = ""
t_purchase = ""
t_redeem = ""


# NTs
CommittedRecord = namedtuple(
    "CommittedRecord", "i vr ccom_i copen_i ccom_ri copen_ri"
)
WitnessRecord = namedtuple(
    "WitnessRecord", "i vr copen_i copen_ri witness sig_i"
)

InstanceRecord = namedtuple("InstanceRecord", "ccom_i ccom_ri")


root's avatar
root committed
117
118
119
120
121
122
def draw_table(headings, data):
    table = Texttable()
    table.add_rows([headings, data])
    print(table.draw())


Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
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
# Sets up the CRS (public parameters for a Vector Commitment with a database of size 'nsize', and parameters for the Pedersen commitment scheme
def setup(nsize):
    par = vector_commitment.setup(nsize)
    par_c = pedersen_commitment.setup()
    f_crs.set(par, par_c)


# Party U (Updater)
class Updater:
    def __init__(self):
        pass

    l_store = []
    #   'sid', 'P', 'flag=1', 'com1'
    #   'sid', 'P', 'flag=0', 'vcom', 'com', 'coms'

    l_par = []
    #   'sid', 'par', 'par_c', 'sps_public_key', 'sps_secret_key'

    # Checks if pseudonym p has been seen before
    def is_unique_pseudonym(self, p):
        for item in self.l_store:
            if p == item["p"]:
                return 0
        return 1

    # Returns the corresponding record in l_store for pseudonym = p
    def get_record_for_pseudonym(self, p):
        for item in self.l_store:
            if p == item["p"]:
                return item
        return 0

    # Responses from U to messages from other parties/functionalities
    def message_in(self, m, p):
        if m["message"] == "com1":
            if not self.is_unique_pseudonym(p):
                print("Abort: (Updater) p is not unique. (com1)")
                exit()
            flag = 1
            self.l_store.append(
                {"sid": sid, "p": p, "flag": flag, "com1": m["com1"]}
            )
        elif m["message"] == "repcom":
            record = self.get_record_for_pseudonym(p)
            if (
                pedersen_commitment.verify(
                    self.l_par[0]["par_c"], record["com"], m["s1"], m["open1"]
                )
                == 1
            ):
                record["com1"] = m["com1"]
            else:
                print("Abort: (Updater) Commitment opening failed. (repcom)")
                exit()
            print(
                "Updater: read.end; sid = "
                + str(record["sid"])
                + "; p = "
                + str(record["p"])
                + "; flag = "
                + str(record["flag"])
                + "; coms = "
                + str(record["coms"])
            )
        else:
Aditya Damodaran's avatar
Aditya Damodaran committed
189
190
191
            print(
                "Abort: (Updater) Unrecognised message: " + str(m["message"])
            )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
            exit()

    # Responses from U to messages from other FZKs
    def proof_in(self, instance, p):
        if not self.is_unique_pseudonym(p):
            print("Abort: (Updater) p is not unique. (proof)")
            exit()
        if (
            instance["sid"] != self.l_par[0]["sid"]
            or instance["par"]["par_g"] != self.l_par[0]["par"]["par_g"]
            or instance["par"]["par_h"] != self.l_par[0]["par"]["par_h"]
            or instance["par_c"]["g"] != self.l_par[0]["par_c"]["g"]
            or instance["par_c"]["h"] != self.l_par[0]["par_c"]["h"]
        ):
            print("Abort: (Updater) Inconsistent instance vars. (proof)")
            exit()
        self.l_store.append(
            {
                "sid": sid,
                "p": p,
                "flag": 0,
                "vcom": instance["vcomd"],
                "com": instance["comd"],
                "coms": instance["ins_i"],
            }
        )
        f_nym.reply({"message": "open", "com": instance["comd"]}, p)

    # update interface
    def update(self, sid, p, values):
        record = self.get_record_for_pseudonym(p)

        if record == 0:
            print("Abort: (Updater) No records found for p. (update)")
            exit()

        if len(self.l_par) == 0:
            t_setup_start = time.time()
            par, par_c = f_crs.get()

            par_sig = [0]
            (
                sps_public_key,
                sps_secret_key,
            ) = structure_preserving_signature.generate_keys(
                {"group": par["group"], "g": par["g"], "h": par["h"]}, 2, 1
            )

            for i in range(1, len(par["par_h"])):
                sps_r, sps_s, sps_t = structure_preserving_signature.sign(
                    sps_secret_key,
                    [0, par["g"] ** i, par["g"] ** sid, par["par_h"][i]],
                )
                par_sig.append({"R": sps_r, "S": sps_s, "T": sps_t})

            f_reg.register(
                {"sps_public_key": sps_public_key, "par_sig": par_sig}
            )

            self.l_par.append(
                {
                    "sid": sid,
                    "par": par,
                    "par_c": par_c,
                    "sps_public_key": sps_public_key,
                    "sps_secret_key": sps_secret_key,
                }
            )

            global t_setup
            t_setup = str(time.time() - t_setup_start)

        x = [0]

        for value_item in values:
            x.append(value_item)

        vcom = vector_commitment.commit(self.l_par[0]["par"], x, 0)
        s2 = self.l_par[0]["par"]["group"].random(ZR)
        com2 = pedersen_commitment.commit_0(self.l_par[0]["par_c"], s2)
        com = record["com1"] * com2["com"]

        if record["flag"] == 1:

            sps_r, sps_s, sps_t = structure_preserving_signature.sign(
                self.l_par[0]["sps_secret_key"],
                [0, vcom, com, self.l_par[0]["par"]["h"] ** sid],
            )

            f_nym.reply(
                {
                    "message": "setup",
                    "x": x,
                    "s2": s2,
                    "sig": {"R": sps_r, "S": sps_s, "T": sps_t},
                },
                record["p"],
            )

        if record["flag"] == 0:
            vcom_u = record["vcom"] * vcom

            sps_r, sps_s, sps_t = structure_preserving_signature.sign(
                self.l_par[0]["sps_secret_key"],
                [0, vcom_u, com, self.l_par[0]["par"]["h"] ** sid],
            )

            f_nym.reply(
                {
                    "message": "update",
                    "x": x,
                    "s2": s2,
                    "sig": {"R": sps_r, "S": sps_s, "T": sps_t},
                },
                record["p"],
            )


# Updater instantiation
updater = Updater()

# Returns a unique identifer for U
updater_id = weak_reference.remember(updater)


class Reader:
    def __init__(self):
        pass

    l_par = []
    #   {'sid','par','par_c','com1','s1','open1'}

    l_store = []
    #   {'sid','par','par_c','sps_public_key','vcom','x','r','com','s','open','sig'}

    # Returns a list consisting of commitments to the positions in i_list and their corresponding values in the database x
    def prepare_committed_record(self, i_list):

        com_list = []
        for i in i_list:
            ccom_i = pedersen_commitment.commit(self.l_par[0]["par_c"], i)
            ccom_v = pedersen_commitment.commit(
                self.l_par[0]["par_c"], self.l_store[0]["x"][i]
            )
            temp_record = CommittedRecord(
                i,
                self.l_store[0]["x"][i],
                ccom_i["com"],
                ccom_i["open"],
                ccom_v["com"],
                ccom_v["open"],
            )
            com_list.append(temp_record)
        return com_list

    def update_table(self, i_list):
        for i in range(1, len(i_list)):
            self.l_store[0]["x"][i] = self.l_store[0]["x"][i] + i_list[i]

    def prepare_blinded_witness(self, instance, witness):
        # Unpack signatures
        sig = witness["sig"]
        R, S, T = sig["R"], sig["S"], sig["T"]
        sps_public_key = instance["sps_public_key"]

        # Bases for blinding elements in g and gt(h from G@)
        h = instance["par"]["group"].random(G1)
        ht = instance["par"]["group"].random(G2)

        #
        [d1, d2, d5] = group.random(ZR, 3)
        d3, d4 = witness["r2"], witness["open2"]

        # Blind sigs
        Rd = R * (h ** d1)
        Sd = S * (h ** d2)
        Td = T * (ht ** d5)

        comd, vcomd = instance["comd"], instance["vcomd"]

        #
        subinstance_list = instance["subinstance"]
        subwitness_list = witness["subwitness"]

        index = 0
        blinded_witness = ZKWitness()
        blinded_witness.set_d(d1, d2, d3, d4, d5)

        blinded_instance = ZKInstance()
        blinded_instance.set_bsig(Rd, Sd, Td)
        blinded_instance.set_comd(comd)
        blinded_instance.set_par_c(instance["par_c"])
        blinded_instance.set_par(instance["par"])
        blinded_instance.set_pk(sps_public_key)
        blinded_instance.set_sid(sid)
        blinded_instance.set_vcomd(vcomd)
        blinded_instance.set_bh(h)
        blinded_instance.set_bht(ht)

        for subinstance_record in subinstance_list:
            subwitness_record = subwitness_list[index]

            # Unpack sigs
            sig_i = subwitness_record.sig_i
            R_i, S_i, T_i = sig_i["R"], sig_i["S"], sig_i["T"]

            #
            [di_1, di_2, di_3, di_4, di_5] = group.random(ZR, 5)

            # Blind sigs
            R_id = R_i * (h ** di_1)
            S_id = S_i * (h ** di_2)
            T_id = T_i * (ht ** di_3)

            # This is par_h[i] from VC par
            par_h_i = instance["par"]["par_h"][subwitness_record.i]

            par_h_id = par_h_i * (ht ** di_4)

            # VC Witness
            w_i = subwitness_record.witness
            w_id = w_i * (h ** di_5)

            temp_blinded_subwitness_record = SubWitnessRecord(
                index,
                subwitness_record.i,
                subwitness_record.vr,
                subwitness_record.copen_i,
                subwitness_record.copen_ri,
                di_1,
                di_2,
                di_3,
                di_4,
                di_5,
            )
            blinded_witness.append_subwitnesses(temp_blinded_subwitness_record)
            temp_blinded_instance_record = SubInstanceRecord(
                index,
                subinstance_record.ccom_i,
                subinstance_record.ccom_ri,
                {"R_id": R_id, "S_id": S_id, "T_id": T_id},
                par_h_id,
                w_id,
            )
            blinded_instance.append_subinstances(temp_blinded_instance_record)
            index = index + 1
Aditya Damodaran's avatar
PEP-8    
Aditya Damodaran committed
438
439
440
        return (
            dict_from_class(blinded_instance),
            dict_from_class(blinded_witness),
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
441
442
443
444
        )

    def test_witness_update(self, sid, p, i_list):
        if len(self.l_par) == 0 or len(self.l_store) == 0:
Aditya Damodaran's avatar
Aditya Damodaran committed
445
446
447
            print(
                "Abort: (Reader) Party hasn't been initialised. (HD witness update tests)"
            )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
448
449
450
451
452
453
454
455
456
457
458
459
460
            exit()

        r2 = self.l_par[0]["par"]["group"].random(ZR)
        rd = self.l_store[0]["r"] + r2
        vcomd = vector_commitment.rerand_com(
            self.l_par[0]["par"], self.l_store[0]["vcom"], r2
        )

        w = []
        instance = []

        for i_list_item in i_list:
            if self.l_store[0]["x"][i_list_item.i] != i_list_item.vr:
Aditya Damodaran's avatar
Aditya Damodaran committed
461
462
463
                print(
                    "Abort: (Reader) input tuples do not match HD state. (HD witness update tests)"
                )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
                exit()
            temp_witness_record = WitnessRecord(
                i_list_item.i,
                i_list_item.vr,
                i_list_item.copen_i,
                i_list_item.copen_ri,
                vector_commitment.generate_witness(
                    self.l_par[0]["par"],
                    i_list_item.i,
                    self.l_store[0]["x"],
                    rd,
                ),
                self.l_store[0]["par_sig"][i_list_item.i],
            )
            witness_1 = vector_commitment.generate_witness(
                self.l_par[0]["par"], i_list_item.i, self.l_store[0]["x"], rd
            )
            witness_2 = vector_commitment.generate_witness(
                self.l_par[0]["par"],
                i_list_item.i + 1,
                self.l_store[0]["x"],
                rd,
            )
            t_one_entry_update_start = time.time()
            global t_one_entry_update
            (witness_2, t_one_entry_update) = vector_commitment.update_witness(
                self.l_par[0]["par"],
                witness_1,
                i_list_item.i + 1,
                i_list_item.i,
                self.l_store[0]["x"][i_list_item.i],
                self.l_store[0]["x"][i_list_item.i] + 2,
            )

    def read(self, sid, p, i_list):

        if len(self.l_par) == 0 or len(self.l_store) == 0:
            print("Abort: (Reader) Party hasn't been initialised. (read)")
            exit()

        r_2 = self.l_par[0]["par"]["group"].random(ZR)
        r_d = self.l_store[0]["r"] + r_2
        vcom_d = vector_commitment.rerand_com(
            self.l_par[0]["par"], self.l_store[0]["vcom"], r_2
        )

        witness_records = []
        instance_records = []
        for i_list_item in i_list:
            if self.l_store[0]["x"][i_list_item.i] != i_list_item.vr:
Aditya Damodaran's avatar
Aditya Damodaran committed
514
515
516
                print(
                    "Abort: (Reader) input tuples do not match HD state. (read)"
                )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
                exit()
            t_comp_vcom_start = time.time()
            temp_witness_record = WitnessRecord(
                i_list_item.i,
                i_list_item.vr,
                i_list_item.copen_i,
                i_list_item.copen_ri,
                vector_commitment.generate_witness(
                    self.l_par[0]["par"],
                    i_list_item.i,
                    self.l_store[0]["x"],
                    r_d,
                ),
                self.l_store[0]["par_sig"][i_list_item.i],
            )

            global t_comp_vcom
            t_comp_vcom = str(time.time() - t_comp_vcom_start)
            witness_records.append(temp_witness_record)
            temp_instance_record = InstanceRecord(
                i_list_item.ccom_i, i_list_item.ccom_ri
            )
            instance_records.append(temp_instance_record)

        open_2 = self.l_par[0]["par"]["group"].random(ZR)
        open_d = self.l_store[0]["open"] + open_2

        com_d = pedersen_commitment.rerand(
            self.l_par[0]["par_c"], self.l_store[0]["com"], open_2
        )

        witness_read = {
            "sig": self.l_store[0]["sig"],
            "vcom": self.l_store[0]["vcom"],
            "com": self.l_store[0]["com"],
            "r2": r_2,
            "open2": open_2,
            "subwitness": witness_records,
        }
        instance_read = {
            "sid": self.l_store[0]["sid"],
            "sps_public_key": self.l_store[0]["sps_public_key"],
            "par": self.l_par[0]["par"],
            "par_c": self.l_par[0]["par_c"],
            "vcomd": vcom_d,
            "comd": com_d,
            "subinstance": instance_records,
        }
        self.l_store[0]["vcom"] = vcom_d
        self.l_store[0]["r"] = r_d
        self.l_store[0]["open"] = open_d
        p = random.randint(0, 99999999)
        self.obj_id = weak_reference.remember(self)
        blinded_instance, blinded_witness = self.prepare_blinded_witness(
            instance_read, witness_read
        )
        # self.l_store[0]['ics'] = bics
        f_zk.prove(
            sid, blinded_witness, blinded_instance, p, self.obj_id, updater_id
        )
        return p

    def first_read(self, sid, p):
        if len(self.l_par) == 0:
            par, par_c = f_crs.get()
            s_1 = par["group"].random(ZR)
            s_1_com = pedersen_commitment.commit_0(par_c, s_1)
            p = random.randint(0, 99999999)
            self.l_par.append(
                {
                    "sid": sid,
                    "par": par,
                    "par_c": par_c,
                    "com1": s_1_com["com"],
                    "s1": s_1,
                    "open1": s_1_com["open"],
                }
            )
            f_nym.send(
                sid,
                {"com1": s_1_com["com"], "message": "com1"},
                p,
                weak_reference.remember(self),
                updater_id,
            )
            return p

    def message_in(self, m, p):
        if m["message"] == "setup":
            vcom = vector_commitment.commit(self.l_par[0]["par"], m["x"], 0)
            com_2 = pedersen_commitment.commit_0(
                self.l_par[0]["par_c"], m["s2"]
            )
            com = self.l_par[0]["com1"] * com_2["com"]
            keys = f_reg.retrieve()
            sps_public_key = keys["sps_public_key"]
            par_sig = keys["par_sig"]
            if (
                structure_preserving_signature.verify(
                    sps_public_key,
                    m["sig"],
                    [
                        0,
                        vcom,
                        com,
                        self.l_par[0]["par"]["h"] ** self.l_par[0]["sid"],
                    ],
                )
                == 1
            ):
                self.l_store.append(
                    {
                        "sid": self.l_par[0]["sid"],
                        "par": self.l_par[0]["par"],
                        "par_c": self.l_par[0]["par_c"],
                        "sps_public_key": sps_public_key,
                        "vcom": vcom,
                        "x": m["x"],
                        "r": 0,
                        "com": com,
                        "s": self.l_par[0]["s1"] + m["s2"],
                        "open": self.l_par[0]["open1"],
                        "sig": m["sig"],
                        "par_sig": par_sig,
                    }
                )
            else:
                print(
                    "Abort: (Reader) Invalid SPS signatures in the setup phase."
                )
                exit()

        if m["message"] == "open":
            s_1 = self.l_par[0]["par"]["group"].random(ZR)
            com_1 = pedersen_commitment.commit(self.l_par[0]["par_c"], s_1)
            old_open = self.l_store[0]["open"]
            old_s_1 = self.l_store[0]["s"]

            self.l_par[0]["s1"] = s_1
            self.l_par[0]["com1"] = com_1["com"]
            self.l_par[0]["open1"] = com_1["open"]

            f_nym.send(
                self.l_store[0]["sid"],
                {
                    "message": "repcom",
                    "s1": old_s_1,
                    "open1": old_open,
                    "com1": com_1["com"],
                },
                p,
                weak_reference.remember(self),
                updater_id,
            )

        elif m["message"] == "update":
            com_2 = pedersen_commitment.commit_0(
                self.l_par[0]["par_c"], m["s2"]
            )
            com_u = self.l_par[0]["com1"] * com_2["com"]

            vcom_2 = vector_commitment.commit(self.l_par[0]["par"], m["x"], 0)
            vcom_u = self.l_store[0]["vcom"] * vcom_2

            if (
                structure_preserving_signature.verify(
                    self.l_store[0]["sps_public_key"],
                    m["sig"],
                    [
                        0,
                        vcom_u,
                        com_u,
                        self.l_par[0]["par"]["h"] ** self.l_store[0]["sid"],
                    ],
                )
                != 1
            ):
                print(
                    "Abort: (Reader) Invalid SPS signatures in the update phase."
                )

            self.l_store[0]["vcom"] = vcom_u
            self.l_store[0]["com"] = com_u
            self.l_store[0]["sig"] = m["sig"]
            self.update_table(m["x"])
            self.l_store[0]["s"] = self.l_par[0]["s1"] + m["s2"]
            self.l_store[0]["open"] = self.l_par[0]["open1"]

            print(
                "Reader: update.end, sid = "
                + str(self.l_par[0]["sid"])
                + "; p = "
                + str(p)
                + "; x = "
                + str(self.l_store[0]["x"])
            )


reader_k = Reader()
sid = random.randint(0, 99999999)
setup(db_size)


# Registration
db_list = []
empty_db_list = []


def register():
    t_register_start = time.time()
    p = reader_k.first_read(sid, 0)
    for i in range(0, db_size):
        if args.randomise == True:
Aditya Damodaran's avatar
Aditya Damodaran committed
730
            db_list.append(random.randint(0, 99))
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
        else:
            db_list.append(0)
        empty_db_list.append(0)
    t_first_update_start = time.time()
    updater.update(sid, p, db_list)
    global t_first_update
    t_first_update = str(time.time() - t_first_update_start)
    global t_register
    t_register = str(time.time() - t_register_start)
    print("lp.register.end; sid = " + str(sid) + "; P = " + str(p))


# Purchase
def purchase(i, v, v_n):
    t_purchase_start = time.time()
    p = random.randint(0, 99999999)
    com_list = reader_k.prepare_committed_record([2, 3])
    p = reader_k.read(sid, p, com_list)
    temp_db_list = list(empty_db_list)
    temp_db_list[i] = v
    temp_db_list[db_size - 1] = v_n
    updater.update(sid, p, temp_db_list)
    global t_purchase
    t_purchase = str(time.time() - t_purchase_start)
    print("lp.purchase.end; sid = " + str(sid) + "; P = " + str(p))


# Redemption
def redeem(points):
    # TODO: Store positional coms
    t_redeem_start = time.time()
    if points > reader_k.l_store[0]["x"][db_size]:
Aditya Damodaran's avatar
Aditya Damodaran committed
763
764
765
        print(
            "Abort: (Reader) Insufficient loyalty points for this operation. (redeem)"
        )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
    else:
        com_db_size = pedersen_commitment.commit(
            reader_k.l_par[0]["par_c"], db_size
        )
        com_v_n = pedersen_commitment.commit(
            reader_k.l_par[0]["par_c"], reader_k.l_store[0]["x"][db_size]
        )
        p = random.randint(0, 99999999)
        witness_rd = {
            "Vn": reader_k.l_store[0]["x"][db_size],
            "openVn": com_v_n["open"],
        }
        instance_rd = {
            "points": points,
            "comVn": com_v_n["com"],
            "comN": com_db_size["com"],
            "N": db_size,
            "openN": com_db_size["open"],
        }
Aditya Damodaran's avatar
Aditya Damodaran committed
785
        f_zkrd = FZK_RD(f_nym, keylength)
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
        f_zkrd.prove(
            sid,
            witness_rd,
            instance_rd,
            weak_reference.remember(reader_k),
            reader_k.l_par[0]["par_c"],
            group,
        )
        if (
            pedersen_commitment.verify(
                reader_k.l_par[0]["par_c"],
                com_v_n["com"],
                reader_k.l_store[0]["x"][db_size],
                com_v_n["open"],
            )
            == 0
        ):
Aditya Damodaran's avatar
Aditya Damodaran committed
803
804
805
            print(
                "Abort: (Updater) Loyalty point commitments do not hold. (redeem)"
            )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
806
807
808
            exit()

        com_list = reader_k.prepare_committed_record(
root's avatar
root committed
809
810
        #    [db_size, reader_k.l_store[0]["x"][db_size]]
            [db_size]
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
        )
        p = reader_k.read(sid, p, com_list)

        temp_db_list = list(empty_db_list)
        temp_db_list[db_size - 1] = -points
        updater.update(sid, p, temp_db_list)
        global t_redeem
        t_redeem = str(time.time() - t_redeem_start)
        print(
            "lp.redeem.end; sid = "
            + str(sid)
            + "; P = "
            + str(p)
            + "; p = "
            + str(points)
        )


def profile(start, end, val):

    com_list = []
    open_list = []

    for i in range(start, end + 1):
        com_position = pedersen_commitment.commit(
            reader_k.l_par[0]["par_c"], i
        )
        com_value = pedersen_commitment.commit(
            reader_k.l_par[0]["par_c"], reader_k.l_store[0]["x"][i + 1]
        )
        com_list.append(
            {"i": i, "comi": com_position["com"], "comv": com_value["com"]}
        )
        open_list.append(
            {
                "i": i,
                "v": reader_k.l_store[0]["x"][i + 1],
                "openi": com_position["open"],
                "openv": com_value["open"],
            }
        )

    witness_pr = open_list
    instance_pr = com_list

    p = random.randint(0, 99999999)
    f_zk_profile = FZK_PR3(f_nym, keylength)

    commitments, result = f_zk_profile.prove(
        sid,
        witness_pr,
        instance_pr,
        weak_reference.remember(reader_k),
        start,
        end,
        reader_k.l_par[0]["par_c"],
        group,
    )

    for i in range(start, end + 1):
        com_list = reader_k.prepare_committed_record(
root's avatar
root committed
872
            [i]
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
        )
        p = reader_k.read(sid, p, com_list)
        updater.update(sid, p, empty_db_list)

    print(
        "lp.profile.end; sid = "
        + str(sid)
        + "; P = "
        + str(p)
        + "; res = "
        + str(result)
    )


register()
purchase(1, 2, 3)
redeem(1)

t_profile_1_start = time.time()
profile(1, 1, 20)
t_profile_1 = str(time.time() - t_profile_1_start)

t_profile_5_start = time.time()
profile(1, 5, 20)
t_profile_5 = str(time.time() - t_profile_5_start)

t_profile_10_start = time.time()
profile(1, 10, 20)
t_profile_10 = str(time.time() - t_profile_10_start)

# Uncomment for storage cost measurements
# print("CRS (par_g) in bytes = " +sys.getsizeof(reader_k.l_par[0]['par']['par_g']))
# print("CRS (par_h) in bytes = " +sys.getsizeof(reader_k.l_par[0]['par']['par_h']))
# print("Reader DB in bytes = " + str(get_real_size(reader_k.l_store[0]['x'])))
# print("VC par in bytes = " + str(get_real_size(reader_k.l_par[0]['par'])))
# print("Vcom in bytes = " + str(get_real_size(reader_k.l_store[0]['vcom'])))
# print("Opening in bytes = " + str(sys.getsizeof(reader_k.lwit [0])))

p = random.randint(0, 99999999)
com_list = reader_k.prepare_committed_record([5])
t_one_entry_read_start = time.time()
p = reader_k.read(sid, 0, com_list)
t_one_entry_read = str(time.time() - t_one_entry_read_start)

temp_db_list = empty_db_list
temp_db_list[4] = temp_db_list[4] + 2
updater.update(sid, p, temp_db_list)

com_list = reader_k.prepare_committed_record([4, 5, 6, 7, 8])

t_five_entry_read_start = time.time()
p = reader_k.read(sid, 0, com_list)
t_five_entry_read = str(time.time() - t_five_entry_read_start)

com_list = reader_k.prepare_committed_record([5])
p = reader_k.test_witness_update(sid, 0, com_list)

output_headings = [
Aditya Damodaran's avatar
Aditya Damodaran committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
    "N",
    "DB Size",
    "Paillier Key Length",
    "First Update",
    "Computation of Vcom",
    "1 Entry Update",
    "1 Entry Read",
    "5 Entry Read",
    "Registration",
    "Purchase",
    "Redemption",
    "1 Entry Profiling",
    "5 Entry Profiling",
    "10 Entry Profiling",
    "Setup",
]
root's avatar
root committed
947
948
949
950
951
952
953
954
955
956
957
file_name = "UUHD-PPLS-Timing-data.xlsx"
if not Path(file_name).exists():
    results_workbook = Workbook()
    results_counter = 1
    results_max_row = 0
else:
    results_workbook = load_workbook(file_name)
    results_max_row = results_workbook.active.max_row
    results_counter = (
        results_workbook.active["A" + str(results_max_row)].value + 1
    )
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
958
results_worksheet = results_workbook.active
root's avatar
root committed
959
960


Aditya Damodaran's avatar
Aditya Damodaran committed
961
if results_max_row == 0:
Aditya Shyam Shankar Damodaran's avatar
Aditya Shyam Shankar Damodaran committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
    results_worksheet.append(output_headings)
timing_data = [
    results_counter,
    db_size,
    keylength,
    float(t_first_update),
    float(t_comp_vcom),
    float(t_one_entry_update),
    float(t_one_entry_read),
    float(t_five_entry_read),
    float(t_register),
    float(t_purchase),
    float(t_redeem),
    float(t_profile_1),
    float(t_profile_5),
    float(t_profile_10),
    float(t_setup),
]
results_worksheet.append(timing_data)
root's avatar
root committed
981
982
983
984
985
results_workbook.save(file_name)

draw_table(output_headings[:5], timing_data[:5])
draw_table(output_headings[5:10], timing_data[5:10])
draw_table(output_headings[10:], timing_data[10:])