Skip to content

Commit

Permalink
feat: cache Axiom SRS (trusted setup)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang committed Jan 15, 2024
1 parent 057642f commit 025948b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
6 changes: 3 additions & 3 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axiom-crypto/halo2-wasm-cli",
"version": "0.1.6-rc.0",
"version": "0.1.7-alpha.0",
"description": "Halo2 Javascript library",
"main": "index.js",
"scripts": {
Expand All @@ -18,8 +18,8 @@
"author": "Intrinsic Technologies",
"license": "ISC",
"dependencies": {
"@axiom-crypto/halo2-lib-js": "0.2.14-rc.0",
"@axiom-crypto/halo2-wasm": "0.2.11-rc.1",
"@axiom-crypto/halo2-lib-js": "0.2.15-alpha.0",
"@axiom-crypto/halo2-wasm": "0.2.12-alpha.0",
"commander": "^11.1.0",
"typescript": "^5.2.2"
},
Expand Down
6 changes: 3 additions & 3 deletions halo2-lib-js/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@axiom-crypto/halo2-lib-js",
"version": "0.2.14-rc.0",
"version": "0.2.15-alpha.0",
"description": "Halo2 Javascript library",
"main": "index.js",
"scripts": {
"build": "rm -rf ./dist && tsc && node ./scripts/postTsc.js && pnpm build:docs",
"build:docs": "./node_modules/.bin/dts-bundle-generator ./dist/halo2lib/functions.d.ts -o src/shared/build.d.ts && node ./scripts/convertDTs.js && rm -rf ./src/shared/build.d.ts",
"test:vk": "./tests/test_vk.sh",
"test:constant": "./tests/test_constant.sh"
"test:constant": "./tests/test_constant.sh"
},
"keywords": [
"axiom",
Expand All @@ -32,7 +32,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@axiom-crypto/halo2-wasm": "0.2.11-rc.1",
"@axiom-crypto/halo2-wasm": "0.2.12-alpha.0",
"ethers": "^6.8.0",
"prettier": "1.18.2"
},
Expand Down
54 changes: 42 additions & 12 deletions halo2-wasm/js/kzg/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import * as os from "os";
import * as path from "path";
import * as fs from "fs";

function fetchAndConvertToUint8Array(url: string): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
// Check if running in Node.js environment
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
const https = require('https');
if (
typeof process !== "undefined" &&
process.versions &&
process.versions.node
) {
const https = require("https");
https.get(url, (res: any) => {
let chunks: any[] = [];
res.on('data', (chunk: any) => chunks.push(chunk));
res.on('end', () => {
res.on("data", (chunk: any) => chunks.push(chunk));
res.on("end", () => {
let binaryData = Buffer.concat(chunks);
resolve(new Uint8Array(binaryData));
});
res.on('error', reject);
res.on("error", reject);
});
}
// Check if running in browser or web worker environment
else if (typeof window !== 'undefined' || typeof self !== 'undefined') {
else if (typeof window !== "undefined" || typeof self !== "undefined") {
fetch(url)
.then(response => response.arrayBuffer())
.then(buffer => {
.then((response) => response.arrayBuffer())
.then((buffer) => {
resolve(new Uint8Array(buffer));
})
.catch(reject);
} else {
reject(new Error('Environment not supported'));
reject(new Error("Environment not supported"));
}
});
}
Expand All @@ -34,11 +42,33 @@ const convertBase64ToUint8Arr = (b64str: string) => {
buf[i] = ch.charCodeAt(0);
});
return buf;
}
};

export const getKzgParams = async (k: number): Promise<Uint8Array> => {
const home = os.homedir();
const axiomSrsPath = path.join(
home,
".axiom",
"srs",
"challenge_0085",
`kzg_bn254_${k}.srs`
);
const exists = fs.existsSync(axiomSrsPath);
if (exists) {
const buffer = fs.readFileSync(axiomSrsPath);
return new Uint8Array(buffer);
}
const folderPath = path.dirname(axiomSrsPath);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
if (k < 6 || k > 19) {
throw new Error(`k=${k} is not supported`);
}
return fetchAndConvertToUint8Array(`https://axiom-crypto.s3.amazonaws.com/challenge_0085/kzg_bn254_${k}.srs`);
};
const srs = await fetchAndConvertToUint8Array(
`https://axiom-crypto.s3.amazonaws.com/challenge_0085/kzg_bn254_${k}.srs`
);
fs.writeFileSync(axiomSrsPath, srs);

return srs;
};
2 changes: 1 addition & 1 deletion halo2-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@axiom-crypto/halo2-wasm",
"description": "Halo2 wasm bindings",
"version": "0.2.11-rc.1",
"version": "0.2.12-alpha.0",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
Expand Down

0 comments on commit 025948b

Please sign in to comment.