Skip to content

zkmopro/noir_flutter

Repository files navigation

Noir Flutter

noir_flutter is a Flutter package for generating and verifying Noir zero-knowledge proofs using the Barretenberg backend.

The Flutter bindings are generated by the mopro CLI using the Noir adapter, which relies on a prebuilt Barretenberg backend.

To learn more about the original Rust implementation before generating bindings, please refer to noir-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 noir_flutter under dependencies.

      ```yaml
      dependencies:
          flutter:
              sdk: flutter
    
          noir_flutter:
              git:
                  url: https://github.com/zkmopro/noir_flutter
          # Or
          # mopro_flutter_package:
          #    path: ../noir_flutter
      ```
    
  2. Update Circuit Asset: Include your compiled Noir circuit .json and .srs 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 .json file(s)
            - assets/....json
            - assets/....srs
            # Or specify the file directly:
            # - assets/noir_multiplier2.json

    Make sure the path points correctly to where you've placed your .json 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:noir_flutter/src/rust/third_party/mopro_example_app_noir.dart';
import 'package:noir_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 circuit and srs

Please checkout noir-rs to see how to generate the circuit and the srs.

final circuitPath = await copyAssetToFileSystem(
      'assets/noir_multiplier2.json');
final srsPath = await copyAssetToFileSystem(
      'assets/noir_multiplier2.srs');

Note

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

getNoirVerificationKey

const bool onChain = true;
const bool lowMemoryMode = false;
var inputs = ["3","5"];

final vk = await getNoirVerificationKey(
    circuitPath: circuitPath,
    srsPath: srsPath,
    onChain: onChain,
    lowMemoryMode: lowMemoryMode,
);

generateNoirProof

final proofData = await generateNoirProof(
    circuitPath: circuitPath,
    srsPath: srsPath,
    inputs: inputs,
    onChain: onChain,
    vk: vk,
    lowMemoryMode: lowMemoryMode
);

verifyNoirProof

final isValid = await verifyNoirProof(
    circuitPath: circuitPath,
    proof: proofData,
    onChain: onChain,
    vk: vk,
    lowMemoryMode: lowMemoryMode,
);

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 \
  noir_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

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages