Browse Source

change mul_bench which is now more of a group_bench.

Michele Orrù 6 years ago
parent
commit
acc592ad97
8 changed files with 102 additions and 31 deletions
  1. 3 0
      configure.ac
  2. 1 0
      src/Makefile.am
  3. 20 10
      src/ddlog.c
  4. 3 2
      src/ddlog.h
  5. 2 2
      src/ddlog_bench.c
  6. 2 2
      src/ddlog_test.c
  7. 1 1
      src/group_test.c
  8. 70 14
      src/mul_bench.c

+ 3 - 0
configure.ac

@@ -55,6 +55,9 @@ AC_ARG_ENABLE(debug,
    CFLAGS+=" -DNDEBUG -O3 -march=native")
 
 
+
+AC_DEFINE([SEP], ["\t"], [separator for benchmarks])
+
 AC_ARG_VAR([failure], [failure prob.])
 AS_IF([test "x$failure" = x], [failure="17"])
 AC_DEFINE_UNQUOTED([FAILURE], [$failure], [log inverse of the failure probability, default: 17.])

+ 1 - 0
src/Makefile.am

@@ -25,6 +25,7 @@ exp_bench_SOURCES = exp_bench.c \
 	$(ENTROPY) $(GROUP) $(FBASE) $(TIMEIT)
 mul_bench_SOURCES = mul_bench.c \
 	$(ENTROPY) $(GROUP)
+mul_bench_LDADD = -lcrypto -lssl
 
 sanity_check_SOURCES = sanity_check.c \
 	$(DDLOG) $(ELGAMAL) $(ENTROPY) $(GROUP) $(FBASE) $(HSS) $(TIMEIT)

+ 20 - 10
src/ddlog.c

@@ -138,7 +138,26 @@ bool distinguished(mpz_t n)
   return (n->_mp_d[23] & failuremask) == distinguished_limb;
 }
 
-uint32_t naif_convert(mpz_t n)
+
+
+
+void dlog_precompute()
+{
+  for (size_t i = 0; i <= bottommask; i++) {
+    uint32_t j = ffs(i) ? ffs(i) - 1 : halfstrip_size;
+    lookup[i] = bottommask >> (halfstrip_size - j);
+    offset[i] = j;
+  }
+}
+
+
+/** Alternative implementations of the conversion method.
+ *  Used for testing and/or comparing past results.
+ */
+
+
+
+uint32_t convert_naif(mpz_t n)
 {
   uint32_t steps;
 
@@ -222,12 +241,3 @@ uint32_t __attribute__((optimize("unroll-loops"))) convert_ec17(uint64_t * nn)
   }
 
 }
-
-void dlog_precompute()
-{
-  for (size_t i = 0; i <= bottommask; i++) {
-    uint32_t j = ffs(i) ? ffs(i) - 1 : halfstrip_size;
-    lookup[i] = bottommask >> (halfstrip_size - j);
-    offset[i] = j;
-  }
-}

+ 3 - 2
src/ddlog.h

@@ -12,11 +12,12 @@
 extern uint64_t lookup[0x01 << halfstrip_size];
 extern uint64_t offset[0x01 << halfstrip_size];
 
-#define convert convert_ec17
+/** default function used for conversion */
+#define convert convert_lookup
 
 uint32_t convert_lookup(uint64_t *nn);
 uint32_t convert_ec17(uint64_t *nn);
-uint32_t naif_convert(mpz_t n);
+uint32_t convert_naif(mpz_t n);
 bool distinguished(mpz_t n);
 
 void dlog_precompute();

+ 2 - 2
src/ddlog_bench.c

@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include <assert.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -8,8 +10,6 @@
 #include "group.h"
 #include "timeit.h"
 
