c-kzg-4844/bindings/node.js/test.ts

327 lines
10 KiB
TypeScript
Raw Normal View History

2022-11-03 22:13:49 +00:00
import { randomBytes } from "crypto";
import { existsSync, readFileSync } from "fs";
import { join } from "path";
import { globSync } from "glob";
const yaml = require("js-yaml");
import {
loadTrustedSetup,
freeTrustedSetup,
2022-11-03 00:17:17 +00:00
blobToKzgCommitment,
computeKzgProof,
2023-02-18 21:49:52 +00:00
computeBlobKzgProof,
verifyKzgProof,
verifyBlobKzgProof,
verifyBlobKzgProofBatch,
2023-02-14 21:11:10 +00:00
BYTES_PER_BLOB,
BYTES_PER_COMMITMENT,
BYTES_PER_PROOF,
2022-11-04 18:44:57 +00:00
BYTES_PER_FIELD_ELEMENT,
transformTrustedSetupJSON,
2022-11-03 22:13:49 +00:00
} from "./kzg";
2022-11-02 20:45:29 +00:00
const setupFileName = "testing_trusted_setups.json";
const SETUP_FILE_PATH = existsSync(setupFileName)
? setupFileName
: `../../src/${setupFileName}`;
2022-12-12 22:48:05 +00:00
const MAX_TOP_BYTE = 114;
2023-02-18 21:49:52 +00:00
const TEST_DIR = "../../tests";
const BLOB_TO_KZG_COMMITMENT_TESTS = join(
2023-02-18 21:49:52 +00:00
TEST_DIR,
"blob_to_kzg_commitment/*/*/data.yaml",
2023-02-18 21:49:52 +00:00
);
const COMPUTE_KZG_PROOF_TESTS = join(
2023-02-18 21:49:52 +00:00
TEST_DIR,
"compute_kzg_proof/*/*/data.yaml",
2023-02-18 21:49:52 +00:00
);
const COMPUTE_BLOB_KZG_PROOF_TESTS = join(
2023-02-18 21:49:52 +00:00
TEST_DIR,
"compute_blob_kzg_proof/*/*/data.yaml",
2023-02-18 21:49:52 +00:00
);
const VERIFY_KZG_PROOF_TESTS = join(TEST_DIR, "verify_kzg_proof/*/*/data.yaml");
const VERIFY_BLOB_KZG_PROOF_TESTS = join(
2023-02-18 21:49:52 +00:00
TEST_DIR,
"verify_blob_kzg_proof/*/*/data.yaml",
);
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = join(
TEST_DIR,
"verify_blob_kzg_proof_batch/*/*/data.yaml",
2023-02-18 21:49:52 +00:00
);
2022-12-12 22:48:05 +00:00
const generateRandomBlob = () => {
return new Uint8Array(
2023-02-14 21:11:10 +00:00
randomBytes(BYTES_PER_BLOB).map((x, i) => {
2022-12-12 22:48:05 +00:00
// Set the top byte to be low enough that the field element doesn't overflow the BLS modulus
if (x > MAX_TOP_BYTE && i % BYTES_PER_FIELD_ELEMENT == 31) {
return Math.floor(Math.random() * MAX_TOP_BYTE);
}
return x;
}),
);
};
2022-11-03 00:17:17 +00:00
function bytesFromHex(hexstring: string): Uint8Array {
return Uint8Array.from(Buffer.from(hexstring.slice(2), "hex"));
}
2022-11-03 22:13:49 +00:00
describe("C-KZG", () => {
beforeAll(async () => {
const file = await transformTrustedSetupJSON(SETUP_FILE_PATH);
loadTrustedSetup(file);
});
afterAll(() => {
2022-11-03 21:39:02 +00:00
freeTrustedSetup();
});
2023-02-21 20:22:44 +00:00
describe("reference tests should pass", () => {
it("reference tests for blobToKzgCommitment should pass", () => {
let tests = globSync(BLOB_TO_KZG_COMMITMENT_TESTS);
tests.forEach((testFile: string) => {
const test = yaml.load(readFileSync(testFile, "ascii"));
let commitment = new Uint8Array();
let blob = bytesFromHex(test.input.blob);
2023-02-18 21:49:52 +00:00
try {
commitment = blobToKzgCommitment(blob);
2023-02-18 21:49:52 +00:00
} catch (err) {
expect(test.output).toBeNull();
return;
2023-02-18 21:49:52 +00:00
}
expect(test.output).not.toBeNull();
let expectedCommitment = bytesFromHex(test.output);
expect(commitment).toEqual(expectedCommitment);
2023-02-18 21:49:52 +00:00
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for computeKzgProof should pass", () => {
let tests = globSync(COMPUTE_KZG_PROOF_TESTS);
tests.forEach((testFile: string) => {
const test = yaml.load(readFileSync(testFile, "ascii"));
let proof = new Uint8Array();
let blob = bytesFromHex(test.input.blob);
let z = bytesFromHex(test.input.z);
2023-02-18 21:49:52 +00:00
try {
proof = computeKzgProof(blob, z);
2023-02-18 21:49:52 +00:00
} catch (err) {
expect(test.output).toBeNull();
return;
2023-02-18 21:49:52 +00:00
}
expect(test.output).not.toBeNull();
let expectedProof = bytesFromHex(test.output);
expect(proof).toEqual(expectedProof);
2023-02-18 21:49:52 +00:00
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for computeBlobKzgProof should pass", () => {
let tests = globSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
tests.forEach((testFile: string) => {
const test = yaml.load(readFileSync(testFile, "ascii"));
let proof = new Uint8Array();
let blob = bytesFromHex(test.input.blob);
2023-02-18 21:49:52 +00:00
try {
proof = computeBlobKzgProof(blob);
2023-02-18 21:49:52 +00:00
} catch (err) {
expect(test.output).toBeNull();
return;
2023-02-18 21:49:52 +00:00
}
expect(test.output).not.toBeNull();
let expectedProof = bytesFromHex(test.output);
expect(proof).toEqual(expectedProof);
2023-02-18 21:49:52 +00:00
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for verifyKzgProof should pass", () => {
let tests = globSync(VERIFY_KZG_PROOF_TESTS);
tests.forEach((testFile: string) => {
const test = yaml.load(readFileSync(testFile, "ascii"));
let valid;
let commitment = bytesFromHex(test.input.commitment);
let z = bytesFromHex(test.input.z);
let y = bytesFromHex(test.input.y);
let proof = bytesFromHex(test.input.proof);
2023-02-18 21:49:52 +00:00
try {
valid = verifyKzgProof(commitment, z, y, proof);
2023-02-18 21:49:52 +00:00
} catch (err) {
expect(test.output).toBeNull();
return;
2023-02-18 21:49:52 +00:00
}
expect(valid).toEqual(test.output);
2023-02-18 21:49:52 +00:00
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for verifyBlobKzgProof should pass", () => {
let tests = globSync(VERIFY_BLOB_KZG_PROOF_TESTS);
tests.forEach((testFile: string) => {
const test = yaml.load(readFileSync(testFile, "ascii"));
let valid;
let blob = bytesFromHex(test.input.blob);
let commitment = bytesFromHex(test.input.commitment);
let proof = bytesFromHex(test.input.proof);
2023-02-18 21:49:52 +00:00
try {
valid = verifyBlobKzgProof(blob, commitment, proof);
2023-02-18 21:49:52 +00:00
} catch (err) {
expect(test.output).toBeNull();
return;
2023-02-18 21:49:52 +00:00
}
expect(valid).toEqual(test.output);
2023-02-18 21:49:52 +00:00
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for verifyBlobKzgProofBatch should pass", () => {
let tests = globSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
tests.forEach((testFile: string) => {
const test = yaml.load(readFileSync(testFile, "ascii"));
let valid;
let blobs = test.input.blobs.map(bytesFromHex);
let commitments = test.input.commitments.map(bytesFromHex);
let proofs = test.input.proofs.map(bytesFromHex);
2023-02-18 21:49:52 +00:00
try {
valid = verifyBlobKzgProofBatch(blobs, commitments, proofs);
2023-02-18 21:49:52 +00:00
} catch (err) {
expect(test.output).toBeNull();
return;
2023-02-18 21:49:52 +00:00
}
expect(valid).toEqual(test.output);
2023-02-18 21:49:52 +00:00
});
});
});
2023-02-21 20:22:44 +00:00
describe("edge cases for blobToKzgCommitment", () => {
2023-02-18 21:49:52 +00:00
it("throws as expected when given an argument of invalid type", () => {
// @ts-expect-error
expect(() => blobToKzgCommitment("wrong type")).toThrowError(
2023-02-22 15:10:09 +00:00
"Expected blob to be a UInt8Array",
2023-02-18 21:49:52 +00:00
);
});
it("throws as expected when given an argument of invalid length", () => {
expect(() =>
blobToKzgCommitment(randomBytes(BYTES_PER_BLOB - 1)),
2023-02-22 15:10:09 +00:00
).toThrowError("Expected blob to be 131072 bytes");
2023-02-18 21:49:52 +00:00
});
2022-11-02 20:45:29 +00:00
});
2022-11-04 00:08:36 +00:00
2023-02-21 20:22:44 +00:00
// TODO: add more tests for this function.
describe("edge cases for computeKzgProof", () => {
2023-02-18 21:49:52 +00:00
it("computes a proof from blob/field element", () => {
let blob = generateRandomBlob();
const zBytes = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(0);
computeKzgProof(blob, zBytes);
});
});
2023-02-21 20:22:44 +00:00
// TODO: add more tests for this function.
describe("edge cases for computeBlobKzgProof", () => {
2023-02-18 21:49:52 +00:00
it("computes a proof from blob", () => {
let blob = generateRandomBlob();
computeBlobKzgProof(blob);
});
});
2023-02-21 20:22:44 +00:00
describe("edge cases for verifyKzgProof", () => {
it("valid proof should result in true", () => {
2023-02-18 21:49:52 +00:00
const commitment = new Uint8Array(BYTES_PER_COMMITMENT).fill(0);
commitment[0] = 0xc0;
const z = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(0);
const y = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(0);
const proof = new Uint8Array(BYTES_PER_PROOF).fill(0);
proof[0] = 0xc0;
2022-12-13 19:21:43 +00:00
2023-02-18 21:49:52 +00:00
expect(verifyKzgProof(commitment, z, y, proof)).toBe(true);
});
2022-12-13 19:24:55 +00:00
2023-02-21 20:22:44 +00:00
it("invalid proof should result in false", () => {
2023-02-18 21:49:52 +00:00
const commitment = new Uint8Array(BYTES_PER_COMMITMENT).fill(0);
commitment[0] = 0xc0;
const z = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(1);
const y = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(1);
const proof = new Uint8Array(BYTES_PER_PROOF).fill(0);
proof[0] = 0xc0;
2022-12-13 19:24:55 +00:00
2023-02-18 21:49:52 +00:00
expect(verifyKzgProof(commitment, z, y, proof)).toBe(false);
});
2022-12-13 19:21:43 +00:00
});
2023-02-21 20:22:44 +00:00
describe("edge cases for verifyBlobKzgProof", () => {
it("correct blob/commitment/proof should verify as true", () => {
2023-02-18 21:49:52 +00:00
let blob = generateRandomBlob();
let commitment = blobToKzgCommitment(blob);
let proof = computeBlobKzgProof(blob);
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(true);
});
2023-02-21 20:22:44 +00:00
it("incorrect commitment should verify as false", () => {
2023-02-18 21:49:52 +00:00
let blob = generateRandomBlob();
let commitment = blobToKzgCommitment(generateRandomBlob());
let proof = computeBlobKzgProof(blob);
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
});
2023-02-21 20:22:44 +00:00
it("incorrect proof should verify as false", () => {
2023-02-18 21:49:52 +00:00
let blob = generateRandomBlob();
let commitment = blobToKzgCommitment(blob);
let proof = computeBlobKzgProof(generateRandomBlob());
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
});
});
2023-02-21 20:22:44 +00:00
describe("edge cases for verifyBlobKzgProofBatch", () => {
2023-02-22 15:10:09 +00:00
it("should reject non-bytearray blob", () => {
expect(() =>
// @ts-expect-error
verifyBlobKzgProofBatch(["foo", "bar"], [], []),
).toThrowError("Expected blob to be a UInt8Array");
});
2023-02-21 20:22:44 +00:00
it("zero blobs/commitments/proofs should verify as true", () => {
2023-02-18 21:49:52 +00:00
expect(verifyBlobKzgProofBatch([], [], [])).toBe(true);
});
2023-02-14 21:11:10 +00:00
2023-02-21 20:22:44 +00:00
it("mismatching blobs/commitments/proofs should throw error", () => {
2023-02-18 21:49:52 +00:00
let count = 3;
let blobs = new Array(count);
let commitments = new Array(count);
let proofs = new Array(count);
for (let [i, _] of blobs.entries()) {
blobs[i] = generateRandomBlob();
commitments[i] = blobToKzgCommitment(blobs[i]);
proofs[i] = computeBlobKzgProof(blobs[i]);
}
expect(verifyBlobKzgProofBatch(blobs, commitments, proofs)).toBe(true);
2023-02-14 21:11:10 +00:00
expect(() =>
2023-02-18 21:49:52 +00:00
verifyBlobKzgProofBatch(blobs.slice(0, 1), commitments, proofs),
).toThrowError("requires equal number of blobs/commitments/proofs");
expect(() =>
verifyBlobKzgProofBatch(blobs, commitments.slice(0, 1), proofs),
).toThrowError("requires equal number of blobs/commitments/proofs");
expect(() =>
verifyBlobKzgProofBatch(blobs, commitments, proofs.slice(0, 1)),
).toThrowError("requires equal number of blobs/commitments/proofs");
2023-02-14 21:11:10 +00:00
});
});
2022-11-02 20:45:29 +00:00
});