Skip to content

Commit 98dd3a5

Browse files
author
tom zhou
committed
added tweetnacl.c bench
1 parent 811b3b7 commit 98dd3a5

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

bench/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
g++ -I../tweetnacl/ -o bench bench.cpp ../tweetnacl/tweetnacl.c

bench/bench.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
#include <iostream>
3+
#include <stdio.h>
4+
#include <sys/time.h>
5+
6+
#include "tweetnacl.h"
7+
8+
using namespace std;
9+
10+
void randombytes(unsigned char * nonce, unsigned long long d) {
11+
for (unsigned long long i = 0; i < d; i ++)
12+
nonce[i] = d;
13+
}
14+
15+
int main(int argc, char *argv[]) {
16+
17+
// Stress on secretBox
18+
19+
// shared key
20+
int i;
21+
22+
unsigned char shk[crypto_secretbox_KEYBYTES];
23+
for (i = 0; i < sizeof(shk); i ++)
24+
shk[i] = 0x66;
25+
26+
unsigned char nonce[crypto_secretbox_NONCEBYTES];
27+
for (i = 0; i < sizeof(nonce); i ++)
28+
nonce[i] = 0x68;
29+
30+
// messages
31+
string m0 = "Helloword, TweetNacl...";
32+
33+
// cipher A -> B
34+
cout << "streess on secret box@"+m0;
35+
36+
timeval curTime;
37+
38+
for (int t = 0; t < 19; t ++, m0 += m0) {
39+
const char * mb0 = m0.c_str();
40+
41+
printf("\n\n\tstreess/%fkB: %d times\n", m0.length()/1000.0, t);
42+
43+
gettimeofday(&curTime, NULL);
44+
printf("secret box ...@%6.3f\n", curTime.tv_sec*1000 + (curTime.tv_usec / 1000));
45+
46+
unsigned char cab[crypto_secretbox_ZEROBYTES+m0.length()];
47+
crypto_secretbox((unsigned char *)cab, (unsigned char *)mb0, m0.length(), (unsigned char *)nonce, (unsigned char *)shk);
48+
49+
gettimeofday(&curTime, NULL);
50+
printf("... secret box@%6.3f\n", curTime.tv_sec*1000 + (curTime.tv_usec / 1000));
51+
52+
53+
gettimeofday(&curTime, NULL);
54+
printf("\nsecret box open ...@%f\n", curTime.tv_sec*1000 + (curTime.tv_usec / 1000));
55+
56+
unsigned char mba[crypto_secretbox_ZEROBYTES+m0.length()];
57+
crypto_secretbox_open((unsigned char *)mba, (unsigned char *)(cab+16), 16+m0.length(), (unsigned char *)nonce, (unsigned char *)shk);
58+
59+
gettimeofday(&curTime, NULL);
60+
printf("... secret box open@%6.3f\n", curTime.tv_sec*1000 + (curTime.tv_usec / 1000));
61+
}
62+
63+
return 0;
64+
}

bench/bench.exe

94.1 KB
Binary file not shown.

tweetnacl/tweetnacl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ typedef i64 gf[16];
1010
extern void randombytes(u8 *,u64);
1111

1212
static const u8
13-
_0[16],
13+
_0[16] = {0},
1414
_9[32] = {9};
1515
static const gf
16-
gf0,
16+
gf0 = {0},
1717
gf1 = {1},
1818
_121665 = {0xDB41,1},
1919
D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203},
@@ -120,7 +120,7 @@ int crypto_core_hsalsa20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
120120
return 0;
121121
}
122122

123-
static const u8 sigma[16] = "expand 32-byte k";
123+
static const u8 sigma[17] = "expand 32-byte k";
124124

125125
int crypto_stream_salsa20_xor(u8 *c,const u8 *m,u64 b,const u8 *n,const u8 *k)
126126
{

tweetnacl/tweetnacl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#ifndef TWEETNACL_H
22
#define TWEETNACL_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
38
#define crypto_auth_PRIMITIVE "hmacsha512256"
49
#define crypto_auth crypto_auth_hmacsha512256
510
#define crypto_auth_verify crypto_auth_hmacsha512256_verify
@@ -269,4 +274,9 @@ extern int crypto_verify_32_tweet(const unsigned char *,const unsigned char *);
269274
#define crypto_verify_32_BYTES crypto_verify_32_tweet_BYTES
270275
#define crypto_verify_32_VERSION crypto_verify_32_tweet_VERSION
271276
#define crypto_verify_32_IMPLEMENTATION "crypto_verify/32/tweet"
277+
278+
#ifdef __cplusplus
279+
}
280+
#endif
281+
272282
#endif

0 commit comments

Comments
 (0)