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

Improve Support for WASM #60

Merged
merged 13 commits into from
Jan 17, 2023
Merged

Conversation

ashutoshvarma
Copy link
Contributor

@ashutoshvarma ashutoshvarma commented Jan 12, 2023

Motivation

Use Spartan in WASM environment (browser or WASM runtimes, wasm32-unknown-unknown).
Ref #54

Things changed

  • move dependencies std features behind new feature std (a58ddef)
    Previously there was no way to disable the std features if someone wants to build it in no_std or partial std target. Now the same can be done by simply disabling the default feature.
  • remove rand_core dependency as rand re-exports it.
  • use XofReader instead of io::Read for no_std compatibility (f63812b)
  • only build bench & profile if std in enabled (074db25)
  • add CI job to test builds with default-features disabled. (5169cf2)
  • add WASM Support section in readme

How to use Spartan in wasm32-unknown-unknown?

When adding Spartan to wasm-bindgen projects or any project that target wasm32-unknown-unknown, simply disable the default features and add getrandom(v0.1) as a dependency with wasm-bindgen feature enabled.

[dependencies]
spartan = { version = "0.7", default-features = false }
# since spartan uses getrandom(rand's OsRng), we need to enable 'wasm-bindgen'
# feature to make it feed rand seed from js/nodejs env
# https://docs.rs/getrandom/0.1.16/getrandom/index.html#support-for-webassembly-and-asmjs
getrandom = { version = "0.1", features = ["wasm-bindgen"] }

Details for getrandom

Spartan uses rand v0.7 (Why not latest rand v0.8? because curve25519-dalek v3's Scalar::random will break as it uses rand v0.7).
The rand::rngs::OsRng depends upon getrandom (v0.1) crate which silently fails in wasm32-unknown-unknown during runtime, see the discussion rust-random/getrandom#87 (comment) unless wasm-bindgen feature is enabled.

@ashutoshvarma
Copy link
Contributor Author

@microsoft-github-policy-service agree

@srinathsetty
Copy link
Contributor

@ashutoshvarma thanks for the PR! It looks great!

From the CI, it looks like there is a minor issue. I don't seem to have the write access to fix this in the PR branch. Can you add a commit to fix this?

error: this impl can be derived
--> src/errors.rs:26:1
|
26 | / impl Default for ProofVerifyError {
27 | | fn default() -> Self {
28 | | ProofVerifyError::InternalError
29 | | }
30 | | }
| |_^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
= note: -D clippy::derivable-impls implied by -D warnings
= help: remove the manual implementation...
help: ...and instead derive it...
|
7 | #[derive(Default)]
|
help: ...and mark the default variant
|
8 ~ #[default]
9 ~ InternalError,
|

@ashutoshvarma
Copy link
Contributor Author

I don't seem to have the write access to fix this in the PR branch.

That's weird, "Allow edits and access to secrets by maintainers" checkbox is checked for this PR.

Can you add a commit to fix this?

Sure.

@ashutoshvarma
Copy link
Contributor Author

@srinathsetty fix pushed!

@srinathsetty srinathsetty merged commit 2be72b2 into microsoft:master Jan 17, 2023
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.

2 participants