Why does snarkjs running on Node.js throw an instance of std::bad_alloc?

170 Views Asked by At

I'm running snarkjs over Node.js on a linux x64 machine with 256GB RAM and I have a zk-SNARK circuit composed by ~28M constraints.

I want to execute the command snarkjs zkey new to generate a zkey file from a circuit compiled with circom, but when the task is processing the constraints from the input file, it always fails while processing constraint 10-11M, throwing a std::bad_alloc exception.

I tried to increase the JavaScript heap size to 200GB by adding the node option --max-old-space-size=204800, but it didn't solve the problem.

It doesn't seem an out of memory related issue because, when it fails, the process is using ~40GB of memory. I also don't think is a problem of trying to allocate a too large contiguous portion of memory because snarkjs is handling this case by creating a vector of vectors if the number of constraints is above a certain threshold (1^20).

I'm using Node.js 18.17.0 and snarkjs 0.7.0, but I also tried other versions and the problem is still there.

Output

The output (I added a print in snarkjs source code to see at which constraint it fails) I'm receiving is:

node --max-old-space-size=204800 node_modules/.bin/snarkjs zkey new build/my_step.r1cs resources/powersOfTau28_hez_final_25.ptau build/my_step_p1.zkey
[INFO]  snarkJS: Reading r1cs
[INFO]  snarkJS: Reading tauG1
[INFO]  snarkJS: Reading tauG2
[INFO]  snarkJS: Reading alphatauG1
[INFO]  snarkJS: Reading betatauG1
processing constraints: 0/28173676
processing constraints: 500000/28173676
processing constraints: 1000000/28173676
processing constraints: 1500000/28173676
processing constraints: 2000000/28173676
processing constraints: 2500000/28173676
processing constraints: 3000000/28173676
processing constraints: 3500000/28173676
processing constraints: 4000000/28173676
processing constraints: 4500000/28173676
processing constraints: 5000000/28173676
processing constraints: 5500000/28173676
processing constraints: 6000000/28173676
processing constraints: 6500000/28173676
processing constraints: 7000000/28173676
processing constraints: 7500000/28173676
processing constraints: 8000000/28173676
processing constraints: 8500000/28173676
processing constraints: 9000000/28173676
processing constraints: 9500000/28173676
processing constraints: 10000000/28173676
processing constraints: 10500000/28173676
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)
Circuit

The circuit for which I'm trying to create the zkey file can be found at https://github.com/leonardovona/beacon-relay/blob/main/circuits/circuits/step.circom.

Steps to reproduce

The steps to reproduce are:

  • Compile the circuit: circom my_step.circom --O1 --r1cs
  • Generate the zkey file: node --max-old-space-size=204800 node_modules/.bin/snarkjs zkey new my_step.r1cs powersOfTau28_hez_final_25.ptau my_step.zkey
0

There are 0 best solutions below