Commit be96115ddce9cf7c1cd97f3309effaeeee259fe5
- Diff rendering mode:
- inline
- side by side
KSW.py
(80 / 0)
|   | |||
| 1 | #! /usr/bin/env python3 | ||
| 2 | |||
| 3 | """ | ||
| 4 | KSW.py | ||
| 5 | |||
| 6 | Written by Geremy Condra | ||
| 7 | Licensed under GPLv3 | ||
| 8 | Released 15 October 2009 | ||
| 9 | |||
| 10 | An implementation of the Katz-Sahai-Waters predicate | ||
| 11 | encryption scheme in Python3. | ||
| 12 | """ | ||
| 13 | |||
| 14 | from pypbc import * | ||
| 15 | |||
| 16 | ############################################# | ||
| 17 | # Utilities # | ||
| 18 | ############################################# | ||
| 19 | |||
| 20 | def dot_product(x, y, n): | ||
| 21 | """Takes the dot product of lists x and y over F_n""" | ||
| 22 | |||
| 23 | if len(x) != len(y): | ||
| 24 | raise ValueError("x and y must be the same length!") | ||
| 25 | if not isinstance(n, int): | ||
| 26 | raise ValueError("n must be an integer!") | ||
| 27 | |||
| 28 | return sum([(x_i * y[i]) % n for i, x_i in enumerate(x)]) | ||
| 29 | |||
| 30 | ############################################# | ||
| 31 | # Cryptosystem # | ||
| 32 | ############################################# | ||
| 33 | |||
| 34 | class Cryptosystem: | ||
| 35 | |||
| 36 | params = None | ||
| 37 | pairing = None | ||
| 38 | # generates G | ||
| 39 | g_G = None | ||
| 40 | # generates G_p | ||
| 41 | g_G_p = None | ||
| 42 | # generates G_q | ||
| 43 | g_G_q = None | ||
| 44 | # generates G_r | ||
| 45 | g_G_r = None | ||
| 46 | # random element of G_r | ||
| 47 | R0 = None | ||
| 48 | # Rs[] = ((Ri,0), (Ri, 1)) * n, R e G_r | ||
| 49 | Rs = None | ||
| 50 | # Hs[] = ((Hi,0), (Hi, 1)) * n, H e G_p | ||
| 51 | Hs = None | ||
| 52 | |||
| 53 | def __init__(self, i) -> "(PK, SK)": | ||
| 54 | # select p, q, r | ||
| 55 | # make n | ||
| 56 | # build the params | ||
| 57 | # build the pairing | ||
| 58 | # find the generators for the G_p, G_q, and G_r subgroups | ||
| 59 | # choose R0 | ||
| 60 | # choose the random R's | ||
| 61 | # choose the random H's | ||
| 62 | # calculate Q | ||
| 63 | pass | ||
| 64 | |||
| 65 | def keygen(sk: "master secret key", f: "description of a predicate") -> "SK_f": | ||
| 66 | pass | ||
| 67 | |||
| 68 | def encrypt(pk: "public key", i: "attribute", m: "message") -> "ciphertext": | ||
| 69 | pass | ||
| 70 | |||
| 71 | def decrypt(c: "ciphertext", sk_f: "secret key corresponding to predicate f") -> "message or T": | ||
| 72 | pass | ||
| 73 | |||
| 74 | ############################################# | ||
| 75 | # Test logic # | ||
| 76 | ############################################# | ||
| 77 | |||
| 78 | if __name__ == "__main__": | ||
| 79 | |||
| 80 | import sys |
pypbc.c
(40 / 1)
|   | |||
| 20 | 20 | mpz_init_set_str(new_n, n_char, 10); | |
| 21 | 21 | } | |
| 22 | 22 | ||
| 23 | PyObject *get_random_prime(PyObject *self, PyObject *args) { | ||
| 24 | // gets the number of bits from the args | ||
| 25 | int num_bits; | ||
| 26 | if (!PyArg_ParseTuple(args, "i", &num_bits)) { | ||
| 27 | PyErr_SetString(PyExc_TypeError, "could not parse arguments"); | ||
| 28 | return NULL; | ||
| 29 | } | ||
| 30 | |||
| 31 | // create the storage number | ||
| 32 | mpz_t c, p; | ||
| 33 | mpz_init(c); | ||
| 34 | mpz_init(p); | ||
| 23 | 35 | ||
| 36 | // get a random n-bit number | ||
| 37 | pbc_mpz_randomb(c, num_bits); | ||
| 38 | |||
| 39 | // get the next prime | ||
| 40 | mpz_nextprime(p, c); | ||
| 41 | |||
| 42 | // get the mpz as a string | ||
| 43 | char *str_prime = mpz_get_str(NULL, 10, p); | ||
| 44 | |||
| 45 | // convert the string to a python long | ||
| 46 | PyObject *rand_prime = PyLong_FromString(str_prime, NULL, 10); | ||
| 47 | |||
| 48 | // clean up the mpz's | ||
| 49 | mpz_clear(c); | ||
| 50 | mpz_clear(p); | ||
| 51 | |||
| 52 | // clean up the str | ||
| 53 | free(str_prime); | ||
| 54 | return rand_prime; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 24 | 58 | /******************************************************************************* | |
| 25 | 59 | * Params * | |
| 26 | 60 | *******************************************************************************/ | |
| … | … | ||
| 1026 | 1026 | * Module * | |
| 1027 | 1027 | *******************************************************************************/ | |
| 1028 | 1028 | ||
| 1029 | PyMethodDef pypbc_methods[] = { | ||
| 1030 | {"get_random_prime", get_random_prime, METH_VARARGS, "get a random n-bit prime"}, | ||
| 1031 | {NULL, NULL, 0, NULL} | ||
| 1032 | }; | ||
| 1033 | |||
| 1029 | 1034 | PyModuleDef pypbc_module = { | |
| 1030 | 1035 | PyModuleDef_HEAD_INIT, | |
| 1031 | 1036 | "pypbc", | |
| 1032 | 1037 | "pypbc", | |
| 1033 | 1038 | -1, | |
| 1034 | NULL, NULL, NULL, NULL, NULL | ||
| 1039 | pypbc_methods | ||
| 1035 | 1040 | }; | |
| 1036 | 1041 | ||
| 1037 | 1042 | PyMODINIT_FUNC |

