zkmopro / mopro

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

Initial Halo2 support #145

Open oskarth opened 1 month ago

oskarth commented 1 month ago

Problem

Some client-side ZK applications use Halo2. We want to support this.

Details

Most likely Halo2 (KZG) since this is more widely used in the ZK app space.

Should be a new adapter (currently: middleware) with bindings to iOS (e.g.).

@ElusAegis is working on this.

Also see https://github.com/zkmopro/mopro/issues/15

Acceptance criteria

oskarth commented 1 month ago

Feel free to add more details here @ElusAegis

ElusAegis commented 4 weeks ago

The current state of things:

Situation

As Halo2 is a library, there is no single way how projects can implement their circuits, as opposed to Circom, which fixes the structure.

This implies that either the solution should accept any Rust Halo2 Circuit implementation, which might be infeasible, or limit and provide a structure that must be implemented by the Rust Halo2 Circuit crate.

Current Approach

So far, we have decided to provide a structure on how such Rust Halo2 crates should look, and ask developers to follow the structure, by exposing the 3 required components, while not limiting how exactly they are implemented.

This should be versatile enough to not be limited to any particular implementation, as well as be rigid enough to go well with the current mopro structure.

Halo2 PR (#149)

We currently support Halo2 circuits in an experimental stage. To use Halo2, you need to set the kind to halo2 in the mopro-config.toml file.

[circuit]
kind = "halo2" # Options: circom, halo2
dir = "mopro-core/examples/halo2/halo2-fibonacci" # Directory of the circuit
name = "fibonacci"                # Name of the circuit

The dir should point to the directory where the Halo2 circuit is located. The name should be the name of the circuit.

Note that currently the Halo2 circuit must be a cargo crate, with the package name hardcoded to halo2-circuit. This is due to us swapping out the default implementation of the halo2-ciurcuit crate with the user's circuit during the build process. This is done using the build.rs script in the mopro-core crate, which changes the path to the default examples/halo2/halo2-fibonacci to $dir. This also requires the name of the package to be halo2-circuit for the substitution to work.

Additionally, the halo2-circuit crate must implement and expose three items, which you can see in the mopro-core/examples/halo2/halo2-fibonacci/src/lib.rs:

This is then used by the mopro-core crate to generate the proof and verify it.