Home | History | Annotate | Download | only in platform
      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/types.h"
     20 
     21 namespace tensorflow {
     22 
     23 struct Fprint128 {
     24   uint64 low64;
     25   uint64 high64;
     26 };
     27 
     28 inline bool operator==(const Fprint128& lhs, const Fprint128& rhs) {
     29   return lhs.low64 == rhs.low64 && lhs.high64 == rhs.high64;
     30 }
     31 
     32 struct Fprint128Hasher {
     33   size_t operator()(const Fprint128& v) const {
     34     // Low64 should be sufficiently mixed to allow use of it as a Hash.
     35     return static_cast<size_t>(v.low64);
     36   }
     37 };
     38 
     39 // TODO(sibyl-Mooth6ku): Change these to accept StringPiece (or make them templated
     40 // on any kind of byte array?).
     41 
     42 // This is a portable fingerprint interface for strings that will never change.
     43 // However, it is not suitable for cryptography.
     44 uint64 Fingerprint64(const string& s);
     45 
     46 // 128-bit variant of Fingerprint64 above (same properties and caveats apply).
     47 Fprint128 Fingerprint128(const string& s);
     48 
     49 namespace internal {
     50 // Mixes some of the bits that got propagated to the high bits back into the
     51 // low bits.
     52 inline uint64 ShiftMix(const uint64 val) { return val ^ (val >> 47); }
     53 }  // namespace internal
     54 
     55 // This concatenates two 64-bit fingerprints. It is a convenience function to
     56 // get a fingerprint for a combination of already fingerprinted components. For
     57 // example this code is used to concatenate the hashes from each of the features
     58 // on sparse crosses.
     59 //
     60 // One shouldn't expect FingerprintCat64(Fingerprint64(x), Fingerprint64(y))
     61 // to indicate anything about FingerprintCat64(StrCat(x, y)). This operation
     62 // is not commutative.
     63 //
     64 // From a security standpoint, we don't encourage this pattern to be used
     65 // for everything as it is vulnerable to length-extension attacks and it
     66 // is easier to compute multicollisions.
     67 inline uint64 FingerprintCat64(const uint64 fp1, const uint64 fp2) {
     68   static const uint64 kMul = 0xc6a4a7935bd1e995ULL;
     69   uint64 result = fp1 ^ kMul;
     70   result ^= internal::ShiftMix(fp2 * kMul) * kMul;
     71   result *= kMul;
     72   result = internal::ShiftMix(result) * kMul;
     73   result = internal::ShiftMix(result);
     74   return result;
     75 }
     76 
     77 }  // namespace tensorflow
     78 
     79 #if defined(PLATFORM_GOOGLE)
     80 #include "tensorflow/core/platform/google/fingerprint.h"
     81 #else
     82 #include "tensorflow/core/platform/default/fingerprint.h"
     83 #endif
     84 
     85 #endif  // TENSORFLOW_CORE_PLATFORM_FINGERPRINT_H_
     86