From 3828e22ae158563dd9970319fa49c22b6243f0c0 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:34:58 +0100 Subject: [PATCH] chore: standardize witness map serialization format in JS (#3104) --- tooling/noir_js/src/index.ts | 2 +- tooling/noir_js/src/program.ts | 5 ++--- tooling/noir_js/src/serialize.ts | 17 ----------------- .../noir_js_backend_barretenberg/src/index.ts | 5 +++-- 4 files changed, 6 insertions(+), 23 deletions(-) delete mode 100644 tooling/noir_js/src/serialize.ts diff --git a/tooling/noir_js/src/index.ts b/tooling/noir_js/src/index.ts index 7c8d1bc7544..3d424ca0dea 100644 --- a/tooling/noir_js/src/index.ts +++ b/tooling/noir_js/src/index.ts @@ -3,6 +3,6 @@ import * as abi from '@noir-lang/noirc_abi'; export { acvm, abi }; -export { acirToUint8Array, witnessMapToUint8Array } from './serialize.js'; +export { WitnessMap } from '@noir-lang/acvm_js'; export { Noir } from './program.js'; diff --git a/tooling/noir_js/src/program.ts b/tooling/noir_js/src/program.ts index 7d11166c0a7..96617396c3a 100644 --- a/tooling/noir_js/src/program.ts +++ b/tooling/noir_js/src/program.ts @@ -2,8 +2,7 @@ import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types'; import { generateWitness } from './witness_generation.js'; import initAbi, { abiDecode, InputMap, InputValue } from '@noir-lang/noirc_abi'; -import initACVM from '@noir-lang/acvm_js'; -import { witnessMapToUint8Array } from './serialize.js'; +import initACVM, { compressWitness } from '@noir-lang/acvm_js'; export class Noir { constructor( @@ -30,7 +29,7 @@ export class Noir { await this.init(); const witness = await generateWitness(this.circuit, inputs); const { return_value: returnValue } = abiDecode(this.circuit.abi, witness); - return { witness: witnessMapToUint8Array(witness), returnValue }; + return { witness: compressWitness(witness), returnValue }; } // Initial inputs to your program diff --git a/tooling/noir_js/src/serialize.ts b/tooling/noir_js/src/serialize.ts deleted file mode 100644 index df01769c06e..00000000000 --- a/tooling/noir_js/src/serialize.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { WitnessMap, compressWitness } from '@noir-lang/acvm_js'; -import { decompressSync as gunzip } from 'fflate'; -import { base64Decode } from './base64_decode.js'; - -// After solving the witness, to pass it a backend, we need to serialize it to a Uint8Array -export function witnessMapToUint8Array(solvedWitness: WitnessMap): Uint8Array { - // TODO: We just want to serialize, but this will zip up the witness - // TODO so its not ideal - const compressedWitness = compressWitness(solvedWitness); - return gunzip(compressedWitness); -} - -// Converts an bytecode to a Uint8Array -export function acirToUint8Array(base64EncodedBytecode): Uint8Array { - const compressedByteCode = base64Decode(base64EncodedBytecode); - return gunzip(compressedByteCode); -} diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index a2f8240f6f7..a974a10581f 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { decompressSync as gunzip } from 'fflate'; import { acirToUint8Array } from './serialize.js'; import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types'; @@ -65,12 +66,12 @@ export class BarretenbergBackend implements Backend { return this.generateProof(witness, makeEasyToVerifyInCircuit); } - async generateProof(decompressedWitness: Uint8Array, makeEasyToVerifyInCircuit: boolean): Promise { + async generateProof(compressedWitness: Uint8Array, makeEasyToVerifyInCircuit: boolean): Promise { await this.instantiate(); const proofWithPublicInputs = await this.api.acirCreateProof( this.acirComposer, this.acirUncompressedBytecode, - decompressedWitness, + gunzip(compressedWitness), makeEasyToVerifyInCircuit, );