summaryrefslogtreecommitdiff
path: root/nacl/try-anything.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/try-anything.c')
-rw-r--r--nacl/try-anything.c173
1 files changed, 173 insertions, 0 deletions
diff --git a/nacl/try-anything.c b/nacl/try-anything.c
new file mode 100644
index 00000000..b6847473
--- /dev/null
+++ b/nacl/try-anything.c
@@ -0,0 +1,173 @@
1/*
2 * try-anything.c version 20090215
3 * D. J. Bernstein
4 * Public domain.
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <time.h>
11#include <unistd.h>
12#include <sys/time.h>
13#include <sys/types.h>
14#include <sys/resource.h>
15#include "cpucycles.h"
16
17typedef int uint32;
18
19static uint32 seed[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 } ;
20static uint32 in[12];
21static uint32 out[8];
22static int outleft = 0;
23
24#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
25#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
26
27static void surf(void)
28{
29 uint32 t[12]; uint32 x; uint32 sum = 0;
30 int r; int i; int loop;
31
32 for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
33 for (i = 0;i < 8;++i) out[i] = seed[24 + i];
34 x = t[11];
35 for (loop = 0;loop < 2;++loop) {
36 for (r = 0;r < 16;++r) {
37 sum += 0x9e3779b9;
38 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
39 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
40 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
41 }
42 for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
43 }
44}
45
46void randombytes(unsigned char *x,unsigned long long xlen)
47{
48 while (xlen > 0) {
49 if (!outleft) {
50 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
51 surf();
52 outleft = 8;
53 }
54 *x = out[--outleft];
55 ++x;
56 --xlen;
57 }
58}
59
60extern void preallocate(void);
61extern void allocate(void);
62extern void predoit(void);
63extern void doit(void);
64extern char checksum[];
65extern const char *checksum_compute(void);
66extern const char *primitiveimplementation;
67
68static void printword(const char *s)
69{
70 if (!*s) putchar('-');
71 while (*s) {
72 if (*s == ' ') putchar('_');
73 else if (*s == '\t') putchar('_');
74 else if (*s == '\r') putchar('_');
75 else if (*s == '\n') putchar('_');
76 else putchar(*s);
77 ++s;
78 }
79 putchar(' ');
80}
81
82static void printnum(long long x)
83{
84 printf("%lld ",x);
85}
86
87static void fail(const char *why)
88{
89 printf("%s\n",why);
90 exit(111);
91}
92
93unsigned char *alignedcalloc(unsigned long long len)
94{
95 unsigned char *x = (unsigned char *) calloc(1,len + 256);
96 long long i;
97 if (!x) fail("out of memory");
98 /* will never deallocate so shifting is ok */
99 for (i = 0;i < len + 256;++i) x[i] = random();
100 x += 64;
101 x += 63 & (-(unsigned long) x);
102 for (i = 0;i < len;++i) x[i] = 0;
103 return x;
104}
105
106#define TIMINGS 63
107static long long cycles[TIMINGS + 1];
108
109void limits()
110{
111#ifdef RLIM_INFINITY
112 struct rlimit r;
113 r.rlim_cur = 0;
114 r.rlim_max = 0;
115#ifdef RLIMIT_NOFILE
116 setrlimit(RLIMIT_NOFILE,&r);
117#endif
118#ifdef RLIMIT_NPROC
119 setrlimit(RLIMIT_NPROC,&r);
120#endif
121#ifdef RLIMIT_CORE
122 setrlimit(RLIMIT_CORE,&r);
123#endif
124#endif
125}
126
127int main()
128{
129 long long i;
130 long long j;
131 long long abovej;
132 long long belowj;
133 long long checksumcycles;
134 long long cyclespersecond;
135 const char *problem;
136
137 cyclespersecond = cpucycles_persecond();
138 preallocate();
139 limits();
140
141 allocate();
142 srandom(getpid());
143
144 cycles[0] = cpucycles();
145 problem = checksum_compute(); if (problem) fail(problem);
146 cycles[1] = cpucycles();
147 checksumcycles = cycles[1] - cycles[0];
148
149 predoit();
150 for (i = 0;i <= TIMINGS;++i) {
151 cycles[i] = cpucycles();
152 }
153 for (i = 0;i <= TIMINGS;++i) {
154 cycles[i] = cpucycles();
155 doit();
156 }
157 for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
158 for (j = 0;j < TIMINGS;++j) {
159 belowj = 0;
160 for (i = 0;i < TIMINGS;++i) if (cycles[i] < cycles[j]) ++belowj;
161 abovej = 0;
162 for (i = 0;i < TIMINGS;++i) if (cycles[i] > cycles[j]) ++abovej;
163 if (belowj * 2 < TIMINGS && abovej * 2 < TIMINGS) break;
164 }
165
166 printword(checksum);
167 printnum(cycles[j]);
168 printnum(checksumcycles);
169 printnum(cyclespersecond);
170 printword(primitiveimplementation);
171 printf("\n");
172 return 0;
173}