Skip to content
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

bun support for groth16.fullProve: add singleThreaded option #490

Closed
wants to merge 1 commit into from

Conversation

cdrappi
Copy link

@cdrappi cdrappi commented Apr 21, 2024

When calling groth16.fullProve with bun (instead of node), the function getCurveFromQ hangs forever. I tracked this down a bit, and it seems the root cause is usage of the worker threads. Bun claims to have supported this, but I was unable to get it working with changes further upstream. It's possible that bun doesn't fully support node Workers; it's also possible I wasn't making the correct upstream changes. However, this solution works for my use-case and should work for anyone else who wishes to use bun

If you guys are interested in merging this, I'm happy to follow up with an update to @types/snarkjs

Maintainers, thank you for your work! I am a big fan of this library

* add singleThreaded option to groth16.fullProve

* fix function call
@OBrezhniev
Copy link
Member

Hi @cdrappi! Thank you for your PR!

We just released a new version of snarkjs, which has some changes in the related code.

Can you confirm that you still have issues with it?

@grmkris
Copy link

grmkris commented Sep 6, 2024

Id love to get this merged to main and also fix the types
I tried patching snarkjs but had issues with lib depending on snarkjs

@prevostc
Copy link

prevostc commented Sep 11, 2024

I can confirm this continues to happen on the latest version (v0.7.4) and master (0c580e5) using bun 1.1.26.

Steps to reproduce:

Create a file named test_crash.js at the project root.

import { getCurveFromName } from "./src/curves";

getCurveFromName("bn128")
  .then(console.log)
  .catch(console.error);

Then run bun test_crash.js

Throws with:

$ bun test_crash.js

170 |     event.data = data;
171 |     if (q == null) self.dispatchEvent(event);else q.push(event);
172 |   });
173 |   threads.parentPort.on('error', err => {
174 |     err.type = 'Error';
175 |     self.dispatchEvent(err);
               ^
TypeError: Argument 1 ('event') to EventTarget.dispatchEvent must be an instance of Event
 code: "ERR_INVALID_ARG_TYPE"

      at .../iden3/snarkjs/node_modules/web-worker/cjs/node.js:175:10
[1]    64609 trace trap  bun test_crash.js

For some reason, running bun mocha does not trigger this error though.

@prevostc
Copy link

After a deeper investigation, it turns out that https://github.com/iden3/ffjavascript is using a web-worker polyfill that is explicitly stating to use it's browser entry point on bun but that information is lost in the way up to snarkjs and bun loads the node entrypoint which is specifically designed for node.

HOWEVER, using the bun Worker is not working correctly yet.
Reproduce using this test file:

import { buildBn128 } from "ffjavascript";

console.log("start single thread");
await buildBn128(true);
console.log("ok single thread");

console.log("start multi thread");
await buildBn128(false);
console.log("ok multi thread");

and if you comment out the import Worker from "web-worker"; line in node_modules/ffjavascript/src/threadman.js to force ffjavascript to use bun's Worker. The first call to buildBn128 ends correctly but the second one hangs forever:

bun test_crash.js
start single thread
ok single thread
start multi thread
    <---- never ends

My conclusion is that this PR is needed as a first step to make 0xPolygonID/js-sdk run on bun.

@OBrezhniev
Copy link
Member

Thanks @prevostc for your research! I will take a look at how to proceed with this PR.

@OBrezhniev
Copy link
Member

@cdrappi @grmkris @prevostc Can you please test if #533 works for you?

@prevostc
Copy link

@OBrezhniev I can confirm that the test file does not crash when passed with that extra argument

import { getCurveFromName } from "./src/curves";

getCurveFromName("bn128", { singleThread: true })
	.then(console.log)
	.catch(console.error);

@OBrezhniev
Copy link
Member

Thank you for your contributions! Closing this PR as alternative one #533 got merged and added to release v0.7.5.

@OBrezhniev OBrezhniev closed this Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants