Initial commit.

This commit is contained in:
2017-05-16 13:48:39 +01:00
commit 2639796a3b
10 changed files with 2049 additions and 0 deletions

308
.gitignore vendored Normal file
View File

@@ -0,0 +1,308 @@
Skip to content
This repository
Search
Pull requests
Issues
Gist
@claunia
Sign out
Watch 2,044
Star 50,143
Fork 21,299 github/gitignore
Code Pull requests 139 Projects 0 Pulse Graphs
Branch: master Find file Copy pathgitignore/VisualStudio.gitignore
5ac1b18 on Mar 13
@matt40k matt40k Add ignore for Secondary data files
102 contributors @shiftkey @arcresu @aroben @bbodenmiller @Haacked @niik @AArnott @sayedihashimi @SaschaNaz @sfhardman @jamiehumphries @Zenuka @OsirisTerje @richorama @RehanSaeed @matma @LunicLynx @elerch @cbadke @bdukes @anurse @anderslundsgard @alexanderKhaustov @zman97211 @xnt @filipw and others
RawBlameHistory
289 lines (234 sloc) 4.72 KB
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Typescript v1 declaration files
typings/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
Contact GitHub API Training Shop Blog About
© 2017 GitHub, Inc. Terms Privacy Security Status Help

47
README.md Normal file
View File

@@ -0,0 +1,47 @@
This is a test reimplementing MD5 using pure managed C# code, with uint, System.Numerics.Vectors.Vector<uint> and Mono.Simd.Vector4ui.
System.Numerics.Vector.IsHardwareAccelerated = false.
BenchmarkDotNet=v0.10.6, OS=Mac OS X 10.11
Processor=Intel Core2 Duo CPU P8700 2.53GHz, ProcessorCount=2
Frequency=10000000 Hz, Resolution=100.0000 ns, Timer=UNKNOWN
[Host] : Mono 5.0.0.100 (2017-02/9667aa6 Fri), 32bit
DefaultJob : Mono 5.0.0.100 (2017-02/9667aa6 Fri), 32bit
Method | Mean | Error | StdDev | Relative |
-------------- |----------:|----------:|----------:|---------:|
Md5 | 1.124 ms | 0.0399 ms | 0.1158 ms | 100.00% |
Md5Managed | 1.978 ms | 0.0385 ms | 0.0360 ms | 175.97% |
Md5Vectorized | 67.204 ms | 1.1802 ms | 1.0462 ms | 5979.00% |
Md5Mono | 8.377 ms | 0.1606 ms | 0.1502 ms | 745.28% |
System.Numerics.Vector.IsHardwareAccelerated = true.
BenchmarkDotNet=v0.10.6, OS=Windows 10.0.16193
Processor=AMD Phenom(tm) 9650 Quad-Core Processor, ProcessorCount=4
Frequency=2246249 Hz, Resolution=445.1866 ns, Timer=TSC
[Host] : Clr 4.0.30319.42000, 64bit RyuJIT-v4.7.2505.0
DefaultJob : Clr 4.0.30319.42000, 64bit RyuJIT-v4.7.2505.0
Method | Mean | Error | StdDev | Relative |
-------------- |-----------:|----------:|----------:|---------:|
Md5 | 212.0 us | 0.9392 us | 0.8785 us | 100.00% |
Md5Managed | 734.2 us | 1.9787 us | 1.6523 us | 346.32% |
Md5Vectorized | 2,817.4 us | 3.9387 us | 3.2890 us | 1328.96% |
System.Numerics.Vector.IsHardwareAccelerated = false.
BenchmarkDotNet=v0.10.6, OS=Linux
Processor=Intel Xeon CPU E5-2690 v3 2.60GHz, ProcessorCount=24
Frequency=10000000 Hz, Resolution=100.0000 ns, Timer=UNKNOWN
[Host] : Mono 5.0.0 (Stable 5.0.0.100/9667aa6), 64bit
DefaultJob : Mono 5.0.0 (Stable 5.0.0.100/9667aa6), 64bit
Method | Mean | Error | StdDev | Relative |
-------------- |------------:|-----------:|-----------:|---------:|
Md5 | 272.5 us | 1.733 us | 1.253 us | 100.00% |
Md5Managed | 750.7 us | 8.095 us | 7.572 us | 275.49% |
Md5Vectorized | 20,467.7 us | 181.869 us | 161.222 us | 7511.08% |
Md5Mono | 2,628.7 us | 30.470 us | 27.011 us | 964.66% |

17
TestSIMD.sln Normal file
View File

@@ -0,0 +1,17 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestSIMD", "TestSIMD\TestSIMD.csproj", "{7EE2543D-1FBE-4D2E-9E36-C4B2D26AC875}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7EE2543D-1FBE-4D2E-9E36-C4B2D26AC875}.Debug|x86.ActiveCfg = Debug|x86
{7EE2543D-1FBE-4D2E-9E36-C4B2D26AC875}.Debug|x86.Build.0 = Debug|x86
{7EE2543D-1FBE-4D2E-9E36-C4B2D26AC875}.Release|x86.ActiveCfg = Release|x86
{7EE2543D-1FBE-4D2E-9E36-C4B2D26AC875}.Release|x86.Build.0 = Release|x86
EndGlobalSection
EndGlobal

453
TestSIMD/MD5Managed.cs Normal file
View File

