diff --git a/halo2-wasm/Cargo.toml b/halo2-wasm/Cargo.toml index defcd99..fe1babd 100644 --- a/halo2-wasm/Cargo.toml +++ b/halo2-wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "halo2-wasm" -version = "0.2.11-rc.1" +version = "0.2.12-alpha.0" edition = "2021" [lib] diff --git a/halo2-wasm/js/js/index.ts b/halo2-wasm/js/js/index.ts index f69b93f..21427fd 100644 --- a/halo2-wasm/js/js/index.ts +++ b/halo2-wasm/js/js/index.ts @@ -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"; diff --git a/halo2-wasm/js/js/kzg.ts b/halo2-wasm/js/js/kzg.ts new file mode 100644 index 0000000..5f03e49 --- /dev/null +++ b/halo2-wasm/js/js/kzg.ts @@ -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 => { + 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; +}; \ No newline at end of file diff --git a/halo2-wasm/js/shared/scaffold.ts b/halo2-wasm/js/shared/scaffold.ts index f2820ea..8823841 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 "../kzg"; +import { getKzgParams } from "../web/kzg"; export abstract class BaseCircuitScaffold { protected halo2wasm!: Halo2Wasm; diff --git a/halo2-wasm/js/kzg/index.ts b/halo2-wasm/js/shared/utils.ts similarity index 53% rename from halo2-wasm/js/kzg/index.ts rename to halo2-wasm/js/shared/utils.ts index 53c947d..45eadf0 100644 --- a/halo2-wasm/js/kzg/index.ts +++ b/halo2-wasm/js/shared/utils.ts @@ -1,8 +1,4 @@ -import * as os from "os"; -import * as path from "path"; -import * as fs from "fs"; - -function fetchAndConvertToUint8Array(url: string): Promise { +export function fetchAndConvertToUint8Array(url: string): Promise { return new Promise((resolve, reject) => { // Check if running in Node.js environment if ( @@ -35,7 +31,7 @@ function fetchAndConvertToUint8Array(url: string): Promise { }); } -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) => { @@ -43,32 +39,3 @@ const convertBase64ToUint8Arr = (b64str: string) => { }); return buf; }; - -export const getKzgParams = async (k: number): Promise => { - 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; -}; diff --git a/halo2-wasm/js/web/index.ts b/halo2-wasm/js/web/index.ts index eb834c5..c746e55 100644 --- a/halo2-wasm/js/web/index.ts +++ b/halo2-wasm/js/web/index.ts @@ -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"; diff --git a/halo2-wasm/js/web/kzg.ts b/halo2-wasm/js/web/kzg.ts new file mode 100644 index 0000000..137c8d9 --- /dev/null +++ b/halo2-wasm/js/web/kzg.ts @@ -0,0 +1,8 @@ +import { fetchAndConvertToUint8Array } from "../shared/utils"; + +export const getKzgParams = async (k: number): Promise => { + 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`); +}; \ No newline at end of file