2016-12-08 08:52:44 -05:00
|
|
|
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
|
2014-03-14 14:13:41 -04:00
|
|
|
|
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
|
|
|
#include "KeyGenerator.h"
|
2014-03-14 14:13:41 -04:00
|
|
|
#include "UnrealPak.h"
|
|
|
|
|
#include "IPlatformFilePak.h"
|
|
|
|
|
#include "SecureHash.h"
|
|
|
|
|
#include "BigInt.h"
|
|
|
|
|
#include "TaskGraphInterfaces.h"
|
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
|
|
|
#include "HAL/Runnable.h"
|
|
|
|
|
#include "HAL/RunnableThread.h"
|
|
|
|
|
#include "Math/RandomStream.h"
|
|
|
|
|
#include "Misc/FileHelper.h"
|
|
|
|
|
#include "Misc/CommandLine.h"
|
2014-03-14 14:13:41 -04:00
|
|
|
#include "Primes.inl"
|
|
|
|
|
|
|
|
|
|
// Global constants
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
const TEncryptionInt Two(2);
|
|
|
|
|
const TEncryptionInt IterationStep(1000);
|
|
|
|
|
TArray<TEncryptionInt> PrimeLookupTable;
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A thread that finds factors in the given range
|
|
|
|
|
*/
|
|
|
|
|
class FPrimeCheckRunnable : public FRunnable
|
|
|
|
|
{
|
|
|
|
|
/** Flag indicating if a factor has been found. Shared across multiple threads. */
|
|
|
|
|
FThreadSafeCounter& FoundFactor;
|
|
|
|
|
/** Candidate for a prime number */
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt PotentialPrime;
|
2014-03-14 14:13:41 -04:00
|
|
|
/** Start of a range to check for factors */
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt InitialValue;
|
2014-03-14 14:13:41 -04:00
|
|
|
/** End of a range to check for factors */
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MaxValue;
|
2014-03-14 14:13:41 -04:00
|
|
|
/** This thread */
|
|
|
|
|
FRunnableThread* Thread;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
FPrimeCheckRunnable(FThreadSafeCounter& InFoundFactor, TEncryptionInt Candidate, TEncryptionInt InInitialValue, TEncryptionInt InMaxValue)
|
2014-03-14 14:13:41 -04:00
|
|
|
: FoundFactor(InFoundFactor)
|
|
|
|
|
, PotentialPrime(Candidate)
|
|
|
|
|
, InitialValue(InInitialValue)
|
|
|
|
|
, MaxValue(InMaxValue)
|
|
|
|
|
{
|
|
|
|
|
// Must be odd number
|
2016-05-10 16:00:39 -04:00
|
|
|
check(!(Candidate & TEncryptionInt::One).IsZero());
|
2014-05-12 08:41:17 -04:00
|
|
|
Thread = FRunnableThread::Create(this, TEXT("FPrimeCheckRunnable"));
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~FPrimeCheckRunnable()
|
|
|
|
|
{
|
|
|
|
|
delete Thread;
|
|
|
|
|
Thread = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FRunnableThread* GetThread()
|
|
|
|
|
{
|
|
|
|
|
return Thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start FRunnable interface
|
2014-06-13 06:14:46 -04:00
|
|
|
virtual bool Init() override { return true; }
|
|
|
|
|
virtual uint32 Run() override
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Remainder;
|
2014-03-14 14:13:41 -04:00
|
|
|
int32 FactorCheckTimer = 0;
|
2016-03-24 13:53:55 -04:00
|
|
|
for (TEncryptionInt Factor = InitialValue; InitialValue <= MaxValue; Factor += Two)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Dividend(PotentialPrime);
|
2014-03-14 14:13:41 -04:00
|
|
|
Dividend.DivideWithRemainder(Factor, Remainder);
|
|
|
|
|
if (Remainder.IsZero())
|
|
|
|
|
{
|
|
|
|
|
FoundFactor.Increment();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Don't check the FoundFactor too often
|
|
|
|
|
FactorCheckTimer++;
|
|
|
|
|
if (FactorCheckTimer >= 100)
|
|
|
|
|
{
|
|
|
|
|
FactorCheckTimer = 0;
|
|
|
|
|
if (FoundFactor.GetValue() != 0)
|
|
|
|
|
{
|
|
|
|
|
// Another thread found a factor
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// End FRunnable interface
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the value is a prime number.
|
|
|
|
|
*/
|
2016-03-24 13:53:55 -04:00
|
|
|
bool IsPrime(const TEncryptionInt& InValue, bool bUseTasks)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
// 2 is but we don't care about small numbers here.
|
2016-05-10 16:00:39 -04:00
|
|
|
if ((InValue & TEncryptionInt::One) == 0)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Remainder;
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
// Check against known prime factors
|
|
|
|
|
int32 Index;
|
|
|
|
|
for (Index = 0; Index < PrimeLookupTable.Num() && PrimeLookupTable[Index] < InValue; ++Index)
|
|
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Dividend(InValue);
|
2014-03-14 14:13:41 -04:00
|
|
|
Dividend.DivideWithRemainder(PrimeLookupTable[Index], Remainder);
|
|
|
|
|
if (Remainder.IsZero())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// This means the number is smaller than one of the primes in the prime table and it has no factors
|
|
|
|
|
if (Index < PrimeLookupTable.Num())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Brute force, check all odd numbers > MaxKnownPrime < sqrt(Number)
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MaxFactorValue(InValue);
|
2014-03-14 14:13:41 -04:00
|
|
|
MaxFactorValue.Sqrt();
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Factor(PrimeLookupTable[PrimeLookupTable.Num() - 1] + Two);
|
2014-03-14 14:13:41 -04:00
|
|
|
if (bUseTasks)
|
|
|
|
|
{
|
|
|
|
|
// Mutithreaded path. Split the range we look for factors over multiple threads. If one thread finds a factor
|
|
|
|
|
// we stop and reject this number.
|
|
|
|
|
// The worst case is when we actually have a prime number.
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Detected potentially prime number %s. This may take a while..."), *InValue.ToString());
|
|
|
|
|
|
|
|
|
|
const int32 TaskCount = FPlatformMisc::NumberOfCoresIncludingHyperthreads();
|
|
|
|
|
TArray<FPrimeCheckRunnable*> Tasks;
|
|
|
|
|
Tasks.Reserve(TaskCount);
|
|
|
|
|
FThreadSafeCounter FoundFactors(0);
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Range(MaxFactorValue - Factor);
|
2014-03-14 14:13:41 -04:00
|
|
|
Range /= TaskCount;
|
|
|
|
|
|
|
|
|
|
// Spawn threads
|
|
|
|
|
for (int32 TaskIndex = 0; TaskIndex < TaskCount; ++TaskIndex)
|
|
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MaxValue(Factor + Range);
|
2014-03-14 14:13:41 -04:00
|
|
|
Tasks.Add(new FPrimeCheckRunnable(FoundFactors, InValue, Factor, MaxValue));
|
|
|
|
|
Factor = MaxValue;
|
2016-05-10 16:00:39 -04:00
|
|
|
if ((Factor & TEncryptionInt::One) == 0)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
++Factor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Wait for all threads to complete
|
|
|
|
|
for (int32 TaskIndex = 0; TaskIndex < Tasks.Num(); ++TaskIndex)
|
|
|
|
|
{
|
|
|
|
|
Tasks[TaskIndex]->GetThread()->WaitForCompletion();
|
|
|
|
|
delete Tasks[TaskIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FoundFactors.GetValue() > 0)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("%s is not prime."), *InValue.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("%s is prime!"), *InValue.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Single threaded path (used for generating prime table)
|
|
|
|
|
while (Factor < MaxFactorValue)
|
|
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Dividend(InValue);
|
2014-03-14 14:13:41 -04:00
|
|
|
Dividend.DivideWithRemainder(Factor, Remainder);
|
|
|
|
|
if (Remainder.IsZero())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Factor += Two;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate two random prime numbers
|
|
|
|
|
*/
|
2016-03-24 13:53:55 -04:00
|
|
|
void GeneratePrimeNumbers(TEncryptionInt& P, TEncryptionInt& Q)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
// Generate a random odd number
|
|
|
|
|
FRandomStream Rand((int32)(FDateTime::Now().GetTicks() % (int64)MAX_int32));
|
|
|
|
|
uint32 RandBits[256/32] =
|
|
|
|
|
{
|
|
|
|
|
0xffffffff,
|
|
|
|
|
(uint32)Rand.RandRange(0, MAX_int32 - 1),
|
|
|
|
|
(uint32)Rand.RandRange(0, MAX_int32 - 1),
|
|
|
|
|
0, //(uint32)Rand.RandRange(0, MAX_int32 - 1) | 0xa0ff0000,
|
|
|
|
|
0, 0, 0, 0
|
|
|
|
|
};
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt InitialValue(RandBits);
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
// We need two primes
|
2016-03-24 13:53:55 -04:00
|
|
|
TArray<TEncryptionInt> DiscoveredPrimes;
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
int64 IterationCounter = 0;
|
|
|
|
|
double TimeAccumulator = 0.0;
|
|
|
|
|
const double StartTime = FPlatformTime::Seconds();
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (IterationCounter == 10)
|
|
|
|
|
{
|
|
|
|
|
IterationCounter = 0;
|
|
|
|
|
TimeAccumulator = 0.0;
|
|
|
|
|
}
|
|
|
|
|
IterationCounter++;
|
|
|
|
|
|
|
|
|
|
const double IterationStartTime = FPlatformTime::Seconds();
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MinValue(InitialValue - IterationStep);
|
2014-03-14 14:13:41 -04:00
|
|
|
while (InitialValue >= MinValue && DiscoveredPrimes.Num() < 2)
|
|
|
|
|
{
|
|
|
|
|
if (IsPrime(InitialValue, true))
|
|
|
|
|
{
|
|
|
|
|
DiscoveredPrimes.Add(InitialValue);
|
|
|
|
|
}
|
|
|
|
|
InitialValue -= Two;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (DiscoveredPrimes.Num() < 2);
|
|
|
|
|
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generated prime numbers in %.2lfs."), FPlatformTime::Seconds() - StartTime);
|
|
|
|
|
P = DiscoveredPrimes[0];
|
|
|
|
|
Q = DiscoveredPrimes[1];
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("P=%s"), *P.ToString());
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Q=%s"), *Q.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Lookup table generation - fill it with precompile primes
|
|
|
|
|
*/
|
|
|
|
|
void FillPrimeLookupTableWithPrecompiledNumbers()
|
|
|
|
|
{
|
|
|
|
|
const int32 PrimeTableLength = ARRAY_COUNT(PrimeTable);
|
|
|
|
|
PrimeLookupTable.Reserve(PrimeTableLength * PrimeTableLength);
|
|
|
|
|
for (int32 Index = 0; Index < PrimeTableLength; ++Index)
|
|
|
|
|
{
|
|
|
|
|
PrimeLookupTable.Add(PrimeTable[Index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeneratePrimeNumberTable(int64 MaxValue, const TCHAR* Filename)
|
|
|
|
|
{
|
|
|
|
|
FillPrimeLookupTableWithPrecompiledNumbers();
|
|
|
|
|
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generating prime number table <= %lld: %s."), MaxValue, Filename);
|
|
|
|
|
|
2016-12-08 08:52:44 -05:00
|
|
|
FString PrimeTableString(TEXT("// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.\nTEncryptionInt PrimeTable[] = \n{\n\t2, "));
|
2014-03-14 14:13:41 -04:00
|
|
|
int64 PrimeCount = 1;
|
|
|
|
|
const double StartTime = FPlatformTime::Seconds();
|
|
|
|
|
for (int64 SmallNumber = 3; SmallNumber <= MaxValue; SmallNumber += 2)
|
|
|
|
|
{
|
|
|
|
|
if (IsPrime(SmallNumber, false))
|
|
|
|
|
{
|
2015-04-22 15:04:48 -04:00
|
|
|
PrimeTableString += FString::Printf(TEXT("%lld, "), SmallNumber);
|
2014-03-14 14:13:41 -04:00
|
|
|
PrimeCount++;
|
|
|
|
|
if ((PrimeCount % 10) == 0)
|
|
|
|
|
{
|
2015-04-22 15:04:48 -04:00
|
|
|
PrimeTableString += TEXT("\n\t");
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-22 15:04:48 -04:00
|
|
|
PrimeTableString += TEXT("\n};\n");
|
2014-03-14 14:13:41 -04:00
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generated %lld primes in %.4lfs."), PrimeCount, FPlatformTime::Seconds() - StartTime);
|
2015-04-22 15:04:48 -04:00
|
|
|
FFileHelper::SaveStringToFile(PrimeTableString, Filename);
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Multithreaded prime number generation (for the prime table)
|
|
|
|
|
* Finds prime numbers in the given range.
|
|
|
|
|
*/
|
|
|
|
|
class FPrimeFinderRunnable : public FRunnable
|
|
|
|
|
{
|
|
|
|
|
/** Range start */
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MinValue;
|
2014-03-14 14:13:41 -04:00
|
|
|
/** Range end */
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MaxValue;
|
2014-03-14 14:13:41 -04:00
|
|
|
/** This thread */
|
|
|
|
|
FRunnableThread* Thread;
|
|
|
|
|
/** All primes found in the given range */
|
2016-03-24 13:53:55 -04:00
|
|
|
TArray<TEncryptionInt> FoundPrimes;
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
FPrimeFinderRunnable(TEncryptionInt InMinValue, TEncryptionInt InMaxValue)
|
2014-03-14 14:13:41 -04:00
|
|
|
: MinValue(InMinValue)
|
|
|
|
|
, MaxValue(InMaxValue)
|
|
|
|
|
{
|
|
|
|
|
// Must be an odd number
|
2016-05-10 16:00:39 -04:00
|
|
|
check(!(MinValue & TEncryptionInt::One).IsZero());
|
2014-05-12 08:42:47 -04:00
|
|
|
Thread = FRunnableThread::Create(this, TEXT("FPrimeFinderRunnable"));
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~FPrimeFinderRunnable()
|
|
|
|
|
{
|
|
|
|
|
delete Thread;
|
|
|
|
|
Thread = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FRunnableThread* GetThread()
|
|
|
|
|
{
|
|
|
|
|
return Thread;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
const TArray<TEncryptionInt>& GetFoundPrimes() const
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
return FoundPrimes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start FRunnable interface
|
2014-06-13 06:14:46 -04:00
|
|
|
virtual bool Init() override { return true; }
|
|
|
|
|
virtual uint32 Run() override
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Remainder;
|
2014-03-14 14:13:41 -04:00
|
|
|
int32 FactorCheckTimer = 0;
|
2016-03-24 13:53:55 -04:00
|
|
|
for (TEncryptionInt Candidate = MinValue; Candidate <= MaxValue; Candidate += Two)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
if (IsPrime(Candidate, false))
|
|
|
|
|
{
|
|
|
|
|
FoundPrimes.Add(Candidate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// End FRunnable interface
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a lookup table in runtime.
|
|
|
|
|
* This is a superset of precompiled prime table and primes generated on startup.
|
|
|
|
|
* The reason for this is that the precompiled table can't be too big because it affects
|
|
|
|
|
* the compile times and we don't usually use UnrealPak for prime number generation anyway.
|
|
|
|
|
*/
|
|
|
|
|
void GeneratePrimeNumberLookupTable()
|
|
|
|
|
{
|
|
|
|
|
const int32 PrimeTableLength = ARRAY_COUNT(PrimeTable);
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generating prime number lookup table (max size: %d)."), PrimeTableLength * PrimeTableLength);
|
|
|
|
|
const double StartTime = FPlatformTime::Seconds();
|
|
|
|
|
|
|
|
|
|
FillPrimeLookupTableWithPrecompiledNumbers();
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MinPrimeValue(PrimeLookupTable[PrimeLookupTable.Num() - 1] + Two);
|
|
|
|
|
TEncryptionInt MaxPrimeValue(MinPrimeValue);
|
2014-03-14 14:13:41 -04:00
|
|
|
MaxPrimeValue *= 100;
|
|
|
|
|
|
|
|
|
|
const int32 TaskCount = FPlatformMisc::NumberOfCoresIncludingHyperthreads();
|
|
|
|
|
TArray<FPrimeFinderRunnable*> Tasks;
|
|
|
|
|
Tasks.Reserve(TaskCount);
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt Range(MaxPrimeValue - MinPrimeValue);
|
2014-03-14 14:13:41 -04:00
|
|
|
Range /= TaskCount;
|
|
|
|
|
|
|
|
|
|
for (int32 TaskIndex = 0; TaskIndex < TaskCount; ++TaskIndex)
|
|
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt MaxValue(MinPrimeValue + Range);
|
2014-03-14 14:13:41 -04:00
|
|
|
Tasks.Add(new FPrimeFinderRunnable(MinPrimeValue, MaxValue));
|
|
|
|
|
MinPrimeValue = MaxValue;
|
2016-05-10 16:00:39 -04:00
|
|
|
if ((MinPrimeValue & TEncryptionInt::One) == 0)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
++MinPrimeValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TArray<TEncryptionInt> NewPrimes;
|
2014-03-14 14:13:41 -04:00
|
|
|
for (int32 TaskIndex = 0; TaskIndex < Tasks.Num(); ++TaskIndex)
|
|
|
|
|
{
|
|
|
|
|
Tasks[TaskIndex]->GetThread()->WaitForCompletion();
|
|
|
|
|
NewPrimes.Append(Tasks[TaskIndex]->GetFoundPrimes());
|
|
|
|
|
delete Tasks[TaskIndex];
|
|
|
|
|
}
|
|
|
|
|
PrimeLookupTable.Append(NewPrimes);
|
|
|
|
|
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generated %d primes in %.4lfs."), PrimeLookupTable.Num(), FPlatformTime::Seconds() - StartTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GenerateKeys(const TCHAR* KeyFilename)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generating keys %s."), KeyFilename);
|
|
|
|
|
|
|
|
|
|
GeneratePrimeNumberLookupTable();
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
TEncryptionInt P;
|
|
|
|
|
TEncryptionInt Q;
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
FString CmdLineP;
|
|
|
|
|
FString CmdLineQ;
|
2014-03-14 14:13:41 -04:00
|
|
|
FParse::Value(FCommandLine::Get(), TEXT("P="), CmdLineP);
|
|
|
|
|
FParse::Value(FCommandLine::Get(), TEXT("Q="), CmdLineQ);
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
const bool bNoVerifyPrimes = FParse::Param(FCommandLine::Get(), TEXT("NoVerifyPrimes"));
|
|
|
|
|
|
|
|
|
|
P.Parse(CmdLineP);
|
|
|
|
|
Q.Parse(CmdLineQ);
|
|
|
|
|
|
2014-03-14 14:13:41 -04:00
|
|
|
// Check if we have valid primes in the command line.
|
|
|
|
|
// @todo: IsPrime check should probably go when we start to use big primes
|
2016-03-24 13:53:55 -04:00
|
|
|
bool bGeneratePrimes = !(P > Two && Q > Two);
|
|
|
|
|
if (!bGeneratePrimes && !bNoVerifyPrimes)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
if (!IsPrime(P, false))
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
UE_LOG(LogPakFile, Warning, TEXT("P=%s is not prime!"), *CmdLineP);
|
2014-03-14 14:13:41 -04:00
|
|
|
bGeneratePrimes = true;
|
|
|
|
|
}
|
2016-03-24 13:53:55 -04:00
|
|
|
if (!IsPrime(Q, false))
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2016-03-24 13:53:55 -04:00
|
|
|
UE_LOG(LogPakFile, Warning, TEXT("Q=%s is not prime!"), *CmdLineQ);
|
2014-03-14 14:13:41 -04:00
|
|
|
bGeneratePrimes = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bGeneratePrimes)
|
|
|
|
|
{
|
|
|
|
|
// Generate random prime numbers
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generating prime numbers..."));
|
|
|
|
|
GeneratePrimeNumbers(P, Q);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Use predefined primes
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Using predefined values to generate keys."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate key pair
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Generating key pair..."));
|
|
|
|
|
FKeyPair Keys;
|
|
|
|
|
FEncryption::GenerateKeyPair(P, Q, Keys.PublicKey, Keys.PrivateKey);
|
|
|
|
|
|
2016-03-24 13:53:55 -04:00
|
|
|
if (TestKeys(Keys))
|
|
|
|
|
{
|
|
|
|
|
return SaveKeysToFile(Keys, KeyFilename);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SaveKeysToFile(const FKeyPair& Keys, const TCHAR* KeyFilename)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Saving key pair in %s"), KeyFilename);
|
|
|
|
|
FString KeyFileContents = FString::Printf(TEXT("%s\n%s\n%s"), *Keys.PrivateKey.Exponent.ToString(), *Keys.PrivateKey.Modulus.ToString(), *Keys.PublicKey.Exponent.ToString());
|
|
|
|
|
return FFileHelper::SaveStringToFile(KeyFileContents, KeyFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ReadKeysFromFile(const TCHAR* KeyFilename, FKeyPair& OutKeys)
|
|
|
|
|
{
|
|
|
|
|
bool bResult = false;
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Loading key pair from %s"), KeyFilename);
|
|
|
|
|
FString KeyFileContents;
|
|
|
|
|
if (FFileHelper::LoadFileToString(KeyFileContents, KeyFilename))
|
|
|
|
|
{
|
|
|
|
|
TArray<FString> KeyValues;
|
2015-03-02 15:51:37 -05:00
|
|
|
KeyFileContents.ParseIntoArrayWS(KeyValues);
|
2014-03-14 14:13:41 -04:00
|
|
|
if (KeyValues.Num() != 3)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Error, TEXT("Expecting 3 values in %s, got %d."), KeyFilename, KeyValues.Num());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OutKeys.PrivateKey.Exponent.Parse(KeyValues[0]);
|
|
|
|
|
OutKeys.PrivateKey.Modulus.Parse(KeyValues[1]);
|
|
|
|
|
OutKeys.PublicKey.Exponent.Parse(KeyValues[2]);
|
|
|
|
|
OutKeys.PublicKey.Modulus = OutKeys.PrivateKey.Modulus;
|
|
|
|
|
bResult = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Error, TEXT("Failed to load key pair from %s"), KeyFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bResult;
|
|
|
|
|
}
|
2016-03-24 13:53:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
bool TestKeys(FKeyPair& Pair)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Testing signature keys."));
|
|
|
|
|
|
|
|
|
|
// Just some random values
|
|
|
|
|
static TEncryptionInt TestData[] =
|
|
|
|
|
{
|
|
|
|
|
11,
|
|
|
|
|
253,
|
|
|
|
|
128,
|
|
|
|
|
234,
|
|
|
|
|
56,
|
|
|
|
|
89,
|
|
|
|
|
34,
|
|
|
|
|
179,
|
|
|
|
|
29,
|
|
|
|
|
1024,
|
|
|
|
|
(int64)(MAX_int32),
|
|
|
|
|
(int64)(MAX_uint32) - 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int32 TestIndex = 0; TestIndex < ARRAY_COUNT(TestData); ++TestIndex)
|
|
|
|
|
{
|
|
|
|
|
TEncryptionInt EncryptedData = FEncryption::ModularPow(TestData[TestIndex], Pair.PrivateKey.Exponent, Pair.PrivateKey.Modulus);
|
|
|
|
|
TEncryptionInt DecryptedData = FEncryption::ModularPow(EncryptedData, Pair.PublicKey.Exponent, Pair.PublicKey.Modulus);
|
|
|
|
|
if (TestData[TestIndex] != DecryptedData)
|
|
|
|
|
{
|
|
|
|
|
UE_LOG(LogPakFile, Error, TEXT("Keys do not properly encrypt/decrypt data (failed test with %lld)"), TestData[TestIndex].ToInt());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UE_LOG(LogPakFile, Display, TEXT("Signature keys check completed successfuly."));
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|