Skip to content

Commit

Permalink
feat: range tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalakkal committed Nov 9, 2023
1 parent 3f468a2 commit 11d63e7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 27 deletions.
27 changes: 3 additions & 24 deletions halo2-lib-js/tests/gate.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import path from "path";
import { Halo2Lib } from "../src/halo2lib";
import { existsSync, mkdirSync, writeFileSync } from 'fs';

const config = {
k: 10,
numAdvice: 20,
numLookupAdvice: 3,
numInstance: 1,
numLookupBits: 9,
numVirtualInstance: 1,
}
import { writeCircuitToFile } from "./utils";

const writeCircuitToFile = (circuit: string, relativePath: string) => {
const configStr = `export const config = ${JSON.stringify(config, null, 4)}\n`;
const inputStr = `export const inputs = {}\n`;
const filePath = path.resolve(__dirname, "./circuits", relativePath);
const folderPath = path.dirname(filePath);
if (!existsSync(folderPath)) {
mkdirSync(folderPath, { recursive: true });
}
writeFileSync(filePath, configStr + inputStr + circuit);
console.log(`Wrote circuit to ${filePath}`);
}

const buildGateTest = (inputs: number[], func: string) => {
return `
Expand All @@ -38,12 +17,12 @@ const buildGateTestFn = (name: string, fn: (halo2Lib: Halo2Lib, inputs: null) =>

const gateTest = (inputs: number[], func: string) => {
const circuit = buildGateTest(inputs, func);
writeCircuitToFile(circuit, `${func}.ts`);
writeCircuitToFile(circuit, `${func}.gate.ts`);
}

const gateTestFn = (name: string, fn: (halo2Lib: Halo2Lib, inputs: null) => void) => {
const circuit = buildGateTestFn(name, fn);
writeCircuitToFile(circuit, `${name}.ts`);
writeCircuitToFile(circuit, `${name}.gate.ts`);
}

gateTest([15, 10], "add");
Expand Down
37 changes: 37 additions & 0 deletions halo2-lib-js/tests/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Halo2Lib } from "../src/halo2lib";
import { writeCircuitToFile } from "./utils";

const buildGateTestFn = (fn: (halo2Lib: Halo2Lib, inputs: null) => void) => {
return `//@ts-ignore\nexport const circuit = async ${fn.toString()}`;
}

const rangeTest = (name: string, fn: (halo2Lib: Halo2Lib, inputs: null) => void) => {
const circuit = buildGateTestFn(fn);
writeCircuitToFile(circuit, `${name}.range.ts`);
}

rangeTest("rangeCheck", (halo2Lib, _) => {
const { witness, rangeCheck } = halo2Lib;
rangeCheck(witness(15141), 128)
})

rangeTest("checkLessThan", (halo2Lib, _) => {
const { witness, checkLessThan } = halo2Lib;
checkLessThan(witness(10), witness(15), "8")
})

rangeTest("isLessThan", (halo2Lib, _) => {
const { witness, isLessThan } = halo2Lib;
isLessThan(witness(10), witness(15), "8")
})

rangeTest("divModVar", (halo2Lib, _) => {
const { witness, div } = halo2Lib;
div(witness(90), witness(50), "12", "12")
});

rangeTest("divModVar.1", (halo2Lib, _) => {
const { witness, mod } = halo2Lib;
mod(witness(90), witness(50), "12", "12")
});

