feat: benchmark using polyfil when wasm is not available
This commit is contained in:
parent
b41cd212b4
commit
78daff1036
7 changed files with 1465 additions and 1263 deletions
45
package-lock.json
generated
45
package-lock.json
generated
|
@ -8,7 +8,8 @@
|
||||||
"name": "mcaptcha-survey",
|
"name": "mcaptcha-survey",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@mcaptcha/pow-wasm": "0.1.0-alpha-1",
|
"@mcaptcha/pow_sha256-polyfill": "^0.1.0-alpha-1",
|
||||||
|
"@mcaptcha/pow-wasm": "^0.1.0-alpha-1",
|
||||||
"@mcaptcha/vanilla-glue": "^0.1.0-alpha-3"
|
"@mcaptcha/vanilla-glue": "^0.1.0-alpha-3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -18,7 +19,7 @@
|
||||||
"@types/sinon": "^10.0.0",
|
"@types/sinon": "^10.0.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
||||||
"@typescript-eslint/parser": "^5.0.0",
|
"@typescript-eslint/parser": "^5.0.0",
|
||||||
"@wasm-tool/wasm-pack-plugin": "^1.4.0",
|
"@wasm-tool/wasm-pack-plugin": "^1.6.0",
|
||||||
"dart-sass": "^1.25.0",
|
"dart-sass": "^1.25.0",
|
||||||
"eslint": "^8.0.1",
|
"eslint": "^8.0.1",
|
||||||
"jest": "^27.2.5",
|
"jest": "^27.2.5",
|
||||||
|
@ -1439,6 +1440,29 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"node_modules/@mcaptcha/pow_sha256-polyfill": {
|
||||||
|
"version": "0.1.0-alpha-1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@mcaptcha/pow_sha256-polyfill/-/pow_sha256-polyfill-0.1.0-alpha-1.tgz",
|
||||||
|
"integrity": "sha512-lnQNBCOnVI9BunHP8FGCsGs310GguQWdxSspXlvWcrLwgl86aq0hlBzZfOV+szG/jeTRAMry0He3MrD/kbqB/Q==",
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"type": "individual",
|
||||||
|
"url": "http://mcaptcha.org/donate"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "liberapay",
|
||||||
|
"url": "https://liberapay.com/mcaptcha"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "individual",
|
||||||
|
"url": "http://batsense.net/donate"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "liberapay",
|
||||||
|
"url": "https://liberapay.com/realaravinth"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"node_modules/@mcaptcha/pow-wasm": {
|
"node_modules/@mcaptcha/pow-wasm": {
|
||||||
"version": "0.1.0-alpha-1",
|
"version": "0.1.0-alpha-1",
|
||||||
"resolved": "https://registry.npmjs.org/@mcaptcha/pow-wasm/-/pow-wasm-0.1.0-alpha-1.tgz",
|
"resolved": "https://registry.npmjs.org/@mcaptcha/pow-wasm/-/pow-wasm-0.1.0-alpha-1.tgz",
|
||||||
|
@ -2919,9 +2943,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/caniuse-lite": {
|
"node_modules/caniuse-lite": {
|
||||||
"version": "1.0.30001447",
|
"version": "1.0.30001448",
|
||||||
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001447.tgz",
|
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001448.tgz",
|
||||||
"integrity": "sha512-bdKU1BQDPeEXe9A39xJnGtY0uRq/z5osrnXUw0TcK+EYno45Y+U7QU9HhHEyzvMDffpYadFXi3idnSNkcwLkTw==",
|
"integrity": "sha512-tq2YI+MJnooG96XpbTRYkBxLxklZPOdLmNIOdIhvf7SNJan6u5vCKum8iT7ZfCt70m1GPkuC7P3TtX6UuhupuA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
|
@ -11164,6 +11188,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/@mcaptcha/core-glue/-/core-glue-0.1.0-alpha-5.tgz",
|
"resolved": "https://registry.npmjs.org/@mcaptcha/core-glue/-/core-glue-0.1.0-alpha-5.tgz",
|
||||||
"integrity": "sha512-16qWm5O5X0Y9LXULULaAks8Vf9FNlUUBcR5KDt49aWhFhG5++JzxNmCwQM9EJSHNU7y0U+FdyAWcGmjfKlkRLA=="
|
"integrity": "sha512-16qWm5O5X0Y9LXULULaAks8Vf9FNlUUBcR5KDt49aWhFhG5++JzxNmCwQM9EJSHNU7y0U+FdyAWcGmjfKlkRLA=="
|
||||||
},
|
},
|
||||||
|
"@mcaptcha/pow_sha256-polyfill": {
|
||||||
|
"version": "0.1.0-alpha-1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@mcaptcha/pow_sha256-polyfill/-/pow_sha256-polyfill-0.1.0-alpha-1.tgz",
|
||||||
|
"integrity": "sha512-lnQNBCOnVI9BunHP8FGCsGs310GguQWdxSspXlvWcrLwgl86aq0hlBzZfOV+szG/jeTRAMry0He3MrD/kbqB/Q=="
|
||||||
|
},
|
||||||
"@mcaptcha/pow-wasm": {
|
"@mcaptcha/pow-wasm": {
|
||||||
"version": "0.1.0-alpha-1",
|
"version": "0.1.0-alpha-1",
|
||||||
"resolved": "https://registry.npmjs.org/@mcaptcha/pow-wasm/-/pow-wasm-0.1.0-alpha-1.tgz",
|
"resolved": "https://registry.npmjs.org/@mcaptcha/pow-wasm/-/pow-wasm-0.1.0-alpha-1.tgz",
|
||||||
|
@ -12363,9 +12392,9 @@
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"caniuse-lite": {
|
"caniuse-lite": {
|
||||||
"version": "1.0.30001447",
|
"version": "1.0.30001448",
|
||||||
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001447.tgz",
|
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001448.tgz",
|
||||||
"integrity": "sha512-bdKU1BQDPeEXe9A39xJnGtY0uRq/z5osrnXUw0TcK+EYno45Y+U7QU9HhHEyzvMDffpYadFXi3idnSNkcwLkTw==",
|
"integrity": "sha512-tq2YI+MJnooG96XpbTRYkBxLxklZPOdLmNIOdIhvf7SNJan6u5vCKum8iT7ZfCt70m1GPkuC7P3TtX6UuhupuA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"chalk": {
|
"chalk": {
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
"@types/sinon": "^10.0.0",
|
"@types/sinon": "^10.0.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
||||||
"@typescript-eslint/parser": "^5.0.0",
|
"@typescript-eslint/parser": "^5.0.0",
|
||||||
"@wasm-tool/wasm-pack-plugin": "^1.4.0",
|
"@wasm-tool/wasm-pack-plugin": "^1.6.0",
|
||||||
"dart-sass": "^1.25.0",
|
"dart-sass": "^1.25.0",
|
||||||
"eslint": "^8.0.1",
|
"eslint": "^8.0.1",
|
||||||
"jest": "^27.2.5",
|
"jest": "^27.2.5",
|
||||||
|
@ -33,6 +33,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@mcaptcha/vanilla-glue": "^0.1.0-alpha-3",
|
"@mcaptcha/vanilla-glue": "^0.1.0-alpha-3",
|
||||||
"@mcaptcha/pow-wasm": "0.1.0-alpha-1"
|
"@mcaptcha/pow_sha256-polyfill": "^0.1.0-alpha-1",
|
||||||
|
"@mcaptcha/pow-wasm": "^0.1.0-alpha-1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
60
templates/bench/prove.ts
Normal file
60
templates/bench/prove.ts
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/*
|
||||||
|
* mCaptcha is a PoW based DoS protection software.
|
||||||
|
* This is the frontend web component of the mCaptcha system
|
||||||
|
* Copyright © 2021 Aravinth Manivnanan <realaravinth@batsense.net>.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by Apache 2.0 or MIT license.
|
||||||
|
* You shoud have received a copy of MIT and Apache 2.0 along with
|
||||||
|
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
|
||||||
|
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as p from "@mcaptcha/pow_sha256-polyfill";
|
||||||
|
import { PoWConfig } from "./types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* proove work
|
||||||
|
* @param {PoWConfig} config - the proof-of-work configuration using which
|
||||||
|
* work needs to be computed
|
||||||
|
* */
|
||||||
|
const prove = async (config: PoWConfig): Promise<number> => {
|
||||||
|
console.log(`Wasm support says ${WasmSupported}`);
|
||||||
|
let duration: number;
|
||||||
|
if (WasmSupported) {
|
||||||
|
const wasm = require("@mcaptcha/pow-wasm");
|
||||||
|
const t0 = performance.now();
|
||||||
|
wasm.gen_pow(config.salt, config.string, config.difficulty_factor);
|
||||||
|
const t1 = performance.now();
|
||||||
|
duration = t1 - t0;
|
||||||
|
} else {
|
||||||
|
console.log("WASM unsupported, expect delay during proof generation");
|
||||||
|
const t0 = performance.now();
|
||||||
|
|
||||||
|
await p.generate_work(config.salt, config.string, config.difficulty_factor);
|
||||||
|
const t1 = performance.now();
|
||||||
|
duration = t1 - t0;
|
||||||
|
}
|
||||||
|
return duration;
|
||||||
|
};
|
||||||
|
|
||||||
|
// credits: @jf-bastien on Stack Overflow
|
||||||
|
// https://stackoverflow.com/questions/47879864/how-can-i-check-if-a-browser-supports-webassembly
|
||||||
|
const WasmSupported = (() => {
|
||||||
|
try {
|
||||||
|
if (
|
||||||
|
typeof WebAssembly === "object" &&
|
||||||
|
typeof WebAssembly.instantiate === "function"
|
||||||
|
) {
|
||||||
|
const module = new WebAssembly.Module(
|
||||||
|
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
|
||||||
|
);
|
||||||
|
if (module instanceof WebAssembly.Module)
|
||||||
|
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
})();
|
||||||
|
|
||||||
|
export default prove;
|
|
@ -15,35 +15,24 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { gen_pow } from "@mcaptcha/pow-wasm";
|
import { Bench, PoWConfig } from "./types";
|
||||||
import { Bench } from "./types";
|
import prove from "./prove";
|
||||||
|
|
||||||
type PoWConfig = {
|
|
||||||
string: string;
|
|
||||||
difficulty_factor: number;
|
|
||||||
salt: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const SALT = "674243647f1c355da8607a8cdda05120d79ca5d1af8b3b49359d056a0a82";
|
const SALT = "674243647f1c355da8607a8cdda05120d79ca5d1af8b3b49359d056a0a82";
|
||||||
const PHRASE = "6e2a53dbc7d307970d7ba3c0000221722cb74f1c325137251ce8fa5c2240";
|
const PHRASE = "6e2a53dbc7d307970d7ba3c0000221722cb74f1c325137251ce8fa5c2240";
|
||||||
|
|
||||||
const config: PoWConfig = {
|
|
||||||
string: PHRASE,
|
|
||||||
difficulty_factor: 1,
|
|
||||||
salt: SALT,
|
|
||||||
};
|
|
||||||
|
|
||||||
console.debug("worker registered");
|
console.debug("worker registered");
|
||||||
|
|
||||||
onmessage = function (event) {
|
onmessage = async (event) => {
|
||||||
console.debug("message received at worker");
|
console.debug("message received at worker");
|
||||||
const difficulty_factor = parseInt(event.data);
|
const difficulty_factor = parseInt(event.data);
|
||||||
config.difficulty_factor = difficulty_factor;
|
const config: PoWConfig = {
|
||||||
|
string: PHRASE,
|
||||||
|
difficulty_factor,
|
||||||
|
salt: SALT,
|
||||||
|
};
|
||||||
|
|
||||||
const t0 = performance.now();
|
let duration = await prove(config);
|
||||||
gen_pow(config.salt, config.string, config.difficulty_factor);
|
|
||||||
const t1 = performance.now();
|
|
||||||
const duration = t1 - t0;
|
|
||||||
|
|
||||||
const msg: Bench = {
|
const msg: Bench = {
|
||||||
difficulty: difficulty_factor,
|
difficulty: difficulty_factor,
|
||||||
|
|
|
@ -35,3 +35,9 @@ export type SubmissionProof = {
|
||||||
export type BenchConfig = {
|
export type BenchConfig = {
|
||||||
difficulties: Array<number>;
|
difficulties: Array<number>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type PoWConfig = {
|
||||||
|
string: string;
|
||||||
|
difficulty_factor: number;
|
||||||
|
salt: string;
|
||||||
|
};
|
||||||
|
|
|
@ -1,30 +1,30 @@
|
||||||
'use strict';
|
"use strict";
|
||||||
const path = require('path');
|
const path = require("path");
|
||||||
//const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
|
//const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
devtool: 'inline-source-map',
|
devtool: "inline-source-map",
|
||||||
mode: 'development',
|
mode: "production",
|
||||||
//mode: 'production',
|
//mode: 'production',
|
||||||
entry: {
|
entry: {
|
||||||
bundle: './templates/index.ts',
|
bundle: "./templates/index.ts",
|
||||||
bench: './templates/bench/service-worker.ts',
|
bench: "./templates/bench/service-worker.ts",
|
||||||
glue: './templates/bench/vendor.ts',
|
glue: "./templates/bench/vendor.ts",
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
filename: '[name].js',
|
filename: "[name].js",
|
||||||
path: path.resolve(__dirname, './static/cache/bundle'),
|
path: path.resolve(__dirname, "./static/cache/bundle"),
|
||||||
},
|
},
|
||||||
module: {
|
module: {
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
test: /\.tsx?$/,
|
test: /\.tsx?$/,
|
||||||
loader: 'ts-loader',
|
loader: "ts-loader",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: ['.ts', '.tsx', '.js'],
|
extensions: [".ts", ".tsx", ".js"],
|
||||||
},
|
},
|
||||||
|
|
||||||
experiments: {
|
experiments: {
|
||||||
|
|
Loading…
Reference in a new issue