From fd0a51aa35bcbcb839c6cfa3f78b1bd045f846c7 Mon Sep 17 00:00:00 2001 From: g11tech Date: Fri, 7 Apr 2023 18:36:13 +0530 Subject: [PATCH] [Nodejs] Change Buffer types to Uint8Array (#280) --- bindings/node.js/lib/kzg.d.ts | 4 ++-- bindings/node.js/test/kzg.test.ts | 32 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bindings/node.js/lib/kzg.d.ts b/bindings/node.js/lib/kzg.d.ts index 91a6bf2..9d7557d 100644 --- a/bindings/node.js/lib/kzg.d.ts +++ b/bindings/node.js/lib/kzg.d.ts @@ -4,8 +4,8 @@ */ export type Bytes32 = Uint8Array; // 32 bytes export type Bytes48 = Uint8Array; // 48 bytes -export type KZGProof = Buffer; // 48 bytes -export type KZGCommitment = Buffer; // 48 bytes +export type KZGProof = Uint8Array; // 48 bytes +export type KZGCommitment = Uint8Array; // 48 bytes export type Blob = Uint8Array; // 4096 * 32 bytes export type ProofResult = [KZGProof, Bytes32]; export interface TrustedSetupJson { diff --git a/bindings/node.js/test/kzg.test.ts b/bindings/node.js/test/kzg.test.ts index 38d2963..e56ae21 100644 --- a/bindings/node.js/test/kzg.test.ts +++ b/bindings/node.js/test/kzg.test.ts @@ -64,8 +64,21 @@ const proofBadLength = randomBytes(BYTES_PER_PROOF - 1); const fieldElementValidLength = randomBytes(BYTES_PER_FIELD_ELEMENT); const fieldElementBadLength = randomBytes(BYTES_PER_FIELD_ELEMENT - 1); -function bytesFromHex(hexString: string): Buffer { - return Buffer.from(hexString.slice(2), "hex"); +function bytesFromHex(hexString: string): Uint8Array { + if (hexString.startsWith("0x")) { + hexString = hexString.slice(2); + } + return Uint8Array.from(Buffer.from(hexString, "hex")); +} + +function bytesEqual(a: Uint8Array | Buffer, b: Uint8Array | Buffer): boolean { + if (a.length !== b.length) { + return false; + } + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) return false; + } + return true; } describe("C-KZG", () => { @@ -81,7 +94,7 @@ describe("C-KZG", () => { tests.forEach((testFile: string) => { const test: BlobToKzgCommitmentTest = yaml.load(readFileSync(testFile, "ascii")); - let commitment: Buffer; + let commitment: Uint8Array; const blob = bytesFromHex(test.input.blob); try { @@ -93,7 +106,7 @@ describe("C-KZG", () => { expect(test.output).not.toBeNull(); const expectedCommitment = bytesFromHex(test.output); - expect(commitment).toEqual(expectedCommitment); + expect(bytesEqual(commitment, expectedCommitment)); }); }); @@ -116,7 +129,12 @@ describe("C-KZG", () => { } expect(test.output).not.toBeNull(); - expect(proof).toEqual(test.output.map((hex) => bytesFromHex(hex))); + + const [proofBytes, yBytes] = proof; + const [expectedProofBytes, expectedYBytes] = test.output.map((out) => bytesFromHex(out)); + + expect(bytesEqual(proofBytes, expectedProofBytes)); + expect(bytesEqual(yBytes, expectedYBytes)); }); }); @@ -127,7 +145,7 @@ describe("C-KZG", () => { tests.forEach((testFile: string) => { const test: ComputeBlobKzgProofTest = yaml.load(readFileSync(testFile, "ascii")); - let proof: Buffer; + let proof: Uint8Array; const blob = bytesFromHex(test.input.blob); const commitment = bytesFromHex(test.input.commitment); @@ -140,7 +158,7 @@ describe("C-KZG", () => { expect(test.output).not.toBeNull(); const expectedProof = bytesFromHex(test.output); - expect(proof).toEqual(expectedProof); + expect(bytesEqual(proof, expectedProof)); }); });