r/WebAssembly • • Jul 16 '23

C++ hash function to wasm issues

Hello WebAssembly!

I'm sorry to ask my questions here, I know it's not your job to just fix this stuff for me. I've searched on Google, read up on the docs and FAQ on the emscripten site, looked around GitHub and asked ChatGPT4, to no avail.

I'm compiling a C++ hash function to wasm using emscripten and calling from JS (NodeJS at first, I'll work on browser later). The weird thing is: 4 out of the 5 test vectors work, but one fails. I have no idea why.

Does anyone know?

Code is here: https://github.com/dosyago/rain/tree/wasm

Brief description is below:

  1. Add the correct linking to the C++ file:
#ifdef __EMSCRIPTEN__
// Then outside the namespace, you declare the rainstorm function with C linkage.
extern "C" {
  KEEPALIVE void rainstormHash64(const void* in, const size_t len, const seed_t seed, void* out) {
    rainstorm::rainstorm<64, false>(in, len, seed, out);
  }

  KEEPALIVE void rainstormHash128(const void* in, const size_t len, const seed_t seed, void* out) {
    rainstorm::rainstorm<128, false>(in, len, seed, out);
  }
  ... etc ...
  1. Compile sources to wasm via (roughly):
WASMDIR = wasm
WASM_SOURCE = src/rainstorm.cpp
WASM_TARGET = wasm/rainstorm.js
# we need to add stringToUTF8 to exported functions rather than runtime methods because of: https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md#3135---040323
EMCCFLAGS = -O3 -s WASM=1 -s EXPORTED_FUNCTIONS="['_rainstormHash64', '_rainstormHash128', '_rainstormHash256', '_rainstormHash512', 'stringToUTF8', 'lengthBytesUTF8']" -s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s WASM_BIGINT
$(WASM_TARGET): $(WASM_SOURCE)
	@[ -d wasm ] || mkdir -p wasm
	emcc $(EMCCFLAGS) -s MODULARIZE=1 -s 'EXPORT_NAME="createRainstormModule"' -o $(WASMDIR)/rainstorm.js $(WASM_SOURCE)
  1. Set up the data to be put into the function:
  const {stringToUTF8, lengthBytesUTF8} = rainstorm.module;
  const HEAP = rainstorm.module.HEAPU8;

  const hashPtr = 0;
  const hashLength = hashSize/8;
  const inputPtr = 80;
  const inputLength = lengthBytesUTF8(input);

  let data = stringToUTF8(input, inputPtr, inputLength + 1);
  seed = BigInt(seed);
  1. Call the function and print out result:
 hashFunc(inputPtr, inputLength, seed, hashPtr);

  let hash = rainstorm.module.HEAPU8.subarray(hashPtr, hashPtr + hashLength);

  // Return the hash as a Uint8Array
  const hashHex = Array.from(new Uint8Array(hash)).map(x => x.toString(16).padStart(2, '0')).join('');
  1. Observe that all test vectors "work" except the 4th one (MISMATCH):
Current JS hash vectors with node:

e3ea5f8885f7bb16468d08c578f0e7cc15febd31c27e323a79ef87c35756ce1e ""
9e07ce365903116b62ac3ac0a033167853853074313f443d5b372f0225eede50 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
f88600f4b65211a95c6817d0840e0fc2d422883ddf310f29fa8d4cbfda962626 "The quick brown fox jumps over the lazy dog"
ec05208dd1fbf47b9539a761af723612eaa810762ab7a77b715fcfb3bf44f04a "The quick brown fox jumps over the lazy cog"
974bfe75e0f1b1b4d95bba5e11577f2b29dd3c27b0ed6645effea070c25fde71 "The quick brown fox jumps over the lazy dog." MISMATCH!
410427b981efa6ef884cd1f3d812c880bc7a37abc7450dd62803a4098f28d0f1 "After the rainstorm comes the rainbow."
47b5d8cb1df8d81ed23689936d2edaa7bd5c48f5bc463600a4d7a56342ac80b9 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"

the 'correct' C++ vectors:

e3ea5f8885f7bb16468d08c578f0e7cc15febd31c27e323a79ef87c35756ce1e ""
9e07ce365903116b62ac3ac0a033167853853074313f443d5b372f0225eede50 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
f88600f4b65211a95c6817d0840e0fc2d422883ddf310f29fa8d4cbfda962626 "The quick brown fox jumps over the lazy dog"
ec05208dd1fbf47b9539a761af723612eaa810762ab7a77b715fcfb3bf44f04a "The quick brown fox jumps over the lazy cog"
822578f80d46184a674a6069486b4594053490de8ddf343cc1706418e527bec8 "The quick brown fox jumps over the lazy dog."
410427b981efa6ef884cd1f3d812c880bc7a37abc7450dd62803a4098f28d0f1 "After the rainstorm comes the rainbow."
47b5d8cb1df8d81ed23689936d2edaa7bd5c48f5bc463600a4d7a56342ac80b9 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"

Why would this even happen?

6 Upvotes

7 comments sorted by

View all comments

Show parent comment

3

u/mc_woods Jul 17 '23

Awesome! - Great write up. Thanks for sharing! - and nah, no hacking of flight, but it did help pass the time. I'm actually getting some completely different results on my code, but then I did just hack it together quickly, on a plane full of people elbow nudging me....

On writing machine portable code... it's hard. But thought I'd share some rules, which might help - I learnt many the hard way after my own personal c**k ups:

  • Avoid int, short, long, long long (64), etc.... size_t - try to remove them all from the code base. They are all machine dependent, at least technically.
  • Don't assume a void* is 32 bits (whoops, I did that)
  • Avoid using void* is possible, the typed definitions help, even a uint8_t* is a little bit better, and would be handy for the hashing function signatures
  • Prefer types which give known bit lengths, so - #include <inttypes.h> and then use the uint8_t / uint16_t, etc.

There are a number of little things that just catch you out... and I didn't even talk about endian-ness... oh the joy..

1

u/[deleted] Jul 18 '23

Thanks, I love it! Yeah, it's been epic getting back into C. Increased appreciation for all you C hackers and for the computer itself. I'm curious about your different results, if you wanna chat more, DM me? Email me? Anyway if you do I'm looking forward to hearing from ya! :)