Skip to content

Commit 54fea42

Browse files
committed
use --output_format json
1 parent d2051f6 commit 54fea42

File tree

6 files changed

+71
-44
lines changed

6 files changed

+71
-44
lines changed

.pnp.cjs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethereum/oracles/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"license": "MIT",
2525
"dependencies": {
26-
"@aztec/bb.js": "3.0.3",
26+
"@aztec/bb.js": "4.0.0-nightly.20260128",
2727
"@ethereumjs/trie": "^6.2.0",
2828
"@iarna/toml": "^2.2.5",
2929
"@noir-lang/noir_js": "1.0.0-beta.18",

ethereum/oracles/src/noir/circuit/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Unfortunately `nargo` does not generate any of the recursion artifacts. Noir tea
2929
- `./bb write_vk -b ${acirPath} -o ${vkPath}`
3030
- We cache it in a file as it's slow to generate
3131
- VK.json - `target/${name}.vk.json`
32-
- generated from VK binary files using the official @aztec/bb.js API
33-
- the `vk_as_fields` CLI command was removed in newer bb versions (moved to msgpack API)
34-
- we use `Barretenberg.vkAsFields()` from @aztec/bb.js to convert VK to field elements
35-
- JSON array that contains `vkHash` as the first element and `vkAsFields` after it
32+
- generated using bb CLI with `--output_format json` flag (available in v4.0.0+)
33+
- command: `./bb write_vk -b ${acirPath} -o ${vkPath} --output_format json`
34+
- the new JSON format includes `fields` array, `vk_hash`, and metadata (bb_version, scheme, etc.)
35+
- we convert it to a JSON array with `vkHash` as the first element and `vkAsFields` after it
3636

3737
## Usage
3838

ethereum/oracles/src/noir/circuit/barretenberg.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
import { $ } from 'execa';
22
import path from 'path';
33
import os from 'os';
4+
import { readFile } from 'fs/promises';
5+
6+
export interface VKJsonOutput {
7+
fields: string[];
8+
vk_hash: string;
9+
file_kind: string;
10+
bb_version: string;
11+
scheme: string;
12+
}
13+
14+
export interface ProofJsonOutput {
15+
fields: string[];
16+
vk_hash: string;
17+
file_kind: string;
18+
bb_version: string;
19+
scheme: string;
20+
}
421

522
export class Barretenberg {
623
public static async create(): Promise<Barretenberg> {
724
const binaryPath = path.join(os.homedir(), '.bb/bb');
825
return new Barretenberg(binaryPath);
926
}
27+
1028
public async writeVK(acirPath: string, vkPath: string) {
1129
await $`${this.binaryPath} write_vk -b ${acirPath} -o ${vkPath}`;
1230
}
1331

14-
// Note: vk_as_fields CLI command was removed in newer bb versions
15-
// VK conversion to fields is now handled in TypeScript
32+
public async writeVKJson(acirPath: string, vkJsonPath: string): Promise<VKJsonOutput> {
33+
await $`${this.binaryPath} write_vk -b ${acirPath} -o ${vkJsonPath} --output_format json`;
34+
const jsonContent = await readFile(vkJsonPath, 'utf-8');
35+
return JSON.parse(jsonContent) as VKJsonOutput;
36+
}
1637

1738
public async proofAsFields(vkPath: string, proofWithInputsPath: string, proofAsFieldsPath: string) {
1839
await $`${this.binaryPath} proof_as_fields -k ${vkPath} -p ${proofWithInputsPath} -o ${proofAsFieldsPath}`;
@@ -30,5 +51,26 @@ export class Barretenberg {
3051
}
3152
}
3253

54+
public async proveJson(
55+
bytecodePath: string,
56+
witnessPath: string,
57+
proofJsonPath: string,
58+
vkPath?: string,
59+
cwd?: string
60+
): Promise<ProofJsonOutput> {
61+
const options = cwd ? { cwd } : {};
62+
if (vkPath) {
63+
await $({
64+
...options
65+
})`${this.binaryPath} prove -b ${bytecodePath} -w ${witnessPath} -o ${proofJsonPath} -k ${vkPath} --output_format json`;
66+
} else {
67+
await $({
68+
...options
69+
})`${this.binaryPath} prove -b ${bytecodePath} -w ${witnessPath} -o ${proofJsonPath} --write_vk --output_format json`;
70+
}
71+
const jsonContent = await readFile(proofJsonPath, 'utf-8');
72+
return JSON.parse(jsonContent) as ProofJsonOutput;
73+
}
74+
3375
private constructor(private binaryPath: string) {}
3476
}

ethereum/oracles/src/noir/circuit/vk.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
1-
import { readObject, withTempFile, writeObject } from '../../util/file.js';
2-
import { writeFile, readFile } from 'fs/promises';
1+
import { readObject, writeObject, withTempFile } from '../../util/file.js';
2+
import { writeFile } from 'fs/promises';
33
import { Barretenberg } from './barretenberg.js';
4-
import { Barretenberg as BarretenbergAPI } from '@aztec/bb.js';
54
import { CompiledCircuit } from '@noir-lang/noir_js';
6-
import path from 'path';
75

86
/**
9-
* Converts binary VK files to field representation for recursive verification.
10-
* Uses @aztec/bb.js API to convert verification keys to field elements.
7+
* Converts VK to field representation using BB CLI JSON output.
8+
* Uses the new --output_format json flag from BB v4.0.0+
119
*/
12-
async function convertVkBinaryToFields(vkDirPath: string, vkAsFieldsPath: string): Promise<void> {
13-
const vkPath = path.join(vkDirPath, 'vk');
14-
const vkHashPath = path.join(vkDirPath, 'vk_hash');
10+
async function generateVkAsFieldsJson(acirPath: string, vkAsFieldsPath: string): Promise<void> {
11+
const barretenberg = await Barretenberg.create();
1512

16-
const vkBinary = await readFile(vkPath);
17-
const vkHashBinary = await readFile(vkHashPath);
13+
// Use new JSON output format to get VK fields directly
14+
const vkJsonPath = vkAsFieldsPath + '.tmp.json';
15+
const vkJson = await barretenberg.writeVKJson(acirPath, vkJsonPath);
1816

19-
// convert VK to fields
20-
const api = await BarretenbergAPI.new({ threads: 1 });
21-
try {
22-
const result = await api.vkAsFields({ verificationKey: vkBinary });
23-
24-
// Convert Fr objects to hex strings
25-
const vkAsFields = result.fields.map((field) => '0x' + Buffer.from(field).toString('hex'));
26-
27-
const vkHash = '0x' + vkHashBinary.toString('hex');
28-
29-
// write as JSON
30-
await writeObject([vkHash, ...vkAsFields], vkAsFieldsPath);
31-
} finally {
32-
await api.destroy();
33-
}
17+
// Format: [vk_hash, ...vk_fields]
18+
await writeObject([vkJson.vk_hash, ...vkJson.fields], vkAsFieldsPath);
3419
}
3520

3621
export async function generateVk(bytecode: string, vkPath: string, vkAsFieldsPath: string): Promise<void>;
@@ -60,8 +45,8 @@ export async function generateVk(
6045
const barretenberg = await Barretenberg.create();
6146
await barretenberg.writeVK(acirPath, vkPath);
6247

63-
// Convert binary VK files to field representation
64-
await convertVkBinaryToFields(vkPath, vkAsFieldsPath);
48+
// Generate VK as fields using new JSON output format
49+
await generateVkAsFieldsJson(acirPath, vkAsFieldsPath);
6550
});
6651
}
6752

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ __metadata:
2222
languageName: node
2323
linkType: hard
2424

