wasm proof generation from browser working. Added cli wasm exporters, html&js browser example, wasm wrapper from go

This commit is contained in:
arnaucube
2019-06-24 16:50:28 +02:00
parent 662d92b697
commit 0a162482ee
14 changed files with 260 additions and 6 deletions

3
wasm/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
node_modules
package-lock.json
wasm_exec.js

28
wasm/README.md Normal file
View File

@@ -0,0 +1,28 @@
# go-snark wasm
*Warning: this is an ongoing experimentation*
## Wasm usage
To compile to wasm, inside the `wasm` directory, execute:
```
GOARCH=wasm GOOS=js go build -o go-snark.wasm go-snark-wasm-wrapper.go
```
Add the file `wasm_exec.js` in the directory:
```
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
```
Call the library from javascript:
```js
let r = generateProofs(
JSON.stringify(circuit),
JSON.stringify(setup),
JSON.stringify(px),
JSON.stringify(inputs),
);
```
Run the http server that allows to load the `.wasm` file:
```
node server.js
```

View File

@@ -0,0 +1,91 @@
package main
import (
"encoding/json"
"syscall/js"
"github.com/arnaucube/go-snark"
"github.com/arnaucube/go-snark/circuitcompiler"
"github.com/arnaucube/go-snark/wasm/utils"
)
func main() {
c := make(chan struct{}, 0)
println("WASM Go Initialized")
// register functions
registerCallbacks()
<-c
}
func registerCallbacks() {
js.Global().Set("generateProofs", js.FuncOf(generateProofs))
}
func generateProofs(this js.Value, i []js.Value) interface{} {
var circuitStr utils.CircuitString
err := json.Unmarshal([]byte(i[0].String()), &circuitStr)
if err != nil {
println(i[0].String())
println("error parsing circuit from stringified json")
}
circuit, err := utils.CircuitFromString(circuitStr)
if err != nil {
println("error " + err.Error())
}
sj, err := json.Marshal(circuit)
if err != nil {
println("error " + err.Error())
}
println("circuit", string(sj))
var setupStr utils.SetupString
println(i[1].String())
err = json.Unmarshal([]byte(i[1].String()), &setupStr)
if err != nil {
println("error parsing setup from stringified json")
}
setup, err := utils.SetupFromString(setupStr)
if err != nil {
println("error " + err.Error())
}
sj, err = json.Marshal(setup)
if err != nil {
println("error " + err.Error())
}
println("set", string(sj))
var pxStr []string
err = json.Unmarshal([]byte(i[2].String()), &pxStr)
if err != nil {
println("error parsing pxStr from stringified json")
}
px, err := utils.ArrayStringToBigInt(pxStr)
if err != nil {
println(err.Error())
}
sj, err = json.Marshal(px)
if err != nil {
println("error " + err.Error())
}
println("px", string(sj))
var inputs circuitcompiler.Inputs
err = json.Unmarshal([]byte(i[3].String()), &inputs)
if err != nil {
println("error parsing inputs from stringified json")
}
w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
proof, err := snark.GenerateProofs(circuit, setup, w, px)
if err != nil {
println("error generating proof", err)
}
proofString := utils.ProofToString(proof)
proofJson, err := json.Marshal(proofString)
if err != nil {
println("error marshal proof to json", err)
}
println("proofJson", string(proofJson))
return js.ValueOf(string(proofJson))
}

BIN
wasm/go-snark.wasm Executable file

Binary file not shown.

19
wasm/index.html Normal file
View File

@@ -0,0 +1,19 @@
<html>
<head>
<meta charset="utf-8">
<title>go-snark wasm experiments</title>
</head>
<body>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch('go-snark.wasm'), go.importObject).then(function(dat) {
go.run(dat.instance);
});
</script>
<button onClick="callGenerateProof();">Generate Proof</button>
<textarea id="proofResult" rows="15" style="width: 80%;">
</textarea>
<script src="index.js"></script>
</body>
</html>

19
wasm/index.js Normal file

File diff suppressed because one or more lines are too long

15
wasm/package.json Normal file
View File

@@ -0,0 +1,15 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
}
}

7
wasm/server.js Normal file
View File

@@ -0,0 +1,7 @@
var express = require('express');
var app = express();
express.static.mime.types['wasm'] = 'application/wasm';
app.use(express.static(__dirname + '/'));
port=8080
app.listen(8080);
console.log("server running at :8080")