Skip to content

Commit

Permalink
WIP: split kzg params getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ytham committed Jan 15, 2024
1 parent 025948b commit 78677fd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion halo2-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "halo2-wasm"
version = "0.2.11-rc.1"
version = "0.2.12-alpha.0"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion halo2-wasm/js/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Halo2Wasm, initPanicHook, Halo2LibWasm, CircuitConfig,
Bn254FqPoint, Bn254G1AffinePoint, Bn254G2AffinePoint, JsCircuitBn254Fq2, JsCircuitBn254G1Affine, JsCircuitBn254G2Affine, JsCircuitSecp256k1Affine, JsCircuitValue256, Secp256k1AffinePoint
} from "../../pkg/js/halo2_wasm";
import { getKzgParams } from "../kzg";
import { getKzgParams } from "./kzg";
import { DEFAULT_CIRCUIT_CONFIG } from "../shared";
import { BaseCircuitScaffold } from "../shared/scaffold";

Expand Down
33 changes: 33 additions & 0 deletions halo2-wasm/js/js/kzg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as os from "os";
import * as path from "path";
import * as fs from "fs";
import { fetchAndConvertToUint8Array } from "../shared/utils";

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`);
}
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/js/shared/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CircuitConfig, Halo2Wasm } from "../../pkg/web/halo2_wasm";
import { getKzgParams } from "../kzg";
import { getKzgParams } from "../web/kzg";

export abstract class BaseCircuitScaffold {
protected halo2wasm!: Halo2Wasm;
Expand Down
37 changes: 2 additions & 35 deletions halo2-wasm/js/kzg/index.ts → halo2-wasm/js/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import * as os from "os";
import * as path from "path";
import * as fs from "fs";

function fetchAndConvertToUint8Array(url: string): Promise<Uint8Array> {
export function fetchAndConvertToUint8Array(url: string): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
// Check if running in Node.js environment
if (
Expand Down Expand Up @@ -35,40 +31,11 @@ function fetchAndConvertToUint8Array(url: string): Promise<Uint8Array> {
});
}

const convertBase64ToUint8Arr = (b64str: string) => {
export const convertBase64ToUint8Arr = (b64str: string) => {
const binstr = atob(b64str);
const buf = new Uint8Array(binstr.length);
Array.prototype.forEach.call(binstr, (ch, i) => {
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`);
}
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/js/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import init, {
initThreadPool, initPanicHook, Halo2Wasm, Halo2LibWasm, CircuitConfig,
Bn254FqPoint, Bn254G1AffinePoint, Bn254G2AffinePoint, JsCircuitBn254Fq2, JsCircuitBn254G1Affine, JsCircuitBn254G2Affine, JsCircuitSecp256k1Affine, JsCircuitValue256, Secp256k1AffinePoint
} from "../../pkg/web/halo2_wasm";
import { getKzgParams } from "../kzg";
import { getKzgParams } from "./kzg";
import { DEFAULT_CIRCUIT_CONFIG } from "../shared";
import { BaseCircuitScaffold } from "../shared/scaffold";

Expand Down
8 changes: 8 additions & 0 deletions halo2-wasm/js/web/kzg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { fetchAndConvertToUint8Array } from "../shared/utils";

export const getKzgParams = async (k: number): Promise<Uint8Array> => {
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`);
};

0 comments on commit 78677fd

Please sign in to comment.