-#define SEP "\t"
-
 int main()
 {
   group_init();

+ 2 - 2
src/ddlog_test.c

@@ -20,13 +20,13 @@ int main()
   mpz_entropy_init();
 
 
-  for (int i=0; i < (int) (1e3); i++) {
+  for (int i=0; i < (int) (1e2); i++) {
     mpz_t n, n0;
     mpz_inits(n, n0, NULL);
 
     mpz_urandomm(n0, _rstate, p);
     mpz_set(n, n0);
-    uint32_t expected = naif_convert(n);
+    uint32_t expected = convert_naif(n);
     mpz_set(n, n0);
     uint32_t converted = convert(n->_mp_d);
     printf("%d %d\n", converted, expected);

+ 1 - 1
src/group_test.c

@@ -14,7 +14,7 @@ int main()
   mpz_entropy_init();
   mpz_inits(test, expected, x, y, NULL);
 
-  for (int i = 0; i < 1e6; i++) {
+  for (int i = 0; i < 1e4; i++) {
     mpz_urandomm(x, _rstate, p);
     mpz_urandomm(y, _rstate, p);
 

+ 70 - 14
src/mul_bench.c

@@ -1,33 +1,89 @@
-#include <assert.h>
+#include "config.h"
+
 #include <stdio.h>
 
 #include <gmp.h>
+#include <openssl/ossl_typ.h>
+#include <openssl/bio.h>
+#include <openssl/bn.h>
+#include <openssl/evp.h>
+#include <openssl/ec.h>
 
 #include "entropy.h"
 #include "group.h"
 #include "timeit.h"
 
+void EC_POINT_get_random(const EC_GROUP *group, EC_POINT *r, BN_CTX *ctx) {
+    BIGNUM *k = NULL;
+    k = BN_new();
+
+    if (!EC_GROUP_get_order(group, k, ctx)) goto err;
+    if (!BN_pseudo_rand(k, BN_num_bits(k), 0, 0)) goto err;
+    if (!EC_POINT_mul(group, r, k, NULL, NULL, ctx)) goto err;
+    if (!EC_POINT_is_on_curve(group, r, ctx)) goto err;
+
+ err:
+    if (k) BN_free(k);
+}
+
 int main()
 {
   mpz_entropy_init();
   group_init();
-  mpz_t test, expected, x, y;
-  mpz_inits(test, expected, x, y, NULL);
+  mpz_t x, y, xy;
+  mpz_inits(x, y, xy, NULL);
+
+  BN_CTX *ctx;
+  EC_GROUP *group;
+  EC_POINT *P, *Q;
+  ctx = BN_CTX_new();
 
-  printf("e6time\n");
-  for (int n = 0; n < 0x01 << 16; n++) {
-    INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
+  group = EC_GROUP_new_by_curve_name(NID_secp521r1);
 
-    for (int i = 0; i < (int) 1e6; i++) {
-      mpz_urandomm(x, _rstate, p);
-      mpz_urandomm(y, _rstate, p);
 
-      START_TIMEIT();
-      mul_modp(expected, x, y);
-      END_TIMEIT();
+  for (int n = 0; n < (int) 1e3; n++) {
+    /* block for Z_p with our reminder */
+    {
+      INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
+      for (int i = 0; i < (int) 1e5; i++) {
+        mpz_urandomm(x, _rstate, p);
+        mpz_urandomm(y, _rstate, p);
+
+        START_TIMEIT();
+        mul_modp(xy, x, y);
+        END_TIMEIT();
+      }
+      printf(TIMEIT_FORMAT SEP, GET_TIMEIT());
+    }
+    /* block for Z_p with vanilla reminder */
+    {
+      INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
+      for (int i = 0; i < (int) 1e5; i++) {
+        mpz_urandomm(x, _rstate, p);
+        mpz_urandomm(y, _rstate, p);
+
+        START_TIMEIT();
+        mpz_mul(xy, x, y);
+        mpz_mod(xy, xy, p);
+        END_TIMEIT();
+      }
+      printf(TIMEIT_FORMAT SEP, GET_TIMEIT());
+    }
+    /* EC  addition */
+    {
+      INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
+      for (int i = 0; i < (int) 1e5; i++) {
+        P = EC_POINT_new(group);
+        Q = EC_POINT_new(group);
+        EC_POINT_get_random(group, Q, ctx);
+
+        START_TIMEIT();
+        EC_POINT_add(group, Q, Q, P, ctx);
+        END_TIMEIT();
+      }
+      printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
     }
-    printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
   }
 
-  mpz_clears(test, expected, x, y, NULL);
+  mpz_clears(x, y, xy, NULL);
 }