diff --git a/halo2-lib-js/src/shared/index.ts b/halo2-lib-js/src/shared/index.ts index 4ef6a2c..a4226f0 100644 --- a/halo2-lib-js/src/shared/index.ts +++ b/halo2-lib-js/src/shared/index.ts @@ -1,2 +1,3 @@ export { RawCircuitInput, ConstantValue } from './types' -export { convertRawInput } from './utils' \ No newline at end of file +export { convertRawInput } from './utils' +export { BaseCircuitScaffold } from "@axiom-crypto/halo2-wasm/shared/scaffold"; \ No newline at end of file diff --git a/halo2-lib-js/src/wasm/js.ts b/halo2-lib-js/src/wasm/js.ts index dacc14b..f873499 100644 --- a/halo2-lib-js/src/wasm/js.ts +++ b/halo2-lib-js/src/wasm/js.ts @@ -1 +1 @@ -export * from "@axiom-crypto/halo2-wasm/js" \ No newline at end of file +export * from "@axiom-crypto/halo2-wasm/js/" \ No newline at end of file diff --git a/halo2-lib-js/src/wasm/web.ts b/halo2-lib-js/src/wasm/web.ts index e8925eb..eddacb3 100644 --- a/halo2-lib-js/src/wasm/web.ts +++ b/halo2-lib-js/src/wasm/web.ts @@ -1 +1 @@ -export * from "@axiom-crypto/halo2-wasm/web" \ No newline at end of file +export * from "@axiom-crypto/halo2-wasm/web/" \ No newline at end of file diff --git a/halo2-wasm/js/js/index.ts b/halo2-wasm/js/js/index.ts index 21427fd..1f69243 100644 --- a/halo2-wasm/js/js/index.ts +++ b/halo2-wasm/js/js/index.ts @@ -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(); @@ -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); } - } diff --git a/halo2-wasm/js/js/kzg.ts b/halo2-wasm/js/js/kzg.ts index 8b02e54..3c18771 100644 --- a/halo2-wasm/js/js/kzg.ts +++ b/halo2-wasm/js/js/kzg.ts @@ -4,31 +4,29 @@ import * as fs from "fs"; import { fetchAndConvertToUint8Array } from "../shared/utils"; export const getKzgParams = async (k: number): Promise => { - 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; }; \ No newline at end of file diff --git a/halo2-wasm/js/shared/index.ts b/halo2-wasm/js/shared/index.ts index 94b90b5..6ed984c 100644 --- a/halo2-wasm/js/shared/index.ts +++ b/halo2-wasm/js/shared/index.ts @@ -4,5 +4,5 @@ export const DEFAULT_CIRCUIT_CONFIG = Object.freeze({ numLookupAdvice: 1, numInstance: 1, numLookupBits: 13, - numVirtualInstance: 1 + numVirtualInstance: 1, }); \ No newline at end of file diff --git a/halo2-wasm/js/shared/scaffold.ts b/halo2-wasm/js/shared/scaffold.ts index 8823841..5ea4c0c 100644 --- a/halo2-wasm/js/shared/scaffold.ts +++ b/halo2-wasm/js/shared/scaffold.ts @@ -1,5 +1,5 @@ import { CircuitConfig, Halo2Wasm } from "../../pkg/web/halo2_wasm"; -import { getKzgParams } from "../web/kzg"; +import { CircuitScaffoldContext } from "./types"; export abstract class BaseCircuitScaffold { protected halo2wasm!: Halo2Wasm; @@ -7,6 +7,7 @@ export abstract class BaseCircuitScaffold { protected shouldTime: boolean; protected proof: Uint8Array | null = null; protected loadedVk: boolean; + protected context: CircuitScaffoldContext; protected timeStart(name: string) { if (this.shouldTime) console.time(name); @@ -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); } @@ -109,6 +114,4 @@ export abstract class BaseCircuitScaffold { const blob = new Blob([proofHex], { type: "text/plain" }); return blob; } - - } diff --git a/halo2-wasm/js/shared/types.ts b/halo2-wasm/js/shared/types.ts new file mode 100644 index 0000000..2996b3e --- /dev/null +++ b/halo2-wasm/js/shared/types.ts @@ -0,0 +1,3 @@ +export interface CircuitScaffoldContext { + getKzgParams: (k: number) => Promise; +} \ No newline at end of file diff --git a/halo2-wasm/js/shared/utils.ts b/halo2-wasm/js/shared/utils.ts index 82407db..45eadf0 100644 --- a/halo2-wasm/js/shared/utils.ts +++ b/halo2-wasm/js/shared/utils.ts @@ -19,11 +19,9 @@ export function fetchAndConvertToUint8Array(url: string): Promise { } // 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); diff --git a/halo2-wasm/js/web/index.ts b/halo2-wasm/js/web/index.ts index c746e55..e873cca 100644 --- a/halo2-wasm/js/web/index.ts +++ b/halo2-wasm/js/web/index.ts @@ -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(); @@ -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 }; diff --git a/halo2-wasm/js/web/kzg.ts b/halo2-wasm/js/web/kzg.ts index 8369eaf..334a767 100644 --- a/halo2-wasm/js/web/kzg.ts +++ b/halo2-wasm/js/web/kzg.ts @@ -1,11 +1,8 @@ import { fetchAndConvertToUint8Array } from "../shared/utils"; export const getKzgParams = async (k: number): Promise => { - 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`) }; \ No newline at end of file