25-
"@aztec/bb.js@npm:3.0.3":
26-
version: 3.0.3
27-
resolution: "@aztec/bb.js@npm:3.0.3"
25+
"@aztec/bb.js@npm:4.0.0-nightly.20260128":
26+
version: 4.0.0-nightly.20260128
27+
resolution: "@aztec/bb.js@npm:4.0.0-nightly.20260128"
2828
dependencies:
2929
comlink: "npm:^4.4.1"
3030
commander: "npm:^12.1.0"
@@ -34,7 +34,7 @@ __metadata:
3434
tslib: "npm:^2.4.0"
3535
bin:
3636
bb: dest/node/bin/index.js
37-
checksum: 10c0/8ba743fd2628fdaa531d97110caa5374b72ccce0c424061a97fd750cacae93ec77011e42e938c225165d34bb51f514bd2d4204f16411e91346a47abcf3262e0e
37+
checksum: 10c0/84d96f13c73945929db4e14eaa76335fe4183c4f1a52e4f8750c20c59275f035d4406ad936c0524b003d5fdad1d177d57eb510d0a9bb9f6efacb99aa2d6930f4
3838
languageName: node
3939
linkType: hard
4040

@@ -4815,7 +4815,7 @@ __metadata:
48154815
version: 0.0.0-use.local
48164816
resolution: "noir-ethereum-api-oracles@workspace:ethereum/oracles"
48174817
dependencies:
4818-
"@aztec/bb.js": "npm:3.0.3"
4818+
"@aztec/bb.js": "npm:4.0.0-nightly.20260128"
48194819
"@ethereumjs/trie": "npm:^6.2.0"
48204820
"@iarna/toml": "npm:^2.2.5"
48214821
"@noir-lang/noir_js": "npm:1.0.0-beta.18"

0 commit comments

Comments
 (0)