daviddesmet / NaCl.Core

🔑 NaCl.Core, a cryptography library for .NET
MIT License
71 stars 8 forks source link

Consider using modern .NET API types like Span<T> and ReadOnlySpan<T> #5

Closed daviddesmet closed 5 years ago

daviddesmet commented 5 years ago

Consider using modern .NET API types like Span and ReadOnlySpan.

More information at: All About Span: Exploring a New .NET Mainstay.

NOTE: It will require .NET Core 2.1; there's a System.Memory.dll package that’s compatible with .NET Standard 1.1.

daviddesmet commented 5 years ago

First usage already seen on ba4a2d7 with some performance improvements in Snuffle base class. There’s an accompanying benchmark.

poettlr commented 5 years ago

Any idea if that would work with Unity 2018.2.X? Also, how does it translate to garbage overall?

At the moment (if I remember correctly) each encrypt decrypt would result in bufferSize + Mac garbage ( +- some bytes)

daviddesmet commented 5 years ago

I'm not very familiar with Unity but you can perhaps give it a try and report back your results. 😃 The package name is System.Memory. The latest commit is already using those types, if you feel adventurous, you could give it a spin. Any feedback is appreciated!

Regarding the performance improvements, in my results comparing 0e7a447 vs 87f8ac4 (current), there were noticeable improvements in memory and speed. There's still work to be done in SnufflePoly1305 (base class of the AEAD constructions) since I've been focusing in Snuffle, the base class of ChaCha20 and XChaCha20.

You can find the benchmarks in the repo, please note that I haven't kept both versions (modern and legacy) for cleanness sake. If you want to try and see the benchmarks for yourself, you can grab the benchmark project from the latest commit and test against current and 0e7a447 to see the results.

poettlr commented 5 years ago

Sorry to get back to you so late; System.Memory can be loaded in Unity but results in a FileNotFoundException (Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies). Unfortunately, I'm not able to replace the library. (Unity complains, but I try to keep investigating). For now, I started using a custom-made ArrayPool instead. Nothing to share as of yet. But if you want to use the standardized ArrayPool class from System.Buffers that works flawlessly in Unity (Mono backend and IL2CPP sofar).

daviddesmet commented 5 years ago

No worries, I’ve been toying around with an U2F key...

Regarding System.Runtime.CompilerServices.Unsafe. Could you post your csproj? At first glance it seems your Nuget didn’t install it.

Maybe adding something along those lines manually?

<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath> </Reference>

Assuming the version is correct.

poettlr commented 5 years ago

Unity auto patches the csproj files on each compilation and seems to come with its own version of the library. `

F:/projects/Ironbyte.Unity/Library/PackageCache/com.unity.collections@0.0.9-preview.10/System.Runtime.CompilerServices.Unsafe.dll

` I will post in the unity forums about it. Maybe somebody has an idea.

Edit: And already got it working. The dll has to be included inside the ProjectRoot/Asset folder. Unity will then patch the correct path into all csproj files used inside unity.

daviddesmet commented 5 years ago

I'm glad to know you got it working! I plan to complete the perf improvements in SnufflePoly1305 by the next month and then release the update if everything goes well. Any feedback/contribution in the meantime it's really appreciated.

daviddesmet commented 5 years ago

I've pushed 1.2.0 which uses System.Memory. Please let me know if you face any issue.

poettlr commented 5 years ago

Everything works fine, thank you!