-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
154 lines (142 loc) · 5.41 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mopro_flutter/mopro_flutter.dart';
import 'package:mopro_flutter/mopro_types.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GenerateProofResult? _proofResult;
final _moproFlutterPlugin = MoproFlutter();
bool isProving = false;
PlatformException? _error;
// Controllers to handle user input
final TextEditingController _controllerA = TextEditingController();
final TextEditingController _controllerB = TextEditingController();
@override
void initState() {
super.initState();
_controllerA.text = "5";
_controllerB.text = "3";
}
@override
Widget build(BuildContext context) {
// The inputs is a base64 string
var inputs = _proofResult?.inputs ?? "";
// The proof is a base64 string
var proof = _proofResult?.proof ?? "";
// Decode the proof and inputs to see the actual values
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter App With MoPro'),
),
body: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (isProving) const CircularProgressIndicator(),
if (_error != null)
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(_error.toString()),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _controllerA,
decoration: const InputDecoration(
labelText: "Public input `a`",
hintText: "For example, 5",
),
keyboardType: TextInputType.number,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _controllerB,
decoration: const InputDecoration(
labelText: "Private input `b`",
hintText: "For example, 3",
),
keyboardType: TextInputType.number,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: OutlinedButton(
onPressed: () async {
if (_controllerA.text.isEmpty ||
_controllerB.text.isEmpty ||
isProving) {
return;
}
setState(() {
isProving = true;
});
FocusManager.instance.primaryFocus?.unfocus();
GenerateProofResult? proofResult;
PlatformException? error;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
var inputs = <String, List<String>>{};
inputs["a"] = [_controllerA.text];
inputs["b"] = [_controllerB.text];
proofResult =
await _moproFlutterPlugin.generateProof(
"assets/multiplier2_final.zkey", inputs);
setState(() {
isProving = false;
});
} on PlatformException catch (e) {
print("Error: $e");
error = e;
proofResult = null;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_proofResult = proofResult;
_error = error;
});
},
child: const Text(
"Generate Proof",
)),
),
],
),
if (_proofResult != null)
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Proof inputs: $inputs'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Proof: $proof'),
),
],
),
],
),
),
),
);
}
}