3 changes: 2 additions & 1 deletion halo2-lib-js/tests/test_vk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def test_vk(filename):
file_path = os.path.join(circuit_dir, filename)
if os.path.isfile(file_path):
test_name = camel_to_snake(filename.split('.')[0])
rust_test = "tests::gate::test_" + test_name
test_type = filename.split('.')[-2]
rust_test = "tests::" + test_type + "::test_" + test_name
print("Testing " + test_name)
subprocess.run(['halo2-wasm', 'keygen', file_path], stdout = subprocess.DEVNULL)
subprocess.run(['cargo', 'test', rust_test, "--quiet", "--", "--exact"], stdout = subprocess.DEVNULL)
Expand Down
5 changes: 5 additions & 0 deletions halo2-lib-js/tests/test_vk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rm -rf tests/circuits
mkdir tests/circuits
pnpm ts-node tests/range.ts
pnpm ts-node tests/gate.ts
python3 tests/test_vk.py
23 changes: 23 additions & 0 deletions halo2-lib-js/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import path from "path";

const config = {
k: 10,
numAdvice: 20,
numLookupAdvice: 3,
numInstance: 1,
numLookupBits: 9,
numVirtualInstance: 1,
}

export const writeCircuitToFile = (circuit: string, relativePath: string) => {
const configStr = `export const config = ${JSON.stringify(config, null, 4)}\n`;
const inputStr = `export const inputs = {}\n`;
const filePath = path.resolve(__dirname, "./circuits", relativePath);
const folderPath = path.dirname(filePath);
if (!existsSync(folderPath)) {
mkdirSync(folderPath, { recursive: true });
}
writeFileSync(filePath, configStr + inputStr + circuit);
console.log(`Wrote circuit to ${filePath}`);
}
1 change: 0 additions & 1 deletion halo2-lib-js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"module": "commonjs",
"target": "es2020",
"moduleResolution": "node",
"rootDir": "src",
"baseUrl": "src",
"outDir": "dist",
"lib": ["esnext", "DOM"],
Expand Down
14 changes: 13 additions & 1 deletion halo2-wasm/src/tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,23 @@ range_test!(
}
);

//performs range check also since that's what halo2-lib-js does as well
//and `check_less_than` assumes that the inputs are already range checked
range_test!(
test_check_less_than,
("10", "15", 8),
|ctx: &mut Context<Fr>, chip: &RangeChip<Fr>, inputs: (&str, &str, usize)| {
let a = ctx.load_witness(Fr::from_str_vartime(inputs.0).unwrap());
let b = ctx.load_witness(Fr::from_str_vartime(inputs.1).unwrap());
chip.range_check(ctx, a, inputs.2);
chip.range_check(ctx, b, inputs.2);
chip.check_less_than(ctx, a, b, inputs.2);
},
|ctx: &mut Halo2LibWasm, inputs: (&str, &str, usize)| {
let a = ctx.witness(inputs.0);
let b = ctx.witness(inputs.0);
let b = ctx.witness(inputs.1);
ctx.range_check(a, &inputs.2.to_string());
ctx.range_check(b, &inputs.2.to_string());
ctx.check_less_than(a, b, &inputs.2.to_string());
}
);
Expand All @@ -64,17 +70,23 @@ range_test!(
}
);

//performs range check also since that's what halo2-lib-js does as well
//and `is_less_than` assumes that the inputs are already range checked
range_test!(
test_is_less_than,
&["10", "15", "8"],
|ctx: &mut Context<Fr>, chip: &RangeChip<Fr>, inputs: &[&str]| {
let a = ctx.load_witness(Fr::from_str_vartime(inputs[0]).unwrap());
let b = ctx.load_witness(Fr::from_str_vartime(inputs[1]).unwrap());
chip.range_check(ctx, a, inputs[2].parse().unwrap());
chip.range_check(ctx, b, inputs[2].parse().unwrap());
chip.is_less_than(ctx, a, b, inputs[2].parse().unwrap());
},
|ctx: &mut Halo2LibWasm, inputs: &[&str]| {
let a = ctx.witness(inputs[0]);
let b = ctx.witness(inputs[1]);
ctx.range_check(a, inputs[2]);
ctx.range_check(b, inputs[2]);
ctx.is_less_than(a, b, inputs[2]);
}
);
Expand Down

0 comments on commit 11d63e7

Please sign in to comment.