Skip to content

Commit 00fea5b

Browse files
Merge pull request #2553 from o1-labs/leon/small-big
feat: small and big zk program for cache regression
2 parents ff0c8bf + 7816e96 commit 00fea5b

File tree

7 files changed

+123
-88
lines changed

7 files changed

+123
-88
lines changed

scripts/tests/check-cache-regressions.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ source ./scripts/lib/ux.sh
99
./run ./src/tests/cache/simple-regression.ts --bundle --mode check --tarball ./tests/test-artifacts/cache/simple-regression.tar.gz
1010

1111
# This pin is generated in ./dump-cache-regressions.sh
12-
ARTIFACT_PIN=2025-10-14T14:08:25-04:00
12+
ARTIFACT_PIN=2025-10-14T14:16:00-04:00
1313

1414
WORKDIR=tests/test-artifacts/cache/
1515
mkdir -p $WORKDIR
@@ -22,8 +22,9 @@ WORKDIR=tests/test-artifacts/cache/$ARTIFACT_PIN
2222
# Regression checks
2323
./run ./src/tests/cache/simple-regression.ts --bundle --mode check --tarball $WORKDIR/simple-regression.tar.gz
2424
./run ./src/tests/cache/complex-regression.ts --bundle --mode check --tarball $WORKDIR/complex-regression.tar.gz
25-
./run ./src/tests/cache/rsa-regression.ts --bundle --keep --mode check --tarball $WORKDIR/rsa-regression.tar.gz
26-
./run ./src/tests/cache/sideloading-regression.ts --bundle --keep --mode check --tarball $WORKDIR/sideloading-regression.tar.gz
27-
./run ./src/tests/cache/runtime-table-regression.ts --bundle --keep --mode check --tarball $WORKDIR/runtime-table-regression.tar.gz
25+
./run ./src/tests/cache/rsa-regression.ts --bundle --mode check --tarball $WORKDIR/rsa-regression.tar.gz
26+
./run ./src/tests/cache/sideloading-regression.ts --bundle --mode check --tarball $WORKDIR/sideloading-regression.tar.gz
27+
./run ./src/tests/cache/runtime-table-regression.ts --bundle --mode check --tarball $WORKDIR/runtime-table-regression.tar.gz
28+
./run ./src/tests/cache/small-big-regression.ts --bundle --mode check --tarball $WORKDIR/small-big-regression.tar.gz
2829

2930
echo "Artifacts checked successfully!"

scripts/tests/dump-cache-regressions.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ WORKDIR=tests/test-artifacts/cache/$ARTIFACT_PIN/
99
mkdir -p $WORKDIR
1010

1111
./run ./src/tests/cache/simple-regression.ts --bundle --mode dump --tarball $WORKDIR/simple-regression.tar.gz
12-
./run ./src/tests/cache/complex-regression.ts --bundle --keep --mode dump --tarball $WORKDIR/complex-regression.tar.gz
13-
./run ./src/tests/cache/rsa-regression.ts --bundle --keep --mode dump --tarball $WORKDIR/rsa-regression.tar.gz
14-
./run ./src/tests/cache/sideloading-regression.ts --bundle --keep --mode dump --tarball $WORKDIR/sideloading-regression.tar.gz
15-
./run ./src/tests/cache/runtime-table-regression.ts --bundle --keep --mode dump --tarball $WORKDIR/runtime-table-regression.tar.gz
12+
./run ./src/tests/cache/complex-regression.ts --bundle --mode dump --tarball $WORKDIR/complex-regression.tar.gz
13+
./run ./src/tests/cache/rsa-regression.ts --bundle --mode dump --tarball $WORKDIR/rsa-regression.tar.gz
14+
./run ./src/tests/cache/sideloading-regression.ts --bundle --mode dump --tarball $WORKDIR/sideloading-regression.tar.gz
15+
./run ./src/tests/cache/runtime-table-regression.ts --bundle --mode dump --tarball $WORKDIR/runtime-table-regression.tar.gz
16+
./run ./src/tests/cache/small-big-regression.ts --bundle --mode dump --tarball $WORKDIR/small-big-regression.tar.gz
1617

1718
gcloud storage cp --recursive $WORKDIR gs://o1js-ci/tests/cache/fixtures/
1819

