Skip to content

zkmopro/circom_flutter

Repository files navigation

Mopro Flutter Package

A Flutter plugin for generating and verifying zero-knowledge proofs (ZKPs) on mobile platforms (iOS/Android). This package provides a simple interface to interact with proof systems such as Circom and Halo2, supporting multiple proof libraries (e.g., Arkworks, Rapidsnark).

Getting Started

Follow these steps to integrate the Mopro Flutter package into your project.

Adding a package dependency to an app

  1. Add Dependency: You can add mopro_flutter_bindings to your project by manually editing pubspec.yaml.

    • Manual Edit (Required for local path or specific Git dependencies): Open your pubspec.yaml file and add mopro_flutter_bindings under dependencies.

      dependencies:
          flutter:
              sdk: flutter
      
          mopro_flutter_bindings:
              git:
                  url: https://github.com/zkmopro/mopro_flutter_package
          # Or
          # mopro_flutter_package:
          #    path: ../mopro_flutter_package
  2. Update Circuit Asset: Include your compiled Circom .zkey file as an asset. Add the asset path to your pubspec.yaml under the flutter: section:

    flutter:
        uses-material-design: true # Ensure this is present
        assets:
            # Add the directory containing your .zkey file(s)
            - assets/...zkey
            # Or specify the file directly:
            # - assets/multiplier2_final.zkey

    Make sure the path points correctly to where you've placed your .zkey file within your Flutter project.

  3. Install Package: Run the following command in your terminal from the root of your Flutter project:

    flutter pub get

Usage Example

Here's a basic example demonstrating how to use the package to generate and verify a proof for a simple multiplier circuit.

Update the main function to initialize the Rust library before running the app:

void main() async {
  await RustLib.init();
  runApp(const MyApp());
}

Import the package and use it:

// Import the package
import 'package:mopro_flutter_bindings/src/rust/third_party/mopro_example_app.dart'; // Change to your library name
import 'package:mopro_flutter_bindings/src/rust/frb_generated.dart';

final zkeyPath = await copyAssetToFileSystem(
    'assets/multiplier2_final.zkey',
)

// Corresponds to the inputs of multiplier2.circom
const int a = 3;
const int b = 5;
final Map<String, List<String>> inputs = {
    'a': [a.toString()],
    'b': [b.toString()],
};
// Convert inputs to JSON string
final String inputsJson = jsonEncode(inputs);

// Use the zkey asset path provided in pubspec.yaml
final CircomProofResult proofResult = await generateCircomProof(
    zkeyPath: zkeyPath,
    circuitInputs: inputsJson,
    proofLib: ProofLib.arkworks,
);

final bool isValid = await verifyCircomProof(
    zkeyPath: zkeyPath,
    proofResult: proofResult,
    proofLib: ProofLib.arkworks,
);

print('Generated Proof: ${proofResult.proof}');
print('Public Inputs/Outputs: ${proofResult.inputs}');
print('Verification result: $isValid');

Note

To learn how to read a .zkey file from an app, please refer to the copyAssetToFileSystem function in the Flutter app.

Warning

The default bindings are built specifically for the multiplier2 circom circuit. If you'd like to update the circuit or switch to a different proving scheme, please refer to the How to Build the Package section.
Circuit source code: https://github.com/zkmopro/circuit-registry/tree/main/multiplier2
Example .zkey file for the circuit: http://ci-keys.zkmopro.org/multiplier2_final.zkey

How to Build the Package

Generate the bindings using the Mopro CLI

This package relies on bindings generated by the Mopro CLI. To learn how to build Mopro bindings, refer to the Getting Started section. If you'd like to generate custom bindings for your own circuits or proving schemes, check out the guide on how to use the Mopro CLI: Rust Setup for Android/iOS Bindings.

Choose Flutter to build the bindings, or run

mopro build --platforms flutter

to generate the flutter package.

Then, replace the entire bindings directory mopro_flutter_package with your generated files in the current folder:

├── android
├── cargokit
├── flutter_rust_bridge.yaml
├── ios
├── lib
├── pubspec.yaml
└── rust

or running e.g.

cp -R \
  mopro_flutter_bindings/android \
  mopro_flutter_bindings/cargokit \
  mopro_flutter_bindings/flutter_rust_bridge.yaml \
  mopro_flutter_bindings/ios \
  mopro_flutter_bindings/lib \
  mopro_flutter_bindings/pubspec.yaml \
  mopro_flutter_bindings/rust \
  mopro_flutter_package/

Flutter Example App

  • Open the example app that uses the defined flutter package in the example/ folder
    cd example
  • Install the dependencies
    flutter pub get
  • Open an iOS simulator/device or an Android emulator/device and run the example app
    flutter run
  • Clean the cache if you update the bindings and it throws errors
    flutter clean

![WARNING] Bindings generated by the mopro CLI use local crate paths by default, which makes them non-reproducible on other devices. To make the build reproducible, publish your Rust crate and reference it via a Git source instead of a local path at here. If you’d like to help address this limitation, contributions are welcome — see zkmopro/mopro#647 .

Community

  • X account:
  • Telegram group:

Acknowledgements

This work was initially sponsored by a joint grant from PSE and 0xPARC. It is currently incubated by PSE.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published