int main()
{
SODIUM_INIT;
- print("Chacha20 ",chacha20() /DIV,"megabytes per second");
- print("Poly1305 ",poly1305() /DIV,"megabytes per second");
- print("Auth'd encryption",authenticated()/DIV,"megabytes per second");
- print("Blake2b ",blake2b() /DIV,"megabytes per second");
- print("Sha512 ",sha512() /DIV,"megabytes per second");
- print("Argon2i, 3 passes",argon2i() /DIV,"megabytes per second");
+ print("Chacha20 ",chacha20() *MUL,"megabytes per second");
+ print("Poly1305 ",poly1305() *MUL,"megabytes per second");
+ print("Auth'd encryption",authenticated()*MUL,"megabytes per second");
+ print("Blake2b ",blake2b() *MUL,"megabytes per second");
+ print("Sha512 ",sha512() *MUL,"megabytes per second");
+ print("Argon2i, 3 passes",argon2i() *MUL,"megabytes per second");
print("x25519 ",x25519() ,"exchanges per second");
print("EdDSA(sign) ",edDSA_sign() ,"signatures per second");
print("EdDSA(check) ",edDSA_check() ,"checks per second");
int main()
{
- print("Salsa20 ",salsa20() /DIV,"megabytes per second");
- print("Poly1305 ",poly1305() /DIV,"megabytes per second");
- print("Auth'd encryption",authenticated()/DIV,"megabytes per second");
- print("Sha512 ",sha512() /DIV,"megabytes per second");
+ print("Salsa20 ",salsa20() *MUL,"megabytes per second");
+ print("Poly1305 ",poly1305() *MUL,"megabytes per second");
+ print("Auth'd encryption",authenticated()*MUL,"megabytes per second");
+ print("Sha512 ",sha512() *MUL,"megabytes per second");
print("x25519 ",x25519() ,"exchanges per second");
print("EdDSA(sign) ",edDSA_sign() ,"signatures per second");
print("EdDSA(check) ",edDSA_check() ,"checks per second");
int main()
{
- print("Chacha20 ",chacha20() /DIV,"megabytes per second");
- print("Poly1305 ",poly1305() /DIV,"megabytes per second");
- print("Auth'd encryption",authenticated()/DIV,"megabytes per second");
- print("Blake2b ",blake2b() /DIV,"megabytes per second");
- print("Sha512 ",sha512() /DIV,"megabytes per second");
- print("Argon2i, 3 passes",argon2i() /DIV,"megabytes per second");
+ print("Chacha20 ",chacha20() *MUL,"megabytes per second");
+ print("Poly1305 ",poly1305() *MUL,"megabytes per second");
+ print("Auth'd encryption",authenticated()*MUL,"megabytes per second");
+ print("Blake2b ",blake2b() *MUL,"megabytes per second");
+ print("Sha512 ",sha512() *MUL,"megabytes per second");
+ print("Argon2i, 3 passes",argon2i() *MUL,"megabytes per second");
print("x25519 ",x25519() ,"exchanges per second");
print("EdDSA(sign) ",edDSA_sign() ,"signatures per second");
print("EdDSA(check) ",edDSA_check() ,"checks per second");
#define KILOBYTE 1024
#define MEGABYTE 1024 * KILOBYTE
#define SIZE (256 * KILOBYTE)
-#define DIV (MEGABYTE / SIZE)
+#define MUL (MEGABYTE / SIZE)
+#define BILLION 1000000000
-static timespec diff(timespec start, timespec end)
+// Difference in nanoseconds
+static u64 diff(timespec start, timespec end)
{
- timespec duration;
- duration.tv_sec = end.tv_sec - start.tv_sec;
- duration.tv_nsec = end.tv_nsec - start.tv_nsec;
- if (duration.tv_nsec < 0) {
- duration.tv_nsec += 1000000000;
- duration.tv_sec -= 1;
- }
- return duration;
-}
-
-static timespec min(timespec a, timespec b)
-{
- if (a.tv_sec < b.tv_sec ||
- (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec)) {
- return a;
- }
- return b;
+ return
+ (end.tv_sec - start.tv_sec ) * BILLION +
+ (end.tv_nsec - start.tv_nsec);
}
-static u64 speed(timespec duration)
+static u64 min(u64 a, u64 b)
{
- static const u64 giga = 1000000000;
- return giga / (duration.tv_nsec + duration.tv_sec * giga);
+ return a < b ? a : b;
}
-static void print(const char *name, u64 speed, const char *unit)
+static void print(const char *name, u64 duration, const char *unit)
{
- printf("%s: %5" PRIu64 " %s\n", name, speed, unit);
+ if (duration == 0) {
+ printf("%s: too fast to be measured\n", name);
+ } else {
+ u64 speed_hz = BILLION / duration;
+ printf("%s: %5" PRIu64 " %s\n", name, speed_hz, unit);
+ }
}
#define TIMESTAMP(t) \
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t)
#define TIMING_START \
- timespec duration; \
- duration.tv_sec = -1; \
- duration.tv_nsec = -1; \
- duration.tv_sec = 3600 * 24; \
- duration.tv_nsec = 0; \
+ u64 duration = -1u; \
FOR (i, 0, 500) { \
TIMESTAMP(start);
TIMESTAMP(end); \
duration = min(duration, diff(start, end)); \
} /* end FOR*/ \
- return speed(duration)
+ return duration