/home/liu/actions-runner/_work/ccv/ccv/lib/3rdparty/siphash/siphash24.c
Line | Count | Source |
1 | | /* |
2 | | SipHash reference C implementation |
3 | | |
4 | | Copyright (c) 2012-2014 Jean-Philippe Aumasson |
5 | | <jeanphilippe.aumasson@gmail.com> |
6 | | Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to> |
7 | | |
8 | | To the extent possible under law, the author(s) have dedicated all copyright |
9 | | and related and neighboring rights to this software to the public domain |
10 | | worldwide. This software is distributed without any warranty. |
11 | | |
12 | | You should have received a copy of the CC0 Public Domain Dedication along |
13 | | with |
14 | | this software. If not, see |
15 | | <http://creativecommons.org/publicdomain/zero/1.0/>. |
16 | | */ |
17 | | #include <stdint.h> |
18 | | #include <stdio.h> |
19 | | #include <string.h> |
20 | | |
21 | | /* default: SipHash-2-4 */ |
22 | 76.9M | #define cROUNDS 2 |
23 | 31.1M | #define dROUNDS 4 |
24 | | |
25 | 457M | #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) |
26 | | |
27 | | #define U32TO8_LE(p, v) \ |
28 | 12.4M | (p)[0] = (uint8_t)((v)); \ |
29 | 12.4M | (p)[1] = (uint8_t)((v) >> 8); \ |
30 | 12.4M | (p)[2] = (uint8_t)((v) >> 16); \ |
31 | 12.4M | (p)[3] = (uint8_t)((v) >> 24); |
32 | | |
33 | | #define U64TO8_LE(p, v) \ |
34 | 6.23M | U32TO8_LE((p), (uint32_t)((v))); \ |
35 | 6.23M | U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); |
36 | | |
37 | | #define U8TO64_LE(p) \ |
38 | 31.8M | (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ |
39 | 31.8M | ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ |
40 | 31.8M | ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ |
41 | 31.8M | ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) |
42 | | |
43 | | #define SIPROUND \ |
44 | 88.6M | do { \ |
45 | 76.2M | v0 += v1; \ |
46 | 76.2M | v1 = ROTL(v1, 13); \ |
47 | 76.2M | v1 ^= v0; \ |
48 | 76.2M | v0 = ROTL(v0, 32); \ |
49 | 76.2M | v2 += v3; \ |
50 | 76.2M | v3 = ROTL(v3, 16); \ |
51 | 76.2M | v3 ^= v2; \ |
52 | 76.2M | v0 += v3; \ |
53 | 76.2M | v3 = ROTL(v3, 21); \ |
54 | 76.2M | v3 ^= v0; \ |
55 | 76.2M | v2 += v1; \ |
56 | 76.2M | v1 = ROTL(v1, 17); \ |
57 | 76.2M | v1 ^= v2; \ |
58 | 76.2M | v2 = ROTL(v2, 32); \ |
59 | 76.2M | } while (0) |
60 | | |
61 | | #define TRACE |
62 | | |
63 | 6.23M | int siphash(uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k) { |
64 | | /* "somepseudorandomlygeneratedbytes" */ |
65 | 6.23M | uint64_t v0 = 0x736f6d6570736575ULL; |
66 | 6.23M | uint64_t v1 = 0x646f72616e646f6dULL; |
67 | 6.23M | uint64_t v2 = 0x6c7967656e657261ULL; |
68 | 6.23M | uint64_t v3 = 0x7465646279746573ULL; |
69 | 6.23M | uint64_t b; |
70 | 6.23M | uint64_t k0 = U8TO64_LE(k); |
71 | 6.23M | uint64_t k1 = U8TO64_LE(k + 8); |
72 | 6.23M | uint64_t m; |
73 | 6.23M | int i; |
74 | 6.23M | const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t)); |
75 | 6.23M | const int left = inlen & 7; |
76 | 6.23M | b = ((uint64_t)inlen) << 56; |
77 | 6.23M | v3 ^= k1; |
78 | 6.23M | v2 ^= k0; |
79 | 6.23M | v1 ^= k1; |
80 | 6.23M | v0 ^= k0; |
81 | | |
82 | | #ifdef DOUBLE |
83 | | v1 ^= 0xee; |
84 | | #endif |
85 | | |
86 | 25.6M | for (; in != end; in += 819.4M ) { |
87 | 19.4M | m = U8TO64_LE(in); |
88 | 19.4M | v3 ^= m; |
89 | | |
90 | 19.4M | TRACE; |
91 | 58.2M | for (i = 0; i < cROUNDS; ++i38.8M ) |
92 | 38.8M | SIPROUND; |
93 | | |
94 | 19.4M | v0 ^= m; |
95 | 19.4M | } |
96 | | |
97 | 6.23M | switch (left) { |
98 | 0 | case 7: |
99 | 0 | b |= ((uint64_t)in[6]) << 48; |
100 | 0 | case 6: |
101 | 0 | b |= ((uint64_t)in[5]) << 40; |
102 | 1 | case 5: |
103 | 1 | b |= ((uint64_t)in[4]) << 32; |
104 | 6.20M | case 4: |
105 | 6.20M | b |= ((uint64_t)in[3]) << 24; |
106 | 6.20M | case 3: |
107 | 6.20M | b |= ((uint64_t)in[2]) << 16; |
108 | 6.20M | case 2: |
109 | 6.20M | b |= ((uint64_t)in[1]) << 8; |
110 | 6.20M | case 1: |
111 | 6.20M | b |= ((uint64_t)in[0]); |
112 | 6.20M | break; |
113 | 22.3k | case 0: |
114 | 22.3k | break; |
115 | 6.23M | } |
116 | | |
117 | 6.23M | v3 ^= b; |
118 | | |
119 | 6.23M | TRACE; |
120 | 18.6M | for (i = 0; i < cROUNDS; ++i12.4M ) |
121 | 12.4M | SIPROUND; |
122 | | |
123 | 6.23M | v0 ^= b; |
124 | | |
125 | 6.23M | #ifndef DOUBLE |
126 | 6.23M | v2 ^= 0xff; |
127 | | #else |
128 | | v2 ^= 0xee; |
129 | | #endif |
130 | | |
131 | 6.23M | TRACE; |
132 | 31.1M | for (i = 0; i < dROUNDS; ++i24.9M ) |
133 | 24.9M | SIPROUND; |
134 | | |
135 | 6.23M | b = v0 ^ v1 ^ v2 ^ v3; |
136 | 6.23M | U64TO8_LE(out, b); |
137 | | |
138 | | #ifdef DOUBLE |
139 | | v1 ^= 0xdd; |
140 | | |
141 | | TRACE; |
142 | | for (i = 0; i < dROUNDS; ++i) |
143 | | SIPROUND; |
144 | | |
145 | | b = v0 ^ v1 ^ v2 ^ v3; |
146 | | U64TO8_LE(out + 8, b); |
147 | | #endif |
148 | | |
149 | 6.23M | return 0; |
150 | 6.23M | } |
151 | | |