Skip to content

Commit

Permalink
Cache SRS for js, halo2-lib-js export passthroughs
Browse files Browse the repository at this point in the history
  • Loading branch information
ytham committed Jan 17, 2024
1 parent 791ed25 commit 610b371
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 48 deletions.
3 changes: 2 additions & 1 deletion halo2-lib-js/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { RawCircuitInput, ConstantValue } from './types'
export { convertRawInput } from './utils'
export { convertRawInput } from './utils'
export { BaseCircuitScaffold } from "@axiom-crypto/halo2-wasm/shared/scaffold";
2 changes: 1 addition & 1 deletion halo2-lib-js/src/wasm/js.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "@axiom-crypto/halo2-wasm/js"
export * from "@axiom-crypto/halo2-wasm/js/"
2 changes: 1 addition & 1 deletion halo2-lib-js/src/wasm/web.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "@axiom-crypto/halo2-wasm/web"
export * from "@axiom-crypto/halo2-wasm/web/"
36 changes: 30 additions & 6 deletions halo2-wasm/js/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
import {
Halo2Wasm, initPanicHook, Halo2LibWasm, CircuitConfig,
Bn254FqPoint, Bn254G1AffinePoint, Bn254G2AffinePoint, JsCircuitBn254Fq2, JsCircuitBn254G1Affine, JsCircuitBn254G2Affine, JsCircuitSecp256k1Affine, JsCircuitValue256, Secp256k1AffinePoint
Halo2Wasm,
initPanicHook,
Halo2LibWasm,
CircuitConfig,
Bn254FqPoint,
Bn254G1AffinePoint,
Bn254G2AffinePoint,
JsCircuitBn254Fq2,
JsCircuitBn254G1Affine,
JsCircuitBn254G2Affine,
JsCircuitSecp256k1Affine,
JsCircuitValue256,
Secp256k1AffinePoint,
} from "../../pkg/js/halo2_wasm";
import { getKzgParams } from "./kzg";
import { DEFAULT_CIRCUIT_CONFIG } from "../shared";
import { BaseCircuitScaffold } from "../shared/scaffold";

export { CircuitConfig, DEFAULT_CIRCUIT_CONFIG, Halo2Wasm, Halo2LibWasm, getKzgParams };
export { Bn254FqPoint, Bn254G1AffinePoint, Bn254G2AffinePoint, JsCircuitBn254Fq2, JsCircuitBn254G1Affine, JsCircuitBn254G2Affine, JsCircuitSecp256k1Affine, JsCircuitValue256, Secp256k1AffinePoint };
export {
CircuitConfig,
DEFAULT_CIRCUIT_CONFIG,
Halo2Wasm,
Halo2LibWasm,
getKzgParams,
Bn254FqPoint,
Bn254G1AffinePoint,
Bn254G2AffinePoint,
JsCircuitBn254Fq2,
JsCircuitBn254G1Affine,
JsCircuitBn254G2Affine,
JsCircuitSecp256k1Affine,
JsCircuitValue256,
Secp256k1AffinePoint
};

