diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index 11a15f22e39..7bad36c7819 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -89,7 +89,6 @@ export async function hashVK(wasm: CircuitsWasm, vkBuf: Buffer) { export async function computeFunctionLeaf(wasm: CircuitsWasm, fnLeaf: FunctionLeafPreimage) { const fnLeafBuf = fnLeaf.toBuffer(); - if (!FunctionLeafPreimage.verifyBufferSize(fnLeafBuf)) throw new Error(`Invalid length for function leaf`); wasm.call('pedersen__init'); return Fr.fromBuffer(await wasmAsyncCall(wasm, 'abis__compute_function_leaf', { toBuffer: () => fnLeafBuf }, 32)); } diff --git a/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts b/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts index dc572de196e..905adc311a3 100644 --- a/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts +++ b/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts @@ -6,9 +6,13 @@ import { serializeToBuffer } from '../utils/serialize.js'; * @see abis/function_leaf_preimage.hpp */ export class FunctionLeafPreimage { + readonly FUNCTION_SELECTOR_LENGTH = 4; + constructor(public functionSelector: Buffer, public isPrivate: boolean, public vkHash: Fr, public acirHash: Fr) { - if (functionSelector.byteLength !== 4) { - throw new Error(`Function selector must be 4 bytes long, got ${functionSelector.byteLength} bytes.`); + if (functionSelector.byteLength !== this.FUNCTION_SELECTOR_LENGTH) { + throw new Error( + `Function selector must be ${this.FUNCTION_SELECTOR_LENGTH} bytes long, got ${functionSelector.byteLength} bytes.`, + ); } } @@ -17,6 +21,11 @@ export class FunctionLeafPreimage { * @returns The buffer. */ toBuffer(): Buffer { + if (this.functionSelector.byteLength !== this.FUNCTION_SELECTOR_LENGTH) { + throw new Error( + `Function selector must be ${this.FUNCTION_SELECTOR_LENGTH} bytes long, got ${this.functionSelector.byteLength} bytes.`, + ); + } return serializeToBuffer(this.functionSelector, this.isPrivate, this.vkHash, this.acirHash); } @@ -28,8 +37,4 @@ export class FunctionLeafPreimage { const reader = BufferReader.asReader(buffer); return new FunctionLeafPreimage(reader.readBytes(4), reader.readBoolean(), reader.readFr(), reader.readFr()); } - - static verifyBufferSize(buffer: Buffer): boolean { - return buffer.length === 4 + 1 + 32 + 32; - } }