From c7abfa8eb1aaf9df56ab34884645c575d442980b Mon Sep 17 00:00:00 2001 From: fcarreiro Date: Wed, 27 May 2026 16:11:55 +0000 Subject: [PATCH] chore(foundation): return pre-allocated ZERO_BUFFER in field toBuffer --- yarn-project/foundation/src/curves/bn254/field.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/yarn-project/foundation/src/curves/bn254/field.ts b/yarn-project/foundation/src/curves/bn254/field.ts index 54048344f24f..8583acf2b530 100644 --- a/yarn-project/foundation/src/curves/bn254/field.ts +++ b/yarn-project/foundation/src/curves/bn254/field.ts @@ -25,6 +25,7 @@ type DerivedField = { */ abstract class BaseField { static SIZE_IN_BYTES = 32; + private static readonly ZERO_BUFFER = Buffer.alloc(BaseField.SIZE_IN_BYTES); private readonly asBigInt: bigint; /** @@ -67,7 +68,10 @@ abstract class BaseField { * Converts the bigint to a Buffer. */ toBuffer(): Buffer { - return toBufferBE(this.asBigInt, 32); + if (this.asBigInt === 0n) { + return BaseField.ZERO_BUFFER; + } + return toBufferBE(this.asBigInt, BaseField.SIZE_IN_BYTES); } toString(): `0x${string}` {