Skip to content

Commit

Permalink
fix: remove verify buffer bytes in favour of check
Browse files Browse the repository at this point in the history
  • Loading branch information
cheethas committed Apr 19, 2023
1 parent 6daf833 commit 55e7ace
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
17 changes: 11 additions & 6 deletions yarn-project/circuits.js/src/structs/function_leaf_preimage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
);
}
}

Expand All @@ -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);
}

Expand All @@ -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;
}
}

0 comments on commit 55e7ace

Please sign in to comment.