diff options
Diffstat (limited to 'moon-abe/pbc-0.5.14/benchmark')
-rw-r--r-- | moon-abe/pbc-0.5.14/benchmark/REPORT.BAT | 18 | ||||
-rw-r--r-- | moon-abe/pbc-0.5.14/benchmark/benchmark.c | 109 | ||||
-rw-r--r-- | moon-abe/pbc-0.5.14/benchmark/ellnet.c | 65 | ||||
-rw-r--r-- | moon-abe/pbc-0.5.14/benchmark/multipairing.c | 62 | ||||
-rw-r--r-- | moon-abe/pbc-0.5.14/benchmark/report_times | 7 | ||||
-rw-r--r-- | moon-abe/pbc-0.5.14/benchmark/timersa.c | 83 |
6 files changed, 344 insertions, 0 deletions
diff --git a/moon-abe/pbc-0.5.14/benchmark/REPORT.BAT b/moon-abe/pbc-0.5.14/benchmark/REPORT.BAT new file mode 100644 index 00000000..c617b436 --- /dev/null +++ b/moon-abe/pbc-0.5.14/benchmark/REPORT.BAT @@ -0,0 +1,18 @@ +@echo off
+rem Batch file for timing various pairings
+echo A
+benchmark < a.param | find "average"
+echo D159
+benchmark < d159.param | find "average"
+echo D201
+benchmark < d201.param | find "average"
+echo D224
+benchmark < d224.param | find "average"
+echo E
+benchmark < e.param | find "average"
+echo F
+benchmark < f.param | find "average"
+echo G
+benchmark < g149.param | find "average"
+echo A1
+benchmark < a1.param | find "average"
diff --git a/moon-abe/pbc-0.5.14/benchmark/benchmark.c b/moon-abe/pbc-0.5.14/benchmark/benchmark.c new file mode 100644 index 00000000..b80c1554 --- /dev/null +++ b/moon-abe/pbc-0.5.14/benchmark/benchmark.c @@ -0,0 +1,109 @@ +#include <stdint.h> // for intptr_t +#include "pbc.h" +#include "pbc_test.h" + +/* I've heard that sometimes automatic garbage collection can outperform + * manual collection, so I briefly tried using the Boehm-Demers-Weiser GC + * library. Both GMP and PBC support custom memory allocation routines so + * incorporating the GC library is trivial. + * + * Automatic garbage collection appears to slow this program down a little, + * even if only PBC collects automatically. (The case where PBC collects + * manually but GMP collects automatically cannot be achieved with the GC + * library because PBC objects point at GMP objects.) + * + * Perhaps specially-tailored memory allocation routines could shave off + * some time, but one would have to thoroughly analyze PBC and GMP memory usage + * patterns. + * + * Below is the commented-out code that collects garbage for PBC. Of course, + * if you want to use it you must also tell the build system where to find + * gc.h and to link with the GC library. + * + * Also, you may wish to write similar code for GMP (which I unfortunately + * deleted before thinking that it might be useful for others). + * Note GC_MALLOC_ATOMIC may be used for GMP since the mpz_t type does not + * store pointers in the memory it allocates. + * + * The malloc and realloc functions should exit on failure but I didn't + * bother since I was only seeing if GC could speed up this program. + +#include <gc.h> +#include <pbc_utils.h> + +void *gc_alloc(size_t size) { + return GC_MALLOC(size); +} + +void *gc_realloc(void *ptr, size_t size) { + return GC_REALLOC(ptr, size); +} + +void gc_free(void *ptr) { + UNUSED_VAR(ptr); +} + + * The following should be the first two statements in main() + +GC_INIT(); +pbc_set_memory_functions(gc_alloc, gc_realloc, gc_free); + + */ + +int main(int argc, char **argv) { + pairing_t pairing; + element_t x, y, r, r2; + int i, n; + double t0, t1, ttotal, ttotalpp; + pairing_pp_t pp; + + // Cheat for slightly faster times: + // pbc_set_memory_functions(malloc, realloc, free); + + pbc_demo_pairing_init(pairing, argc, argv); + + element_init_G1(x, pairing); + element_init_G2(y, pairing); + element_init_GT(r, pairing); + element_init_GT(r2, pairing); + + n = 10; + ttotal = 0.0; + ttotalpp = 0.0; + for (i=0; i<n; i++) { + element_random(x); + element_random(y); + + pairing_pp_init(pp, x, pairing); + t0 = pbc_get_time(); + pairing_pp_apply(r, y, pp); + t1 = pbc_get_time(); + ttotalpp += t1 - t0; + pairing_pp_clear(pp); + + t0 = pbc_get_time(); + + element_pairing(r2, x, y); + t1 = pbc_get_time(); + ttotal += t1 - t0; + + element_printf("x = %B\n", x); + element_printf("y = %B\n", y); + element_printf("e(x,y) = %B\n", r); + if (element_cmp(r, r2)) { + printf("BUG!\n"); + exit(1); + } + } + printf("average pairing time = %f\n", ttotal / n); + printf("average pairing time (preprocessed) = %f\n", ttotalpp / n); + + element_clear(x); + element_clear(y); + element_clear(r); + element_clear(r2); + + pairing_clear(pairing); + + return 0; +} diff --git a/moon-abe/pbc-0.5.14/benchmark/ellnet.c b/moon-abe/pbc-0.5.14/benchmark/ellnet.c new file mode 100644 index 00000000..8a866a65 --- /dev/null +++ b/moon-abe/pbc-0.5.14/benchmark/ellnet.c @@ -0,0 +1,65 @@ +#include <pbc.h> +#include "pbc_test.h" + +void time_pairing(pairing_ptr pairing) { + int i, n; + double t0, t1, ttotal, ttotalpp; + pairing_pp_t pp; + element_t x, y, r, r2; + element_init_G1(x, pairing); + element_init_G2(y, pairing); + element_init_GT(r, pairing); + element_init_GT(r2, pairing); + + n = 10; + ttotal = 0.0; + ttotalpp = 0.0; + for (i=0; i<n; i++) { + element_random(x); + element_random(y); + + pairing_pp_init(pp, x, pairing); + t0 = pbc_get_time(); + pairing_pp_apply(r, y, pp); + t1 = pbc_get_time(); + ttotalpp += t1 - t0; + pairing_pp_clear(pp); + + t0 = pbc_get_time(); + element_pairing(r2, x, y); + t1 = pbc_get_time(); + ttotal += t1 - t0; + + //element_printf("x = %B\n", x); + //element_printf("y = %B\n", y); + //element_printf("e(x,y) = %B\n", r); + if (element_cmp(r, r2)) { + printf("BUG!\n"); + exit(1); + } + } + printf("average pairing time = %f\n", ttotal / n); + printf("average pairing time (preprocessed) = %f\n", ttotalpp / n); + + element_clear(x); + element_clear(y); + element_clear(r); + element_clear(r2); +} + +int main(int argc, char **argv) { + pairing_t pairing; + + pbc_demo_pairing_init(pairing, argc, argv); + + printf("Miller's algorithm\n"); + time_pairing(pairing); + + pairing_option_set(pairing, "method", "shipsey-stange"); + printf("Shipsey-Stange algorithm\n"); + time_pairing(pairing); + + pairing_clear(pairing); + + return 0; +} diff --git a/moon-abe/pbc-0.5.14/benchmark/multipairing.c b/moon-abe/pbc-0.5.14/benchmark/multipairing.c new file mode 100644 index 00000000..39c9ce77 --- /dev/null +++ b/moon-abe/pbc-0.5.14/benchmark/multipairing.c @@ -0,0 +1,62 @@ +// Compares dedicated multipairing (product of pairings) routine with naive +// method. +#include <pbc.h> +#include "pbc_test.h" + +int main(int argc, char **argv) { + enum { K = 5 }; + pairing_t pairing; + element_t x[K], y[K], r, r2, tmp; + int i, n; + double t0, t1, ttotal, ttotalm; + + pbc_demo_pairing_init(pairing, argc, argv); + + for(i = 0; i < K; i++) { + element_init_G1(x[i], pairing); + element_init_G2(y[i], pairing); + } + element_init_GT(r, pairing); + element_init_GT(r2, pairing); + element_init_GT(tmp, pairing); + + n = 10; + ttotal = 0.0; + ttotalm = 0.0; + for (i=0; i<n; i++) { + int j; + for(j = 0; j < K; j++) { + element_random(x[j]); + element_random(y[j]); + } + + t0 = pbc_get_time(); + element_prod_pairing(r, x, y, K); + t1 = pbc_get_time(); + ttotalm += t1 - t0; + + t0 = pbc_get_time(); + element_pairing(r2, x[0], y[0]); + for(j = 1; j < K; j++) { + element_pairing(tmp, x[j], y[j]); + element_mul(r2, r2, tmp); + } + t1 = pbc_get_time(); + ttotal += t1 - t0; + + element_printf("e(x,y) = %B\n", r); + EXPECT(!element_cmp(r, r2)); + } + printf("average pairing time = %f\n", ttotal / n); + printf("average multi-pairing time = %f\n", ttotalm / n); + + for(i = 0; i < K; i++) { + element_clear(x[i]); + element_clear(y[i]); + } + element_clear(r); + element_clear(r2); + + pairing_clear(pairing); + return 0; +} diff --git a/moon-abe/pbc-0.5.14/benchmark/report_times b/moon-abe/pbc-0.5.14/benchmark/report_times new file mode 100644 index 00000000..d296efd4 --- /dev/null +++ b/moon-abe/pbc-0.5.14/benchmark/report_times @@ -0,0 +1,7 @@ +#!/bin/bash +#report pairing times for each type of parameter +for p in a d159 d201 d224 e f g149 a1 +do + echo -en $p'\t' + benchmark/benchmark < param/$p.param | awk '/time =/ {printf "%s", $5 "\t" } /prepro/ {print $6}' +done diff --git a/moon-abe/pbc-0.5.14/benchmark/timersa.c b/moon-abe/pbc-0.5.14/benchmark/timersa.c new file mode 100644 index 00000000..53a64cfb --- /dev/null +++ b/moon-abe/pbc-0.5.14/benchmark/timersa.c @@ -0,0 +1,83 @@ +#include <pbc.h> +#include "pbc_fp.h" +#include "pbc_test.h" + +int main(void) { + mpz_t p, q, N, d; + mpz_t dmp1, dmq1; + mpz_t ipmq, iqmp; + mpz_t adq, adp; + + field_t f; + element_t a, b; + double t0, t1, tnaive = 0, tcrt=0; + int i, n; + + mpz_init(p); + mpz_init(q); + mpz_init(N); + mpz_init(d); + mpz_init(dmp1); + mpz_init(dmq1); + mpz_init(ipmq); + mpz_init(iqmp); + mpz_init(adp); + mpz_init(adq); + pbc_mpz_randomb(p, 512); + pbc_mpz_randomb(q, 512); + mpz_nextprime(p, p); + mpz_nextprime(q, q); + mpz_mul(N, p, q); + mpz_invert(ipmq, p, q); + mpz_invert(iqmp, q, p); + + field_init_fp(f, N); + element_init(a, f); + element_init(b, f); + n = 10; + for (i=0; i<n; i++) { + pbc_mpz_random(d, N); + element_random(a); + t0 = pbc_get_time(); + element_pow_mpz(b, a, d); + t1 = pbc_get_time(); + tnaive += t1 - t0; + + mpz_sub_ui(p, p, 1); + mpz_sub_ui(q, q, 1); + + mpz_mod(dmp1, d, p); + mpz_mod(dmq1, d, q); + + mpz_add_ui(p, p, 1); + mpz_add_ui(q, q, 1); + + element_to_mpz(adq, a); + element_to_mpz(adp, a); + + t0 = pbc_get_time(); + mpz_powm(adp, adp, d, p); + mpz_powm(adq, adq, d, q); + + /* textbook CRT + mpz_mul(adp, adp, q); + mpz_mul(adp, adp, iqmp); + mpz_mul(adq, adq, p); + mpz_mul(adq, adq, ipmq); + mpz_add(adp, adp, adq); + */ + // Garner's algorithm + mpz_sub(adq, adq, adp); + mpz_mul(adq, adq, ipmq); + mpz_mod(adq, adq, q); + mpz_mul(adq, adq, p); + mpz_add(adp, adp, adq); + + t1 = pbc_get_time(); + tcrt += t1 - t0; + element_set_mpz(b, adp); + } + printf("average RSA exp time = %lf\n", tnaive / n); + printf("average RSA exp time (CRT) = %lf\n", tcrt / n); + return 0; +} |