From 9866d8ef7fe7ea43255060441fedce3d62e0b9b8 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Wed, 26 Apr 2017 17:25:30 +0300 Subject: [PATCH] PhraseSet.FillPhraseSet moved out to unmanaged code --- .../WhiteRabbit.UnmanagedBridge.cpp | 45 +++++++++++++++++++ .../WhiteRabbit.UnmanagedBridge.h | 1 + dotnet/WhiteRabbit/PhraseSet.cs | 43 +----------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.cpp b/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.cpp index 21d049b..320c742 100644 --- a/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.cpp +++ b/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.cpp @@ -25,3 +25,48 @@ void WhiteRabbitUnmanagedBridge::MD5Unmanaged::ComputeMD5(unsigned __int32 * inp } #endif } + +void WhiteRabbitUnmanagedBridge::MD5Unmanaged::FillPhraseSet(__int64* bufferPointer, __int64* allWordsPointer, __int32* wordIndexes, unsigned __int64* permutationsPointer, int permutationOffset, int numberOfCharacters, int numberOfWords) +{ + long* longBuffer = (long*)bufferPointer; + + auto currentPermutationPointer = permutationsPointer + permutationOffset; + for (auto i = 0; i < WhiteRabbitUnmanagedBridge::MD5Unmanaged::PhrasesPerSet; i++, currentPermutationPointer++) + { + auto permutation = *currentPermutationPointer; + if (permutation == 0) + { + continue; + } + + auto cumulativeWordOffsetX4 = 0; + for (auto j = 0; j < numberOfWords; j++) + { + auto currentWord = allWordsPointer + wordIndexes[permutation & 15] * 128; + permutation = permutation >> 4; + longBuffer[0] |= currentWord[cumulativeWordOffsetX4 + 0]; + longBuffer[1] |= currentWord[cumulativeWordOffsetX4 + 1]; + longBuffer[2] ^= currentWord[cumulativeWordOffsetX4 + 2]; + longBuffer[3] ^= currentWord[cumulativeWordOffsetX4 + 3]; + cumulativeWordOffsetX4 += (int)currentWord[127]; + } + + longBuffer += 4; + } + + auto length = numberOfCharacters + numberOfWords - 1; + unsigned char* byteBuffer = ((unsigned char*)bufferPointer) + length; + for (auto i = 0; i < WhiteRabbitUnmanagedBridge::MD5Unmanaged::PhrasesPerSet; i++) + { + *byteBuffer = 128; + byteBuffer += 32; + } + + auto lengthInBits = (unsigned int)(length << 3); + unsigned int* uintBuffer = ((unsigned int*)bufferPointer) + 7; + for (auto i = 0; i < WhiteRabbitUnmanagedBridge::MD5Unmanaged::PhrasesPerSet; i++) + { + *uintBuffer = lengthInBits; + uintBuffer += 8; + } +} diff --git a/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.h b/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.h index 10a11c6..bd4c54d 100644 --- a/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.h +++ b/dotnet/WhiteRabbit.UnmanagedBridge/WhiteRabbit.UnmanagedBridge.h @@ -11,5 +11,6 @@ namespace WhiteRabbitUnmanagedBridge { public: literal int PhrasesPerSet = 16; static void ComputeMD5(unsigned int* input, unsigned int* output); + static void FillPhraseSet(__int64* bufferPointer, __int64* allWordsPointer, __int32* wordIndexes, unsigned __int64* permutationsPointer, int permutationOffset, int numberOfCharacters, int numberOfWords); }; } diff --git a/dotnet/WhiteRabbit/PhraseSet.cs b/dotnet/WhiteRabbit/PhraseSet.cs index 4f4b0af..38c5316 100644 --- a/dotnet/WhiteRabbit/PhraseSet.cs +++ b/dotnet/WhiteRabbit/PhraseSet.cs @@ -21,7 +21,7 @@ { fixed (Word* allWordsPointer = allWords) { - FillPhraseSet(bufferPointer, (long*)allWordsPointer, wordIndexesPointer, permutationsPointer, permutationOffset, numberOfCharacters, wordIndexes.Length); + WhiteRabbitUnmanagedBridge.MD5Unmanaged.FillPhraseSet(bufferPointer, (long*)allWordsPointer, wordIndexesPointer, permutationsPointer, permutationOffset, numberOfCharacters, wordIndexes.Length); } } } @@ -30,47 +30,6 @@ private static unsafe void FillPhraseSet(long* bufferPointer, long* allWordsPointer, int* wordIndexes, ulong* permutationsPointer, int permutationOffset, int numberOfCharacters, int numberOfWords) { - long* longBuffer = (long*)bufferPointer; - - var currentPermutationPointer = permutationsPointer + permutationOffset; - for (var i = 0; i < Constants.PhrasesPerSet; i++, currentPermutationPointer++) - { - var permutation = *currentPermutationPointer; - if (permutation == 0) - { - continue; - } - - var cumulativeWordOffsetX4 = 0; - for (var j = 0; j < numberOfWords; j++) - { - var currentWord = allWordsPointer + wordIndexes[permutation & 15] * 128; - permutation = permutation >> 4; - longBuffer[0] |= currentWord[cumulativeWordOffsetX4 + 0]; - longBuffer[1] |= currentWord[cumulativeWordOffsetX4 + 1]; - longBuffer[2] ^= currentWord[cumulativeWordOffsetX4 + 2]; - longBuffer[3] ^= currentWord[cumulativeWordOffsetX4 + 3]; - cumulativeWordOffsetX4 += unchecked((int)currentWord[127]); - } - - longBuffer += 4; - } - - var length = numberOfCharacters + numberOfWords - 1; - byte* byteBuffer = ((byte*)bufferPointer) + length; - for (var i = 0; i < Constants.PhrasesPerSet; i++) - { - *byteBuffer = 128; - byteBuffer += 32; - } - - var lengthInBits = (uint)(length << 3); - uint* uintBuffer = ((uint*)bufferPointer) + 7; - for (var i = 0; i < Constants.PhrasesPerSet; i++) - { - *uintBuffer = lengthInBits; - uintBuffer += 8; - } } public unsafe byte[] GetBytes(int number)