diff --git a/yarn-project/foundation/src/bigint-buffer/index.ts b/yarn-project/foundation/src/bigint-buffer/index.ts index 79ffa1170056..3c764a277d7f 100644 --- a/yarn-project/foundation/src/bigint-buffer/index.ts +++ b/yarn-project/foundation/src/bigint-buffer/index.ts @@ -33,6 +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.`); const hex = num.toString(16); const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); buffer.reverse(); @@ -46,6 +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.`); 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}`);