浏览代码

Make Fingerprint (a full length identity hash) a first class type

Adam Ierymenko 5 年之前
父节点
当前提交
5cec5fe6b1
共有 5 个文件被更改,包括 24 次插入37 次删除
  1. 1 1
      node/CMakeLists.txt
  2. 17 25
      node/Fingerprint.hpp
  3. 1 6
      node/Identity.hpp
  4. 2 2
      node/Tests.cpp
  5. 3 3
      node/Topology.hpp

+ 1 - 1
node/CMakeLists.txt

@@ -16,7 +16,7 @@ set(core_headers
 	ECC384.hpp
 	ECC384.hpp
 	Expect.hpp
 	Expect.hpp
 	FCV.hpp
 	FCV.hpp
-	Hash.hpp
+	Fingerprint.hpp
 	Hashtable.hpp
 	Hashtable.hpp
 	Identity.hpp
 	Identity.hpp
 	InetAddress.hpp
 	InetAddress.hpp

+ 17 - 25
node/Hash.hpp → node/Fingerprint.hpp

@@ -11,8 +11,8 @@
  */
  */
 /****/
 /****/
 
 
-#ifndef ZT_HASH_HPP
-#define ZT_HASH_HPP
+#ifndef ZT_FINGERPRINT_HPP
+#define ZT_FINGERPRINT_HPP
 
 
 #include "Constants.hpp"
 #include "Constants.hpp"
 #include "TriviallyCopyable.hpp"
 #include "TriviallyCopyable.hpp"
@@ -20,7 +20,7 @@
 namespace ZeroTier {
 namespace ZeroTier {
 
 
 /**
 /**
- * Container for cryptographic hashes
+ * Container for 384-bit identity hashes
  *
  *
  * The size of the hash used with this container must be a multiple of 64 bits.
  * The size of the hash used with this container must be a multiple of 64 bits.
  * Currently it's used as H<384> and H<512>.
  * Currently it's used as H<384> and H<512>.
@@ -29,25 +29,17 @@ namespace ZeroTier {
  *
  *
  * @tparam BITS Bits in hash, must be a multiple of 64
  * @tparam BITS Bits in hash, must be a multiple of 64
  */
  */
-template<unsigned int BITS>
-class Hash : public TriviallyCopyable
+class Fingerprint : public TriviallyCopyable
 {
 {
 public:
 public:
-	ZT_ALWAYS_INLINE Hash() noexcept {}
+	ZT_ALWAYS_INLINE Fingerprint() noexcept {}
+	explicit ZT_ALWAYS_INLINE Fingerprint(const void *h384) noexcept { memcpy(_h,h384,48); }
 
 
-	/**
-	 * @param h Hash value of size BITS / 8
-	 */
-	explicit ZT_ALWAYS_INLINE Hash(const void *h) noexcept { memcpy(_h,h,BITS / 8); }
-
-	/**
-	 * @param h Hash value of size BITS / 8
-	 */
-	ZT_ALWAYS_INLINE void set(const void *h) noexcept { memcpy(_h,h,BITS / 8); }
+	ZT_ALWAYS_INLINE void set(const void *h384) noexcept { memcpy(_h,h384,48); }
 
 
 	ZT_ALWAYS_INLINE void zero() noexcept
 	ZT_ALWAYS_INLINE void zero() noexcept
 	{
 	{
-		for(int i=0;i<(BITS / (sizeof(unsigned long) * 8));++i)
+		for(int i=0;i<(384 / (sizeof(unsigned long) * 8));++i)
 			_h[i] = 0;
 			_h[i] = 0;
 	}
 	}
 
 
@@ -57,28 +49,28 @@ public:
 	ZT_ALWAYS_INLINE uint8_t operator[](const unsigned int i) const noexcept { return reinterpret_cast<const uint8_t *>(_h)[i]; }
 	ZT_ALWAYS_INLINE uint8_t operator[](const unsigned int i) const noexcept { return reinterpret_cast<const uint8_t *>(_h)[i]; }
 	ZT_ALWAYS_INLINE uint8_t &operator[](const unsigned int i) noexcept { return reinterpret_cast<uint8_t *>(_h)[i]; }
 	ZT_ALWAYS_INLINE uint8_t &operator[](const unsigned int i) noexcept { return reinterpret_cast<uint8_t *>(_h)[i]; }
 
 
-	static constexpr unsigned int size() noexcept { return BITS / 8; }
+	static constexpr unsigned int size() noexcept { return 48; }
 
 
 	ZT_ALWAYS_INLINE unsigned long hashCode() const noexcept { return _h[0]; }
 	ZT_ALWAYS_INLINE unsigned long hashCode() const noexcept { return _h[0]; }
 
 
 	ZT_ALWAYS_INLINE operator bool() const noexcept
 	ZT_ALWAYS_INLINE operator bool() const noexcept
 	{
 	{
-		for(int i=0;i<(BITS / (sizeof(unsigned long) * 8));++i) {
+		for(int i=0;i<(384 / (sizeof(unsigned long) * 8));++i) {
 			if (_h[i] != 0)
 			if (_h[i] != 0)
 				return true;
 				return true;
 		}
 		}
 		return false;
 		return false;
 	}
 	}
 
 
-	ZT_ALWAYS_INLINE bool operator==(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) == 0; }
-	ZT_ALWAYS_INLINE bool operator!=(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) != 0; }
-	ZT_ALWAYS_INLINE bool operator<(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) < 0; }
-	ZT_ALWAYS_INLINE bool operator>(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) > 0; }
-	ZT_ALWAYS_INLINE bool operator<=(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) <= 0; }
-	ZT_ALWAYS_INLINE bool operator>=(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) >= 0; }
+	ZT_ALWAYS_INLINE bool operator==(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) == 0; }
+	ZT_ALWAYS_INLINE bool operator!=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) != 0; }
+	ZT_ALWAYS_INLINE bool operator<(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) < 0; }
+	ZT_ALWAYS_INLINE bool operator>(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) > 0; }
+	ZT_ALWAYS_INLINE bool operator<=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) <= 0; }
+	ZT_ALWAYS_INLINE bool operator>=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) >= 0; }
 
 
 private:
 private:
