-
Notifications
You must be signed in to change notification settings - Fork 849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BouncyCastle EC math is slow in Blazor WebAssembly #963
Comments
I am surprised it even works. WASM does not support |
I can do a special build of NBitcoin for WASM environment, but you will need to guide me here about what should be done for browser environment. |
I built it with following compilation symbols set:
This way, I managed to build it for target 'netstandard2.0'. Blazor WASM has no support for System.Security.Cryptography, that's why I toggled "USEBC" (and the other NO_NATIVE...'s). Or is there perhaps a more efficient implementation that is not using System.Security.Cryptography? (in that case I must have missed it) I don't recall exactly if I was able to use Span, I can investigate this later. |
Can you try to toggle |
Also check if you can use using (var sha = new SHA256Managed())
{
return sha.ComputeHash(data, offset, count);
} So we can find what is supported or not. Same with |
I checked the code, toggling |
I will try. Should this speed up the bouncy castle EC math? I can try the SHA256Managed, but this is probably not the slowest part. |
If you set |
I tried it again, now targetting 'netstandard2.1'. I had to add these 2 compilation symbols, because System.Security.Cryptography is not supported in wasm:
However, the project won't compile like this because the BouncyCastle source files get removed from the project when targetting 'netstandard2.1', while BouncyCastle is the fallback for the native HMACSHA512 that is now missing. I can restore the files by editing the csproj file to switch Furthermore, I also had to remove the compilation symbol There are a few more issues that I can fix, but the result is that key derivation still runs slow in the browser. |
They are not supported. |
@NicolasDorier please review my pull request: As I wrote earlier, managed SHA256 and SHA512 are not supported in Blazor wasm. After applying the changes from my patch/pull-request, you should be able to build the project for
Unlike my first attempt at compiling it for Blazor, I am not forcing the use of Bouncy Castle with the Now the sad news: in Blazor wasm it takes my system 300-500ms to derive an ExtPubKey from a parent key, which is slow. |
@JVanloofsvelt you did not open the PR on the right repo, you did on your fork. I will check what I can do about the Your PR seems good. |
I think rather than
Have you tried to set |
@JVanloofsvelt I am wondering if instead of relying on BC, we could not just import After all, this is open source. |
Looking at the source code of dotnet I found implementation: Which is very strange, as the name of the file seems to indicate it is supported by browsers oO But this say otherwise : https://docs.microsoft.com/en-us/dotnet/core/compatibility/cryptography/5.0/cryptography-apis-not-supported-on-blazor-webassembly |
@NicolasDorier I opened the pull request on your repo this time, sorry for that.
|
Checking on here if we can't just copy/paste dotnet managed code rather than depending on BC I don't know if hashing is the reason why it is slow though. |
@NicolasDorier I tried it once more, with the same configuration as I described here, but now using Microsoft Edge. The key derivation is taking only 10s of milliseconds (as opposed to 100s). My dotnet version is 5.0.102 by the way. Edit: in Firefox it's running equally fast |
If we could port SHA256Managed then we could exclude BC again. |
I don't know if that's worth it, as I don't know if that's the real bottleneck. |
@JVanloofsvelt can you reply to dotnet/runtime#40074 they say hash functions are supported on .net5.0 |
@NicolasDorier Hi Nicolas, I verified it, and indeed hash functions seem to be supported. The problem with NBitcoin is that for the HMACs it either uses a native HMAC with a native hash function, or it uses BC HMAC with BC hash function. I modified the code a little so that it can use BC HMAC with a native hash function. My changes allow me to switch on compilation symbol "NO_BC" again. Furthermore, I can drop "NONATIVEHASH" which I had switched on earlier. This is a win. I will create another pull request. |
Pull request: #965 Use following compilation symbols to get a build that works in Blazor wasm: The gist being that you need to add |
@JVanloofsvelt have you also check if the perfs were better? |
It takes about a second to derive the next key from an ExtendedKey. Perhaps this library can make use of JSInterop for the Webcrypto APIs when the platform is 'browser'.
The text was updated successfully, but these errors were encountered: