microsoft / Spartan

Spartan: High-speed zkSNARKs without trusted setup
MIT License
689 stars 119 forks source link

Improve Support for WASM #60

Closed ashutoshvarma closed 1 year ago

ashutoshvarma commented 1 year ago

Motivation

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

Things changed

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 https://github.com/rust-random/getrandom/issues/87#issuecomment-522138650 unless wasm-bindgen feature is enabled.

ashutoshvarma commented 1 year ago

@microsoft-github-policy-service agree

srinathsetty commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

@srinathsetty fix pushed!