mirror of
https://github.com/arnaucube/shamirsecretsharing.git
synced 2026-02-07 03:26:45 +01:00
add Rust implementation, reorganize directories
This commit is contained in:
3
go-shamirsecretsharing/wasm/.gitignore
vendored
Normal file
3
go-shamirsecretsharing/wasm/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
package-lock.json
|
||||
wasm_exec.js
|
||||
23
go-shamirsecretsharing/wasm/index.html
Normal file
23
go-shamirsecretsharing/wasm/index.html
Normal file
@@ -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" title="secret" />
|
||||
<input id="nShares" value="5" title="nShares" />
|
||||
<input id="nNeededShares" value="3" title="nNeededShares" />
|
||||
<input id="p" value="171879804367519690300554482894542984104793303095416535421575087397479872914568537681950898838967105588522810694196545562424558060609619552125935120277761650477937931034625352827489211639332858312433285611335853953593672332879461761823122965792721080501957411113790396112857552217933702513677475650967659977873" title="random p" />
|
||||
<button onClick="callCreateShares();">Create Shares</button>
|
||||
<textarea id="sharesResult" rows="15" style="width: 80%;">
|
||||
</textarea>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
10
go-shamirsecretsharing/wasm/index.js
Normal file
10
go-shamirsecretsharing/wasm/index.js
Normal file
@@ -0,0 +1,10 @@
|
||||
function callCreateShares() {
|
||||
let secret = document.getElementById("secret").value;
|
||||
let nShares = document.getElementById("nShares").value;
|
||||
let nNeededShares = document.getElementById("nNeededShares").value;
|
||||
let p = document.getElementById("p").value;
|
||||
console.log(p)
|
||||
|
||||
let r = createShares(nNeededShares, nShares, p, secret);
|
||||
console.log("r", r);
|
||||
}
|
||||
15
go-shamirsecretsharing/wasm/package.json
Normal file
15
go-shamirsecretsharing/wasm/package.json
Normal 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
go-shamirsecretsharing/wasm/server.js
Normal file
7
go-shamirsecretsharing/wasm/server.js
Normal 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")
|
||||
101
go-shamirsecretsharing/wasm/shamirsecretsharing-wasm-wrapper.go
Normal file
101
go-shamirsecretsharing/wasm/shamirsecretsharing-wasm-wrapper.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/arnaucube/shamirsecretsharing"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := make(chan struct{}, 0)
|
||||
|
||||
println("WASM Go Initialized")
|
||||
// register functions
|
||||
registerCallbacks()
|
||||
<-c
|
||||
}
|
||||
|
||||
func registerCallbacks() {
|
||||
js.Global().Set("createShares", js.FuncOf(createShares))
|
||||
js.Global().Set("lagrangeInterpolation", js.FuncOf(lagrangeInterpolation))
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
nShares, ok := new(big.Int).SetString(i[1].String(), 10)
|
||||
if !ok {
|
||||
println("error parsing parameter in position 1")
|
||||
}
|
||||
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", shares)
|
||||
sharesStr := sharesToString(shares)
|
||||
println("sharesStr", sharesStr)
|
||||
return js.ValueOf(sharesStr)
|
||||
}
|
||||
|
||||
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(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)")
|
||||
}
|
||||
|
||||
// parse the shares array
|
||||
var shares [][]*big.Int
|
||||
for n := 1; n < len(i); n = n + 2 {
|
||||
a, ok := new(big.Int).SetString(i[n].String(), 10)
|
||||
if !ok {
|
||||
println("error parsing parameter in position ", n)
|
||||
}
|
||||
b, ok := new(big.Int).SetString(i[n+1].String(), 10)
|
||||
if !ok {
|
||||
println("error parsing parameter in position ", n+1)
|
||||
}
|
||||
var share []*big.Int
|
||||
share = append(share, a)
|
||||
share = append(share, b)
|
||||
shares = append(shares, share)
|
||||
}
|
||||
|
||||
secr := shamirsecretsharing.LagrangeInterpolation(p, shares)
|
||||
println(secr.String())
|
||||
return js.ValueOf(secr)
|
||||
}
|
||||
BIN
go-shamirsecretsharing/wasm/shamirsecretsharing.wasm
Executable file
BIN
go-shamirsecretsharing/wasm/shamirsecretsharing.wasm
Executable file
Binary file not shown.
Reference in New Issue
Block a user