Skip to content

Commit c7abfa8

Browse files
committed
chore(foundation): return pre-allocated ZERO_BUFFER in field toBuffer
1 parent dcf7354 commit c7abfa8

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • yarn-project/foundation/src/curves/bn254

yarn-project/foundation/src/curves/bn254/field.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type DerivedField<T extends BaseField> = {
2525
*/
2626
abstract class BaseField {
2727
static SIZE_IN_BYTES = 32;
28+
private static readonly ZERO_BUFFER = Buffer.alloc(BaseField.SIZE_IN_BYTES);
2829
private readonly asBigInt: bigint;
2930

3031
/**
@@ -67,7 +68,10 @@ abstract class BaseField {
6768
* Converts the bigint to a Buffer.
6869
*/
6970
toBuffer(): Buffer {
70-
return toBufferBE(this.asBigInt, 32);
71+
if (this.asBigInt === 0n) {
72+
return BaseField.ZERO_BUFFER;
73+
}
74+
return toBufferBE(this.asBigInt, BaseField.SIZE_IN_BYTES);
7175
}
7276

7377
toString(): `0x${string}` {

0 commit comments

Comments
 (0)