feat: fetch resources and fix bundle (#15)

This commit is contained in:
Richard Ramos 2022-09-24 13:59:13 -04:00 committed by GitHub
parent 7752534855
commit 29f09af131
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 203 additions and 41 deletions

View File

@ -2,7 +2,7 @@
"version": "0.1",
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/master/cspell.schema.json",
"language": "en",
"words": ["Waku", "keypair", "merkle", "zkey", "circom", "zerokit"],
"words": ["Waku", "keypair", "merkle", "vkey", "zkey", "circom", "zerokit"],
"flagWords": [],
"ignorePaths": [
"package.json",

View File

@ -19,10 +19,10 @@
},
"..": {
"name": "@waku/rln",
"version": "0.0.5",
"version": "0.0.6",
"license": "MIT OR Apache-2.0",
"dependencies": {
"@waku/zerokit-rln-wasm": "^0.0.1"
"@waku/zerokit-rln-wasm": "^0.0.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
@ -49,7 +49,6 @@
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"fast-check": "^2.14.0",
"file-loader": "^6.2.0",
"gh-pages": "^3.2.3",
"husky": "^7.0.4",
"ignore-loader": "^0.1.2",
@ -67,11 +66,11 @@
"prettier": "^2.1.1",
"process": "^0.11.10",
"puppeteer": "^13.0.1",
"resolve-typescript-plugin": "^1.2.0",
"rollup": "^2.75.0",
"rollup-plugin-copy": "^3.4.0",
"size-limit": "^8.0.0",
"tail": "^2.2.0",
"ts-jest": "^29.0.1",
"ts-loader": "^9.3.1",
"ts-node": "^10.9.1",
"typedoc": "^0.23.10",
@ -3955,7 +3954,7 @@
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"@waku/zerokit-rln-wasm": "^0.0.1",
"@waku/zerokit-rln-wasm": "^0.0.2",
"app-root-path": "^3.0.0",
"chai": "^4.3.4",
"cspell": "^5.14.0",
@ -3983,6 +3982,7 @@
"prettier": "^2.1.1",
"process": "^0.11.10",
"puppeteer": "^13.0.1",
"resolve-typescript-plugin": "^1.2.0",
"rollup": "^2.75.0",
"rollup-plugin-copy": "^3.4.0",
"size-limit": "^8.0.0",

51
package-lock.json generated
View File

@ -26,6 +26,7 @@
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"@web/rollup-plugin-import-meta-assets": "^1.0.7",
"app-root-path": "^3.0.0",
"chai": "^4.3.4",
"cspell": "^5.14.0",
@ -1107,6 +1108,33 @@
"resolved": "https://registry.npmjs.org/@waku/zerokit-rln-wasm/-/zerokit-rln-wasm-0.0.2.tgz",
"integrity": "sha512-WFJXVhh5mlp1ActepsyCQ8RLWIp/BE0VmXdI+7Og0ealEtoaUzpHF1oxKeJpGLgYJWLYQc6DWgRz5Zz8vf+uKw=="
},
"node_modules/@web/rollup-plugin-import-meta-assets": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@web/rollup-plugin-import-meta-assets/-/rollup-plugin-import-meta-assets-1.0.7.tgz",
"integrity": "sha512-ft44CqITUkNd8stwNb4ZOvrZ8DlPifM821jplksmxRGetg5Lx684oFrpfQJ7mfkU/Sa7B3dI1mHTX0DE52eBwg==",
"dev": true,
"dependencies": {
"@rollup/pluginutils": "^4.1.0",
"estree-walker": "^2.0.2",
"magic-string": "^0.25.7"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/@web/rollup-plugin-import-meta-assets/node_modules/@rollup/pluginutils": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz",
"integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==",
"dev": true,
"dependencies": {
"estree-walker": "^2.0.1",
"picomatch": "^2.2.2"
},
"engines": {
"node": ">= 8.0.0"
}
},
"node_modules/@webassemblyjs/ast": {
"version": "1.11.1",
"dev": true,
@ -8972,6 +9000,29 @@
"resolved": "https://registry.npmjs.org/@waku/zerokit-rln-wasm/-/zerokit-rln-wasm-0.0.2.tgz",
"integrity": "sha512-WFJXVhh5mlp1ActepsyCQ8RLWIp/BE0VmXdI+7Og0ealEtoaUzpHF1oxKeJpGLgYJWLYQc6DWgRz5Zz8vf+uKw=="
},
"@web/rollup-plugin-import-meta-assets": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@web/rollup-plugin-import-meta-assets/-/rollup-plugin-import-meta-assets-1.0.7.tgz",
"integrity": "sha512-ft44CqITUkNd8stwNb4ZOvrZ8DlPifM821jplksmxRGetg5Lx684oFrpfQJ7mfkU/Sa7B3dI1mHTX0DE52eBwg==",
"dev": true,
"requires": {
"@rollup/pluginutils": "^4.1.0",
"estree-walker": "^2.0.2",
"magic-string": "^0.25.7"
},
"dependencies": {
"@rollup/pluginutils": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz",
"integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==",
"dev": true,
"requires": {
"estree-walker": "^2.0.1",
"picomatch": "^2.2.2"
}
}
}
},
"@webassemblyjs/ast": {
"version": "1.11.1",
"dev": true,

View File

@ -1,6 +1,6 @@
{
"name": "@waku/rln",
"version": "0.0.6",
"version": "0.0.7",
"description": "Rate Limit Nullifier for js-waku",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
@ -66,6 +66,7 @@
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"@web/rollup-plugin-import-meta-assets": "^1.0.7",
"app-root-path": "^3.0.0",
"chai": "^4.3.4",
"cspell": "^5.14.0",

View File

@ -3,7 +3,7 @@ import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { wasm } from "@rollup/plugin-wasm";
import copy from "rollup-plugin-copy";
import { importMetaAssets } from "@web/rollup-plugin-import-meta-assets";
export default {
input: {
index: "dist/index.js",
@ -15,7 +15,10 @@ export default {
plugins: [
copy({
hook: "buildStart",
targets: [{ src: "src/zerokit/rln_wasm_bg.wasm", dest: "dist/zerokit" }],
targets: [
{ src: "src/resources/rln.wasm", dest: "dist/resources" },
{ src: "src/resources/rln_final.zkey", dest: "dist/resources" },
],
}),
commonjs(),
json(),
@ -27,5 +30,6 @@ export default {
preferBuiltins: false,
extensions: [".js", ".ts", ".wasm"],
}),
importMetaAssets(),
],
};

File diff suppressed because one or more lines are too long

BIN
src/resources/rln.wasm Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,119 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 6,
"vk_alpha_1": [
"1805378556360488226980822394597799963030511477964155500103132920745199284516",
"11990395240534218699464972016456017378439762088320057798320175886595281336136",
"1"
],
"vk_beta_2": [
[
"11031529986141021025408838211017932346992429731488270384177563837022796743627",
"16042159910707312759082561183373181639420894978640710177581040523252926273854"
],
[
"20112698439519222240302944148895052359035104222313380895334495118294612255131",
"19441583024670359810872018179190533814486480928824742448673677460151702019379"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"1948496782571164085469528023647105317580208688174386157591917599801657832035",
"20445814069256658101339037520922621162739470138213615104905368409238414511981"
],
[
"10024680869920840984813249386422727863826862577760330492647062850849851925340",
"10512156247842686783409460795717734694774542185222602679117887145206209285142"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"5151991366823434428398919091000210787450832786814248297320989361921939794156",
"15735191313289001022885148627913534790382722933676436876510746491415970766821"
],
[
"3387907257437913904447588318761906430938415556102110876587455322225272831272",
"1998779853452712881084781956683721603875246565720647583735935725110674288056"
],
[
"14280074182991498185075387990446437410077692353432005297922275464876153151820",
"17092408446352310039633488224969232803092763095456307462247653153107223117633"
]
],
[
[
"4359046709531668109201634396816565829237358165496082832279660960675584351266",
"4511888308846208349307186938266411423935335853916317436093178288331845821336"
],
[
"11429499807090785857812316277335883295048773373068683863667725283965356423273",
"16232274853200678548795010078253506586114563833318973594428907292096178657392"
],
[
"18068999605870933925311275504102553573815570223888590384919752303726860800970",
"17309569111965782732372130116757295842160193489132771344011460471298173784984"
]
]
],
"IC": [
[
"18693301901828818437917730940595978397160482710354161265484535387752523310572",
"17985273354976640088538673802000794244421192643855111089693820179790551470769",
"1"
],
[
"21164641723988537620541455173278629777250883365474191521194244273980931825942",
"998385854410718613441067082771678946155853656328717326195057262123686425518",
"1"
],
[
"21666968581672145768705229094968410656430989593283335488162701230986314747515",
"17996457608540683483506630273632100555125353447506062045735279661096094677264",
"1"
],
[
"20137761979695192602424300886442379728165712610493092740175904438282083668117",
"19184814924890679891263780109959113289320127263583260218200636509492157834679",
"1"
],
[
"10943171273393803842589314082509655332154393332394322726077270895078286354146",
"10872472035685319847811233167729172672344935625121511932198535224727331126439",
"1"
],
[
"13049169779481227658517545034348883391527506091990880778783387628208561946597",
"10083689369261379027228809473568899816311684698866922944902456565434209079955",
"1"
],
[
"19633516378466409167014413361365552102431118630694133723053441455184566611083",
"8059525100726933978719058611146131904598011633549012007359165766216730722269",
"1"
]
]
}

View File

@ -1,23 +1,8 @@
import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
import * as resources from "./resources.js";
import verificationKey from "./resources/verification_key.json";
import * as wc from "./witness_calculator.js";
/**
* Convert a base64 string into uint8Array
* @param base64
* @returns Uint8Array
*/
function base64ToUint8Array(base64: string): Uint8Array {
const binary_string = window.atob(base64);
const len = binary_string.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
}
/**
* Concatenate Uint8Arrays
* @param input
@ -37,10 +22,21 @@ function concatenate(...input: Uint8Array[]): Uint8Array {
return result;
}
const stringEncoder = new TextEncoder();
const DEPTH = 20;
const VERIFICATION_KEY = base64ToUint8Array(resources.verification_key);
const ZKEY = base64ToUint8Array(resources.zkey);
const CIRCUIT = base64ToUint8Array(resources.circuit);
async function loadWitnessCalculator(): Promise<any> {
const url = new URL("./resources/rln.wasm", import.meta.url);
const response = await fetch(url);
return await wc.builder(new Uint8Array(await response.arrayBuffer()), false);
}
async function loadZkey(): Promise<Uint8Array> {
const url = new URL("./resources/rln_final.zkey", import.meta.url);
const response = await fetch(url);
return new Uint8Array(await response.arrayBuffer());
}
/**
* Create an instance of RLN
@ -49,9 +45,10 @@ const CIRCUIT = base64ToUint8Array(resources.circuit);
export async function create(): Promise<RLNInstance> {
await init();
zerokitRLN.init_panic_hook();
const witnessCalculator = await wc.builder(CIRCUIT, false);
const zkRLN = zerokitRLN.newRLN(DEPTH, ZKEY, VERIFICATION_KEY);
const witnessCalculator = await loadWitnessCalculator();
const zkey = await loadZkey();
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
return new RLNInstance(zkRLN, witnessCalculator);
}