1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_CORE_PLATFORM_FINGERPRINT_H_
17 #define TENSORFLOW_CORE_PLATFORM_FINGERPRINT_H_
18
19 #include "tensorflow/core/platform/stringpiece.h"
20 #include "tensorflow/core/platform/types.h"
21
22 // Use "libtextclassifier_hash_static" for farmhash
23 #include "utils/hash/farmhash.h"
24
25 namespace tensorflow {
26
27 struct Fprint128 {
28 uint64 low64;
29 uint64 high64;
30 };
31
32 inline bool operator==(const Fprint128& lhs, const Fprint128& rhs) {
33 return lhs.low64 == rhs.low64 && lhs.high64 == rhs.high64;
34 }
35
36 struct Fprint128Hasher {
operatorFprint128Hasher37 size_t operator()(const Fprint128& v) const {
38 // Low64 should be sufficiently mixed to allow use of it as a Hash.
39 return static_cast<size_t>(v.low64);
40 }
41 };
42
43 namespace internal {
44 // Mixes some of the bits that got propagated to the high bits back into the
45 // low bits.
ShiftMix(const uint64 val)46 inline uint64 ShiftMix(const uint64 val) { return val ^ (val >> 47); }
47 } // namespace internal
48
49 // This concatenates two 64-bit fingerprints. It is a convenience function to
50 // get a fingerprint for a combination of already fingerprinted components. For
51 // example this code is used to concatenate the hashes from each of the features
52 // on sparse crosses.
53 //
54 // One shouldn't expect FingerprintCat64(Fingerprint64(x), Fingerprint64(y))
55 // to indicate anything about FingerprintCat64(StrCat(x, y)). This operation
56 // is not commutative.
57 //
58 // From a security standpoint, we don't encourage this pattern to be used
59 // for everything as it is vulnerable to length-extension attacks and it
60 // is easier to compute multicollisions.
FingerprintCat64(const uint64 fp1,const uint64 fp2)61 inline uint64 FingerprintCat64(const uint64 fp1, const uint64 fp2) {
62 static const uint64 kMul = 0xc6a4a7935bd1e995ULL;
63 uint64 result = fp1 ^ kMul;
64 result ^= internal::ShiftMix(fp2 * kMul) * kMul;
65 result *= kMul;
66 result = internal::ShiftMix(result) * kMul;
67 result = internal::ShiftMix(result);
68 return result;
69 }
70
71 // This is a portable fingerprint interface for strings that will never change.
72 // However, it is not suitable for cryptography.
Fingerprint64(const StringPiece s)73 inline uint64 Fingerprint64(const StringPiece s) {
74 #ifdef USE_OSS_FARMHASH
75 return ::util::Fingerprint64(s.data(), s.size());
76 #else
77 // Fingerprint op depends on the fact that Fingerprint64() is implemented by
78 // Farmhash. If the implementation ever changes, Fingerprint op should be
79 // modified to keep using Farmhash.
80 // LINT.IfChange
81 return farmhash::Fingerprint64(s.data(), s.size());
82 // LINT.ThenChange(//third_party/tensorflow/core/kernels/fingerprint_op.cc)
83 #endif
84 }
85
86 // 32-bit variant of Fingerprint64 above (same properties and caveats apply).
Fingerprint32(const StringPiece s)87 inline uint32 Fingerprint32(const StringPiece s) {
88 #ifdef USE_OSS_FARMHASH
89 return ::util::Fingerprint32(s.data(), s.size());
90 #else
91 return farmhash::Fingerprint32(s.data(), s.size());
92 #endif
93 }
94
95 // 128-bit variant of Fingerprint64 above (same properties and caveats apply).
Fingerprint128(const StringPiece s)96 inline Fprint128 Fingerprint128(const StringPiece s) {
97 #ifdef USE_OSS_FARMHASH
98 const auto fingerprint = ::util::Fingerprint128(s.data(), s.size());
99 return {::util::Uint128Low64(fingerprint),
100 ::util::Uint128High64(fingerprint)};
101 #else
102 const auto fingerprint = farmhash::Fingerprint128(s.data(), s.size());
103 return {farmhash::Uint128Low64(fingerprint),
104 farmhash::Uint128High64(fingerprint)};
105 #endif
106 }
107
108 } // namespace tensorflow
109
110 #endif // TENSORFLOW_CORE_PLATFORM_FINGERPRINT_H_
111