libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_scalarmult / try.c
blob671b1089908ff992db5018d5620fe4edfd372d87
1 /*
2 * crypto_scalarmult/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_scalarmult.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_scalarmult_IMPLEMENTATION;
15 #define mlen crypto_scalarmult_SCALARBYTES
16 #define nlen crypto_scalarmult_SCALARBYTES
17 #define plen crypto_scalarmult_BYTES
18 #define qlen crypto_scalarmult_BYTES
19 #define rlen crypto_scalarmult_BYTES
21 static unsigned char *m;
22 static unsigned char *n;
23 static unsigned char *p;
24 static unsigned char *q;
25 static unsigned char *r;
27 static unsigned char *m2;
28 static unsigned char *n2;
29 static unsigned char *p2;
30 static unsigned char *q2;
31 static unsigned char *r2;
33 void preallocate(void)
37 void allocate(void)
39 m = alignedcalloc(mlen);
40 n = alignedcalloc(nlen);
41 p = alignedcalloc(plen);
42 q = alignedcalloc(qlen);
43 r = alignedcalloc(rlen);
44 m2 = alignedcalloc(mlen + crypto_scalarmult_BYTES);
45 n2 = alignedcalloc(nlen + crypto_scalarmult_BYTES);
46 p2 = alignedcalloc(plen + crypto_scalarmult_BYTES);
47 q2 = alignedcalloc(qlen + crypto_scalarmult_BYTES);
48 r2 = alignedcalloc(rlen + crypto_scalarmult_BYTES);
51 void predoit(void)
55 void doit(void)
57 crypto_scalarmult(q,n,p);
58 crypto_scalarmult_base(r,n);
61 char checksum[crypto_scalarmult_BYTES * 2 + 1];
63 const char *checksum_compute(void)
65 long long i;
66 long long j;
67 long long tests;
69 for (i = 0;i < mlen;++i) m[i] = i;
70 for (i = 0;i < nlen;++i) n[i] = i + 1;
71 for (i = 0;i < plen;++i) p[i] = i + 2;
72 for (i = 0;i < qlen;++i) q[i] = i + 3;
73 for (i = 0;i < rlen;++i) r[i] = i + 4;
75 for (i = -16;i < 0;++i) p[i] = rand();
76 for (i = -16;i < 0;++i) n[i] = rand();
77 for (i = plen;i < plen + 16;++i) p[i] = rand();
78 for (i = nlen;i < nlen + 16;++i) n[i] = rand();
79 for (i = -16;i < plen + 16;++i) p2[i] = p[i];
80 for (i = -16;i < nlen + 16;++i) n2[i] = n[i];
82 if (crypto_scalarmult_base(p,n) != 0) return "crypto_scalarmult_base returns nonzero";
84 for (i = -16;i < nlen + 16;++i) if (n2[i] != n[i]) return "crypto_scalarmult_base overwrites input";
85 for (i = -16;i < 0;++i) if (p2[i] != p[i]) return "crypto_scalarmult_base writes before output";
86 for (i = plen;i < plen + 16;++i) if (p2[i] != p[i]) return "crypto_scalarmult_base writes after output";
88 for (tests = 0;tests < 100;++tests) {
89 for (i = -16;i < 0;++i) q[i] = rand();
90 for (i = -16;i < 0;++i) p[i] = rand();
91 for (i = -16;i < 0;++i) m[i] = rand();
92 for (i = qlen;i < qlen + 16;++i) q[i] = rand();
93 for (i = plen;i < plen + 16;++i) p[i] = rand();
94 for (i = mlen;i < mlen + 16;++i) m[i] = rand();
95 for (i = -16;i < qlen + 16;++i) q2[i] = q[i];
96 for (i = -16;i < plen + 16;++i) p2[i] = p[i];
97 for (i = -16;i < mlen + 16;++i) m2[i] = m[i];
99 if (crypto_scalarmult(q,m,p) != 0) return "crypto_scalarmult returns nonzero";
101 for (i = -16;i < mlen + 16;++i) if (m2[i] != m[i]) return "crypto_scalarmult overwrites n input";
102 for (i = -16;i < plen + 16;++i) if (p2[i] != p[i]) return "crypto_scalarmult overwrites p input";
103 for (i = -16;i < 0;++i) if (q2[i] != q[i]) return "crypto_scalarmult writes before output";
104 for (i = qlen;i < qlen + 16;++i) if (q2[i] != q[i]) return "crypto_scalarmult writes after output";
106 if (crypto_scalarmult(m2,m2,p) != 0) return "crypto_scalarmult returns nonzero";
107 for (i = 0;i < qlen;++i) if (q[i] != m2[i]) return "crypto_scalarmult does not handle n overlap";
108 for (i = 0;i < qlen;++i) m2[i] = m[i];
110 if (crypto_scalarmult(p2,m2,p2) != 0) return "crypto_scalarmult returns nonzero";
111 for (i = 0;i < qlen;++i) if (q[i] != p2[i]) return "crypto_scalarmult does not handle p overlap";
113 if (crypto_scalarmult(r,n,q) != 0) return "crypto_scalarmult returns nonzero";
114 if (crypto_scalarmult(q,n,p) != 0) return "crypto_scalarmult returns nonzero";
115 if (crypto_scalarmult(p,m,q) != 0) return "crypto_scalarmult returns nonzero";
116 for (j = 0;j < plen;++j) if (p[j] != r[j]) return "crypto_scalarmult not associative";
117 for (j = 0;j < mlen;++j) m[j] ^= q[j % qlen];
118 for (j = 0;j < nlen;++j) n[j] ^= p[j % plen];
121 for (i = 0;i < crypto_scalarmult_BYTES;++i) {
122 checksum[2 * i] = "0123456789abcdef"[15 & (p[i] >> 4)];
123 checksum[2 * i + 1] = "0123456789abcdef"[15 & p[i]];
125 checksum[2 * i] = 0;
126 return 0;