Compare commits

...

6 Commits

17 changed files with 726 additions and 416 deletions

View File

@@ -88,66 +88,41 @@ jobs:
- name: Install dependencies
run: make installdeps
- name: Build rln-wasm
run: |
if [ ${{ matrix.feature }} == default ]; then
cargo make build
else
cargo make build_${{ matrix.feature }}
fi
run: cargo make build
working-directory: ${{ matrix.crate }}
- name: Test rln-wasm
run: |
if [ ${{ matrix.feature }} == default ]; then
cargo make test --release
else
cargo make test_${{ matrix.feature }} --release
fi
- name: Test rln-wasm on node
run: cargo make test --release
working-directory: ${{ matrix.crate }}
- name: Test rln-wasm on browser
run: |
if [ ${{ matrix.feature }} == default ]; then
cargo make test_browser --release
else
cargo make test_browser_${{ matrix.feature }} --release
fi
run: cargo make test_browser --release
working-directory: ${{ matrix.crate }}
# rln-wasm-parallel-test:
# strategy:
# matrix:
# platform: [ubuntu-latest, macos-latest]
# crate: [rln-wasm]
# feature: ["parallel"]
# runs-on: ${{ matrix.platform }}
# timeout-minutes: 60
rln-wasm-parallel-test:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
crate: [rln-wasm]
feature: ["parallel"]
runs-on: ${{ matrix.platform }}
timeout-minutes: 60
# name: Test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
# steps:
# - uses: actions/checkout@v4
# - name: Install nightly toolchain
# uses: dtolnay/rust-toolchain@nightly
# with:
# components: rust-src
# targets: wasm32-unknown-unknown
# - uses: Swatinem/rust-cache@v2
# - name: Install dependencies
# run: make installdeps
# - name: Build rln-wasm in parallel mode
# run: |
# if [ ${{ matrix.feature }} == default ]; then
# cargo make build
# else
# cargo make build_${{ matrix.feature }}
# fi
# working-directory: ${{ matrix.crate }}
# - name: Test rln-wasm in parallel mode
# run: |
# if [ ${{ matrix.feature }} == default ]; then
# cargo make test --release
# else
# cargo make test_${{ matrix.feature }} --release
# fi
# working-directory: ${{ matrix.crate }}
name: Test - ${{ matrix.crate }} - ${{ matrix.platform }} - ${{ matrix.feature }}
steps:
- uses: actions/checkout@v4
- name: Install nightly toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: rust-src
targets: wasm32-unknown-unknown
- uses: Swatinem/rust-cache@v2
- name: Install dependencies
run: make installdeps
- name: Build rln-wasm in parallel mode
run: cargo make build_parallel
working-directory: ${{ matrix.crate }}
- name: Test rln-wasm in parallel mode on browser
run: cargo make test_parallel --release
working-directory: ${{ matrix.crate }}
lint:
strategy:

View File

@@ -125,14 +125,6 @@ jobs:
wasm-bindgen --target web --split-linked-modules --out-dir ./pkg \
./target/wasm32-unknown-unknown/release/rln_wasm.wasm
find ./pkg/snippets -name "workerHelpers.worker.js" \
-exec sed -i.bak 's|from '\''\.\.\/\.\.\/\.\.\/'\'';|from "../../../rln_wasm.js";|g' {} \; \
-exec rm -f {}.bak \;
find ./pkg/snippets -name "workerHelpers.worker.js" \
-exec sed -i.bak 's|await initWbg(module, memory);|await initWbg({ module, memory });|g' {} \; \
-exec rm -f {}.bak \;
else
wasm-pack build --release --target web --scope waku --features ${{ matrix.feature }}
fi

View File

@@ -16,7 +16,9 @@ num-bigint = { version = "0.4.6", default-features = false }
js-sys = "0.3.77"
wasm-bindgen = "0.2.100"
serde-wasm-bindgen = "0.6.5"
wasm-bindgen-rayon = { version = "1.2.0", optional = true }
wasm-bindgen-rayon = { version = "1.3.0", features = [
"no-bundler",
], optional = true }
# The `console_error_panic_xhook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires

