Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache SRS, split js and web #55

Merged
merged 15 commits into from
Jan 18, 2024
25 changes: 14 additions & 11 deletions .github/workflows/halo2-browser-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Build halo2-wasm
working-directory: halo2-wasm
run: |
rustup toolchain install nightly-2023-08-12-x86_64-unknown-linux-gnu
rustup component add rust-src --toolchain nightly-2023-08-12-x86_64-unknown-linux-gnu
cargo build --target=x86_64-unknown-linux-gnu --verbose
- name: Build halo2-lib-js
working-directory: halo2-lib-js
run: |
npm install
npm add @axiom-crypto/[email protected]
- name: Set all packages to local and build
working-directory: ./
run: ./scripts/toggleLocal.sh ci
# - name: Build halo2-wasm
# working-directory: halo2-wasm
# run: |
# rustup toolchain install nightly-2023-08-12-x86_64-unknown-linux-gnu
# rustup component add rust-src --toolchain nightly-2023-08-12-x86_64-unknown-linux-gnu
# cargo build --target=x86_64-unknown-linux-gnu --verbose
# - name: Build halo2-lib-js
# working-directory: halo2-lib-js
# run: |
# npm install
# npm add @axiom-crypto/[email protected]
- name: Test halo2-lib-js/halo2-wasm VK equivalence
working-directory: halo2-lib-js
run: npm run test:vk
Expand Down
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
8 changes: 7 additions & 1 deletion cli/scripts/toggleLocal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const fs = require('fs');
const packageJson = require('../package.json');

let ci = false;
if (process.argv[2] === "ci") {
ci = true;
}
const local = ci ? "file:" : "link:";

const main = () => {
const packageJsonCopy = { ...packageJson };
packageJsonCopy.dependencies['@axiom-crypto/halo2-wasm'] = 'link:../halo2-wasm/pkg';
packageJsonCopy.dependencies['@axiom-crypto/halo2-wasm'] = `${local}../halo2-wasm/pkg`;
fs.writeFileSync('./package.json', JSON.stringify(packageJsonCopy, null, 2));
}

Expand Down
8 changes: 4 additions & 4 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.16-rc2.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,11 +32,11 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@axiom-crypto/halo2-wasm": "0.2.11-rc.1",
"@axiom-crypto/halo2-wasm": "link:../halo2-wasm/pkg",
"ethers": "^6.8.0",
"prettier": "1.18.2"
},
"publishConfig": {
"directory": "dist"
}
}
}
8 changes: 2 additions & 6 deletions halo2-lib-js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion halo2-lib-js/scripts/toggleLocal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const fs = require('fs');
const packageJson = require('../package.json');

let ci = false;
if (process.argv[2] === "ci") {
ci = true;
}
const local = ci ? "file:" : "link:";

const main = () => {
const packageJsonCopy = { ...packageJson };
packageJsonCopy.dependencies['@axiom-crypto/halo2-wasm'] = 'link:../halo2-wasm/pkg';
packageJsonCopy.dependencies['@axiom-crypto/halo2-wasm'] = `${local}../halo2-wasm/pkg`;
fs.writeFileSync('./package.json', JSON.stringify(packageJsonCopy, null, 2));
}

Expand Down
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/"
10 changes: 8 additions & 2 deletions halo2-repl/scripts/toggleLocal.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const fs = require('fs');
const packageJson = require('../package.json');

let ci = false;
if (process.argv[2] === "ci") {
ci = true;
}
const local = ci ? "file:" : "link:";

const main = () => {
const packageJsonCopy = { ...packageJson };
packageJsonCopy.dependencies['@axiom-crypto/halo2-wasm'] = 'link:../halo2-wasm/pkg';
packageJsonCopy.dependencies['@axiom-crypto/halo2-lib-js'] = 'link:../halo2-lib-js/dist';
packageJsonCopy.dependencies['@axiom-crypto/halo2-wasm'] = `${local}../halo2-wasm/pkg`;
packageJsonCopy.dependencies['@axiom-crypto/halo2-lib-js'] = `${local}../halo2-lib-js/dist`;
fs.writeFileSync('./package.json', JSON.stringify(packageJsonCopy, null, 2));
}

Expand Down
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.13-rc2.0"
edition = "2021"

[lib]
Expand Down
38 changes: 31 additions & 7 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 { 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);
}

}
32 changes: 32 additions & 0 deletions halo2-wasm/js/js/kzg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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;
};
44 changes: 0 additions & 44 deletions halo2-wasm/js/kzg/index.ts

This file was deleted.

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 "../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>;
}
Loading
Loading