-
Notifications
You must be signed in to change notification settings - Fork 7
/
App.tsx
170 lines (142 loc) Β· 4.49 KB
/
App.tsx
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as React from 'react';
import { Button, StyleSheet, View } from 'react-native';
import * as WebAssembly from 'react-native-webassembly';
import LocalHello from './sources/Local.Hello.wasm';
import LocalCallback from './sources/Local.Callback.wasm';
import LocalSimpleMemory from './sources/Local.SimpleMemory.wasm';
import { useWasmCircomRuntime, useWasmHelloWorld } from './hooks';
import { fetchWasm } from './utils';
export default function App() {
const helloWorld = useWasmHelloWorld();
const helloWorldResult =
'result' in helloWorld ? helloWorld.result : undefined;
const helloWorldError = 'error' in helloWorld ? helloWorld.error : undefined;
const { calculateWTNSBin, error: circomError } = useWasmCircomRuntime();
/* Hook I/O. */
React.useEffect(
() => void (helloWorldError && console.error(helloWorldError)),
[helloWorldError]
);
/* ZK Snark */
React.useEffect(
() => void (circomError && console.error(circomError)),
[circomError]
);
/* Add. */
React.useEffect(() => {
if (!helloWorldResult) return;
const result = helloWorldResult.instance.exports.add(103, 202);
if (result !== 305) throw new Error('Failed to add.');
}, [helloWorldResult]);
/* Local imports. */
React.useEffect(
() =>
void (async () => {
try {
const localModule = await WebAssembly.instantiate<{
readonly add: (a: number, b: number) => number;
}>(LocalHello);
const result = localModule.instance.exports.add(1000, 2000);
if (result !== 3000) throw new Error('Failed to add. (Local)');
} catch (e) {
console.error(e);
}
})(),
[]
);
/* complex allocation */
React.useEffect(
() =>
void (async () => {
try {
/* complex */
await WebAssembly.instantiate(
await fetchWasm(
'https://github.com/tact-lang/ton-wasm/raw/main/output/wasm/emulator-emscripten.wasm'
),
{}
);
} catch (e) {
console.error(e);
}
})(),
[]
);
/* callback e2e */
React.useEffect(
() =>
void (async () => {
try {
const localCallback = await WebAssembly.instantiate<{
readonly callBackFunction: (a: number) => number;
}>(LocalCallback, {
runtime: {
callback: (a: number): number => a * 2,
},
});
const result = localCallback.instance.exports.callBackFunction(25);
if (result !== 50) throw new Error('Callback failure.');
} catch (e) {
console.error(e);
}
})(),
[]
);
/* Simple memory. */
React.useEffect(
() =>
void (async () => {
try {
const localSimpleMemory = await WebAssembly.instantiate<{
readonly write_byte_to_memory: (value: number) => void;
readonly read_byte_from_memory: () => number;
}>(LocalSimpleMemory);
const testMemory = (withValue: number) => {
localSimpleMemory.instance.exports.write_byte_to_memory(withValue);
const wasmResult =
localSimpleMemory.instance.exports.read_byte_from_memory();
if (wasmResult !== withValue)
throw new Error(
`Expected ${withValue}, encountered wasm ${wasmResult}.`
);
const ab: ArrayBuffer = localSimpleMemory.instance.exports.memory!;
const jsResult = new Uint8Array(ab.slice(0, 1))[0];
// Ensure the JavaScript buffer is up-to-date.
if (jsResult !== withValue)
throw new Error(
`Expected ${withValue}, encountered js ${jsResult}.`
);
};
for (let i = 0; i < 255; i += 1) testMemory(i);
} catch (e) {
console.error(e);
}
})(),
[]
);
return (
<View style={styles.container}>
<Button
title="Circom"
onPress={() => {
try {
// https://github.com/cawfree/zk-starter
const witnessBuffer = calculateWTNSBin({ a: 3, b: 11 });
console.log(JSON.stringify(witnessBuffer));
} catch (e) {
// Ignore instantiation errors.
if (String(e) === 'Not ready to calculate.') return;
console.error(e);
}
}}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});