|
19 | 19 | /// * Daniel J. Bernstein < [email protected]>
|
20 | 20 | //===----------------------------------------------------------------------===//
|
21 | 21 |
|
22 |
| -// FIXME: Remove @usableFromInline and @_fixed_layout once Hasher is resilient. |
23 |
| -// rdar://problem/38549901 |
24 |
| -@usableFromInline @_fixed_layout |
25 |
| -internal struct _SipHashState { |
26 |
| - // "somepseudorandomlygeneratedbytes" |
27 |
| - fileprivate var v0: UInt64 = 0x736f6d6570736575 |
28 |
| - fileprivate var v1: UInt64 = 0x646f72616e646f6d |
29 |
| - fileprivate var v2: UInt64 = 0x6c7967656e657261 |
30 |
| - fileprivate var v3: UInt64 = 0x7465646279746573 |
31 |
| - // The fields below are reserved for future use. They aren't currently used. |
32 |
| - fileprivate var v4: UInt64 = 0 |
33 |
| - fileprivate var v5: UInt64 = 0 |
34 |
| - fileprivate var v6: UInt64 = 0 |
35 |
| - fileprivate var v7: UInt64 = 0 |
36 |
| - |
37 |
| - @inline(__always) |
38 |
| - fileprivate init(rawSeed: (UInt64, UInt64)) { |
39 |
| - v3 ^= rawSeed.1 |
40 |
| - v2 ^= rawSeed.0 |
41 |
| - v1 ^= rawSeed.1 |
42 |
| - v0 ^= rawSeed.0 |
43 |
| - } |
44 |
| - |
45 |
| - @inline(__always) |
46 |
| - fileprivate |
47 |
| - static func _rotateLeft(_ x: UInt64, by amount: UInt64) -> UInt64 { |
48 |
| - return (x &<< amount) | (x &>> (64 - amount)) |
49 |
| - } |
50 |
| - |
51 |
| - @inline(__always) |
52 |
| - fileprivate mutating func _round() { |
53 |
| - v0 = v0 &+ v1 |
54 |
| - v1 = _SipHashState._rotateLeft(v1, by: 13) |
55 |
| - v1 ^= v0 |
56 |
| - v0 = _SipHashState._rotateLeft(v0, by: 32) |
57 |
| - v2 = v2 &+ v3 |
58 |
| - v3 = _SipHashState._rotateLeft(v3, by: 16) |
59 |
| - v3 ^= v2 |
60 |
| - v0 = v0 &+ v3 |
61 |
| - v3 = _SipHashState._rotateLeft(v3, by: 21) |
62 |
| - v3 ^= v0 |
63 |
| - v2 = v2 &+ v1 |
64 |
| - v1 = _SipHashState._rotateLeft(v1, by: 17) |
65 |
| - v1 ^= v2 |
66 |
| - v2 = _SipHashState._rotateLeft(v2, by: 32) |
67 |
| - } |
68 |
| - |
69 |
| - @inline(__always) |
70 |
| - fileprivate func _extract() -> UInt64 { |
71 |
| - return v0 ^ v1 ^ v2 ^ v3 |
72 |
| - } |
73 |
| -} |
74 |
| - |
75 |
| -// FIXME: Remove @usableFromInline and @_fixed_layout once Hasher is resilient. |
76 |
| -// rdar://problem/38549901 |
77 |
| -@usableFromInline @_fixed_layout |
78 |
| -internal struct _SipHash13Core: _HasherCore { |
79 |
| - private var _state: _SipHashState |
| 22 | +extension Hasher { |
| 23 | + // FIXME: Remove @usableFromInline and @_fixed_layout once Hasher is resilient. |
| 24 | + // rdar://problem/38549901 |
| 25 | + @usableFromInline @_fixed_layout |
| 26 | + internal struct _State { |
| 27 | + // "somepseudorandomlygeneratedbytes" |
| 28 | + fileprivate var v0: UInt64 = 0x736f6d6570736575 |
| 29 | + fileprivate var v1: UInt64 = 0x646f72616e646f6d |
| 30 | + fileprivate var v2: UInt64 = 0x6c7967656e657261 |
| 31 | + fileprivate var v3: UInt64 = 0x7465646279746573 |
| 32 | + // The fields below are reserved for future use. They aren't currently used. |
| 33 | + fileprivate var v4: UInt64 = 0 |
| 34 | + fileprivate var v5: UInt64 = 0 |
| 35 | + fileprivate var v6: UInt64 = 0 |
| 36 | + fileprivate var v7: UInt64 = 0 |
| 37 | + |
| 38 | + @inline(__always) |
| 39 | + fileprivate init(rawSeed: (UInt64, UInt64)) { |
| 40 | + v3 ^= rawSeed.1 |
| 41 | + v2 ^= rawSeed.0 |
| 42 | + v1 ^= rawSeed.1 |
| 43 | + v0 ^= rawSeed.0 |
| 44 | + } |
80 | 45 |
|
81 |
| - @inline(__always) |
82 |
| - internal init(rawSeed: (UInt64, UInt64)) { |
83 |
| - _state = _SipHashState(rawSeed: rawSeed) |
84 |
| - } |
| 46 | + @inline(__always) |
| 47 | + fileprivate |
| 48 | + static func _rotateLeft(_ x: UInt64, by amount: UInt64) -> UInt64 { |
| 49 | + return (x &<< amount) | (x &>> (64 - amount)) |
| 50 | + } |
85 | 51 |
|
86 |
| - @inline(__always) |
87 |
| - internal mutating func compress(_ m: UInt64) { |
88 |
| - _state.v3 ^= m |
89 |
| - _state._round() |
90 |
| - _state.v0 ^= m |
91 |
| - } |
| 52 | + @inline(__always) |
| 53 | + fileprivate mutating func _round() { |
| 54 | + v0 = v0 &+ v1 |
| 55 | + v1 = Hasher._State._rotateLeft(v1, by: 13) |
| 56 | + v1 ^= v0 |
| 57 | + v0 = Hasher._State._rotateLeft(v0, by: 32) |
| 58 | + v2 = v2 &+ v3 |
| 59 | + v3 = Hasher._State._rotateLeft(v3, by: 16) |
| 60 | + v3 ^= v2 |
| 61 | + v0 = v0 &+ v3 |
| 62 | + v3 = Hasher._State._rotateLeft(v3, by: 21) |
| 63 | + v3 ^= v0 |
| 64 | + v2 = v2 &+ v1 |
| 65 | + v1 = Hasher._State._rotateLeft(v1, by: 17) |
| 66 | + v1 ^= v2 |
| 67 | + v2 = Hasher._State._rotateLeft(v2, by: 32) |
| 68 | + } |
92 | 69 |
|
93 |
| - @inline(__always) |
94 |
| - internal mutating func finalize(tailAndByteCount: UInt64) -> UInt64 { |
95 |
| - compress(tailAndByteCount) |
96 |
| - _state.v2 ^= 0xff |
97 |
| - for _ in 0..<3 { |
98 |
| - _state._round() |
| 70 | + @inline(__always) |
| 71 | + fileprivate func _extract() -> UInt64 { |
| 72 | + return v0 ^ v1 ^ v2 ^ v3 |
99 | 73 | }
|
100 |
| - return _state._extract() |
101 | 74 | }
|
102 | 75 | }
|
103 | 76 |
|
104 |
| -internal struct _SipHash24Core: _HasherCore { |
105 |
| - private var _state: _SipHashState |
106 |
| - |
107 |
| - @inline(__always) |
108 |
| - internal init(rawSeed: (UInt64, UInt64)) { |
109 |
| - _state = _SipHashState(rawSeed: rawSeed) |
110 |
| - } |
| 77 | +extension Hasher { |
| 78 | + // FIXME: Remove @usableFromInline and @_fixed_layout once Hasher is resilient. |
| 79 | + // rdar://problem/38549901 |
| 80 | + @usableFromInline @_fixed_layout |
| 81 | + internal struct _Core: _HasherCore { |
| 82 | + private var _state: Hasher._State |
111 | 83 |
|
112 |
| - @inline(__always) |
113 |
| - internal mutating func compress(_ m: UInt64) { |
114 |
| - _state.v3 ^= m |
115 |
| - _state._round() |
116 |
| - _state._round() |
117 |
| - _state.v0 ^= m |
118 |
| - } |
119 |
| - |
120 |
| - @inline(__always) |
121 |
| - internal mutating func finalize(tailAndByteCount: UInt64) -> UInt64 { |
122 |
| - compress(tailAndByteCount) |
| 84 | + @inline(__always) |
| 85 | + internal init(rawSeed: (UInt64, UInt64)) { |
| 86 | + _state = Hasher._State(rawSeed: rawSeed) |
| 87 | + } |
123 | 88 |
|
124 |
| - _state.v2 ^= 0xff |
125 |
| - for _ in 0..<4 { |
| 89 | + @inline(__always) |
| 90 | + internal mutating func compress(_ m: UInt64) { |
| 91 | + _state.v3 ^= m |
126 | 92 | _state._round()
|
| 93 | + _state.v0 ^= m |
127 | 94 | }
|
128 |
| - return _state._extract() |
129 |
| - } |
130 |
| -} |
131 |
| - |
132 |
| -// FIXME: This type only exists to facilitate testing, and should not exist in |
133 |
| -// production builds. |
134 |
| -@usableFromInline // @testable |
135 |
| -internal struct _SipHash13 { |
136 |
| - internal typealias Core = _SipHash13Core |
137 |
| - |
138 |
| - internal var _core: _BufferingHasher<Core> |
139 |
| - |
140 |
| - @usableFromInline // @testable |
141 |
| - internal init(_rawSeed: (UInt64, UInt64)) { |
142 |
| - _core = _BufferingHasher(core: Core(rawSeed: _rawSeed)) |
143 |
| - } |
144 |
| - @usableFromInline // @testable |
145 |
| - internal mutating func _combine(_ v: UInt) { _core.combine(v) } |
146 |
| - @usableFromInline // @testable |
147 |
| - internal mutating func _combine(_ v: UInt64) { _core.combine(v) } |
148 |
| - @usableFromInline // @testable |
149 |
| - internal mutating func _combine(_ v: UInt32) { _core.combine(v) } |
150 |
| - @usableFromInline // @testable |
151 |
| - internal mutating func _combine(_ v: UInt16) { _core.combine(v) } |
152 |
| - @usableFromInline // @testable |
153 |
| - internal mutating func _combine(_ v: UInt8) { _core.combine(v) } |
154 |
| - @usableFromInline // @testable |
155 |
| - internal mutating func _combine(bytes v: UInt64, count: Int) { |
156 |
| - _core.combine(bytes: v, count: count) |
157 |
| - } |
158 |
| - @usableFromInline // @testable |
159 |
| - internal mutating func combine(bytes: UnsafeRawBufferPointer) { |
160 |
| - _core.combine(bytes: bytes) |
161 |
| - } |
162 |
| - @usableFromInline // @testable |
163 |
| - internal __consuming func finalize() -> UInt64 { |
164 |
| - var core = _core |
165 |
| - return core.finalize() |
166 |
| - } |
167 |
| -} |
168 |
| - |
169 |
| -// FIXME: This type only exists to facilitate testing, and should not exist in |
170 |
| -// production builds. |
171 |
| -@usableFromInline // @testable |
172 |
| -internal struct _SipHash24 { |
173 |
| - internal typealias Core = _SipHash24Core |
174 |
| - |
175 |
| - internal var _core: _BufferingHasher<Core> |
176 | 95 |
|
177 |
| - @usableFromInline // @testable |
178 |
| - internal init(_rawSeed: (UInt64, UInt64)) { |
179 |
| - _core = _BufferingHasher(core: Core(rawSeed: _rawSeed)) |
180 |
| - } |
181 |
| - @usableFromInline // @testable |
182 |
| - internal mutating func _combine(_ v: UInt) { _core.combine(v) } |
183 |
| - @usableFromInline // @testable |
184 |
| - internal mutating func _combine(_ v: UInt64) { _core.combine(v) } |
185 |
| - @usableFromInline // @testable |
186 |
| - internal mutating func _combine(_ v: UInt32) { _core.combine(v) } |
187 |
| - @usableFromInline // @testable |
188 |
| - internal mutating func _combine(_ v: UInt16) { _core.combine(v) } |
189 |
| - @usableFromInline // @testable |
190 |
| - internal mutating func _combine(_ v: UInt8) { _core.combine(v) } |
191 |
| - @usableFromInline // @testable |
192 |
| - internal mutating func _combine(bytes v: UInt64, count: Int) { |
193 |
| - _core.combine(bytes: v, count: count) |
194 |
| - } |
195 |
| - @usableFromInline // @testable |
196 |
| - internal mutating func combine(bytes: UnsafeRawBufferPointer) { |
197 |
| - _core.combine(bytes: bytes) |
198 |
| - } |
199 |
| - @usableFromInline // @testable |
200 |
| - internal __consuming func finalize() -> UInt64 { |
201 |
| - var core = _core |
202 |
| - return core.finalize() |
| 96 | + @inline(__always) |
| 97 | + internal mutating func finalize(tailAndByteCount: UInt64) -> UInt64 { |
| 98 | + compress(tailAndByteCount) |
| 99 | + _state.v2 ^= 0xff |
| 100 | + for _ in 0..<3 { |
| 101 | + _state._round() |
| 102 | + } |
| 103 | + return _state._extract() |
| 104 | + } |
203 | 105 | }
|
204 | 106 | }
|
0 commit comments