zkmopro / mopro

Making client-side proving on mobile simple.
Apache License 2.0
102 stars 28 forks source link

refactor: remove wasm, add native proof generation #179

Closed vimwitch closed 6 days ago

vimwitch commented 1 week ago

This PR removes the wasm logic as well as the dylib and calc-native-witness logic. Instead it relies on the rust-witness package to natively build witnesses. This happens by taking the wasm, compiling to c, then using a macro to automatically write a function that calls the c function to build the witness.

The UDL bindings are very complex right now. Instead of passing paths between the rust side and the mobile side I think it makes more sense to build a dictionary on the rust side for each circuit. Imagine there is a build with 2 circuits: multiplier2 and keccak256. Instead of having initialize(zkey_path, wasm_path) we could have initialize(circuit_name), and define the paths to zkeys, witnesses, etc at compile time in rust - which should already be happening. Then we have minimal complexity moving over the bridge.

Instead of giving the user a config file with strings we could give them a config file as a rust source file. This way they can define their own logic for calculating witnesses, loading zkeys, etc. In practice they would probably use whatever loaders we recommend.

This is what such a config function might look like.

vimwitch commented 6 days ago

@vivianjeng I debugged the slow build time and it's because of the anon-aadhaar circuit. The C source code for the witness generator is 70 MB which takes quite a long time to compile. I put the aadhaar test behind a feature flag so we don't need need to build it.

vimwitch commented 6 days ago

@vivianjeng The CLI tests are failing right now. To fix this we need to (at a minimum) add logic to bundle zkeys for each mobile platform. If we do that we can prove the pre-compiled circuits. To prove custom circuits we need to add the ability to configure via rust.

I think these items are both out of scope of this PR.

vivianjeng commented 6 days ago

not sure why the CI shows the error

error[E0308]: mismatched types
   --> /Users/runner/.cargo/git/checkouts/mopro-af576ae5c07fa51d/6e341a7/mopro-core/src/middleware/circom/mod.rs:198:44
    |
198 |             WitnessCalculator::from_module(module).expect("Failed to create WitnessCalculator");
    |             ------------------------------ ^^^^^^ expected `wasmer::sys::module::Module`, found `wasmer::Module`

but it doesn't happen in my local

I am also thinking about can we move the transpile_wasm to mopro prepare instead of mopro build? If we have large circuits it might take too much time to build/update bindings.

vimwitch commented 6 days ago

I am also thinking about can we move the transpile_wasm to mopro prepare instead of mopro build? If we have large circuits it might take too much time to build/update bindings.

This is possible, but will expose a decent amount of complexity from rust-witness. We should instead look at caching more aggressively in the rust-witness package - possibly using a global cache with a static library keyed to the hash of the wasm file or something. But we can implement that without changing mopro.

Related zkmopro/rust-witness#4