View File

@@ -37,9 +37,7 @@ args = [
[tasks.post_build_parallel]
script = '''
wasm-bindgen --target web --split-linked-modules --out-dir ./pkg ./target/wasm32-unknown-unknown/release/rln_wasm.wasm && \
find ./pkg/snippets -name "workerHelpers.worker.js" -exec sed -i.bak 's|from '\''\.\.\/\.\.\/\.\.\/'\'';|from "../../../rln_wasm.js";|g' {} \; -exec rm -f {}.bak \; && \
find ./pkg/snippets -name "workerHelpers.worker.js" -exec sed -i.bak 's|await initWbg(module, memory);|await initWbg({ module, memory });|g' {} \; -exec rm -f {}.bak \;
wasm-bindgen --target web --split-linked-modules --out-dir ./pkg ./target/wasm32-unknown-unknown/release/rln_wasm.wasm
'''
[tasks.pack_rename]
@@ -77,8 +75,6 @@ args = [
"test",
"--release",
"--chrome",
# "--firefox",
# "--safari",
"--headless",
"--target",
"wasm32-unknown-unknown",
@@ -98,8 +94,6 @@ args = [
"test",
"--release",
"--chrome",
# "--firefox",
# "--safari",
"--headless",
"--target",
"wasm32-unknown-unknown",

File diff suppressed because one or more lines are too long

561
rln-wasm/benches/index.html Normal file
View File

@@ -0,0 +1,561 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>RLN WASM Benchmark</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
sans-serif;
max-width: 700px;
margin: 0 auto;
padding: 20px;
color: #333;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.panel {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.file-input {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
}
button {
background: #4361ee;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
margin-top: 10px;
width: 100%;
}
button:hover {
background: #3a56d4;
}
button:disabled {
background: #cccccc;
cursor: not-allowed;
}
.results-table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
.results-table th,
.results-table td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.results-table th {
font-weight: 600;
background-color: #f1f3f5;
}
.operation {
font-weight: 500;
}
.time {
font-family: monospace;
text-align: right;
}
.status {
padding: 10px;
margin-top: 15px;
border-radius: 4px;
text-align: center;
}
.success {
background-color: #d4edda;
color: #155724;
}
.error {
background-color: #f8d7da;
color: #721c24;
}
.running {
background-color: #cce5ff;
color: #004085;
}
/* Download button style */
.download-btn {
background: #28a745;
margin-top: 15px;
}
.download-btn:hover {
background: #218838;
}
/* Summary section */
.summary {
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid #ddd;
}
.summary h3 {
margin-top: 0;
}
/* Thread info */
.thread-info {
margin-bottom: 15px;
padding: 10px;
background-color: #f1f3f5;
border-radius: 4px;
font-weight: 500;
}
</style>
<script>
// Check if cross-origin isolation is available for SharedArrayBuffer
document.addEventListener("DOMContentLoaded", () => {
if (!crossOriginIsolated) {
const statusElement = document.getElementById("status");
if (statusElement) {
statusElement.innerHTML =
'<strong style="color: #721c24;">Error:</strong> ' +
"Cross-Origin Isolation is not enabled. Please run the server.js script with:<br>" +
"<code>node server.js</code><br>" +
'Then access this page via <a href="http://localhost:8000">http://localhost:8000</a>';
statusElement.className = "status error";
}
}
});
</script>
</head>
<body>
<h1>RLN WASM Benchmark</h1>
<div class="panel">
<div class="thread-info" id="threadInfo">Detecting CPU cores...</div>
<div
class="thread-mode-selector"
style="display: flex; gap: 10px; margin-bottom: 15px"
>
<button id="singleThreaded" class="thread-btn" style="flex: 1">
Single-Threaded Mode
</button>
<button
id="multiThreaded"
class="thread-btn"
style="flex: 1; background: #4361ee"
>
Multi-Threaded Mode
</button>
</div>
<button id="initThreads" class="init-btn">Initialize Thread Pool</button>
<div class="file-input">
<label for="zkeyFile">zKey File:</label>
<input type="file" id="zkeyFile" />
</div>
<div class="file-input">
<label for="rootFile">Root File:</label>
<input type="file" id="rootFile" />
</div>
<div class="file-input">
<label for="witnessFile">Witness File:</label>
<input type="file" id="witnessFile" />
</div>
<div class="file-input">
<label for="messageFile">Message File:</label>
<input type="file" id="messageFile" />
</div>
<div class="file-input">
<label for="proofFile">Proof File:</label>
<input type="file" id="proofFile" />
</div>
<button id="runBenchmark" disabled>Run Benchmark</button>
<div id="status" class="status">Please initialize thread pool first</div>
</div>
<div class="panel">
<h2>Results</h2>
<table class="results-table">
<thead>
<tr>
<th>Operation</th>
<th>Time (ms)</th>
</tr>
</thead>
<tbody id="results">
<!-- Results will be populated here -->
</tbody>
</table>
<div id="summarySection" class="summary" style="display: none">
<h3>Summary</h3>
<div id="summaryContent"></div>
<button id="downloadResults" class="download-btn">
Download Results
</button>
</div>
</div>
<script type="module">
import init, * as RLN from "../pkg/rln_wasm.js";
// Get DOM elements
const initThreadsBtn = document.getElementById("initThreads");
const runBtn = document.getElementById("runBenchmark");
const results = document.getElementById("results");
const status = document.getElementById("status");
const summarySection = document.getElementById("summarySection");
const summaryContent = document.getElementById("summaryContent");
const downloadBtn = document.getElementById("downloadResults");
const threadInfo = document.getElementById("threadInfo");
// Track benchmark operations
const benchmarks = [];
let threadPoolInitialized = false;
let cpuCount = navigator.hardwareConcurrency || 4; // Default to 4 if detection fails
let useMultiThreaded = true; // Default to multi-threaded mode
// Update the thread info display
updateThreadInfo();
// Function to update thread info display
function updateThreadInfo() {
if (useMultiThreaded) {
threadInfo.textContent = `Automatically detected ${cpuCount} CPU cores for optimal performance`;
} else {
threadInfo.textContent = `Using single-threaded mode (1 CPU core)`;
}
}
// Thread mode selection
document
.getElementById("singleThreaded")
.addEventListener("click", () => {
if (!threadPoolInitialized) {
useMultiThreaded = false;
document.getElementById("singleThreaded").style.background =
"#4361ee";
document.getElementById("multiThreaded").style.background =
"#6c757d";
updateThreadInfo();
} else {
updateStatus(
"Thread pool already initialized. Please refresh page to change mode.",
"error"
);
}
});
document.getElementById("multiThreaded").addEventListener("click", () => {
if (!threadPoolInitialized) {
useMultiThreaded = true;
document.getElementById("multiThreaded").style.background = "#4361ee";
document.getElementById("singleThreaded").style.background =
"#6c757d";
updateThreadInfo();
} else {
updateStatus(
"Thread pool already initialized. Please refresh page to change mode.",
"error"
);
}
});
// Initialize thread pool
initThreadsBtn.addEventListener("click", async () => {
if (!crossOriginIsolated) {
updateStatus(
"Cross-Origin Isolation is required. Please run with server.js",
"error"
);
return;
}
if (threadPoolInitialized) {
updateStatus("Thread pool already initialized.", "error");
return;
}
updateStatus(
`Initializing thread pool with ${cpuCount} cores...`,
"running"
);
initThreadsBtn.disabled = true;
try {
const start = performance.now();
await init();
await RLN.initThreadPool(useMultiThreaded ? cpuCount : 1);
const duration = performance.now() - start;
benchmarks.push({
name: "Initialize WASM Module",
duration: duration,
success: true,
});
updateResults();
threadPoolInitialized = true;
updateStatus(
`Thread pool initialized with ${
useMultiThreaded ? cpuCount : 1
} cores`,
"success"
);
runBtn.disabled = false;
} catch (error) {
console.error("Thread pool initialization error:", error);
updateStatus(
`Error initializing thread pool: ${error.message}`,
"error"
);
} finally {
initThreadsBtn.disabled = false;
}
});
// Measure operation time
async function benchmark(name, fn) {
updateStatus(`Running: ${name}...`, "running");
const start = performance.now();
try {
const result = await fn();
const duration = performance.now() - start;
// Record result
benchmarks.push({ name, duration, success: true });
updateResults();
return { result, duration };
} catch (error) {
const duration = performance.now() - start;
benchmarks.push({
name: `${name} (FAILED)`,
duration,
success: false,
error: error.message,
});
updateResults();
throw error;
}
}
// Update results table
function updateResults() {
results.innerHTML = "";
benchmarks.forEach((b) => {
const row = document.createElement("tr");
const nameCell = document.createElement("td");
nameCell.className = "operation";
nameCell.textContent = b.name;
const timeCell = document.createElement("td");
timeCell.className = "time";
timeCell.textContent = b.duration.toFixed(2);
row.appendChild(nameCell);
row.appendChild(timeCell);
if (!b.success) {
row.style.color = "#dc3545";
}
results.appendChild(row);
});
}
// Update status message
function updateStatus(message, type = "") {
status.textContent = message;
status.className = `status ${type}`;
}
// Show benchmark summary
function showSummary() {
if (benchmarks.length === 0) return;
const successfulOps = benchmarks.filter((b) => b.success).length;
let summaryHTML = `
<p><strong>Operations:</strong> ${successfulOps}/${
benchmarks.length
} successful</p>
<p><strong>CPU cores used:</strong> ${
useMultiThreaded ? cpuCount : 1
}</p>
<p><strong>Mode:</strong> ${
useMultiThreaded ? "Multi-threaded" : "Single-threaded"
}</p>
`;
summaryContent.innerHTML = summaryHTML;
summarySection.style.display = "block";
}
// Download results as JSON
downloadBtn.addEventListener("click", () => {
const dataStr = JSON.stringify(
{
timestamp: new Date().toISOString(),
mode: useMultiThreaded ? "multi-threaded" : "single-threaded",
cpuCount: useMultiThreaded ? cpuCount : 1,
operations: benchmarks,
},
null,
2
);
const blob = new Blob([dataStr], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `rln-benchmark-${new Date()
.toISOString()
.slice(0, 19)}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Read file as Uint8Array
async function readFile(file) {
const buffer = await file.arrayBuffer();
return new Uint8Array(buffer);
}
// Parse witness JSON
async function parseWitness(file) {
const text = await file.text();
try {
const data = JSON.parse(text);
if (!Array.isArray(data)) {
throw new Error("Witness JSON must be an array");
}
return data.map((value) => BigInt(value));
} catch (e) {
throw new Error(`Failed to parse witness JSON: ${e.message}`);
}
}
// Main benchmark runner
runBtn.addEventListener("click", async () => {
if (!threadPoolInitialized) {
updateStatus("Please initialize thread pool first", "error");
return;
}
const files = {
zkey: document.getElementById("zkeyFile").files[0],
root: document.getElementById("rootFile").files[0],
witness: document.getElementById("witnessFile").files[0],
message: document.getElementById("messageFile").files[0],
proof: document.getElementById("proofFile").files[0],
};
// Validation
if (!files.zkey || !files.root || !files.proof) {
updateStatus("Please select zKey, Root, and Proof files", "error");
return;
}
const canGenerate = files.witness && files.message;
try {
// Keep file references but clear previous benchmark results except initialization
const initBenchmark = benchmarks.find(
(b) => b.name === "Initialize WASM Module"
);
benchmarks.length = 0;
if (initBenchmark) {
benchmarks.push(initBenchmark);
}
updateResults();
summarySection.style.display = "none";
runBtn.disabled = true;
initThreadsBtn.disabled = true;
// Load files
const zkeyData = await readFile(files.zkey);
const rootData = await readFile(files.root);
// Create RLN instance
const { result: instance } = await benchmark(
"Create RLN Instance",
async () => {
return RLN.newRLN(zkeyData);
}
);
// Handle proof generation (if witness and message files provided)
if (canGenerate) {
const witnessData = await parseWitness(files.witness);
const messageData = await readFile(files.message);
await benchmark("Generate RLN Proof", async () => {
return RLN.generateRLNProofWithWitness(
instance,
witnessData,
messageData
);
});
}
// Verify uploaded proof (required)
const proofData = await readFile(files.proof);
await benchmark("Verify Proof", async () => {
return RLN.verifyWithRoots(instance, proofData, rootData);
});
updateStatus("Benchmark completed successfully!", "success");
showSummary();
} catch (error) {
console.error(error);
updateStatus(`Error: ${error.message}`, "error");
showSummary();
} finally {
runBtn.disabled = false;
initThreadsBtn.disabled = false;
}
});
</script>
</body>
</html>

Binary file not shown.

Binary file not shown.

BIN
rln-wasm/benches/root Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,83 @@
const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");
const PORT = 8000;
// MIME type mapping
const MIME_TYPES = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".json": "application/json",
".wasm": "application/wasm",
".png": "image/png",
".jpg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
};
// Create HTTP server
const server = http.createServer((req, res) => {
// Set COOP and COEP headers for SharedArrayBuffer support
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
// Parse URL
const parsedUrl = url.parse(req.url);
let requestPath = parsedUrl.pathname;
// Ignore favicon
if (requestPath === "/favicon.ico") {
res.writeHead(204);
res.end();
return;
}
// Handle root path
let filePath = "." + requestPath;
if (filePath === "./") {
filePath = "./index.html";
}
// Handle pkg files (including snippets)
if (requestPath.startsWith("/pkg/")) {
filePath = ".." + requestPath;
}
// Determine content type based on file extension
const extname = path.extname(filePath);
const contentType = MIME_TYPES[extname] || "application/octet-stream";
// ❗ Block directory reads
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
console.error(`Attempted directory read: ${filePath}`);
res.writeHead(403);
res.end("Forbidden: Cannot read directory directly");
return;
}
// Read and serve the file
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === "ENOENT") {
console.error(`File not found: ${filePath}`);
res.writeHead(404);
res.end(`File not found: ${requestPath}`);
} else {
console.error(`Server error (${error.code}): ${filePath}`);
res.writeHead(500);
res.end(`Server Error: ${error.code}`);
}
} else {
res.writeHead(200, { "Content-Type": contentType });
res.end(content, "utf-8");
}
});
});
// Start the server
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});

View File

@@ -1,5 +1,8 @@
// Node.js module compatible witness calculator
module.exports = async function builder(code, options) {
// File generated with https://github.com/iden3/circom
// following the instructions from:
// https://github.com/vacp2p/zerokit/tree/master/rln#advanced-custom-circuit-compilation
export async function builder(code, options) {
options = options || {};
let wasmModule;
@@ -102,7 +105,7 @@ module.exports = async function builder(code, options) {
// Then append the value to the message we are creating
msgStr += fromArray32(arr).toString();
}
};
}
class WitnessCalculator {
constructor(instance, sanityCheck) {

View File

@@ -1,335 +0,0 @@
// Browser compatible witness calculator
(function (global) {
async function builder(code, options) {
options = options || {};
let wasmModule;
try {
wasmModule = await WebAssembly.compile(code);
} catch (err) {
console.log(err);
console.log(
"\nTry to run circom --c in order to generate c++ code instead\n"
);
throw new Error(err);
}
let wc;
let errStr = "";
let msgStr = "";
const instance = await WebAssembly.instantiate(wasmModule, {
runtime: {
exceptionHandler: function (code) {
let err;
if (code == 1) {
err = "Signal not found.\n";
} else if (code == 2) {
err = "Too many signals set.\n";
} else if (code == 3) {
err = "Signal already set.\n";
} else if (code == 4) {
err = "Assert Failed.\n";
} else if (code == 5) {
err = "Not enough memory.\n";
} else if (code == 6) {
err = "Input signal array access exceeds the size.\n";
} else {
err = "Unknown error.\n";
}
throw new Error(err + errStr);
},
printErrorMessage: function () {
errStr += getMessage() + "\n";
// console.error(getMessage());
},
writeBufferMessage: function () {
const msg = getMessage();
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
if (msg === "\n") {
console.log(msgStr);
msgStr = "";
} else {
// If we've buffered other content, put a space in between the items
if (msgStr !== "") {
msgStr += " ";
}
// Then append the message to the message we are creating
msgStr += msg;
}
},
showSharedRWMemory: function () {
printSharedRWMemory();
},
},
});
const sanityCheck = options;
// options &&
// (
// options.sanityCheck ||
// options.logGetSignal ||
// options.logSetSignal ||
// options.logStartComponent ||
// options.logFinishComponent
// );
wc = new WitnessCalculator(instance, sanityCheck);
return wc;
function getMessage() {
var message = "";
var c = instance.exports.getMessageChar();
while (c != 0) {
message += String.fromCharCode(c);
c = instance.exports.getMessageChar();
}
return message;
}
function printSharedRWMemory() {
const shared_rw_memory_size = instance.exports.getFieldNumLen32();
const arr = new Uint32Array(shared_rw_memory_size);
for (let j = 0; j < shared_rw_memory_size; j++) {
arr[shared_rw_memory_size - 1 - j] =
instance.exports.readSharedRWMemory(j);
}
// If we've buffered other content, put a space in between the items
if (msgStr !== "") {
msgStr += " ";
}
// Then append the value to the message we are creating
msgStr += fromArray32(arr).toString();
}
}
class WitnessCalculator {
constructor(instance, sanityCheck) {
this.instance = instance;
this.version = this.instance.exports.getVersion();
this.n32 = this.instance.exports.getFieldNumLen32();
this.instance.exports.getRawPrime();
const arr = new Uint32Array(this.n32);
for (let i = 0; i < this.n32; i++) {
arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
}
this.prime = fromArray32(arr);
this.witnessSize = this.instance.exports.getWitnessSize();
this.sanityCheck = sanityCheck;
}
circom_version() {
return this.instance.exports.getVersion();
}
async _doCalculateWitness(input, sanityCheck) {
//input is assumed to be a map from signals to arrays of bigints
this.instance.exports.init(this.sanityCheck || sanityCheck ? 1 : 0);
const keys = Object.keys(input);
var input_counter = 0;
keys.forEach((k) => {
const h = fnvHash(k);
const hMSB = parseInt(h.slice(0, 8), 16);
const hLSB = parseInt(h.slice(8, 16), 16);
const fArr = flatArray(input[k]);
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
if (signalSize < 0) {
throw new Error(`Signal ${k} not found\n`);
}
if (fArr.length < signalSize) {
throw new Error(`Not enough values for input signal ${k}\n`);
}
if (fArr.length > signalSize) {
throw new Error(`Too many values for input signal ${k}\n`);
}
for (let i = 0; i < fArr.length; i++) {
const arrFr = toArray32(BigInt(fArr[i]) % this.prime, this.n32);
for (let j = 0; j < this.n32; j++) {
this.instance.exports.writeSharedRWMemory(
j,
arrFr[this.n32 - 1 - j]
);
}
try {
this.instance.exports.setInputSignal(hMSB, hLSB, i);
input_counter++;
} catch (err) {
// console.log(`After adding signal ${i} of ${k}`)
throw new Error(err);
}
}
});
if (input_counter < this.instance.exports.getInputSize()) {
throw new Error(
`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`
);
}
}
async calculateWitness(input, sanityCheck) {
const w = [];
await this._doCalculateWitness(input, sanityCheck);
for (let i = 0; i < this.witnessSize; i++) {
this.instance.exports.getWitness(i);
const arr = new Uint32Array(this.n32);
for (let j = 0; j < this.n32; j++) {
arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
}
w.push(fromArray32(arr));
}
return w;
}
async calculateBinWitness(input, sanityCheck) {
const buff32 = new Uint32Array(this.witnessSize * this.n32);
const buff = new Uint8Array(buff32.buffer);
await this._doCalculateWitness(input, sanityCheck);
for (let i = 0; i < this.witnessSize; i++) {
this.instance.exports.getWitness(i);
const pos = i * this.n32;
for (let j = 0; j < this.n32; j++) {
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
}
}
return buff;
}
async calculateWTNSBin(input, sanityCheck) {
const buff32 = new Uint32Array(
this.witnessSize * this.n32 + this.n32 + 11
);
const buff = new Uint8Array(buff32.buffer);
await this._doCalculateWitness(input, sanityCheck);
//"wtns"
buff[0] = "w".charCodeAt(0);
buff[1] = "t".charCodeAt(0);
buff[2] = "n".charCodeAt(0);
buff[3] = "s".charCodeAt(0);
//version 2
buff32[1] = 2;
//number of sections: 2
buff32[2] = 2;
//id section 1
buff32[3] = 1;
const n8 = this.n32 * 4;
//id section 1 length in 64bytes
const idSection1length = 8 + n8;
const idSection1lengthHex = idSection1length.toString(16);
buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
//this.n32
buff32[6] = n8;
//prime number
this.instance.exports.getRawPrime();
var pos = 7;
for (let j = 0; j < this.n32; j++) {
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
}
pos += this.n32;
// witness size
buff32[pos] = this.witnessSize;
pos++;
//id section 2
buff32[pos] = 2;
pos++;
// section 2 length
const idSection2length = n8 * this.witnessSize;
const idSection2lengthHex = idSection2length.toString(16);
buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
pos += 2;
for (let i = 0; i < this.witnessSize; i++) {
this.instance.exports.getWitness(i);
for (let j = 0; j < this.n32; j++) {
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
}
pos += this.n32;
}
return buff;
}
}
function toArray32(rem, size) {
const res = []; //new Uint32Array(size); //has no unshift
const radix = BigInt(0x100000000);
while (rem) {
res.unshift(Number(rem % radix));
rem = rem / radix;
}
if (size) {
var i = size - res.length;
while (i > 0) {
res.unshift(0);
i--;
}
}
return res;
}
function fromArray32(arr) {
//returns a BigInt
var res = BigInt(0);
const radix = BigInt(0x100000000);
for (let i = 0; i < arr.length; i++) {
res = res * radix + BigInt(arr[i]);
}
return res;
}
function flatArray(a) {
var res = [];
fillArray(res, a);
return res;
function fillArray(res, a) {
if (Array.isArray(a)) {
for (let i = 0; i < a.length; i++) {
fillArray(res, a[i]);
}
} else {
res.push(a);
}
}
}
function fnvHash(str) {
const uint64_max = BigInt(2) ** BigInt(64);
let hash = BigInt("0xCBF29CE484222325");
for (var i = 0; i < str.length; i++) {
hash ^= BigInt(str[i].charCodeAt());
hash *= BigInt(0x100000001b3);
hash %= uint64_max;
}
let shash = hash.toString(16);
let n = 16 - shash.length;
shash = "0".repeat(n).concat(shash);
return shash;
}
// Make it globally available
global.witnessCalculatorBuilder = builder;
})(typeof self !== "undefined" ? self : window);

View File

@@ -29,7 +29,15 @@ mod tests {
}
export function initWitnessCalculator(jsCode) {
eval(jsCode);
const processedCode = jsCode
.replace(/export\s+async\s+function\s+builder/, 'async function builder')
.replace(/export\s*\{\s*builder\s*\};?/g, '');
const moduleFunc = new Function(processedCode + '\nreturn { builder };');
const witnessCalculatorModule = moduleFunc();
window.witnessCalculatorBuilder = witnessCalculatorModule.builder;
if (typeof window.witnessCalculatorBuilder !== 'function') {
return false;
}
@@ -63,7 +71,7 @@ mod tests {
async fn calculateWitness(circom_data: &[u8], inputs: Object) -> Result<JsValue, JsValue>;
}
const WITNESS_CALCULATOR_JS: &str = include_str!("../resources/witness_calculator_browser.js");
const WITNESS_CALCULATOR_JS: &str = include_str!("../resources/witness_calculator.js");
const ARKZKEY_BYTES: &[u8] =
include_bytes!("../../rln/resources/tree_height_20/rln_final.arkzkey");
@@ -89,7 +97,7 @@ mod tests {
.expect("Failed to initialize thread pool");
}
// Initialize the witness calculator
// Initialize witness calculator
initWitnessCalculator(WITNESS_CALCULATOR_JS)
.expect("Failed to initialize witness calculator");

View File

@@ -18,22 +18,39 @@ mod tests {
OptimalMerkleProof, OptimalMerkleTree, ZerokitMerkleProof, ZerokitMerkleTree,
};
const WITNESS_CALCULATOR_JS: &str = include_str!("../resources/witness_calculator.js");
#[wasm_bindgen(inline_js = r#"
const fs = require("fs");
let witnessCalculatorModule = null;
module.exports = {
initWitnessCalculator: function(code) {
const processedCode = code
.replace(/export\s+async\s+function\s+builder/, 'async function builder')
.replace(/export\s*\{\s*builder\s*\};?/g, '');
const moduleFunc = new Function(processedCode + '\nreturn { builder };');
witnessCalculatorModule = moduleFunc();
if (typeof witnessCalculatorModule.builder !== 'function') {
return false;
}
return true;
},
readFile: function (path) {
return fs.readFileSync(path);
},
calculateWitness: async function (circom_path, inputs) {
const wc = require("resources/witness_calculator_node.js");
const wasmFile = fs.readFileSync(circom_path);
const wasmFileBuffer = wasmFile.slice(
wasmFile.byteOffset,
wasmFile.byteOffset + wasmFile.byteLength
);
const witnessCalculator = await wc(wasmFileBuffer);
const witnessCalculator = await witnessCalculatorModule.builder(wasmFileBuffer);
const calculatedWitness = await witnessCalculator.calculateWitness(
inputs,
false
@@ -45,6 +62,9 @@ mod tests {
};
"#)]
extern "C" {
#[wasm_bindgen(catch)]
fn initWitnessCalculator(code: &str) -> Result<bool, JsValue>;
#[wasm_bindgen(catch)]
fn readFile(path: &str) -> Result<Uint8Array, JsValue>;
@@ -58,6 +78,10 @@ mod tests {
#[wasm_bindgen_test]
pub async fn rln_wasm_benchmark() {
// Initialize witness calculator
initWitnessCalculator(WITNESS_CALCULATOR_JS)
.expect("Failed to initialize witness calculator");
let mut results = String::from("\nbenchmarks:\n");
let iterations = 10;

View File

@@ -30,7 +30,7 @@ ark-serialize = { version = "0.5.0", default-features = false }
thiserror = "2.0.12"
# utilities
rayon = { version = "1.7.0" }
rayon = { version = "1.10.0", optional = true }
byteorder = "1.5.0"
cfg-if = "1.0"
num-bigint = { version = "0.4.6", default-features = false, features = ["std"] }
@@ -58,6 +58,7 @@ criterion = { version = "0.7.0", features = ["html_reports"] }
default = ["parallel", "pmtree-ft"]
stateless = []
parallel = [
"rayon",
"utils/parallel",
"ark-ff/parallel",
"ark-ec/parallel",

View File

@@ -14,12 +14,13 @@ bench = false
[dependencies]
ark-ff = { version = "0.5.0", default-features = false }
num-bigint = { version = "0.4.6", default-features = false }
pmtree = { package = "vacp2p_pmtree", version = "2.0.2", optional = true }
# pmtree = { package = "vacp2p_pmtree", version = "2.0.2", optional = true }
pmtree = { git = "https://github.com/vacp2p/pmtree", branch = "upgrade-rayon-version", package = "vacp2p_pmtree", optional = true }
sled = "0.34.7"
serde_json = "1.0.141"
lazy_static = "1.5.0"
hex = "0.4.3"
rayon = "1.7.0"
rayon = "1.10.0"
thiserror = "2.0"
[dev-dependencies]