@@ -0,0 +1,453 @@
//
// MD5Managed.cs
//
// Author:
// Natalia Portillo <claunia@claunia.com>
//
// Copyright (c) 2017 © Claunia.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
namespace TestSIMD
{
/// <summary>
/// MD5Managed: A HashAlgorithm implementation that acts as a thin wrapper
/// around a C# translation of the MD5 reference implementation. The C code
/// has been translated as closely as possible so that most of the original
/// structure remains and comparisons between the two are straightforward.
/// </summary>
/// <remarks>
/// Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
///
/// Specification:
/// RFC1321 - The MD5 Message-Digest Algorithm
/// http://www.faqs.org/rfcs/rfc1321.html
///
/// Original license:
/// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
/// rights reserved.
///
/// License to copy and use this software is granted provided that it
/// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
/// Algorithm" in all material mentioning or referencing this software
/// or this function.
///
/// License is also granted to make and use derivative works provided
/// that such works are identified as "derived from the RSA Data
/// Security, Inc. MD5 Message-Digest Algorithm" in all material
/// mentioning or referencing the derived work.
///
/// RSA Data Security, Inc. makes no representations concerning either
/// the merchantability of this software or the suitability of this
/// software for any particular purpose. It is provided "as is"
/// without express or implied warranty of any kind.
///
/// These notices must be retained in any copies of any part of this
/// documentation and/or software.
/// </remarks>
public class MD5Managed : HashAlgorithm
{
// Current context
private readonly MD5_CTX _context = new MD5_CTX();
// Last hash result
private readonly byte[] _digest = new byte[16];
// True if HashCore has been called
private bool _hashCoreCalled;
// True if HashFinal has been called
private bool _hashFinalCalled;
/// <summary>
/// Initializes a new instance.
/// </summary>
public MD5Managed()
{
InitializeVariables();
}
/// <summary>
/// Initializes internal state.
/// </summary>
public override void Initialize()
{
InitializeVariables();
}
/// <summary>
/// Initializes variables.
/// </summary>
private void InitializeVariables()
{
MD5Init(_context);
_hashCoreCalled = false;
_hashFinalCalled = false;
}
/// <summary>
/// Updates the hash code with the data provided.
/// </summary>
/// <param name="array">Data to hash.</param>
/// <param name="ibStart">Start position.</param>
/// <param name="cbSize">Number of bytes.</param>
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
if (null == array)
{
throw new ArgumentNullException("array");
}
if (_hashFinalCalled)
{
throw new CryptographicException("Hash not valid for use in specified state.");
}
_hashCoreCalled = true;
MD5Update(_context, array, (uint)ibStart, (uint)cbSize);
}
/// <summary>
/// Finalizes the hash code and returns it.
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal()
{
_hashFinalCalled = true;
MD5Final(_digest, _context);
return Hash;
}
/// <summary>
/// Returns the hash as an array of bytes.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "Matching .NET behavior by throwing here.")]
[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Justification = "Matching .NET behavior by throwing NullReferenceException.")]
public override byte[] Hash
{
get
{
if (!_hashCoreCalled)
{
throw new NullReferenceException();
}
if (!_hashFinalCalled)
{
// Note: Not CryptographicUnexpectedOperationException because that can't be instantiated on Silverlight 4
throw new CryptographicException("Hash must be finalized before the hash value is retrieved.");
}
return _digest;
}
}
// Return size of hash in bits.
public override int HashSize
{
get
{
return _digest.Length * 8;
}
}
///////////////////////////////////////////////
// MD5 reference implementation begins here. //
///////////////////////////////////////////////
/* MD5 context. */
private class MD5_CTX
{
public readonly uint[] state; /* state (ABCD) */
public readonly uint[] count; /* number of bits, modulo 2^64 (lsb first) */
public readonly byte[] buffer; /* input buffer */
public MD5_CTX()
{
state = new uint[4];
count = new uint[2];
buffer = new byte[64];
}
public void Clear()
{
Array.Clear(state, 0, state.Length);
Array.Clear(count, 0, count.Length);
Array.Clear(buffer, 0, buffer.Length);
}
}
/* Constants for MD5Transform routine. */
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
private static byte[] PADDING;
[SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "More compact this way")]
static MD5Managed()
{
PADDING = new byte[64];
PADDING[0] = 0x80;
}
/* F, G, H and I are basic MD5 functions. */
private static uint F(uint x, uint y, uint z) { return (((x) & (y)) | ((~x) & (z))); }
private static uint G(uint x, uint y, uint z) { return (((x) & (z)) | ((y) & (~z))); }
private static uint H(uint x, uint y, uint z) { return ((x) ^ (y) ^ (z)); }
private static uint I(uint x, uint y, uint z) { return ((y) ^ ((x) | (~z))); }
/* ROTATE_LEFT rotates x left n bits. */
private static uint ROTATE_LEFT(uint x, int n) { return (((x) << (n)) | ((x) >> (32 - (n)))); }
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation. */
private static void FF(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
{
(a) += F((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void GG(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
{
(a) += G((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void HH(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
{
(a) += H((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void II(ref uint a, uint b, uint c, uint d, uint x, int s, uint ac)
{
(a) += I((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
private static void MD5Init(MD5_CTX context) /* context */
{
context.count[0] = context.count[1] = 0;
/* Load magic initialization constants. */
context.state[0] = 0x67452301;
context.state[1] = 0xefcdab89;
context.state[2] = 0x98badcfe;
context.state[3] = 0x10325476;
}
/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context. */
private static void MD5Update(MD5_CTX context, /* context */
byte[] input, /* input block */
uint inputIndex, // Starting index for input block
uint inputLen) /* length of input block */
{
/* Compute number of bytes mod 64 */
uint index = (uint)((context.count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context.count[0] += ((uint)inputLen << 3)) < ((uint)inputLen << 3))
{
context.count[1]++;
}
context.count[1] += ((uint)inputLen >> 29);
uint partLen = 64 - index;
/* Transform as many times as possible. */
uint i = 0;
if (inputLen >= partLen)
{
Buffer.BlockCopy(input, (int)inputIndex, context.buffer, (int)index, (int)partLen);
MD5Transform(context.state, context.buffer, 0);
for (i = partLen; i + 63 < inputLen; i += 64)
{
MD5Transform(context.state, input, inputIndex + i);
}
index = 0;
}
/* Buffer remaining input */
Buffer.BlockCopy(input, (int)(inputIndex + i), context.buffer, (int)index, (int)(inputLen - i));
}
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context. */
private static void MD5Final(byte[] digest, /* message digest */
MD5_CTX context) /* context */
{
byte[] bits = new byte[8];
/* Save number of bits */
Encode(bits, context.count, 8);
/* Pad out to 56 mod 64. */
uint index = (uint)((context.count[0] >> 3) & 0x3f);
uint padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update(context, PADDING, 0, padLen);
/* Append length (before padding) */
MD5Update(context, bits, 0, 8);
/* Store state in digest */
Encode(digest, context.state, 16);
/* Zeroize sensitive information. */
context.Clear();
}
/* MD5 basic transformation. Transforms state based on block. */
private static void MD5Transform(uint[] state,
byte[] block,
uint blockIndex)
{
uint a = state[0], b = state[1], c = state[2], d = state[3];
uint[] x = new uint[16];
Decode(x, block, blockIndex, 64);
/* Round 1 */
FF(ref a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(ref d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(ref c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(ref b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(ref a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(ref d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(ref c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(ref b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(ref a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(ref d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(ref c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(ref b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(ref a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(ref d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(ref c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(ref b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(ref a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(ref d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(ref c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(ref b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(ref a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(ref d, a, b, c, x[10], S22, 0x02441453); /* 22 */
GG(ref c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(ref b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(ref a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(ref d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(ref c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
GG(ref b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(ref a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(ref d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(ref c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(ref b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(ref a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(ref d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(ref c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(ref b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(ref a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(ref d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(ref c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(ref b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(ref a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(ref d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(ref c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(ref b, c, d, a, x[6], S34, 0x04881d05); /* 44 */
HH(ref a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(ref d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(ref c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(ref b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(ref a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(ref d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(ref c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(ref b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(ref a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(ref d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(ref c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(ref b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(ref a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(ref d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(ref c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(ref b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(ref a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(ref d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(ref c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(ref b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information. */
Array.Clear(x, 0, x.Length);
}
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4. */
private static void Encode(byte[] output,
uint[] input,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (byte)(input[i] & 0xff);
output[j + 1] = (byte)((input[i] >> 8) & 0xff);
output[j + 2] = (byte)((input[i] >> 16) & 0xff);
output[j + 3] = (byte)((input[i] >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4. */
private static void Decode(uint[] output,
byte[] input,
uint inputIndex,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[i] = ((uint)input[inputIndex + j]) |
(((uint)input[inputIndex + j + 1]) << 8) |
(((uint)input[inputIndex + j + 2]) << 16) |
(((uint)input[inputIndex + j + 3]) << 24);
}
}
}
}

450
TestSIMD/MonoMD5.cs Normal file
View File

@@ -0,0 +1,450 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using Mono.Simd;
namespace TestSIMD
{
/// <summary>
/// MD5Managed: A HashAlgorithm implementation that acts as a thin wrapper
/// around a C# translation of the MD5 reference implementation. The C code
/// has been translated as closely as possible so that most of the original
/// structure remains and comparisons between the two are straightforward.
/// </summary>
/// <remarks>
/// Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
///
/// Specification:
/// RFC1321 - The MD5 Message-Digest Algorithm
/// http://www.faqs.org/rfcs/rfc1321.html
///
/// Original license:
/// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
/// rights reserved.
///
/// License to copy and use this software is granted provided that it
/// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
/// Algorithm" in all material mentioning or referencing this software
/// or this function.
///
/// License is also granted to make and use derivative works provided
/// that such works are identified as "derived from the RSA Data
/// Security, Inc. MD5 Message-Digest Algorithm" in all material
/// mentioning or referencing the derived work.
///
/// RSA Data Security, Inc. makes no representations concerning either
/// the merchantability of this software or the suitability of this
/// software for any particular purpose. It is provided "as is"
/// without express or implied warranty of any kind.
///
/// These notices must be retained in any copies of any part of this
/// documentation and/or software.
/// </remarks>
public class MonoMD5 : HashAlgorithm
{
// Current context
private readonly MD5_CTX _context = new MD5_CTX();
// Last hash result
private readonly byte[] _digest = new byte[16];
// True if HashCore has been called
private bool _hashCoreCalled;
// True if HashFinal has been called
private bool _hashFinalCalled;
/// <summary>
/// Initializes a new instance.
/// </summary>
public MonoMD5()
{
InitializeVariables();
}
/// <summary>
/// Initializes internal state.
/// </summary>
public override void Initialize()
{
InitializeVariables();
}
/// <summary>
/// Initializes variables.
/// </summary>
private void InitializeVariables()
{
MD5Init(_context);
_hashCoreCalled = false;
_hashFinalCalled = false;
}
/// <summary>
/// Updates the hash code with the data provided.
/// </summary>
/// <param name="array">Data to hash.</param>
/// <param name="ibStart">Start position.</param>
/// <param name="cbSize">Number of bytes.</param>
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
if (null == array)
{
throw new ArgumentNullException("array");
}
if (_hashFinalCalled)
{
throw new CryptographicException("Hash not valid for use in specified state.");
}
_hashCoreCalled = true;
MD5Update(_context, array, (uint)ibStart, (uint)cbSize);
}
/// <summary>
/// Finalizes the hash code and returns it.
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal()
{
_hashFinalCalled = true;
MD5Final(_digest, _context);
return Hash;
}
/// <summary>
/// Returns the hash as an array of bytes.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "Matching .NET behavior by throwing here.")]
[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Justification = "Matching .NET behavior by throwing NullReferenceException.")]
public override byte[] Hash
{
get
{
if (!_hashCoreCalled)
{
throw new NullReferenceException();
}
if (!_hashFinalCalled)
{
// Note: Not CryptographicUnexpectedOperationException because that can't be instantiated on Silverlight 4
throw new CryptographicException("Hash must be finalized before the hash value is retrieved.");
}
return _digest;
}
}
// Return size of hash in bits.
public override int HashSize
{
get
{
return _digest.Length * 8;
}
}
///////////////////////////////////////////////
// MD5 reference implementation begins here. //
///////////////////////////////////////////////
/* MD5 context. */
private class MD5_CTX
{
public readonly Vector4ui[] state; /* state (ABCD) */
public readonly uint[] count; /* number of bits, modulo 2^64 (lsb first) */
public readonly byte[] buffer; /* input buffer */
public MD5_CTX()
{
state = new Vector4ui[4];
count = new uint[2];
buffer = new byte[64];
}
public void Clear()
{
Array.Clear(state, 0, state.Length);
Array.Clear(count, 0, count.Length);
Array.Clear(buffer, 0, buffer.Length);
}
}
/* Constants for MD5Transform routine. */
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
private static byte[] PADDING;
[SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "More compact this way")]
static MonoMD5()
{
PADDING = new byte[64];
PADDING[0] = 0x80;
}
/* F, G, H and I are basic MD5 functions. */
private static Vector4ui F(Vector4ui x, Vector4ui y, Vector4ui z) { return z ^ (x & (y ^ z)); }
private static Vector4ui G(Vector4ui x, Vector4ui y, Vector4ui z) { return y ^ (z & (x ^ y)); }
private static Vector4ui H(Vector4ui x, Vector4ui y, Vector4ui z) { return ((x) ^ (y) ^ (z)); }
private static Vector4ui I(Vector4ui x, Vector4ui y, Vector4ui z) { return ((y) ^ ((x) | (new Vector4ui(~z.X)))); }
/* ROTATE_LEFT rotates x left n bits. */
private static Vector4ui ROTATE_LEFT(Vector4ui x, int n) { return new Vector4ui(((x[0]) << (n)) | ((x[0]) >> (32 - (n)))); }
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation. */
private static void FF(ref Vector4ui a, Vector4ui b, Vector4ui c, Vector4ui d, Vector4ui x, int s, uint ac)
{
(a) += F((b), (c), (d)) + (x) + new Vector4ui(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void GG(ref Vector4ui a, Vector4ui b, Vector4ui c, Vector4ui d, Vector4ui x, int s, uint ac)
{
(a) += G((b), (c), (d)) + (x) + new Vector4ui(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void HH(ref Vector4ui a, Vector4ui b, Vector4ui c, Vector4ui d, Vector4ui x, int s, uint ac)
{
(a) += H((b), (c), (d)) + (x) + new Vector4ui(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void II(ref Vector4ui a, Vector4ui b, Vector4ui c, Vector4ui d, Vector4ui x, int s, uint ac)
{
(a) += I((b), (c), (d)) + (x) + new Vector4ui(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
const uint H0 = 0x67452301;
const uint H1 = 0xefcdab89;
const uint H2 = 0x98badcfe;
const uint H3 = 0x10325476;
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
private static void MD5Init(MD5_CTX context) /* context */
{
context.count[0] = context.count[1] = 0;
/* Load magic initialization constants. */
context.state[0] = new Vector4ui(H0);
context.state[1] = new Vector4ui(H1);
context.state[2] = new Vector4ui(H2);
context.state[3] = new Vector4ui(H3);
}
/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context. */
private static void MD5Update(MD5_CTX context, /* context */
byte[] input, /* input block */
uint inputIndex, // Starting index for input block
uint inputLen) /* length of input block */
{
/* Compute number of bytes mod 64 */
uint index = (uint)((context.count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context.count[0] += ((uint)inputLen << 3)) < ((uint)inputLen << 3))
{
context.count[1]++;
}
context.count[1] += ((uint)inputLen >> 29);
uint partLen = 64 - index;
/* Transform as many times as possible. */
uint i = 0;
if (inputLen >= partLen)
{
Buffer.BlockCopy(input, (int)inputIndex, context.buffer, (int)index, (int)partLen);
MD5Transform(context.state, context.buffer, 0);
for (i = partLen; i + 63 < inputLen; i += 64)
{
MD5Transform(context.state, input, inputIndex + i);
}
index = 0;
}
/* Buffer remaining input */
Buffer.BlockCopy(input, (int)(inputIndex + i), context.buffer, (int)index, (int)(inputLen - i));
}
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context. */
private static void MD5Final(byte[] digest, /* message digest */
MD5_CTX context) /* context */
{
byte[] bits = new byte[8];
/* Save number of bits */
Encode(bits, context.count, 8);
/* Pad out to 56 mod 64. */
uint index = (uint)((context.count[0] >> 3) & 0x3f);
uint padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update(context, PADDING, 0, padLen);
/* Append length (before padding) */
MD5Update(context, bits, 0, 8);
/* Store state in digest */
Encode(digest, context.state, 16);
/* Zeroize sensitive information. */
context.Clear();
}
/* MD5 basic transformation. Transforms state based on block. */
private static void MD5Transform(Vector4ui[] state,
byte[] block,
uint blockIndex)
{
Vector4ui a = state[0], b = state[1], c = state[2], d = state[3];
Vector4ui[] x = new Vector4ui[16];
Decode(x, block, blockIndex, 64);
/* Round 1 */
FF(ref a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(ref d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(ref c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(ref b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(ref a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(ref d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(ref c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(ref b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(ref a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(ref d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(ref c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(ref b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(ref a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(ref d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(ref c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(ref b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(ref a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(ref d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(ref c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(ref b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(ref a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(ref d, a, b, c, x[10], S22, 0x02441453); /* 22 */
GG(ref c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(ref b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(ref a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(ref d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(ref c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
GG(ref b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(ref a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(ref d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(ref c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(ref b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(ref a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(ref d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(ref c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(ref b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(ref a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(ref d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(ref c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(ref b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(ref a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(ref d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(ref c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(ref b, c, d, a, x[6], S34, 0x04881d05); /* 44 */
HH(ref a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(ref d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(ref c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(ref b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(ref a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(ref d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(ref c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(ref b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(ref a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(ref d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(ref c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(ref b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(ref a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(ref d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(ref c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(ref b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(ref a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(ref d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(ref c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(ref b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information. */
Array.Clear(x, 0, x.Length);
}
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4. */
private static void Encode(byte[] output,
uint[] input,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (byte)(input[i] & 0xff);
output[j + 1] = (byte)((input[i] >> 8) & 0xff);
output[j + 2] = (byte)((input[i] >> 16) & 0xff);
output[j + 3] = (byte)((input[i] >> 24) & 0xff);
}
}
private static void Encode(byte[] output,
Vector4ui[] input,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (byte)(input[i][0] & 0xff);
output[j + 1] = (byte)((input[i][0] >> 8) & 0xff);
output[j + 2] = (byte)((input[i][0] >> 16) & 0xff);
output[j + 3] = (byte)((input[i][0] >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4. */
private static void Decode(Vector4ui[] output,
byte[] input,
uint inputIndex,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[i] = new Vector4ui(((uint)input[inputIndex + j]) |
(((uint)input[inputIndex + j + 1]) << 8) |
(((uint)input[inputIndex + j + 2]) << 16) |
(((uint)input[inputIndex + j + 3]) << 24));
}
}
}
}

81
TestSIMD/Program.cs Normal file
View File

@@ -0,0 +1,81 @@
//
// Program.cs
//
// Author:
// Natalia Portillo <claunia@claunia.com>
//
// Copyright (c) 2017 © Claunia.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Numerics;
namespace TestSIMD
{
public class MD5Benchs
{
private const int N = 65536;
private readonly byte[] data;
private readonly MD5 md5 = MD5.Create();
private readonly MD5Managed md5m = new MD5Managed();
private readonly VectorMD5 md5v =new VectorMD5();
private readonly MonoMD5 md5s = new MonoMD5();
public MD5Benchs()
{
data = new byte[N];
new Random(42).NextBytes(data);
}
[Benchmark]
public byte[] Md5() => md5.ComputeHash(data);
[Benchmark]
public byte[] Md5Managed() => md5m.ComputeHash(data);
[Benchmark]
public byte[] Md5Vectorized() => md5v.ComputeHash(data);
[Benchmark]
public byte[] Md5Mono() => md5s.ComputeHash(data);
}
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Vector.IsHardwareAccelerated = {0}", Vector.IsHardwareAccelerated);
MD5Benchs foo = new MD5Benchs();
byte[] std = foo.Md5();
byte[] mgd = foo.Md5Managed();
byte[] vec = foo.Md5Vectorized();
byte[] mno = foo.Md5Mono();
Console.WriteLine("std: {0:x2}{1:x2}{2:x2}{3:x2}", std[0], std[1], std[2], std[3]);
Console.WriteLine("mgd: {0:x2}{1:x2}{2:x2}{3:x2}", mgd[0], mgd[1], mgd[2], mgd[3]);
Console.WriteLine("vec: {0:x2}{1:x2}{2:x2}{3:x2}", vec[0], vec[1], vec[2], vec[3]);
Console.WriteLine("mno: {0:x2}{1:x2}{2:x2}{3:x2}", mno[0], mno[1], mno[2], mno[3]);
var summary = BenchmarkRunner.Run<MD5Benchs>();
}
}
}

View File

@@ -0,0 +1,48 @@
//
// AssemblyInfo.cs
//
// Author:
// Natalia Portillo <claunia@claunia.com>
//
// Copyright (c) 2017 © Claunia.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("TestSIMD")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Claunia.com")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("© Claunia.com")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

142
TestSIMD/TestSIMD.csproj Normal file
View File

@@ -0,0 +1,142 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{7EE2543D-1FBE-4D2E-9E36-C4B2D26AC875}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>TestSIMD</RootNamespace>
<AssemblyName>TestSIMD</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Microsoft.DotNet.InternalAbstractions">
<HintPath>..\packages\Microsoft.DotNet.InternalAbstractions.1.0.0\lib\net451\Microsoft.DotNet.InternalAbstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DotNet.PlatformAbstractions">
<HintPath>..\packages\Microsoft.DotNet.PlatformAbstractions.1.1.1\lib\net451\Microsoft.DotNet.PlatformAbstractions.dll</HintPath>
</Reference>
<Reference Include="System.AppContext">
<HintPath>..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="System.Core" />
<Reference Include="System.Collections.Immutable">
<HintPath>..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Console">
<HintPath>..\packages\System.Console.4.3.0\lib\net46\System.Console.dll</HintPath>
</Reference>
<Reference Include="System.Diagnostics.FileVersionInfo">
<HintPath>..\packages\System.Diagnostics.FileVersionInfo.4.3.0\lib\net46\System.Diagnostics.FileVersionInfo.dll</HintPath>
</Reference>
<Reference Include="System.Diagnostics.StackTrace">
<HintPath>..\packages\System.Diagnostics.StackTrace.4.3.0\lib\net46\System.Diagnostics.StackTrace.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression">
<HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
</Reference>
<Reference Include="System.IO.FileSystem.Primitives">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.IO.FileSystem">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
</Reference>
<Reference Include="System.Reflection.Metadata">
<HintPath>..\packages\System.Reflection.Metadata.1.4.2\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Security.Cryptography.Encoding">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encoding.CodePages">
<HintPath>..\packages\System.Text.Encoding.CodePages.4.3.0\lib\net46\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Thread">
<HintPath>..\packages\System.Threading.Thread.4.3.0\lib\net46\System.Threading.Thread.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple">
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="BenchmarkDotNet.Core">
<HintPath>..\packages\BenchmarkDotNet.Core.0.10.6\lib\net46\BenchmarkDotNet.Core.dll</HintPath>
</Reference>
<Reference Include="System.Xml.ReaderWriter">
<HintPath>..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml.XmlDocument">
<HintPath>..\packages\System.Xml.XmlDocument.4.3.0\lib\net46\System.Xml.XmlDocument.dll</HintPath>
</Reference>
<Reference Include="System.Xml.XPath">
<HintPath>..\packages\System.Xml.XPath.4.3.0\lib\net46\System.Xml.XPath.dll</HintPath>
</Reference>
<Reference Include="System.Xml.XPath.XDocument">
<HintPath>..\packages\System.Xml.XPath.XDocument.4.3.0\lib\net46\System.Xml.XPath.XDocument.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis">
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.2.0.0\lib\netstandard1.3\Microsoft.CodeAnalysis.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis.CSharp">
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.2.0.0\lib\netstandard1.3\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
</Reference>
<Reference Include="BenchmarkDotNet.Toolchains.Roslyn">
<HintPath>..\packages\BenchmarkDotNet.Toolchains.Roslyn.0.10.6\lib\net46\BenchmarkDotNet.Toolchains.Roslyn.dll</HintPath>
</Reference>
<Reference Include="BenchmarkDotNet">
<HintPath>..\packages\BenchmarkDotNet.0.10.6\lib\net46\BenchmarkDotNet.dll</HintPath>
</Reference>
<Reference Include="System.Numerics.Vectors">
<HintPath>..\packages\System.Numerics.Vectors.4.3.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="Mono.Simd" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VectorMD5.cs" />
<Compile Include="MD5Managed.cs" />
<Compile Include="MonoMD5.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="..\README.md">
<Link>README.md</Link>
</None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

450
TestSIMD/VectorMD5.cs Normal file
View File

@@ -0,0 +1,450 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Numerics;
namespace TestSIMD
{
/// <summary>
/// MD5Managed: A HashAlgorithm implementation that acts as a thin wrapper
/// around a C# translation of the MD5 reference implementation. The C code
/// has been translated as closely as possible so that most of the original
/// structure remains and comparisons between the two are straightforward.
/// </summary>
/// <remarks>
/// Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
///
/// Specification:
/// RFC1321 - The MD5 Message-Digest Algorithm
/// http://www.faqs.org/rfcs/rfc1321.html
///
/// Original license:
/// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
/// rights reserved.
///
/// License to copy and use this software is granted provided that it
/// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
/// Algorithm" in all material mentioning or referencing this software
/// or this function.
///
/// License is also granted to make and use derivative works provided
/// that such works are identified as "derived from the RSA Data
/// Security, Inc. MD5 Message-Digest Algorithm" in all material
/// mentioning or referencing the derived work.
///
/// RSA Data Security, Inc. makes no representations concerning either
/// the merchantability of this software or the suitability of this
/// software for any particular purpose. It is provided "as is"
/// without express or implied warranty of any kind.
///
/// These notices must be retained in any copies of any part of this
/// documentation and/or software.
/// </remarks>
public class VectorMD5 : HashAlgorithm
{
// Current context
private readonly MD5_CTX _context = new MD5_CTX();
// Last hash result
private readonly byte[] _digest = new byte[16];
// True if HashCore has been called
private bool _hashCoreCalled;
// True if HashFinal has been called
private bool _hashFinalCalled;
/// <summary>
/// Initializes a new instance.
/// </summary>
public VectorMD5()
{
InitializeVariables();
}
/// <summary>
/// Initializes internal state.
/// </summary>
public override void Initialize()
{
InitializeVariables();
}
/// <summary>
/// Initializes variables.
/// </summary>
private void InitializeVariables()
{
MD5Init(_context);
_hashCoreCalled = false;
_hashFinalCalled = false;
}
/// <summary>
/// Updates the hash code with the data provided.
/// </summary>
/// <param name="array">Data to hash.</param>
/// <param name="ibStart">Start position.</param>
/// <param name="cbSize">Number of bytes.</param>
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
if (null == array)
{
throw new ArgumentNullException("array");
}
if (_hashFinalCalled)
{
throw new CryptographicException("Hash not valid for use in specified state.");
}
_hashCoreCalled = true;
MD5Update(_context, array, (uint)ibStart, (uint)cbSize);
}
/// <summary>
/// Finalizes the hash code and returns it.
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal()
{
_hashFinalCalled = true;
MD5Final(_digest, _context);
return Hash;
}
/// <summary>
/// Returns the hash as an array of bytes.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "Matching .NET behavior by throwing here.")]
[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Justification = "Matching .NET behavior by throwing NullReferenceException.")]
public override byte[] Hash
{
get
{
if (!_hashCoreCalled)
{
throw new NullReferenceException();
}
if (!_hashFinalCalled)
{
// Note: Not CryptographicUnexpectedOperationException because that can't be instantiated on Silverlight 4
throw new CryptographicException("Hash must be finalized before the hash value is retrieved.");
}
return _digest;
}
}
// Return size of hash in bits.
public override int HashSize
{
get
{
return _digest.Length * 8;
}
}
///////////////////////////////////////////////
// MD5 reference implementation begins here. //
///////////////////////////////////////////////
/* MD5 context. */
private class MD5_CTX
{
public readonly Vector<uint>[] state; /* state (ABCD) */
public readonly uint[] count; /* number of bits, modulo 2^64 (lsb first) */
public readonly byte[] buffer; /* input buffer */
public MD5_CTX()
{
state = new Vector<uint>[4];
count = new uint[2];
buffer = new byte[64];
}
public void Clear()
{
Array.Clear(state, 0, state.Length);
Array.Clear(count, 0, count.Length);
Array.Clear(buffer, 0, buffer.Length);
}
}
/* Constants for MD5Transform routine. */
private const int S11 = 7;
private const int S12 = 12;
private const int S13 = 17;
private const int S14 = 22;
private const int S21 = 5;
private const int S22 = 9;
private const int S23 = 14;
private const int S24 = 20;
private const int S31 = 4;
private const int S32 = 11;
private const int S33 = 16;
private const int S34 = 23;
private const int S41 = 6;
private const int S42 = 10;
private const int S43 = 15;
private const int S44 = 21;
private static byte[] PADDING;
[SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "More compact this way")]
static VectorMD5()
{
PADDING = new byte[64];
PADDING[0] = 0x80;
}
/* F, G, H and I are basic MD5 functions. */
private static Vector<uint> F(Vector<uint> x, Vector<uint> y, Vector<uint> z) { return z ^ (x & (y ^ z)); }
private static Vector<uint> G(Vector<uint> x, Vector<uint> y, Vector<uint> z) { return y ^ (z & (x ^ y)); }
private static Vector<uint> H(Vector<uint> x, Vector<uint> y, Vector<uint> z) { return ((x) ^ (y) ^ (z)); }
private static Vector<uint> I(Vector<uint> x, Vector<uint> y, Vector<uint> z) { return ((y) ^ ((x) | (~z))); }
/* ROTATE_LEFT rotates x left n bits. */
private static Vector<uint> ROTATE_LEFT(Vector<uint> x, int n) { return new Vector<uint>(((x[0]) << (n)) | ((x[0]) >> (32 - (n)))); }
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation. */
private static void FF(ref Vector<uint> a, Vector<uint> b, Vector<uint> c, Vector<uint> d, Vector<uint> x, int s, uint ac)
{
(a) += F((b), (c), (d)) + (x) + new Vector<uint>(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void GG(ref Vector<uint> a, Vector<uint> b, Vector<uint> c, Vector<uint> d, Vector<uint> x, int s, uint ac)
{
(a) += G((b), (c), (d)) + (x) + new Vector<uint>(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void HH(ref Vector<uint> a, Vector<uint> b, Vector<uint> c, Vector<uint> d, Vector<uint> x, int s, uint ac)
{
(a) += H((b), (c), (d)) + (x) + new Vector<uint>(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
private static void II(ref Vector<uint> a, Vector<uint> b, Vector<uint> c, Vector<uint> d, Vector<uint> x, int s, uint ac)
{
(a) += I((b), (c), (d)) + (x) + new Vector<uint>(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
const uint H0 = 0x67452301;
const uint H1 = 0xefcdab89;
const uint H2 = 0x98badcfe;
const uint H3 = 0x10325476;
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
private static void MD5Init(MD5_CTX context) /* context */
{
context.count[0] = context.count[1] = 0;
/* Load magic initialization constants. */
context.state[0] = new Vector<uint>(H0);
context.state[1] = new Vector<uint>(H1);
context.state[2] = new Vector<uint>(H2);
context.state[3] = new Vector<uint>(H3);
}
/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context. */
private static void MD5Update(MD5_CTX context, /* context */
byte[] input, /* input block */
uint inputIndex, // Starting index for input block
uint inputLen) /* length of input block */
{
/* Compute number of bytes mod 64 */
uint index = (uint)((context.count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((context.count[0] += ((uint)inputLen << 3)) < ((uint)inputLen << 3))
{
context.count[1]++;
}
context.count[1] += ((uint)inputLen >> 29);
uint partLen = 64 - index;
/* Transform as many times as possible. */
uint i = 0;
if (inputLen >= partLen)
{
Buffer.BlockCopy(input, (int)inputIndex, context.buffer, (int)index, (int)partLen);
MD5Transform(context.state, context.buffer, 0);
for (i = partLen; i + 63 < inputLen; i += 64)
{
MD5Transform(context.state, input, inputIndex + i);
}
index = 0;
}
/* Buffer remaining input */
Buffer.BlockCopy(input, (int)(inputIndex + i), context.buffer, (int)index, (int)(inputLen - i));
}
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context. */
private static void MD5Final(byte[] digest, /* message digest */
MD5_CTX context) /* context */
{
byte[] bits = new byte[8];
/* Save number of bits */
Encode(bits, context.count, 8);
/* Pad out to 56 mod 64. */
uint index = (uint)((context.count[0] >> 3) & 0x3f);
uint padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update(context, PADDING, 0, padLen);
/* Append length (before padding) */
MD5Update(context, bits, 0, 8);
/* Store state in digest */
Encode(digest, context.state, 16);
/* Zeroize sensitive information. */
context.Clear();
}
/* MD5 basic transformation. Transforms state based on block. */
private static void MD5Transform(Vector<uint>[] state,
byte[] block,
uint blockIndex)
{
Vector<uint> a = state[0], b = state[1], c = state[2], d = state[3];
Vector<uint>[] x = new Vector<uint>[16];
Decode(x, block, blockIndex, 64);
/* Round 1 */
FF(ref a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
FF(ref d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
FF(ref c, d, a, b, x[2], S13, 0x242070db); /* 3 */
FF(ref b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
FF(ref a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
FF(ref d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
FF(ref c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
FF(ref b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
FF(ref a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
FF(ref d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
FF(ref c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(ref b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(ref a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(ref d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(ref c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(ref b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(ref a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
GG(ref d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
GG(ref c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(ref b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
GG(ref a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
GG(ref d, a, b, c, x[10], S22, 0x02441453); /* 22 */
GG(ref c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(ref b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
GG(ref a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
GG(ref d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(ref c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
GG(ref b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
GG(ref a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(ref d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
GG(ref c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
GG(ref b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(ref a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
HH(ref d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
HH(ref c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(ref b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(ref a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
HH(ref d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
HH(ref c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
HH(ref b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(ref a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(ref d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
HH(ref c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
HH(ref b, c, d, a, x[6], S34, 0x04881d05); /* 44 */
HH(ref a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
HH(ref d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(ref c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(ref b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(ref a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
II(ref d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
II(ref c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(ref b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
II(ref a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(ref d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
II(ref c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(ref b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
II(ref a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
II(ref d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(ref c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
II(ref b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(ref a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
II(ref d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(ref c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
II(ref b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information. */
Array.Clear(x, 0, x.Length);
}
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4. */
private static void Encode(byte[] output,
uint[] input,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (byte)(input[i] & 0xff);
output[j + 1] = (byte)((input[i] >> 8) & 0xff);
output[j + 2] = (byte)((input[i] >> 16) & 0xff);
output[j + 3] = (byte)((input[i] >> 24) & 0xff);
}
}
private static void Encode(byte[] output,
Vector<uint>[] input,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[j] = (byte)(input[i][0] & 0xff);
output[j + 1] = (byte)((input[i][0] >> 8) & 0xff);
output[j + 2] = (byte)((input[i][0] >> 16) & 0xff);
output[j + 3] = (byte)((input[i][0] >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4. */
private static void Decode(Vector<uint>[] output,
byte[] input,
uint inputIndex,
uint len)
{
for (uint i = 0, j = 0; j < len; i++, j += 4)
{
output[i] = new Vector<uint>(((uint)input[inputIndex + j]) |
(((uint)input[inputIndex + j + 1]) << 8) |
(((uint)input[inputIndex + j + 2]) << 16) |
(((uint)input[inputIndex + j + 3]) << 24));
}
}
}
}

53
TestSIMD/packages.config Normal file
View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BenchmarkDotNet" version="0.10.6" targetFramework="net461" />
<package id="BenchmarkDotNet.Core" version="0.10.6" targetFramework="net461" />
<package id="BenchmarkDotNet.Toolchains.Roslyn" version="0.10.6" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Common" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.CSharp" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.DotNet.InternalAbstractions" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.DotNet.PlatformAbstractions" version="1.1.1" targetFramework="net461" />
<package id="System.AppContext" version="4.3.0" targetFramework="net461" />
<package id="System.Collections" version="4.3.0" targetFramework="net461" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net461" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net461" />
<package id="System.Console" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net461" />
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net461" />
<package id="System.Globalization" version="4.3.0" targetFramework="net461" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Linq" version="4.3.0" targetFramework="net461" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net461" />
<package id="System.Numerics.Vectors" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection.Metadata" version="1.4.2" targetFramework="net461" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding.CodePages" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Threading" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XPath" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net461" />
</packages>