add html and js to interact with the wasm lib, add server serving wasm lib mime type, wasm wrapper print shares result into document
arnaucube arnaucube@gmail.com
Fri, 12 Apr 2019 22:23:52 +0200
7 files changed,
79 insertions(+),
6 deletions(-)
A
wasm/index.html
@@ -0,0 +1,23 @@
+<html> +<head> + <meta charset="utf-8"> + <title>shamirsecretsharing wasm</title> +</head> +<body> + <script src="wasm_exec.js"></script> + <script> + const go = new Go(); + WebAssembly.instantiateStreaming(fetch('shamirsecretsharing.wasm'), go.importObject).then(function(dat) { + go.run(dat.instance); + }); + </script> + <input id="secret" value="123456789" /> + <input id="nShares" value="5"/> + <input id="nNeededShares" value="3"/> + <input id="p" value="7"/> + <button onClick="callCreateShares();">Create Shares</button> + <textarea id="sharesResult" rows="10" style="width: 80%;"> + </textarea> + <script src="index.js"></script> +</body> +</html>
A
wasm/index.js
@@ -0,0 +1,8 @@
+function callCreateShares() { + let secret = Number(document.getElementById("secret").value); + let nShares = Number(document.getElementById("nShares").value); + let nNeededShares = Number(document.getElementById("nNeededShares").value); + let p = Number(document.getElementById("p").value); + + createShares(nNeededShares, nShares, p, secret); +}
A
wasm/package.json
@@ -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" + } +}
A
wasm/server.js
@@ -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")
M
wasm/shamirsecretsharing-wasm-wrapper.go
→
wasm/shamirsecretsharing-wasm-wrapper.go
@@ -1,6 +1,7 @@
package main import ( + "crypto/rand" "math/big" "syscall/js"@@ -17,11 +18,11 @@ <-c
} func registerCallbacks() { - js.Global().Set("createShares", js.ValueOf(createShares)) - js.Global().Set("lagrangeInterpolation", js.ValueOf(lagrangeInterpolation)) + js.Global().Set("createShares", js.FuncOf(createShares)) + js.Global().Set("lagrangeInterpolation", js.FuncOf(lagrangeInterpolation)) } -func createShares(i []js.Value) { +func createShares(this js.Value, i []js.Value) interface{} { nNeededShares, ok := new(big.Int).SetString(i[0].String(), 10) if !ok { println("error parsing parameter in position 0")@@ -34,29 +35,45 @@ p, ok := new(big.Int).SetString(i[2].String(), 10)
if !ok { println("error parsing parameter in position 2") } + bits := 2048 + p, err := rand.Prime(rand.Reader, bits/2) // move this out from wasm, it tooks too much time + if err != nil { + println(err.Error()) + } k, ok := new(big.Int).SetString(i[3].String(), 10) if !ok { println("error parsing parameter in position 3") } + println("nNeededShares", nNeededShares.String()) + println("nShares", nShares.String()) + println("p", p.String()) + println("k (secret)", k.String()) shares, err := shamirsecretsharing.Create(nNeededShares, nShares, p, k) if err != nil { println("error generating the shares") + println(err.Error()) } - println(shares) + println("shares", shares) sharesStr := sharesToString(shares) - println(sharesStr) + println("sharesStr", sharesStr) + return nil } func sharesToString(shares [][]*big.Int) []string { + var printString string var s []string for i := 0; i < len(shares); i++ { s = append(s, shares[i][0].String()) s = append(s, shares[i][1].String()) + printString = printString + "[" + shares[i][0].String() + ", " + shares[i][1].String() + "]\n" + println(shares[i][0].String()) + println(shares[i][1].String()) } + js.Global().Get("document").Call("getElementById", "sharesResult").Set("innerHTML", printString) return s } -func lagrangeInterpolation(i []js.Value) { +func lagrangeInterpolation(this js.Value, i []js.Value) interface{} { p, ok := new(big.Int).SetString(i[0].String(), 10) if !ok { println("error parsing parameter in position 0 (p)")@@ -81,4 +98,5 @@ }
secr := shamirsecretsharing.LagrangeInterpolation(p, shares) println(secr.String()) + return nil }