1
1
import { WASI , PreopenDirectory , File , OpenFile , ConsoleStdout } from "@bjorn3/browser_wasi_shim/dist" ;
2
- import { groth16 , wtns } from "snarkjs" ;
2
+ import { groth16 } from "snarkjs" ;
3
3
4
- async function load_external_file ( path ) {
5
- const response = await fetch ( path ) ;
6
- const buffer = await response . arrayBuffer ( ) ;
7
- return new File ( buffer ) ;
8
- }
4
+ import * as fastFile from "fastfile" ;
5
+ import { WitnessCalculatorBuilder } from "circom_runtime" ;
9
6
10
7
async function _fullProve ( input ) {
11
- const wasmFile = "circuit.wasm" ;
12
8
const zkeyFile = "circuit_final.zkey" ;
13
- let fds = [
9
+ let instance = await createWASMInstance ( ) ;
10
+ const w = {
11
+ type : "mem"
12
+ } ;
13
+ await wtnsCalculate ( input , instance , w ) ;
14
+ const { proof, publicSignals } = await groth16 . prove ( zkeyFile , w ) ;
15
+ return { proof, inputs : publicSignals } ;
16
+ }
17
+
18
+ export const fullProveImpl = input => ( ) => _fullProve ( input ) ;
19
+
20
+ async function createWASMInstance ( ) {
21
+ let memorySize = 32767 ;
22
+ let memory ;
23
+ let memoryAllocated = false ;
24
+ while ( ! memoryAllocated ) {
25
+ try {
26
+ memory = new WebAssembly . Memory ( { initial : memorySize } ) ;
27
+ memoryAllocated = true ;
28
+ } catch ( err ) {
29
+ if ( memorySize === 1 ) {
30
+ throw err ;
31
+ }
32
+ console . warn ( "Could not allocate " + memorySize * 1024 * 64 + " bytes. This may cause severe instability. Trying with " + memorySize * 1024 * 64 / 2 + " bytes" ) ;
33
+ memorySize = Math . floor ( memorySize / 2 ) ;
34
+ }
35
+ }
36
+
37
+ const fds = [
14
38
new OpenFile ( new File ( [ ] ) ) , // stdin
15
39
ConsoleStdout . lineBuffered ( msg => console . log ( `[WASI stdout] ${ msg } ` ) ) ,
16
40
ConsoleStdout . lineBuffered ( msg => console . warn ( `[WASI stderr] ${ msg } ` ) ) ,
17
41
new PreopenDirectory ( "/" , [
18
42
[ "circuit.bin" , await load_external_file ( "circuit.bin" ) ]
19
43
] )
20
44
] ;
45
+
21
46
const wasi = new WASI ( [ ] , [ ] , fds , { debug : true } ) ;
22
- let options = {
23
- initializeWasiReactorModuleInstance : ( instance ) => {
24
- wasi . initialize ( instance ) ;
25
- } ,
26
- additionalWASMImports : {
27
- wasi_snapshot_preview1 : wasi . wasiImport
28
- }
29
- } ;
30
- const w = {
31
- type : "mem"
32
- } ;
33
- await wtns . calculate ( input , wasmFile , w , options ) ;
34
- const { proof, publicSignals} = await groth16 . prove ( zkeyFile , w ) ;
35
- return { proof, inputs : publicSignals } ;
36
- }
37
47
38
- export const fullProveImpl = input => ( ) => _fullProve ( input ) ;
48
+ const fdWasm = await fastFile . readExisting ( "circuit.wasm" ) ;
49
+ const code = await fdWasm . read ( fdWasm . totalSize ) ;
50
+ await fdWasm . close ( ) ;
51
+
52
+ const wasmModule = await WebAssembly . compile ( code ) ;
53
+
54
+ const instance = await WebAssembly . instantiate ( wasmModule , { wasi_snapshot_preview1 : wasi . wasiImport } ) ;
55
+
56
+ wasi . initialize ( instance ) ;
57
+
58
+ return instance ;
59
+
60
+ } ;
61
+
62
+ async function wtnsCalculate ( input , wasm , wtnsFileName ) {
63
+
64
+ const wc = await WitnessCalculatorBuilder ( wasm ) ;
65
+ const fdWtns = await fastFile . createOverride ( wtnsFileName ) ;
66
+
67
+ const w = await wc . calculateWTNSBin ( input ) ;
68
+
69
+ await fdWtns . write ( w ) ;
70
+ await fdWtns . close ( ) ;
71
+ } ;
72
+
73
+ async function load_external_file ( path ) {
74
+ const response = await fetch ( path ) ;
75
+ const buffer = await response . arrayBuffer ( ) ;
76
+ return new File ( buffer ) ;
77
+ } ;
0 commit comments