From 3baeb11a8fbcfcdbc31976d421f17b85503b3ecd Mon Sep 17 00:00:00 2001 From: WuKong Date: Fri, 4 Sep 2015 09:25:34 +0200 Subject: init attribute-based encryption Change-Id: Iba1a3d722110abf747a0fba366f3ebc911d25b25 --- moon-abe/pbc-0.5.14/arith/fp.c | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 moon-abe/pbc-0.5.14/arith/fp.c (limited to 'moon-abe/pbc-0.5.14/arith/fp.c') diff --git a/moon-abe/pbc-0.5.14/arith/fp.c b/moon-abe/pbc-0.5.14/arith/fp.c new file mode 100644 index 00000000..e0127a8e --- /dev/null +++ b/moon-abe/pbc-0.5.14/arith/fp.c @@ -0,0 +1,49 @@ +// F_p initialization. +// +// Specific implementations of F_p are found in naivefp.c, fastfp.c, fasterfp.c +// and montfp.c. For pairing-based cryptosystems, montfp.c is the fastest. +// I keep all versions around for testing, and also to show off the modularity +// of the code. + +#include +#include +#include // for intptr_t +#include +#include +#include "pbc_utils.h" +#include "pbc_field.h" +#include "pbc_fp.h" + +// By default, use the montfp.c implementation of F_p. After +// pbc_tweak_use_fp(), future field_init_fp calls will use the specified +// implementation. This is useful for benchmarking and testing. +static void (*option_fpinit) (field_ptr f, mpz_t prime) = field_init_mont_fp; + +void pbc_tweak_use_fp(char *s) { + if (!strcmp(s, "naive")) { + option_fpinit = field_init_naive_fp; + } else if (!strcmp(s, "fast")) { + option_fpinit = field_init_fast_fp; + } else if (!strcmp(s, "faster")) { + option_fpinit = field_init_faster_fp; + } else if (!strcmp(s, "mont")) { + option_fpinit = field_init_mont_fp; + } else { + pbc_error("no such Fp implementation: %s", s); + } +} + +void field_init_fp(field_ptr f, mpz_t modulus) { + if (mpz_fits_ulong_p(modulus)) { + // If this case mattered, I'd have written a F_p implementation specialized + // for moduli that fits into machine words. + field_init_naive_fp(f, modulus); + } else { + if (mpz_odd_p(modulus)) { + option_fpinit(f, modulus); + } else { + // montfp.c only supports odd moduli. + field_init_faster_fp(f, modulus); + } + } +} -- cgit 1.2.3-korg