Little fun with WASM

So to take my mind off of things and just trying a little something on the side to get me to become productive again I decided to look into a thing called AssemblyScript.

The reason for needing WASM was that my personal site for the family has an implementation for SRP. I think years back I already wanted to implement this but JavaScript in the browser did not have a good way to support large numbers. Then came BigInt, and with it the opportunity to re-implement this protocol. Still there were some problems with BigInt, mostly it did not support all the necessary mathematical operations I needed. Enter this small library called bn.js. It supported my need and when I implemented it, together with the Python code in the backend it all worked.

Sluggish

It worked but still took around 200ms to 400ms to do everything in the browser. That is okay for my personal family site as logging in will not happen very often. Still recently I wondered, could I easily port the relevant code from bn.js over to WebAssembly into a WASM and utilize that to do the mathematical steps and see if it increases the speed of execution.

Transpiling JS to WebAssembly

So transpiling JS directly to WebAssembly does not seem to exist, I would have to rewrite/port the code myself to something and then compile to WebAssembly. The problem with porting code is there is a potential loss of information as things might not work the same or you need a different approach to the same problem and potentially introduce a bug.

Keep this in mind, this will come back later

Still looking around for a suitable candidate to port the code to I came across AssemblyScript. It consists of a very small set of features and is akin to TypeScript. This is something I already know and understand so I gave it a shot.

Pure unbridled joy

It was very fun working in AssemblyScript and compiling it and learning it and just moving through. I think I had a working baseline in a couple of hours. It was very majestic experience. More importantly I had a working WASM I could load into the browser to interact with after this time as well.

Cleaning up

There still needs to be some cleanup and removal of some code and then I will introduce the code I have written so far into a GitHub repo for the world to see.

Bugs, bugs and some more bugs

Because I have to do the implementation in AssemblyScript giving types to numbers, there exists no BigInt in AssemblyScript, I have access to signed and unsigned integers and floats and the size of them. I used some wrong number types here and there and that caused some implementation bugs which further caused inconsistencies between the WASM and the original JS method. So I will further refine the uses and have already considerable improved it by reducing the number of random bits gotten that represented the power it needed to be raised by.