-	unsigned long _h[BITS / (sizeof(unsigned long) * 8)];
+	unsigned long _h[384 / (sizeof(unsigned long) * 8)];
 };
 };
 
 
 } // namespace ZeroTier
 } // namespace ZeroTier

+ 1 - 6
node/Identity.hpp

@@ -21,7 +21,7 @@
 #include "SHA512.hpp"
 #include "SHA512.hpp"
 #include "ECC384.hpp"
 #include "ECC384.hpp"
 #include "TriviallyCopyable.hpp"
 #include "TriviallyCopyable.hpp"
-#include "Hash.hpp"
+#include "Fingerprint.hpp"
 
 
 #include <cstdio>
 #include <cstdio>
 #include <cstdlib>
 #include <cstdlib>
@@ -55,11 +55,6 @@ public:
 		P384 = ZT_CRYPTO_ALG_P384      // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
 		P384 = ZT_CRYPTO_ALG_P384      // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+)
 	};
 	};
 
 
-	/**
-	 * 384-bit full hash of identity's public key(s)
-	 */
-	typedef Hash<384> Fingerprint;
-
 	/**
 	/**
 	 * A nil/empty identity instance
 	 * A nil/empty identity instance
 	 */
 	 */

+ 2 - 2
node/Tests.cpp

@@ -40,6 +40,7 @@
 #include "SHA512.hpp"
 #include "SHA512.hpp"
 #include "Defragmenter.hpp"
 #include "Defragmenter.hpp"
 #include "MIMC52.hpp"
 #include "MIMC52.hpp"
+#include "Fingerprint.hpp"
 
 
 #include <cstdint>
 #include <cstdint>
 #include <cstring>
 #include <cstring>
@@ -284,8 +285,7 @@ extern "C" const char *ZTT_general()
 			ZT_T_ASSERT(sizeof(sockaddr_in) <= sizeof(InetAddress));
 			ZT_T_ASSERT(sizeof(sockaddr_in) <= sizeof(InetAddress));
 			ZT_T_ASSERT(sizeof(sockaddr_in6) <= sizeof(InetAddress));
 			ZT_T_ASSERT(sizeof(sockaddr_in6) <= sizeof(InetAddress));
 			ZT_T_ASSERT(sizeof(sockaddr) <= sizeof(InetAddress));
 			ZT_T_ASSERT(sizeof(sockaddr) <= sizeof(InetAddress));
-			ZT_T_ASSERT(sizeof(Hash<384>) == 48);
-			ZT_T_ASSERT(sizeof(Hash<512>) == 64);
+			ZT_T_ASSERT(sizeof(Fingerprint) == 48);
 			ZT_T_PRINTF("OK" ZT_EOL_S);
 			ZT_T_PRINTF("OK" ZT_EOL_S);
 		}
 		}
 
 

+ 3 - 3
node/Topology.hpp

@@ -30,7 +30,7 @@
 #include "Hashtable.hpp"
 #include "Hashtable.hpp"
 #include "SharedPtr.hpp"
 #include "SharedPtr.hpp"
 #include "ScopedPtr.hpp"
 #include "ScopedPtr.hpp"
-#include "Hash.hpp"
+#include "Fingerprint.hpp"
 
 
 namespace ZeroTier {
 namespace ZeroTier {
 
 
@@ -94,7 +94,7 @@ public:
 	 * @param hash Identity hash
 	 * @param hash Identity hash
 	 * @return Peer or NULL if no peer is currently in memory for this hash (cache is not checked in this case)
 	 * @return Peer or NULL if no peer is currently in memory for this hash (cache is not checked in this case)
 	 */
 	 */
-	ZT_ALWAYS_INLINE SharedPtr<Peer> peerByHash(const Hash<384> &hash)
+	ZT_ALWAYS_INLINE SharedPtr<Peer> peerByHash(const Fingerprint &hash)
 	{
 	{
 		RWMutex::RLock _l(_peers_l);
 		RWMutex::RLock _l(_peers_l);
 		const SharedPtr<Peer> *const ap = _peersByIdentityHash.get(hash);
 		const SharedPtr<Peer> *const ap = _peersByIdentityHash.get(hash);
@@ -364,7 +364,7 @@ private:
 
 
 	Hashtable< Address,SharedPtr<Peer> > _peers;
 	Hashtable< Address,SharedPtr<Peer> > _peers;
 	Hashtable< uint64_t,SharedPtr<Peer> > _peersByIncomingProbe;
 	Hashtable< uint64_t,SharedPtr<Peer> > _peersByIncomingProbe;
-	Hashtable< Hash<384>,SharedPtr<Peer> > _peersByIdentityHash;
+	Hashtable< Fingerprint,SharedPtr<Peer> > _peersByIdentityHash;
 	Hashtable< uint64_t,SharedPtr<Path> > _paths;
 	Hashtable< uint64_t,SharedPtr<Path> > _paths;
 	std::set< Identity > _roots; // locked by _peers_l
 	std::set< Identity > _roots; // locked by _peers_l
 	std::vector< SharedPtr<Peer> > _rootPeers; // locked by _peers_l
 	std::vector< SharedPtr<Peer> > _rootPeers; // locked by _peers_l