Skip to content

zkmopro/circom_flutter

Repository files navigation

Circom Flutter

circom_flutter is a Flutter package for generating and verifying Circom zero-knowledge proofs using the groth16 backend.

The Swift bindings are generated by the mopro CLI using the Circom adapter, which utilizes circom-prover with circom-witnesscalc for witness generation and arkworks for groth16 proof construction.

To learn more about the original Rust implementation before generating bindings, please refer to zkmopro documentation.

Note

To use circom-witnesscalc, follow the README instructions to generate the graph file from your Circom circuits. Or the script from semaphore-rs

How to Import the Package

Adding a package dependency to an app

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

      ```yaml
      dependencies:
          flutter:
              sdk: flutter
    
          circom_flutter:
              git:
                  url: https://github.com/zkmopro/circom_flutter
          # Or
          # circom_flutter:
          #    path: ../circom_flutter
      ```
    
  2. Update Circuit Asset: Include your compiled Circom circuit .zkey and .bin 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 circom circuit file(s)
            - assets/....zkey
            - assets/....bin
            # Or specify the file directly:
            # - assets/multiplier2_final.zkey

    Make sure the path points correctly to where you've placed your .zkey and .bin 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

How to Use the Package

Import the package

import 'package:circom_flutter/src/rust/third_party/circom_prover_bindings.dart';
import 'package:circom_flutter/src/rust/frb_generated.dart';

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

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

Load graph and zkey

Please checkout circom and circom-witensscalc to see how to generate the zkey and the graph.

final zkeyPath = await copyAssetToFileSystem(
      'assets/multiplier2_final.zkey');
final graphPath = await copyAssetToFileSystem(
      'assets/multiplier2.bin');

Note

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

circomProve

var inputs = '{"a":["3"],"b":["5"]}';

final proof = await circomProve(
    graphPath: graphPath,
    inputs: inputs,
    zkeyPath: zkeyPath,
);

verifyCircomProof

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

How to Build the Package

  1. Use mopro-cli and choose circom
mopro init

choose circom

Update the circom-prover resource in Cargo.toml

circom-prover = { git = "https://github.com/zkmopro/mopro", branch = "circom-witnesscalc-path", features = [
    "circom-witnesscalc",
] }

and run

mopro build

choose flutter.

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