Commit f4555f5c650f1005c018d18d178ec936dcae9742
- Diff rendering mode:
- inline
- side by side
pypbc.c
(36 / 9)
|   | |||
| 9 | 9 | ||
| 10 | 10 | This file contains the types and functions needed to use PBC from Python3. | |
| 11 | 11 | *******************************************************************************/ | |
| 12 | |||
| 12 | PyDoc_STRVAR(pynum_to_mpz__doc__, | ||
| 13 | "Converts a Python long type to a GMP MPZ type"); | ||
| 13 | 14 | void pynum_to_mpz(PyObject *n, mpz_t new_n) { | |
| 14 | 15 | // coerce it into a string | |
| 15 | 16 | PyObject *n_unicode = PyNumber_ToBase(n, 10); | |
| … | … | ||
| 21 | 21 | mpz_init_set_str(new_n, n_char, 10); | |
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | PyDoc_STRVAR(mpz_to_pynum__doc__, | ||
| 25 | "Converts a GMP MPZ type to a Python long"); | ||
| 24 | 26 | PyObject *mpz_to_pynum(mpz_t n) { | |
| 25 | 27 | // get the mpz as a string | |
| 26 | 28 | char *s = mpz_get_str(NULL, 10, n); | |
| … | … | ||
| 37 | 37 | return l; | |
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | PyDoc_STRVAR(get_random_prime__doc__, | ||
| 41 | "Returns a random prime in the given bitlength."); | ||
| 40 | 42 | PyObject *get_random_prime(PyObject *self, PyObject *args) { | |
| 41 | 43 | // gets the number of bits from the args | |
| 42 | 44 | int num_bits; | |
| … | … | ||
| 66 | 66 | return rand_prime; | |
| 67 | 67 | } | |
| 68 | 68 | ||
| 69 | PyDoc_STRVAR(get_random__doc__, | ||
| 70 | "Returns a random integer less than the given value."); | ||
| 69 | 71 | PyObject *get_random(PyObject *self, PyObject *args) { | |
| 70 | 72 | // gets the number of bits from the args | |
| 71 | 73 | PyObject *max; | |
| … | … | ||
| 101 | 101 | /******************************************************************************* | |
| 102 | 102 | * Params * | |
| 103 | 103 | *******************************************************************************/ | |
| 104 | PyDoc_STRVAR(Parameters__doc__, | ||
| 105 | "A representation of the parameters of an elliptic curve.\n\n\ | ||
| 106 | There are three basic ways to instantiate a Parameters object:\n\ | ||
| 107 | Parameters(param_string=s) -> a set of parameters built according to s.\n\ | ||
| 108 | Parameters(n=x, short=True/False) -> a type A1 or F curve.\n\ | ||
| 109 | Parameters(qbits=q, rbits=r, short=True/False) -> type A or E curve.\n\ | ||
| 110 | \n\ | ||
| 111 | These objects are essentially only used for creating Pairings."); | ||
| 104 | 112 | ||
| 105 | 113 | // allocate the object | |
| 106 | 114 | PyObject *Parameters_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { | |
| … | … | ||
| 249 | 249 | 0, /*tp_setattro*/ | |
| 250 | 250 | 0, /*tp_as_buffer*/ | |
| 251 | 251 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
| 252 | "Parameters objects", /* tp_doc */ | ||
| 252 | Parameters__doc__, /* tp_doc */ | ||
| 253 | 253 | 0, /* tp_traverse */ | |
| 254 | 254 | 0, /* tp_clear */ | |
| 255 | 255 | 0, /* tp_richcompare */ | |
| … | … | ||
| 274 | 274 | * Pairings * | |
| 275 | 275 | *******************************************************************************/ | |
| 276 | 276 | ||
| 277 | // personal note: to generate the a1 pairings you need, use | ||
| 278 | // pbc_param_init_a1_gen(pbc_param_t param, mpz_t n) with | ||
| 279 | // n = pqr, p, q, r large primes! | ||
| 280 | |||
| 277 | PyDoc_STRVAR(Pairing__doc__, | ||
| 278 | "Pairing(parameters) -> Pairing object\n\n\ | ||
| 279 | Represents a bilinear pairing, frequently referred to as e-hat.\n"); | ||
| 281 | 280 | // allocate the object | |
| 282 | 281 | PyObject *Pairing_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { | |
| 283 | 282 | // create the new Pairing object | |
| … | … | ||
| 408 | 408 | 0, /*tp_setattro*/ | |
| 409 | 409 | 0, /*tp_as_buffer*/ | |
| 410 | 410 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
| 411 | "Pairing objects", /* tp_doc */ | ||
| 411 | Pairing__doc__, /* tp_doc */ | ||
| 412 | 412 | 0, /* tp_traverse */ | |
| 413 | 413 | 0, /* tp_clear */ | |
| 414 | 414 | 0, /* tp_richcompare */ | |
| … | … | ||
| 432 | 432 | * Elements * | |
| 433 | 433 | *******************************************************************************/ | |
| 434 | 434 | ||
| 435 | PyDoc_STRVAR(Element__doc__, | ||
| 436 | "Represents an element of a bilinear group.\n\n\ | ||
| 437 | Basic usage:\n\ | ||
| 438 | \n\ | ||
| 439 | Element(pairing, G1||G2||GT||Zr, value=v) -> Element\n\ | ||
| 440 | Element.one(pairing, G1||G2||GT||Zr) -> identity element for the given group.\n\ | ||
| 441 | Element.zero(pairing, G1||G2||GT||Zr) -> identity element for the given group.\n\ | ||
| 442 | Element.random(pairing, G1||G2||GT||Zr) -> random element of the given group.\n\ | ||
| 443 | Element.from_hash(pairing, G1||G2||GT||Zr -> element whose value is determined by the given hash value.\n\ | ||
| 444 | \n\ | ||
| 445 | Most of the basic arithmetic operations apply. Please note that many of them\n\ | ||
| 446 | do not make sense between groups, and that not all of these are checked for."); | ||
| 447 | |||
| 435 | 448 | Element *Element_create(void) { | |
| 436 | 449 | // build ourselves | |
| 437 | 450 | Element *self = (Element*)(&ElementType)->tp_alloc(&ElementType, 0); | |
| … | … | ||
| 534 | 534 | } | |
| 535 | 535 | ||
| 536 | 536 | PyObject *Element_from_hash(PyObject *cls, PyObject *args) { | |
| 537 | // required arguments are the pairing, the group, and the value to be hashed | ||
| 537 | // required arguments are the pairing, the group, and the hashed value | ||
| 538 | 538 | PyObject *pypairing; | |
| 539 | 539 | enum Group group; | |
| 540 | 540 | char *hash; | |
| … | … | ||
| 1092 | 1092 | 0, /*tp_setattro*/ | |
| 1093 | 1093 | 0, /*tp_as_buffer*/ | |
| 1094 | 1094 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
| 1095 | "Element objects", /* tp_doc */ | ||
| 1095 | Element__doc__, /* tp_doc */ | ||
| 1096 | 1096 | 0, /* tp_traverse */ | |
| 1097 | 1097 | 0, /* tp_clear */ | |
| 1098 | 1098 | Element_cmp, /* tp_richcompare */ |