export const getHalo2Wasm = () => {
initPanicHook();
Expand All @@ -22,12 +47,11 @@ export const getHalo2LibWasm = (halo2wasm: Halo2Wasm) => {

export abstract class CircuitScaffold extends BaseCircuitScaffold {
constructor(options?: { config?: CircuitConfig, shouldTime?: boolean }) {
super()
super();
this.halo2wasm = getHalo2Wasm();
this.config = options?.config ?? { ...DEFAULT_CIRCUIT_CONFIG };
this.shouldTime = options?.shouldTime ?? false;
this.loadedVk = false;
this.halo2wasm.config(this.config);
}

}
44 changes: 21 additions & 23 deletions halo2-wasm/js/js/kzg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@ import * as fs from "fs";
import { fetchAndConvertToUint8Array } from "../shared/utils";

export const getKzgParams = async (k: number): Promise<Uint8Array> => {
console.log("KZG: js");
// 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 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);

fs.writeFileSync(axiomSrsPath, srs);
return srs;
};
2 changes: 1 addition & 1 deletion halo2-wasm/js/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export const DEFAULT_CIRCUIT_CONFIG = Object.freeze({
numLookupAdvice: 1,
numInstance: 1,
numLookupBits: 13,
numVirtualInstance: 1
numVirtualInstance: 1,
});
11 changes: 7 additions & 4 deletions halo2-wasm/js/shared/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { CircuitConfig, Halo2Wasm } from "../../pkg/web/halo2_wasm";
import { getKzgParams } from "../web/kzg";
import { CircuitScaffoldContext } from "./types";

export abstract class BaseCircuitScaffold {
protected halo2wasm!: Halo2Wasm;
protected config: CircuitConfig;
protected shouldTime: boolean;
protected proof: Uint8Array | null = null;
protected loadedVk: boolean;
protected context: CircuitScaffoldContext;

protected timeStart(name: string) {
if (this.shouldTime) console.time(name);
Expand All @@ -16,13 +17,17 @@ export abstract class BaseCircuitScaffold {
if (this.shouldTime) console.timeEnd(name);
}

protected setContext(context: CircuitScaffoldContext) {
this.context = context;
}

newCircuitFromConfig(config: CircuitConfig) {
this.config = config;
this.halo2wasm.config(config);
}

async loadParams() {
const kzgParams = await getKzgParams(this.config.k);
const kzgParams = await this.context.getKzgParams(this.config.k);
this.halo2wasm.loadParams(kzgParams);
}

Expand Down Expand Up @@ -109,6 +114,4 @@ export abstract class BaseCircuitScaffold {
const blob = new Blob([proofHex], { type: "text/plain" });
return blob;
}


}
3 changes: 3 additions & 0 deletions halo2-wasm/js/shared/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface CircuitScaffoldContext {
getKzgParams: (k: number) => Promise<Uint8Array>;
}
2 changes: 0 additions & 2 deletions halo2-wasm/js/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export function fetchAndConvertToUint8Array(url: string): Promise<Uint8Array> {
}
// Check if running in browser or web worker environment
else if (typeof window !== "undefined" || typeof self !== "undefined") {
console.log("fetching SRS...");
fetch(url)
.then((response) => response.arrayBuffer())
.then((buffer) => {
console.log("got SRS successfully");
resolve(new Uint8Array(buffer));
})
.catch(reject);
Expand Down
35 changes: 30 additions & 5 deletions halo2-wasm/js/web/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import init, {
initThreadPool, initPanicHook, Halo2Wasm, Halo2LibWasm, CircuitConfig,
Bn254FqPoint, Bn254G1AffinePoint, Bn254G2AffinePoint, JsCircuitBn254Fq2, JsCircuitBn254G1Affine, JsCircuitBn254G2Affine, JsCircuitSecp256k1Affine, JsCircuitValue256, Secp256k1AffinePoint
initThreadPool,
initPanicHook,
Halo2Wasm,
Halo2LibWasm,
CircuitConfig,
Bn254FqPoint,
Bn254G1AffinePoint,
Bn254G2AffinePoint,
JsCircuitBn254Fq2,
JsCircuitBn254G1Affine,
JsCircuitBn254G2Affine,
JsCircuitSecp256k1Affine,
JsCircuitValue256,
Secp256k1AffinePoint,
} from "../../pkg/web/halo2_wasm";
import { getKzgParams } from "./kzg";
import { DEFAULT_CIRCUIT_CONFIG } from "../shared";
import { BaseCircuitScaffold } from "../shared/scaffold";

export { CircuitConfig, DEFAULT_CIRCUIT_CONFIG, Halo2Wasm, Halo2LibWasm, getKzgParams };
export { Bn254FqPoint, Bn254G1AffinePoint, Bn254G2AffinePoint, JsCircuitBn254Fq2, JsCircuitBn254G1Affine, JsCircuitBn254G2Affine, JsCircuitSecp256k1Affine, JsCircuitValue256, Secp256k1AffinePoint };
export {
CircuitConfig,
DEFAULT_CIRCUIT_CONFIG,
Halo2Wasm,
Halo2LibWasm,
getKzgParams,
Bn254FqPoint,
Bn254G1AffinePoint,
Bn254G2AffinePoint,
JsCircuitBn254Fq2,
JsCircuitBn254G1Affine,
JsCircuitBn254G2Affine,
JsCircuitSecp256k1Affine,
JsCircuitValue256,
Secp256k1AffinePoint
};

export const getHalo2Wasm = async (numThreads: number) => {
await init();
Expand All @@ -25,7 +51,6 @@ export const getHalo2LibWasm = (halo2wasm: Halo2Wasm) => {
}

export abstract class CircuitScaffold extends BaseCircuitScaffold {

constructor(options?: { config?: CircuitConfig, shouldTime?: boolean }) {
super();
this.config = options?.config ?? { ...DEFAULT_CIRCUIT_CONFIG };
Expand Down
5 changes: 1 addition & 4 deletions halo2-wasm/js/web/kzg.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { fetchAndConvertToUint8Array } from "../shared/utils";

export const getKzgParams = async (k: number): Promise<Uint8Array> => {
console.log("KZG: web");
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`)
console.log("KZG: web", srs);
return srs;
return await fetchAndConvertToUint8Array(`https://axiom-crypto.s3.amazonaws.com/challenge_0085/kzg_bn254_${k}.srs`)
};

0 comments on commit 610b371

Please sign in to comment.