Browse Source

add html and js to interact with the wasm lib, add server serving wasm lib mime type, wasm wrapper print shares result into document

master
arnaucube 5 years ago
parent
commit
9d84e20a5f
7 changed files with 79 additions and 6 deletions
  1. +2
    -0
      wasm/.gitignore
  2. +23
    -0
      wasm/index.html
  3. +8
    -0
      wasm/index.js
  4. +15
    -0
      wasm/package.json
  5. +7
    -0
      wasm/server.js
  6. +24
    -6
      wasm/shamirsecretsharing-wasm-wrapper.go
  7. BIN
      wasm/shamirsecretsharing.wasm

+ 2
- 0
wasm/.gitignore

@ -0,0 +1,2 @@
node_modules
package-lock.json

+ 23
- 0
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>

+ 8
- 0
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);
}

+ 15
- 0
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"
}
}

+ 7
- 0
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")

+ 24
- 6
wasm/shamirsecretsharing-wasm-wrapper.go

@ -1,6 +1,7 @@
package main
import (
"crypto/rand"
"math/big"
"syscall/js"
@ -17,11 +18,11 @@ func main() {
}
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 @@ func createShares(i []js.Value) {
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 @@ func lagrangeInterpolation(i []js.Value) {
secr := shamirsecretsharing.LagrangeInterpolation(p, shares)
println(secr.String())
return nil
}

BIN
wasm/shamirsecretsharing.wasm


Loading…
Cancel
Save