forked from anoma/ferveo
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cc7036
commit 4a92ed6
Showing
15 changed files
with
3,273 additions
and
2,712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! 🎉"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Hello World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.