Browse Source

move group description to group.c, higher-resolution clock.

Michele Orrù 7 years ago
parent
commit
efa6c82734
10 changed files with 74 additions and 59 deletions
  1. 4 2
      src/Makefile.am
  2. 3 2
      src/ddlog.c
  3. 3 3
      src/ddlog.h
  4. 1 0
      src/elgamal.c
  5. 28 0
      src/group.c
  6. 15 0
      src/group.h
  7. 1 30
      src/hss.c
  8. 0 8
      src/hss.h
  9. 7 6
      src/rms.c
  10. 12 8
      src/timeit.h

+ 4 - 2
src/Makefile.am

@@ -1,11 +1,13 @@
-bin_PROGRAMS = rms
+bin_PROGRAMS = rms ddlog_bench
 #check_programs = test_ssl1
 
 DDLOG = ddlog.c ddlog.h
 ELGAMAL = elgamal.c elgamal.h
 ENTROPY = entropy.c entropy.h
+GROUP = group.c group.h
 HSS = hss.c hss.h
 #TESTS = $(check_programs)
 
 #test_ssl1_SOURCES = test_ssl1.c
-rms_SOURCES = rms.c $(HSS) $(ENTROPY) $(ELGAMAL) $(DDLOG)
+rms_SOURCES = rms.c $(DDLOG) $(ELGAMAL) $(ENTROPY) $(GROUP) $(HSS)
+ddlog_bench_SOURCES = ddlog_bench.c $(DDLOG) $(ENTROPY) $(GROUP)

+ 3 - 2
src/ddlog.c

@@ -5,8 +5,11 @@
 #include <gmp.h>
 
 #include "ddlog.h"
+#include "group.h"
 #include "hss.h"
 
+typedef __uint128_t uint128_t;
+
 uint8_t lookup[256];
 uint8_t offset[256];
 
@@ -29,7 +32,6 @@ uint32_t convert(uint64_t * nn)
   static const uint64_t topmask = ~(ULLONG_MAX >> halfstrip_size);
   static const uint64_t topbigmask = ~(ULLONG_MAX >> strip_size);
   static const uint64_t bottommask = (0x01  << halfstrip_size) -1;
-
   uint32_t w;
   uint32_t steps;
   size_t head = 23;
@@ -87,7 +89,6 @@ uint32_t convert(uint64_t * nn)
   return steps;
 }
 
