Deserialization failure in OpenFHE when sending serialized keys from JS (WASM) to C++ backend via Base64

Hello,

I am building a client–server application using OpenFHE, where the client side runs in JavaScript using OpenFHE compiled to WebAssembly (via Emscripten), and the server side is a C++ backend.

On the client side, I serialize OpenFHE objects including the public key, ciphertexts, and evaluation keys (EvalMult and EvalRotation keys) using the SerializeToBuffer() function from the OpenFHE WASM bindings. The resulting buffer (Uint8Array) is converted to Base64 and sent to the server via an HTTP POST request.

On the C++ backend, I decode the Base64 string, convert it into a std::vector<uint8_t>, wrap the bytes into a binary stream using std::istringstream, and then attempt to deserialize the objects using Serial::Deserialize(object, stream, SerType::BINARY).

During deserialization on the C++ side, the application crashes with the following error:

Internal error: can’t free this _ntl_gbigint
Aborted (core dumped)

I have verified that the byte buffers appear identical on both the client and server sides (by comparing head/tail bytes and checksums). Serialization and deserialization work correctly when everything is done entirely in C++. The issue only occurs when the data is serialized in JavaScript/WASM and deserialized in native C++.

Any guidance would be greatly appreciated.

Thank you in advance.

It is strange that you are getting an error related to NTL. By default, NTL is not on when OpenFHE is built. NTL gets turned on only if it is explicitly set using CMake flags, e.g., using MATHBACKEND 6. Are you using NTL for something in your application?

Other things to consider:

I would try JSON serialization instead of BINARY just to see if this works (see openfhe-wasm/examples/js/pke/simple_integer_serialization.js at main · openfheorg/openfhe-wasm · GitHub for examples).

Does the end-to-end example work if you do everything in JavaScript (similar to the provided example)?

From an initial look, It seems that the issue is related the extra steps taken between serialization from JavaScript and deserializing it in C++.

1 Like

Thank you so much for your response!!!

Yes, the issue was caused by NTL. I initially built the native OpenFHE backend with MATHBACKEND=6.

After rebuilding it with MATHBACKEND=4, the serialization and deserialization now work correctly across JavaScript and C++.

Thank you again for the valuable insight.