|
1 | | -import { readObject, withTempFile } from '../../util/file.js'; |
2 | | -import { writeFile } from 'fs/promises'; |
| 1 | +import { readObject, withTempFile, writeObject } from '../../util/file.js'; |
| 2 | +import { writeFile, readFile } from 'fs/promises'; |
3 | 3 | import { Barretenberg } from './barretenberg.js'; |
| 4 | +import { Barretenberg as BarretenbergAPI } from '@aztec/bb.js'; |
| 5 | +import { CompiledCircuit } from '@noir-lang/noir_js'; |
| 6 | +import path from 'path'; |
4 | 7 |
|
5 | | -export async function generateVk(bytecode: string, vkPath: string, vkAsFieldsPath: string): Promise<void> { |
| 8 | +/** |
| 9 | + * Converts binary VK files to field representation for recursive verification. |
| 10 | + * Uses the official @aztec/bb.js API to convert verification keys to field elements. |
| 11 | + */ |
| 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'); |
| 15 | + |
| 16 | + // Read binary files |
| 17 | + const vkBinary = await readFile(vkPath); |
| 18 | + const vkHashBinary = await readFile(vkHashPath); |
| 19 | + |
| 20 | + // Use official bb.js API to convert VK to fields |
| 21 | + const api = await BarretenbergAPI.new({ threads: 1 }); |
| 22 | + try { |
| 23 | + const result = await api.vkAsFields({ verificationKey: vkBinary }); |
| 24 | + |
| 25 | + // Convert Fr objects (Uint8Array) to hex strings |
| 26 | + const vkAsFields = result.fields.map((field) => '0x' + Buffer.from(field).toString('hex')); |
| 27 | + |
| 28 | + // Convert hash to hex string |
| 29 | + const vkHash = '0x' + vkHashBinary.toString('hex'); |
| 30 | + |
| 31 | + // Write as JSON: [vkHash, ...vkAsFields] |
| 32 | + await writeObject([vkHash, ...vkAsFields], vkAsFieldsPath); |
| 33 | + } finally { |
| 34 | + await api.destroy(); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export async function generateVk(bytecode: string, vkPath: string, vkAsFieldsPath: string): Promise<void>; |
| 39 | +export async function generateVk(artifact: CompiledCircuit, vkPath: string, vkAsFieldsPath: string): Promise<void>; |
| 40 | +export async function generateVk( |
| 41 | + bytecodeOrArtifact: string | CompiledCircuit, |
| 42 | + vkPath: string, |
| 43 | + vkAsFieldsPath: string |
| 44 | +): Promise<void> { |
6 | 45 | return await withTempFile(async (acirPath) => { |
7 | | - await writeFile(acirPath, Buffer.from(bytecode, 'base64')); |
| 46 | + let artifact: CompiledCircuit; |
| 47 | + |
| 48 | + if (typeof bytecodeOrArtifact === 'string') { |
| 49 | + // Legacy: just bytecode string - create minimal artifact |
| 50 | + // bb expects circuit JSON (not raw bytecode) when file has .json extension |
| 51 | + artifact = { |
| 52 | + noir_version: '1.0.0', |
| 53 | + hash: 0, |
| 54 | + abi: { parameters: [], return_type: null, error_types: {} }, |
| 55 | + bytecode: bytecodeOrArtifact, // Keep compressed - bb handles decompression |
| 56 | + } as any; |
| 57 | + } else { |
| 58 | + // New: full artifact - use as-is |
| 59 | + // bb expects bytecode to remain compressed (gzipped + base64) |
| 60 | + artifact = bytecodeOrArtifact; |
| 61 | + } |
| 62 | + |
| 63 | + // Write circuit JSON to temp file |
| 64 | + // bb reads .json files as JSON format and decompresses bytecode internally |
| 65 | + await writeFile(acirPath, JSON.stringify(artifact)); |
8 | 66 |
|
9 | 67 | const barretenberg = await Barretenberg.create(); |
| 68 | + // write_vk creates a directory with 'vk' and 'vk_hash' files |
10 | 69 | await barretenberg.writeVK(acirPath, vkPath); |
11 | | - await barretenberg.vkAsFields(vkPath, vkAsFieldsPath); |
| 70 | + |
| 71 | + // Convert binary VK files to field representation |
| 72 | + // vk_as_fields CLI command was removed in newer bb, so we do it in TypeScript |
| 73 | + await convertVkBinaryToFields(vkPath, vkAsFieldsPath); |
12 | 74 | }); |
13 | 75 | } |
14 | 76 |
|
|
0 commit comments