-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96a608b
commit 4ced269
Showing
4 changed files
with
76 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <random> | ||
#include <vector> | ||
#include <pdq/cpp/common/pdqutils.h> | ||
#include <algorithm> | ||
|
||
namespace facebook { | ||
namespace pdq { | ||
namespace hashing { | ||
|
||
// Generate random hash | ||
Hash256 generateRandomHash(std::mt19937& gen) { | ||
Hash256 hash; | ||
std::uniform_int_distribution<uint16_t> dist(0, UINT16_MAX); | ||
|
||
for (int i = 0; i < HASH256_NUM_WORDS; i++) { | ||
hash.w[i] = dist(gen); | ||
} | ||
return hash; | ||
} | ||
|
||
// Add noise to hash by flipping random bits | ||
Hash256 addNoise( | ||
const Hash256& original, | ||
int numBitsToFlip, | ||
std::mt19937& gen) { | ||
Hash256 noisy = original; | ||
std::vector<int> bitIndices(256); | ||
for (int i = 0; i < 256; i++) bitIndices[i] = i; | ||
std::shuffle(bitIndices.begin(), bitIndices.end(), gen); | ||
for (int i = 0; i < numBitsToFlip; i++) { | ||
int bitIndex = bitIndices[i]; | ||
int wordIndex = bitIndex / 16; | ||
int position = bitIndex % 16; | ||
noisy.w[wordIndex] ^= (1 << position); | ||
} | ||
return noisy; | ||
} | ||
|
||
} // namespace hashing | ||
} // namespace pdq | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef PDQ_UTILS_H | ||
#define PDQ_UTILS_H | ||
|
||
#include <pdq/cpp/common/pdqhashtypes.h> | ||
#include <random> | ||
#include <vector> | ||
|
||
namespace facebook { | ||
namespace pdq { | ||
namespace hashing { | ||
|
||
// Generate random hash | ||
Hash256 generateRandomHash(std::mt19937& gen); | ||
|
||
// Add noise to hash by flipping random bits | ||
Hash256 addNoise( | ||
const Hash256& original, | ||
int numBitsToFlip, | ||
std::mt19937& gen); | ||
|
||
} // namespace hashing | ||
} // namespace pdq | ||
} // namespace facebook | ||
|
||
#endif |