Skip to content

Instantly share code, notes, and snippets.

@hintjens
Created March 11, 2013 20:55
Show Gist options
  • Save hintjens/5137671 to your computer and use it in GitHub Desktop.
Save hintjens/5137671 to your computer and use it in GitHub Desktop.
Simple performance test of NaCl (requires CZMQ) - placed into the public domain
// Exploring functionality of NaCl
#include <czmq.h>
#include <crypto_box.h>
#include <crypto_secretbox.h>
#include <randombytes.h>
int main (void)
{
// Generate public+secret key pair
byte public_key [crypto_box_PUBLICKEYBYTES];
byte secret_key [crypto_box_SECRETKEYBYTES];
int rc = crypto_box_keypair (public_key, secret_key);
assert (rc == 0);
byte precomputed [crypto_box_BEFORENMBYTES];
rc == crypto_box_beforenm (precomputed, public_key, secret_key);
assert (rc == 0);
byte nonce [crypto_box_NONCEBYTES] = { 0 };
byte plaintext [crypto_box_ZEROBYTES + 256];
byte encrypted [crypto_box_ZEROBYTES + 256];
memset (plaintext, 0, crypto_box_ZEROBYTES);
memcpy (plaintext + crypto_box_ZEROBYTES, (byte *) "Hello World", 11);
unsigned long long length = crypto_box_ZEROBYTES + 11;
// How often can we encrypt and decrypt in 1 second?
int64_t end_at = zclock_time () + 1000;
int iteration;
int cycles = 0;
end_at = zclock_time () + 1000;
cycles = 0;
while (true) {
for (iteration = 0; iteration < 100; iteration++) {
rc = crypto_box_afternm (encrypted, plaintext, length, nonce, precomputed);
assert (rc == 0);
}
cycles += 100;
if (zclock_time () >= end_at)
break;
}
printf ("Encryptions: %d/second\n", cycles);
end_at = zclock_time () + 1000;
cycles = 0;
while (true) {
for (iteration = 0; iteration < 100; iteration++) {
rc = crypto_box_open_afternm (plaintext, encrypted, length, nonce, precomputed);
assert (rc == 0);
}
cycles += 100;
if (zclock_time () >= end_at)
break;
}
printf ("Decryptions: %d/second\n", cycles);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment