-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpypbc.c
1260 lines (1195 loc) · 53.8 KB
/
pypbc.c
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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
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
514
515
516
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
730
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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
872
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pypbc.h"
#include <stdio.h>
/*******************************************************************************
* pypbc.c *
* *
* Modifications by Jemtaly *
* Copyright (c) 2024 *
* *
* Modifications by Joseph deBlaquiere *
* Copyright (c) 2017 *
* *
* Originally written by Geremy Condra *
* Licensed under GPLv3 *
* Released 11 October 2009 *
* *
* This file contains the types and functions needed to use PBC from Python 3. *
*******************************************************************************/
// initialize a GMP integer from a Python number
void mpz_init_from_pynum(mpz_t mpz_n, PyObject *py_n) {
// convert the Python number to a C string
PyObject *py_n_unicode = PyNumber_ToBase(py_n, 10);
PyObject *py_n_bytes = PyUnicode_AsASCIIString(py_n_unicode);
char *str_n = PyBytes_AsString(py_n_bytes);
// initialize the GMP integer from the ASCII string
mpz_init_set_str(mpz_n, str_n, 10);
// decrement the reference count on the objects
Py_DECREF(py_n_unicode);
Py_DECREF(py_n_bytes);
}
// get a Python number from a GMP integer
PyObject *mpz_to_pynum(mpz_t mpz_n) {
// convert the GMP integer to a C string
char *str_n = mpz_get_str(NULL, 10, mpz_n);
// convert the C string to a Python number
PyObject *py_n = PyLong_FromString(str_n, NULL, 10);
// free the string
free(str_n);
return py_n;
}
/*******************************************************************************
* Params *
*******************************************************************************/
PyDoc_STRVAR(Parameters__doc__,
"A representation of the parameters of an elliptic curve.\n"
"\n"
"There are three basic ways to instantiate a Parameters object:\n"
"\n"
"Parameters(string: str) -> Parameters\n"
"\n"
"These objects are essentially only used for creating Pairings.");
Parameters *Parameters_create(void) {
// allocate the object
Parameters *params = (Parameters *)ParametersType.tp_alloc(&ParametersType, 0);
// check if the object was allocated
if (!params) {
PyErr_SetString(PyExc_TypeError, "could not create Parameters object");
return NULL;
}
// set the ready flag to 0
params->ready = 0;
return params;
}
PyObject *Parameters_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
// required argument is the parameter string
char *string = NULL;
if (!PyArg_ParseTuple(args, "s", &string)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected a string");
return NULL;
}
// create the object
Parameters *params = Parameters_create();
// initialize the parameters from a string
if (pbc_param_init_set_str(params->pbc_params, string)) {
PyErr_SetString(PyExc_ValueError, "could not parse parameters from string");
return NULL;
}
// set the ready flag
params->ready = 1;
return (PyObject *)params;
}
void Parameters_dealloc(Parameters *params) {
// clear the parameters if they're ready
if (params->ready) {
pbc_param_clear(params->pbc_params);
}
// free the object
Py_TYPE(params)->tp_free((PyObject *)params);
}
PyObject *Parameters_str(PyObject *py_params) {
// cast the argument
Parameters *params = (Parameters *)py_params;
// declare a buffer
char buffer[4096];
// open a file in memory
FILE *fp = fmemopen((void *)buffer, sizeof(buffer), "w+");
// check if the file was opened
if (fp == NULL) {
PyErr_SetString(PyExc_IOError, "could not write parameters to buffer");
return NULL;
}
// write the parameters to the buffer
pbc_param_out_str(fp, params->pbc_params);
// close the file
fclose(fp);
// return the buffer as a string
return PyUnicode_FromString(buffer);
}
PyMemberDef Parameters_members[] = {
{NULL},
};
PyMethodDef Parameters_methods[] = {
{NULL},
};
PyTypeObject ParametersType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pypbc.Parameters", /* tp_name */
sizeof(Parameters), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Parameters_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
Parameters_str, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
Parameters_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Parameters__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Parameters_methods, /* tp_methods */
Parameters_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
Parameters_new, /* tp_new */
};
/*******************************************************************************
* Pairings *
*******************************************************************************/
PyDoc_STRVAR(Pairing__doc__,
"Represents a bilinear pairing, frequently referred to as e-hat.\n"
"\n"
"Basic usage:\n"
"\n"
"Pairing(params: Parameters) -> Pairing\n"
"\n"
"This object is used to apply the bilinear map to two elements.");
Pairing *Pairing_create(void) {
// allocate the object
Pairing *pairing = (Pairing *)PairingType.tp_alloc(&PairingType, 0);
// check if the object was allocated
if (!pairing) {
PyErr_SetString(PyExc_TypeError, "could not create Pairing object");
return NULL;
}
// set the ready flag to 0
pairing->ready = 0;
return pairing;
}
PyObject *Pairing_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
// required argument is the parameters
PyObject *py_params;
if (!PyArg_ParseTuple(args, "O!", &ParametersType, &py_params)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Parameters object");
return NULL;
}
// cast the argument
Parameters *params = (Parameters *)py_params;
// create the object
Pairing *pairing = Pairing_create();
// initialize the pairing with the parameters
pairing_init_pbc_param(pairing->pbc_pairing, params->pbc_params);
// set the ready flag
pairing->ready = 1;
return (PyObject *)pairing;
}
void Pairing_dealloc(Pairing *pairing) {
// clear the pairing if it's ready
if (pairing->ready) {
pairing_clear(pairing->pbc_pairing);
}
// free the object
Py_TYPE(pairing)->tp_free((PyObject *)pairing);
}
PyObject *Pairing_apply(PyObject *py_pairing, PyObject *args) {
// we expect two elements
PyObject *py_lft;
PyObject *py_rgt;
if (!PyArg_ParseTuple(args, "O!O!", &ElementType, &py_lft, &ElementType, &py_rgt)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected two Elements");
return NULL;
}
// declare the result element
Element *ele_res;
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
Element *ele_lft = (Element *)py_lft;
Element *ele_rgt = (Element *)py_rgt;
// check the groups of the elements
if (ele_lft->pbc_element->field == pairing->pbc_pairing->G1 && ele_rgt->pbc_element->field == pairing->pbc_pairing->G2) {
// build the result element and initialize it with the pairing and group
ele_res = Element_create();
element_init_GT(ele_res->pbc_element, pairing->pbc_pairing);
ele_res->pairing = ele_lft->pairing;
// apply the pairing
pairing_apply(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element, pairing->pbc_pairing);
} else if (ele_lft->pbc_element->field == pairing->pbc_pairing->G2 && ele_rgt->pbc_element->field == pairing->pbc_pairing->G1) {
// build the result element and initialize it with the pairing and group
ele_res = Element_create();
element_init_GT(ele_res->pbc_element, pairing->pbc_pairing);
ele_res->pairing = ele_lft->pairing;
// apply the pairing
pairing_apply(ele_res->pbc_element, ele_rgt->pbc_element, ele_lft->pbc_element, pairing->pbc_pairing);
} else {
PyErr_SetString(PyExc_ValueError, "only Elements in G1 and G2 can be paired");
return NULL;
}
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Pairing_order(PyObject *py_pairing) {
// cast the argument
Pairing *pairing = (Pairing *)py_pairing;
// return the r value
return mpz_to_pynum(pairing->pbc_pairing->r);
}
PyObject *Pairing_is_symmetric(PyObject *py_pairing) {
// cast the argument
Pairing *pairing = (Pairing *)py_pairing;
// return whether the pairing is symmetric
if (pairing->pbc_pairing->G1 == pairing->pbc_pairing->G2) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
PyMemberDef Pairing_members[] = {
{NULL},
};
PyMethodDef Pairing_methods[] = {
{"apply", (PyCFunction)Pairing_apply, METH_VARARGS, "Applies the pairing."},
{"order", (PyCFunction)Pairing_order, METH_NOARGS, "Returns the order of the pairing."},
{"is_symmetric", (PyCFunction)Pairing_is_symmetric, METH_NOARGS, "Returns whether the pairing is symmetric."},
{NULL},
};
PyTypeObject PairingType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pypbc.Pairing", /* tp_name */
sizeof(Pairing), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Pairing_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Pairing__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Pairing_methods, /* tp_methods */
Pairing_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
Pairing_new, /* tp_new */
};
/*******************************************************************************
* Elements *
*******************************************************************************/
PyDoc_STRVAR(Element__doc__,
"Represents an element of a bilinear group.\n"
"\n"
"Basic usage:\n"
"\n"
"Element(pairing: Pairing, group: int, string: str) -> Element\n"
"\n"
"Most of the basic arithmetic operations apply. Please note that many of them\n"
"do not make sense between groups, and that not all of these are checked for.");
Element *Element_create(void) {
// allocate the object
Element *element = (Element *)ElementType.tp_alloc(&ElementType, 0);
// check if the object was allocated
if (!element) {
PyErr_SetString(PyExc_TypeError, "could not create Element object");
return NULL;
}
// set the ready flag to 0
element->ready = 0;
return element;
}
PyObject *Element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
char *string = NULL;
if (!PyArg_ParseTuple(args, "O!is", &PairingType, &py_pairing, &group, &string)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object, group, and string");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT: element_init_GT(element->pbc_element, pairing->pbc_pairing); break;
case Zr: element_init_Zr(element->pbc_element, pairing->pbc_pairing); break;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// set the element to the string
if (element_set_str(element->pbc_element, string, 10) == 0) {
PyErr_SetString(PyExc_ValueError, "could not parse element from string");
return NULL;
}
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
void Element_dealloc(Element *element) {
// clear the element and decrement the reference count on the pairing if it's ready
if (element->ready){
element_clear(element->pbc_element);
Py_DECREF(element->pairing);
}
// free the object
Py_TYPE(element)->tp_free((PyObject *)element);
}
PyObject *Element_from_int(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
PyObject *py_val;
if (!PyArg_ParseTuple(args, "O!O!", &PairingType, &py_pairing, &PyLong_Type, &py_val)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object and number");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// convert the number to an mpz_t
mpz_t mpz_val;
mpz_init_from_pynum(mpz_val, py_val);
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
element_init_Zr(element->pbc_element, pairing->pbc_pairing);
element->pairing = pairing;
// set the element to the number
element_set_mpz(element->pbc_element, mpz_val);
// clear the mpz_t
mpz_clear(mpz_val);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_zero(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
if (!PyArg_ParseTuple(args, "O!i", &PairingType, &py_pairing, &group)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object and group");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT: element_init_GT(element->pbc_element, pairing->pbc_pairing); break;
case Zr: element_init_Zr(element->pbc_element, pairing->pbc_pairing); break;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// set the element to 0
element_set0(element->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_one(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
if (!PyArg_ParseTuple(args, "O!i", &PairingType, &py_pairing, &group)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object and group");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT: element_init_GT(element->pbc_element, pairing->pbc_pairing); break;
case Zr: element_init_Zr(element->pbc_element, pairing->pbc_pairing); break;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// set the element to 1
element_set1(element->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_random(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
if (!PyArg_ParseTuple(args, "O!i", &PairingType, &py_pairing, &group)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object and group");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT: element_init_GT(element->pbc_element, pairing->pbc_pairing); break;
case Zr: element_init_Zr(element->pbc_element, pairing->pbc_pairing); break;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// make the element random
element_random(element->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_from_hash(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
PyObject *py_bytes;
if (!PyArg_ParseTuple(args, "O!iO!", &PairingType, &py_pairing, &group, &PyBytes_Type, &py_bytes)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object, group, and bytes");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT: element_init_GT(element->pbc_element, pairing->pbc_pairing); break;
case Zr: element_init_Zr(element->pbc_element, pairing->pbc_pairing); break;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// convert the bytes to an element
int size = PyBytes_Size(py_bytes);
unsigned char *bytes = (unsigned char *)PyBytes_AsString(py_bytes);
element_from_hash(element->pbc_element, bytes, size);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_from_bytes(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
PyObject *py_bytes;
if (!PyArg_ParseTuple(args, "O!iO!", &PairingType, &py_pairing, &group, &PyBytes_Type, &py_bytes)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object, group, and bytes");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT: element_init_GT(element->pbc_element, pairing->pbc_pairing); break;
case Zr: element_init_Zr(element->pbc_element, pairing->pbc_pairing); break;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// check the size of the bytes
if (PyBytes_Size(py_bytes) != element_length_in_bytes(element->pbc_element)) {
Py_DECREF(element);
PyErr_SetString(PyExc_ValueError, "invalid number of bytes");
return NULL;
}
// convert the bytes to an element
unsigned char *bytes = (unsigned char *)PyBytes_AsString(py_bytes);
element_from_bytes(element->pbc_element, bytes);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_from_bytes_compressed(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
PyObject *py_bytes;
if (!PyArg_ParseTuple(args, "O!iO!", &PairingType, &py_pairing, &group, &PyBytes_Type, &py_bytes)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object, group, and bytes");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT:
case Zr: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "only Elements in G1 or G2 can be created from compressed bytes"); return NULL;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// check the size of the bytes
if (PyBytes_Size(py_bytes) != element_length_in_bytes_compressed(element->pbc_element)) {
Py_DECREF(element);
PyErr_SetString(PyExc_ValueError, "invalid number of bytes");
return NULL;
}
// convert the bytes to an element
unsigned char *bytes = (unsigned char *)PyBytes_AsString(py_bytes);
element_from_bytes_compressed(element->pbc_element, bytes);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_from_bytes_x_only(PyObject *cls, PyObject *args) {
// required arguments are the pairing and the group
PyObject *py_pairing;
enum Group group;
PyObject *py_bytes;
if (!PyArg_ParseTuple(args, "O!iO!", &PairingType, &py_pairing, &group, &PyBytes_Type, &py_bytes)) {
PyErr_SetString(PyExc_TypeError, "could not parse arguments, expected Pairing object, group, and bytes");
return NULL;
}
// cast the arguments
Pairing *pairing = (Pairing *)py_pairing;
// build the result element and initialize it with the pairing and group
Element *element = Element_create();
switch (group) {
case G1: element_init_G1(element->pbc_element, pairing->pbc_pairing); break;
case G2: element_init_G2(element->pbc_element, pairing->pbc_pairing); break;
case GT:
case Zr: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "only Elements in G1 or G2 can be created from x-only bytes"); return NULL;
default: Py_DECREF(element); PyErr_SetString(PyExc_ValueError, "invalid group"); return NULL;
}
element->pairing = pairing;
// check the size of the bytes
if (PyBytes_Size(py_bytes) != element_length_in_bytes_x_only(element->pbc_element)) {
Py_DECREF(element);
PyErr_SetString(PyExc_ValueError, "invalid number of bytes");
return NULL;
}
// convert the bytes to an element
unsigned char *bytes = (unsigned char *)PyBytes_AsString(py_bytes);
element_from_bytes_x_only(element->pbc_element, bytes);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(element->pairing);
element->ready = 1;
return (PyObject *)element;
}
PyObject *Element_to_bytes(PyObject *py_element) {
// cast the argument
Element *element = (Element *)py_element;
// get the size of the buffer and allocate it
int size = element_length_in_bytes(element->pbc_element);
unsigned char buffer[size];
// convert the element to bytes
element_to_bytes(buffer, element->pbc_element);
// return the buffer as a bytes object
return PyBytes_FromStringAndSize((char *)buffer, size);
}
Py_hash_t Element_hash(PyObject *py_element) {
// cast the argument
Element *element = (Element *)py_element;
// get the size of the buffer and allocate it
int size = element_length_in_bytes(element->pbc_element);
unsigned char buffer[size];
// convert the element to bytes
element_to_bytes(buffer, element->pbc_element);
// initialize the hash
Py_uhash_t hash = 14695981039346656037U;
// iterate over the string
for (Py_ssize_t i = 0; i < size; i++) {
hash ^= (Py_uhash_t)buffer[i];
hash *= 1099511628211U;
}
// add the pointer to the hash
Py_uhash_t ptr = (Py_uhash_t)element->pbc_element->field;
hash ^= ptr >> 4 | ptr << (8 * sizeof(ptr) - 4);
// check if the hash is invalid
if (hash == (Py_uhash_t)-1) {
hash = (Py_uhash_t)-2;
}
// return the hash
return (Py_hash_t)hash;
}
PyObject *Element_to_bytes_compressed(PyObject *py_element) {
// cast the argument
Element *element = (Element *)py_element;
// make sure the element is in G1 or G2
if (element->pbc_element->field != element->pairing->pbc_pairing->G1 && element->pbc_element->field != element->pairing->pbc_pairing->G2) {
PyErr_SetString(PyExc_TypeError, "only Elements in G1 or G2 can be converted to compressed bytes");
return NULL;
}
// get the size of the buffer and allocate it
int size = element_length_in_bytes_compressed(element->pbc_element);
unsigned char buffer[size];
// convert the element to compressed bytes
element_to_bytes_compressed(buffer, element->pbc_element);
// return the buffer as a bytes object
return PyBytes_FromStringAndSize((char *)buffer, size);
}
PyObject *Element_to_bytes_x_only(PyObject *py_element) {
// cast the argument
Element *element = (Element *)py_element;
// make sure the element is in G1 or G2
if (element->pbc_element->field != element->pairing->pbc_pairing->G1 && element->pbc_element->field != element->pairing->pbc_pairing->G2) {
PyErr_SetString(PyExc_TypeError, "only Elements in G1 or G2 can be converted to x-only bytes");
return NULL;
}
// get the size of the buffer and allocate it
int size = element_length_in_bytes_x_only(element->pbc_element);
unsigned char buffer[size];
// convert the element to x-only bytes
element_to_bytes_x_only(buffer, element->pbc_element);
// return the buffer as a bytes object
return PyBytes_FromStringAndSize((char *)buffer, size);
}
PyObject *Element_add(PyObject *py_lft, PyObject *py_rgt) {
// check the type of arguments
if (!PyObject_TypeCheck(py_lft, &ElementType) || !PyObject_TypeCheck(py_rgt, &ElementType)) {
PyErr_SetString(PyExc_TypeError, "operands must be Elements");
return NULL;
}
// declare the result element
Element *ele_res;
// convert both objects to Elements
Element *ele_lft = (Element *)py_lft;
Element *ele_rgt = (Element *)py_rgt;
// make sure they're in the same ring
if (ele_lft->pbc_element->field != ele_rgt->pbc_element->field) {
PyErr_SetString(PyExc_ValueError, "only Elements in the same group can be added");
return NULL;
}
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// add the two elements
element_add(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_sub(PyObject *py_lft, PyObject *py_rgt) {
// check the type of arguments
if (!PyObject_TypeCheck(py_lft, &ElementType) || !PyObject_TypeCheck(py_rgt, &ElementType)) {
PyErr_SetString(PyExc_TypeError, "operands must be Elements");
return NULL;
}
// declare the result element
Element *ele_res;
// convert both objects to Elements
Element *ele_lft = (Element *)py_lft;
Element *ele_rgt = (Element *)py_rgt;
// make sure they're in the same ring
if (ele_lft->pbc_element->field != ele_rgt->pbc_element->field) {
PyErr_SetString(PyExc_ValueError, "only Elements in the same group can be subtracted");
return NULL;
}
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// subtract the two elements
element_sub(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_div(PyObject *py_lft, PyObject *py_rgt) {
// check the type of arguments
if (!PyObject_TypeCheck(py_lft, &ElementType) || !PyObject_TypeCheck(py_rgt, &ElementType)) {
PyErr_SetString(PyExc_TypeError, "operands must be Elements");
return NULL;
}
// declare the result element
Element *ele_res;
// convert both objects to Elements
Element *ele_lft = (Element *)py_lft;
Element *ele_rgt = (Element *)py_rgt;
// make sure they're in the same ring or one is in Zr
if (ele_lft->pbc_element->field != ele_rgt->pbc_element->field) {
PyErr_SetString(PyExc_ValueError, "only Elements in the same group can be divided");
return NULL;
}
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// divide the two elements
element_div(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_pow(PyObject *py_lft, PyObject *py_rgt, PyObject *py_mod) {
// check the type of the first argument
if (!PyObject_TypeCheck(py_lft, &ElementType)) {
PyErr_SetString(PyExc_TypeError, "the base must be an Element");
return NULL;
}
// declare the result element
Element *ele_res;
// convert the first argument to an Element
Element *ele_lft = (Element *)py_lft;
// check the type of the second argument
if (PyObject_TypeCheck(py_rgt, &ElementType)) {
// convert the second argument to an Element
Element *ele_rgt = (Element *)py_rgt;
// make sure the second element is in Zr
if (ele_rgt->pbc_element->field == ele_lft->pairing->pbc_pairing->Zr && (ele_lft->pbc_element->field == ele_lft->pairing->pbc_pairing->Zr || ele_lft->pbc_element->field->pairing)) {
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// raise the element to the power
element_pow_zn(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element);
} else {
PyErr_SetString(PyExc_TypeError, "if the exponent is an Element, it must be in Zr and the base must be in Zr, G1, G2, or GT");
return NULL;
}
} else if (PyLong_Check(py_rgt)) {
// convert it to an mpz
mpz_t mpz_lft;
mpz_init_from_pynum(mpz_lft, py_rgt);
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// raise the element to the power
element_pow_mpz(ele_res->pbc_element, ele_lft->pbc_element, mpz_lft);
// clean up the mpz
mpz_clear(mpz_lft);
} else {
PyErr_SetString(PyExc_TypeError, "the exponent must be an Element or an integer");
return NULL;
}
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_mul(PyObject *py_lft, PyObject *py_rgt) {
// declare the result element
Element *ele_res;
// check the type of arguments
if (PyObject_TypeCheck(py_lft, &ElementType) && PyObject_TypeCheck(py_rgt, &ElementType)) {
// convert both objects to Elements
Element *ele_lft = (Element *)py_lft;
Element *ele_rgt = (Element *)py_rgt;
// make sure they're in the same ring or one is in Zr
if (ele_lft->pbc_element->field == ele_rgt->pbc_element->field) {
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// multiply the two elements
element_mul(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element);
} else if (ele_rgt->pbc_element->field == ele_lft->pairing->pbc_pairing->Zr && ele_lft->pbc_element->field->pairing) {
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// multiply the two elements
element_mul_zn(ele_res->pbc_element, ele_lft->pbc_element, ele_rgt->pbc_element);
} else if (ele_lft->pbc_element->field == ele_rgt->pairing->pbc_pairing->Zr && ele_rgt->pbc_element->field->pairing) {
// build and initialize the result element to the same group as the right element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_rgt->pbc_element);
ele_res->pairing = ele_rgt->pairing;
// multiply the two elements
element_mul_zn(ele_res->pbc_element, ele_rgt->pbc_element, ele_lft->pbc_element);
} else {
PyErr_SetString(PyExc_ValueError, "only Elements in the same group can be multiplied, or one must be in Zr and the other in G1, G2, or GT");
return NULL;
}
} else if (PyLong_Check(py_rgt)) {
// convert the left object to an Element
Element *ele_lft = (Element *)py_lft;
// convert the right object to an mpz
mpz_t mpz_rgt;
mpz_init_from_pynum(mpz_rgt, py_rgt);
// build and initialize the result element to the same group as the left element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_lft->pbc_element);
ele_res->pairing = ele_lft->pairing;
// multiply the two elements
element_mul_mpz(ele_res->pbc_element, ele_lft->pbc_element, mpz_rgt);
// clean up the mpz
mpz_clear(mpz_rgt);
} else if (PyLong_Check(py_lft)) {
// convert the right object to an Element
Element *ele_rgt = (Element *)py_rgt;
// convert the left object to an mpz
mpz_t mpz_lft;
mpz_init_from_pynum(mpz_lft, py_lft);
// build and initialize the result element to the same group as the right element
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_rgt->pbc_element);
ele_res->pairing = ele_rgt->pairing;
// multiply the two elements
element_mul_mpz(ele_res->pbc_element, ele_rgt->pbc_element, mpz_lft);
// clean up the mpz
mpz_clear(mpz_lft);
} else {
PyErr_SetString(PyExc_TypeError, "operands must be Elements or integers");
return NULL;
}
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_neg(PyObject *py_arg) {
// declare the result element
Element *ele_res;
// cast the argument
Element *ele_arg = (Element *)py_arg;
// build and initialize the result element to the same group as the argument
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_arg->pbc_element);
ele_res->pairing = ele_arg->pairing;
// negate the element
element_neg(ele_res->pbc_element, ele_arg->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_invert(PyObject *py_arg) {
// declare the result element
Element *ele_res;
// cast the argument
Element *ele_arg = (Element *)py_arg;
// build and initialize the result element to the same group as the argument
ele_res = Element_create();
element_init_same_as(ele_res->pbc_element, ele_arg->pbc_element);
ele_res->pairing = ele_arg->pairing;
// invert the element
element_invert(ele_res->pbc_element, ele_arg->pbc_element);
// increment the reference count on the pairing and set the ready flag
Py_INCREF(ele_res->pairing);
ele_res->ready = 1;
return (PyObject *)ele_res;
}
PyObject *Element_cmp(PyObject *py_lft, PyObject *py_rgt, int op) {
// check the type of arguments
if (!PyObject_TypeCheck(py_lft, &ElementType) || !PyObject_TypeCheck(py_rgt, &ElementType)) {
PyErr_SetString(PyExc_TypeError, "operands must be Elements");
return NULL;
}
// convert both objects to Elements
Element *ele_lft = (Element *)py_lft;
Element *ele_rgt = (Element *)py_rgt;
// make sure they're in the same ring
if (ele_lft->pbc_element->field != ele_rgt->pbc_element->field) {
PyErr_SetString(PyExc_ValueError, "only Elements in the same group can be compared");
return NULL;
}
// compare the two elements
switch (op) {
case Py_EQ:
if (element_cmp(ele_lft->pbc_element, ele_rgt->pbc_element)) {
Py_RETURN_FALSE;
} else {
Py_RETURN_TRUE;
}
case Py_NE:
if (element_cmp(ele_lft->pbc_element, ele_rgt->pbc_element)) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
default:
PyErr_SetString(PyExc_ValueError, "only == and != comparisons are supported");
return NULL;
}
}
PyObject *Element_is0(PyObject *py_element) {
// cast the argument
Element *element = (Element *)py_element;
// check if the element is 0
if (element_is0(element->pbc_element)) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}