src/examples/zkprogram/program-small-big.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Bytes, Field, Hash, Poseidon, UInt8, ZkProgram } from 'o1js';
2+
3+
export const SmallProgram = ZkProgram({
4+
name: 'small-program',
5+
publicOutput: Field,
6+
7+
methods: {
8+
poseidonHash: {
9+
privateInputs: [Field],
10+
async method(preimage: Field) {
11+
const digest = Poseidon.hash([preimage]);
12+
13+
return { publicOutput: digest };
14+
},
15+
},
16+
},
17+
});
18+
19+
class PoseidonProof extends SmallProgram.Proof {}
20+
class Bytes32 extends Bytes(32) {}
21+
22+
export const BigProgram = ZkProgram({
23+
name: 'big-program',
24+
publicOutput: Bytes32,
25+
26+
methods: {
27+
combinedHash: {
28+
privateInputs: [PoseidonProof],
29+
async method(poseidonProof: PoseidonProof) {
30+
poseidonProof.verify();
31+
32+
const poseidonOutput = poseidonProof.publicOutput;
33+
const outputBits = poseidonOutput.toBits();
34+
35+
const byteChunks = Array.from({ length: 32 }, (_, i) => outputBits.slice(i * 8, i * 8 + 8));
36+
const outputBytes = byteChunks.map((byteChunks) => UInt8.fromBits(byteChunks));
37+
38+
const hash1 = Hash.SHA2_256.hash(outputBytes);
39+
const hash2 = Hash.BLAKE2B.hash(hash1);
40+
const hash3 = Hash.SHA3_512.hash(Bytes.from([...hash1.bytes, ...hash2.bytes]));
41+
const hash4 = Hash.Keccak256.hash(hash3);
42+
43+
return { publicOutput: hash4 };
44+
},
45+
},
46+
},
47+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Field, verify } from 'o1js';
2+
import { Performance } from '../../../lib/testing/perf-regression.js';
3+
import { BigProgram, SmallProgram } from './program-small-big.js';
4+
5+
const csSmall = await SmallProgram.analyzeMethods();
6+
console.log('small program rows: ', csSmall.poseidonHash.rows);
7+
8+
const csBig = await BigProgram.analyzeMethods();
9+
console.log('big program rows: ', csBig.combinedHash.rows, '\n');
10+
11+
const perfSmall = Performance.create(SmallProgram.name, csSmall);
12+
const perfBig = Performance.create(BigProgram.name, csBig);
13+
14+
perfSmall.start('compile');
15+
await SmallProgram.compile();
16+
perfSmall.end();
17+
18+
perfBig.start('compile');
19+
const { verificationKey: verificationKeyBig } = await BigProgram.compile();
20+
perfBig.end();
21+
22+
perfSmall.start('prove', 'poseidonHash');
23+
const proofSmall = await SmallProgram.poseidonHash(Field.random());
24+
perfSmall.end();
25+
26+
perfBig.start('prove', 'combinedHash');
27+
const { proof: proofBig } = await BigProgram.combinedHash(proofSmall.proof);
28+
perfBig.end();
29+
30+
perfBig.start('verify', 'combinedHash');
31+
await verify(proofBig, verificationKeyBig);
32+
perfBig.end();
33+
34+
console.log('Final Digest: ', proofBig.publicOutput.toHex());
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import minimist from 'minimist';
2+
import assert from 'node:assert';
3+
import { Field } from 'o1js';
4+
import { BigProgram, SmallProgram } from '../../examples/zkprogram/small-big/program-small-big.js';
5+
import { CacheHarness } from './harness.js';
6+
7+
const { mode, tarball } = minimist(process.argv.slice(2));
8+
9+
const harness = await CacheHarness({ mode, tarball });
10+
11+
const { verificationKey: smallVk } = await SmallProgram.compile({ cache: harness.cache });
12+
harness.check(smallVk, 'smallVk');
13+
14+
const { verificationKey: bigVk } = await BigProgram.compile({ cache: harness.cache });
15+
harness.check(bigVk, 'bigVk');
16+
17+
{
18+
const { proof } = await SmallProgram.poseidonHash(Field.random());
19+
{
20+
const ok = await harness.verify(proof, 'smallVk');
21+
assert.equal(ok, true, 'small proof should verify');
22+
}
23+
24+
const { proof: derived } = await BigProgram.combinedHash(proof);
25+
{
26+
const ok = await harness.verify(derived, 'bigVk');
27+
assert.equal(ok, true, 'derived proof should verify');
28+
}
29+
}
30+
31+
await harness.finish();

tests/perf-regression/perf-regression.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ MODE=$1
2020
./run src/examples/zkprogram/gadgets.ts --bundle "$MODE"
2121
./run src/examples/zkprogram/side-loading/run.ts --bundle "$MODE"
2222
./run src/examples/zkprogram/runtime-table/run.ts --bundle "$MODE"
23-
./run src/examples/zkprogram/program-small-big.ts --bundle "$MODE"
23+
./run src/examples/zkprogram/small-big/run.ts --bundle "$MODE"
2424

2525
# Run CS + zkApps performance regression tests
2626
./run tests/perf-regression/perf-regression.ts --bundle "$MODE"

0 commit comments

Comments
 (0)