-
 uint32_t naif_convert(mpz_t n)
 {
   uint32_t i;

+ 3 - 3
src/ddlog.h

@@ -1,16 +1,16 @@
 #pragma once
 
 #include <stdint.h>
+#include <gmp.h>
 
 #define strip_size 16
 #define halfstrip_size ((strip_size)/2)
 
-typedef __uint128_t uint128_t;
-
-
 extern uint8_t lookup[256];
 extern uint8_t offset[256];
 
 
 uint32_t convert(uint64_t *nn);
+uint32_t naif_convert(mpz_t n);
+
 void dlog_precompute();

+ 1 - 0
src/elgamal.c

@@ -4,6 +4,7 @@
 
 #include "elgamal.h"
 #include "entropy.h"
+#include "group.h"
 #include "hss.h"
 
 

+ 28 - 0
src/group.c

@@ -0,0 +1,28 @@
+#include "group.h"
+
+const char* p_str =
+  "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+  "505CAF";
+
+mpz_t p, q;
+const uint64_t gg = 11510609;
+
+void group_init()
+{
+  mpz_init_set_str(p, p_str, 0);
+
+  mpz_init_set(q, p);
+  mpz_sub_ui(q, q, 1);
+  mpz_divexact_ui(q, q, 2);
+
+}
+
+
+void group_clear()
+{
+  mpz_clears(p, q, NULL);
+}

+ 15 - 0
src/group.h

@@ -0,0 +1,15 @@
+#pragma once
+
+#include <stdint.h>
+#include <gmp.h>
+
+/**
+ * p is our prime modulus, and is 2^n - g
+ * where g is referred to as "gamma" (built-in function in C, so transliterated)
+ */
+extern const char* p_str;
+extern mpz_t p, q;
+extern const uint64_t gg;
+
+void group_init();
+void group_clear();

+ 1 - 30
src/hss.c

@@ -4,38 +4,9 @@
 #include <stdlib.h>
 
 #include "entropy.h"
+#include "elgamal.h"
 #include "hss.h"
 
-/**
- * p is our prime modulus, and is 2^n - g
- * where g is referred to as "gamma" (built-in function in C, so transliterated)
- */
-const char* p_str =
-  "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-  "505CAF";
-
-mpz_t p, q;
-const uint64_t gg = 11510609;
-
-void hss_init()
-{
-  mpz_init_set_str(p, p_str, 0);
-
-  mpz_init_set(q, p);
-  mpz_sub_ui(q, q, 1);
-  mpz_divexact_ui(q, q, 2);
-}
-
-
-void hss_del()
-{
-  mpz_clear(p);
-}
-
 void ssl1_init(ssl1_t s)
 {
   ELGAMAL_CIPHER(init, s->w);

+ 0 - 8
src/hss.h

@@ -5,14 +5,6 @@
 
 #include "elgamal.h"
 
-/**
- * p is our prime modulus, and is 2^n - g
- * where g is referred to as "gamma" (built-in function in C, so transliterated)
- */
-extern const char* p_str;
-extern mpz_t p, q;
-extern const uint64_t gg;
-
 void hss_init();
 void hss_del();
 

+ 7 - 6
src/rms.c

@@ -10,14 +10,15 @@
 #include "ddlog.h"
 #include "elgamal.h"
 #include "entropy.h"
-#include "rms.h"
+#include "group.h"
 #include "hss.h"
+#include "rms.h"
 #include "timeit.h"
 
 static inline
 void remp(mpz_t rop)
 {
-  const int limbs = rop->_mp_size - 24;
+  int limbs = rop->_mp_size - 24;
 
   if (limbs < 0) return;
   else if (limbs == 0 && mpz_cmp(rop, p) < 0) return;
@@ -100,8 +101,8 @@ void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
 
 int main()
 {
+  group_init();
   mpz_entropy_init();
-  hss_init();
   dlog_precompute();
 
   mpz_t test;
@@ -136,8 +137,8 @@ int main()
   ssl2_init(t1);
   ssl2_init(t2);
 
-  INIT_TIMEIT();
-  for (int i = 0; i <  (int) 1e1; i++) {
+  INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
+  for (int i = 0; i <  (int) 1e2; i++) {
 
     mpz_urandomb(y, _rstate, 1);
     mpz_urandomb(x, _rstate, 1);
@@ -181,6 +182,6 @@ int main()
 
   mpz_clears(x, y, NULL);
   ELGAMAL_KEY(clear, key);
-  hss_del();
+  group_clear();
   return 0;
 }

+ 12 - 8
src/timeit.h

@@ -1,18 +1,22 @@
+#include <time.h>
 #include <sys/time.h>
 
-#define INIT_TIMEIT()                           \
-  struct timeval __start, __end;                \
-  double __sdiff = 0, __udiff = 0
+#define INIT_TIMEIT(flags)                      \
+  struct timespec __start, __end;               \
+  double __sdiff = 0, __udiff = 0;              \
+  int __clock_flags = flags
 
-#define START_TIMEIT()                          \
-  gettimeofday(&__start, NULL)
+#define START_TIMEIT()  clock_gettime(__clock_flags, &__start)
 
 #define END_TIMEIT()                                                    \
-  gettimeofday(&__end, NULL);                                           \
+  clock_gettime(__clock_flags, &__end);                                 \
   __sdiff += (__end.tv_sec - __start.tv_sec);                           \
-  __udiff += (__end.tv_usec - __start.tv_usec)
+  __udiff += (__end.tv_nsec - __start.tv_nsec)
 
 #define GET_TIMEIT()                            \
-  __sdiff + __udiff * 1e-6
+  __sdiff + __udiff * 1e-9
 
 #define TIMEIT_FORMAT "%lf"
+
+
+#define INIT_CLOCK