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
-
Manual Edit (Required for local path or specific Git dependencies): Open your
pubspec.yamlfile and addcircom_flutterunderdependencies.```yaml dependencies: flutter: sdk: flutter circom_flutter: git: url: https://github.com/zkmopro/circom_flutter # Or # circom_flutter: # path: ../circom_flutter ``` -
Update Circuit Asset: Include your compiled Circom circuit
.zkeyand.binfile as an asset. Add the asset path to yourpubspec.yamlunder theflutter: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
.zkeyand.binfile within your Flutter project. -
Install Package: Run the following command in your terminal from the root of your Flutter project:
flutter pub get
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());
}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.
var inputs = '{"a":["3"],"b":["5"]}';
final proof = await circomProve(
graphPath: graphPath,
inputs: inputs,
zkeyPath: zkeyPath,
);final isValid = await verifyCircomProof(
zkeyPath: zkeyPath,
proofResult: proof,
proofLib: ProofLib.arkworks,
);- Use
mopro-cliand choosecircom
mopro initchoose 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 buildchoose flutter.
- Open the example app that uses the defined flutter package in the
example/foldercd 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 .
This work was initially sponsored by a joint grant from PSE and 0xPARC. It is currently incubated by PSE.