From dad644be85225aedaacc18f7c01b4c5c071a34f8 Mon Sep 17 00:00:00 2001 From: dbanks12 Date: Thu, 6 Jul 2023 18:22:44 +0000 Subject: [PATCH 1/2] add errors when negative bigint is converted to buffer --- yarn-project/foundation/src/bigint-buffer/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/yarn-project/foundation/src/bigint-buffer/index.ts b/yarn-project/foundation/src/bigint-buffer/index.ts index 79ffa1170056..f3020f8c155b 100644 --- a/yarn-project/foundation/src/bigint-buffer/index.ts +++ b/yarn-project/foundation/src/bigint-buffer/index.ts @@ -33,6 +33,9 @@ export function toBigIntBE(buf: Buffer): bigint { * @returns A little-endian buffer representation of num. */ export function toBufferLE(num: bigint, width: number): Buffer { + if (num < BigInt(0)) { + throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`); + } const hex = num.toString(16); const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); buffer.reverse(); @@ -46,6 +49,9 @@ export function toBufferLE(num: bigint, width: number): Buffer { * @returns A big-endian buffer representation of num. */ export function toBufferBE(num: bigint, width: number): Buffer { + if (num < BigInt(0)) { + throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`); + } const hex = num.toString(16); const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); if (buffer.length > width) throw new Error(`Number ${num.toString(16)} does not fit in ${width}`); From e977e8e543fbe581e476b70e4f90df750a0adaec Mon Sep 17 00:00:00 2001 From: dbanks12 Date: Thu, 6 Jul 2023 18:46:39 +0000 Subject: [PATCH 2/2] formatting --- yarn-project/foundation/src/bigint-buffer/index.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/yarn-project/foundation/src/bigint-buffer/index.ts b/yarn-project/foundation/src/bigint-buffer/index.ts index f3020f8c155b..3c764a277d7f 100644 --- a/yarn-project/foundation/src/bigint-buffer/index.ts +++ b/yarn-project/foundation/src/bigint-buffer/index.ts @@ -33,9 +33,7 @@ export function toBigIntBE(buf: Buffer): bigint { * @returns A little-endian buffer representation of num. */ export function toBufferLE(num: bigint, width: number): Buffer { - if (num < BigInt(0)) { - throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`); - } + if (num < BigInt(0)) throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`); const hex = num.toString(16); const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); buffer.reverse(); @@ -49,9 +47,7 @@ export function toBufferLE(num: bigint, width: number): Buffer { * @returns A big-endian buffer representation of num. */ export function toBufferBE(num: bigint, width: number): Buffer { - if (num < BigInt(0)) { - throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`); - } + if (num < BigInt(0)) throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`); const hex = num.toString(16); const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); if (buffer.length > width) throw new Error(`Number ${num.toString(16)} does not fit in ${width}`);