Skip to content

Commit fa7c1db

Browse files
committed
test: trigger two processes each for one lazy mode
1 parent f50296b commit fa7c1db

File tree

1 file changed

+56
-33
lines changed

1 file changed

+56
-33
lines changed

src/lib/proof-system/lazy-mode.unit-test.ts

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import { Random } from '../testing/property.js';
66
import { assert } from '../provable/gadgets/common.js';
77
import { Gadgets } from '../provable/gadgets/gadgets.js';
88
import { wasm } from '../../bindings.js';
9+
import { spawn } from 'child_process';
10+
import { fileURLToPath } from 'url';
11+
import { dirname, join, basename } from 'path';
912

13+
// Path resolution for subprocess execution
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = dirname(__filename);
16+
const scriptPath = join(__dirname, basename(__filename));
1017

1118
function getMemory() {
1219
return {
@@ -19,49 +26,65 @@ let uint = (n: number | bigint): Spec<bigint, Field> => {
1926
return fieldWithRng(Random.bignat((1n << BigInt(n)) - 1n));
2027
};
2128

22-
23-
// dummy circuit
24-
let LazyMode = ZkProgram({
25-
name: 'lazy-mode',
26-
methods: {
27-
baseCase: {
28-
privateInputs: [Field],
29-
async method(v: Field) {
30-
for (let i = 0; i < 1 << 15; i++) {
31-
let w = v.add(new Field(i));
32-
Gadgets.rangeCheck64(w);
33-
}
34-
},
29+
// Dummy circuit
30+
let LazyMode = ZkProgram({
31+
name: 'lazy-mode',
32+
methods: {
33+
baseCase: {
34+
privateInputs: [Field],
35+
async method(v: Field) {
36+
for (let i = 0; i < 1 << 15; i++) {
37+
let w = v.add(new Field(i));
38+
Gadgets.rangeCheck64(w);
39+
}
3540
},
3641
},
37-
});
38-
42+
},
43+
});
3944

40-
async function testLazyMode(lazyMode: boolean) {
45+
export async function testLazyMode(lazyMode: boolean) {
46+
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory before compilation`, getMemory());
4147

42-
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory before compilation`, getMemory());
48+
await LazyMode.compile({
49+
lazyMode,
50+
cache: Cache.None,
51+
forceRecompile: true,
52+
});
4353

44-
await LazyMode.compile({
45-
lazyMode,
46-
cache: Cache.None,
47-
forceRecompile: true,
48-
});
54+
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory after compilation`, getMemory());
55+
console.log(' ');
56+
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory before proof`, getMemory());
4957

50-
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory after compilation`, getMemory());
51-
console.log(' ');
52-
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory before proof`, getMemory());
58+
let { proof } = await LazyMode.baseCase(new Field(1n));
5359

54-
let { proof } = await LazyMode.baseCase(new Field(1n));
60+
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory after proof`, getMemory());
61+
console.log(' ');
5562

56-
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory after proof`, getMemory());
57-
console.log(' ');
63+
let isValid = await LazyMode.verify(proof);
5864

59-
let isValid = await LazyMode.verify(proof);
65+
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory after verify`, getMemory());
6066

61-
console.log(`(${lazyMode ? 'Lazy' : 'Eager'}) Memory after verify`, getMemory());
67+
assert(isValid);
68+
}
6269

63-
assert(isValid);
70+
// If launched as subprocess: run the actual test
71+
if (process.argv[2] === 'true' || process.argv[2] === 'false') {
72+
const lazyMode = process.argv[2] === 'true';
73+
await testLazyMode(lazyMode);
74+
process.exit(0);
75+
}
6476

65-
}
77+
// Parent process logic: spawn two subprocesses
78+
function runSubprocess(lazyMode: boolean): Promise<void> {
79+
return new Promise((resolve, reject) => {
80+
const child = spawn('node', [scriptPath, String(lazyMode)], {
81+
stdio: 'inherit',
82+
});
83+
child.on('exit', (code) => {
84+
console.log(`(Parent) Process lazyMode=${lazyMode} exited with code ${code}`);
85+
code === 0 ? resolve() : reject(new Error(`Test failed for lazyMode=${lazyMode}`));
86+
});
87+
});
88+
}
6689

67-
await testLazyMode(false);
90+
await Promise.all([runSubprocess(true), runSubprocess(false)]);

0 commit comments

Comments
 (0)