Skip to content

Commit

Permalink
update js examples
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Apr 27, 2023
1 parent 1cc7036 commit 4a92ed6
Show file tree
Hide file tree
Showing 15 changed files with 3,273 additions and 2,712 deletions.
4 changes: 2 additions & 2 deletions ferveo-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub struct Dkg(ferveo::api::Dkg);
impl Dkg {
#[new]
pub fn new(
tau: u64,
tau: u32,
shares_num: u32,
security_threshold: u32,
validators: Vec<Validator>,
Expand Down Expand Up @@ -488,7 +488,7 @@ mod test_ferveo_python {
type TestInputs = (Vec<ValidatorMessage>, Vec<Validator>, Vec<Keypair>);

fn make_test_inputs(
tau: u64,
tau: u32,
security_threshold: u32,
shares_num: u32,
) -> TestInputs {
Expand Down
2 changes: 2 additions & 0 deletions ferveo-wasm/examples/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
## Usage

Make sure `pkg` is build with correct target:

```bash
cd ../../
wasm-pack build --target web
```

Install dependencies and run tests:

```bash
$ yarn install
$ yarn start
Expand Down
5 changes: 3 additions & 2 deletions ferveo-wasm/examples/browser/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));
import("./index.js").catch((e) =>
console.error("Error importing `index.js`:", e)
);
9 changes: 6 additions & 3 deletions ferveo-wasm/examples/browser/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8" />
<title>ferveo-wasm example</title>
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script src="./bootstrap.js"></script>
<noscript
>This page contains webassembly and javascript content, please enable
javascript in your browser.</noscript
>
<script src="./main.js"></script>
<h1>Check console for results</h1>
</body>
</html>
86 changes: 5 additions & 81 deletions ferveo-wasm/examples/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,8 @@
import {
Ciphertext,
decryptWithSharedSecret,
Dkg,
encrypt,
SharedSecretSimpleBuilder,
DecryptionShareSimple,
} from "ferveo-wasm";
import { Keypair } from "ferveo-wasm";

const zip = (a, b) => a.map((k, i) => [k, b[i]]);
// Just testing that the WASM module is loaded correctly
// JS tests are already written in the examples/node

const areEqual = (first, second) =>
first.length === second.length && first.every((value, index) => value === second[index]);
Keypair.random();

const sharesNum = 16;
const threshold = (sharesNum * 2) / 3;
const msg = new TextEncoder().encode("my-msg");
const aad = new TextEncoder().encode("my-aad");

const dkg = new Dkg(threshold, sharesNum);

//
// On the client side
//

// Encrypt the message
const ciphertext = encrypt(msg, aad, dkg.public_key);

// Serialize and send to validators
const ciphertext_bytes = ciphertext.toBytes();

//
// On the server side
//

const ciphertext2 = Ciphertext.fromBytes(ciphertext_bytes);
console.assert(areEqual(ciphertext.toBytes(), ciphertext2.toBytes()));

// Create decryption shares

const decryptionShares = [];
for (let i = 0; i < threshold; i++) {
const share = dkg.makeDecryptionShareSimple(ciphertext, aad, i);
decryptionShares.push(share);
}

const domainPoints = [];
for (let i = 0; i < threshold; i++) {
const point = dkg.getDomainPoint(i);
domainPoints.push(point);
}

// Serialize and send back to client
const decryptionSharesBytes = decryptionShares.map((s) => s.toBytes());

//
// On the client side
//

const decryptionShares2 = decryptionSharesBytes.map((b) =>
DecryptionShareSimple.fromBytes(b)
);
zip(decryptionShares, decryptionShares2).map(([s1, s2]) =>
console.assert(areEqual(s1.toBytes(), s2.toBytes()))
)

// Combine shares into a shared secret
const ssBuilder = new SharedSecretSimpleBuilder(threshold);
decryptionShares.forEach((share) => ssBuilder.addDecryptionShare(share));

domainPoints.forEach((point) => ssBuilder.addDomainPoint(point));

const shared_secret = ssBuilder.build();

// Decrypt the message
const plaintext = decryptWithSharedSecret(
ciphertext,
aad,
shared_secret,
dkg.gInv
);
console.assert(areEqual(plaintext, msg));

console.log("Success! 🎉")
console.log("Success! 🎉");
24 changes: 13 additions & 11 deletions ferveo-wasm/examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
"private": true,
"author": "Piotr Rosłaniec <p.roslaniec@gmail.com>",
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server"
"build": "webpack --mode=production --node-env=production",
"start": "webpack-dev-server",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"watch": "webpack --watch",
"serve": "webpack serve"
},
"license": "GPL-3.0-only",
"dependencies": {
"ferveo-wasm": "../../pkg"
"ferveo-wasm": "../../pkg",
"glob-parent": "6.0.2"
},
"devDependencies": {
"copy-webpack-plugin": "^5.0.0",
"webpack": "^4.29.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"resolutions": {
"glob-parent": "5.1.2"
"copy-webpack-plugin": "^11.0.0",
"webpack": "^5.81.0",
"webpack-cli": "^5.0.2",
"webpack-dev-server": "^4.13.3"
}
}
}
1 change: 1 addition & 0 deletions ferveo-wasm/examples/browser/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World!");
39 changes: 34 additions & 5 deletions ferveo-wasm/examples/browser/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
// Generated using webpack-cli https://github.com/webpack/webpack-cli

const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');

module.exports = {
const path = require("path");
const isProduction = process.env.NODE_ENV == "production";

const config = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
mode: "development",
plugins: [new CopyWebpackPlugin(["index.html"])],
devServer: {
open: true,
host: "localhost",
},
plugins: [new CopyWebpackPlugin({ patterns: ["index.html"]})],
module: {
rules: [
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: "asset",
},

// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
],
},
experiments: {
asyncWebAssembly: true,
}
};

module.exports = () => {
if (isProduction) {
config.mode = "production";
} else {
config.mode = "development";
}
return config;
};
Loading

0 comments on commit 4a92ed6

Please sign in to comment.