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

320 lines
11 KiB
TypeScript
Raw Normal View History

2022-11-03 22:13:49 +00:00
import { randomBytes } from "crypto";
import { existsSync } from "fs";
2023-02-18 21:49:52 +00:00
import path = require("path");
import fs = require("fs");
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 = path.join(
TEST_DIR,
"blob_to_kzg_commitment",
);
const COMPUTE_KZG_PROOF_TESTS = path.join(TEST_DIR, "compute_kzg_proof");
const COMPUTE_BLOB_KZG_PROOF_TESTS = path.join(
TEST_DIR,
"compute_blob_kzg_proof",
);
const VERIFY_KZG_PROOF_TESTS = path.join(TEST_DIR, "verify_kzg_proof");
const VERIFY_BLOB_KZG_PROOF_TESTS = path.join(
TEST_DIR,
"verify_blob_kzg_proof",
);
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = path.join(
TEST_DIR,
"verify_blob_kzg_proof_batch",
);
function getBytes(file: String): Uint8Array {
const data = require("fs").readFileSync(file, "ascii");
return Buffer.from(data, "hex");
}
function getBoolean(file: String): boolean {
const data = require("fs").readFileSync(file, "ascii");
return data.includes("true");
}
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
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", () => {
2023-02-18 21:49:52 +00:00
let tests = fs.readdirSync(BLOB_TO_KZG_COMMITMENT_TESTS);
tests.forEach((test) => {
let testPath = path.join(BLOB_TO_KZG_COMMITMENT_TESTS, test);
let blob = getBytes(path.join(testPath, "blob.txt"));
try {
let commitment = blobToKzgCommitment(blob);
let expectedCommitment = getBytes(
path.join(testPath, "commitment.txt"),
);
expect(commitment.buffer).toEqual(expectedCommitment.buffer);
} catch (err) {
expect(fs.existsSync(path.join(testPath, "commitment.txt"))).toBe(
false,
);
}
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for computeKzgProof should pass", () => {
2023-02-18 21:49:52 +00:00
let tests = fs.readdirSync(COMPUTE_KZG_PROOF_TESTS);
tests.forEach((test) => {
let testPath = path.join(COMPUTE_KZG_PROOF_TESTS, test);
let blob = getBytes(path.join(testPath, "blob.txt"));
let inputPoint = getBytes(path.join(testPath, "input_point.txt"));
try {
let proof = computeKzgProof(blob, inputPoint);
let expectedProof = getBytes(path.join(testPath, "proof.txt"));
expect(proof.buffer).toEqual(expectedProof.buffer);
} catch (err) {
expect(fs.existsSync(path.join(testPath, "proof.txt"))).toBe(false);
}
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for computeBlobKzgProof should pass", () => {
2023-02-18 21:49:52 +00:00
let tests = fs.readdirSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
tests.forEach((test) => {
let testPath = path.join(COMPUTE_BLOB_KZG_PROOF_TESTS, test);
let blob = getBytes(path.join(testPath, "blob.txt"));
try {
let proof = computeBlobKzgProof(blob);
let expectedProof = getBytes(path.join(testPath, "proof.txt"));
expect(proof.buffer).toEqual(expectedProof.buffer);
} catch (err) {
expect(fs.existsSync(path.join(testPath, "proof.txt"))).toBe(false);
}
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for verifyKzgProof should pass", () => {
2023-02-18 21:49:52 +00:00
let tests = fs.readdirSync(VERIFY_KZG_PROOF_TESTS);
tests.forEach((test) => {
let testPath = path.join(VERIFY_KZG_PROOF_TESTS, test);
let commitment = getBytes(path.join(testPath, "commitment.txt"));
let inputPoint = getBytes(path.join(testPath, "input_point.txt"));
let claimedValue = getBytes(path.join(testPath, "claimed_value.txt"));
let proof = getBytes(path.join(testPath, "proof.txt"));
try {
let ok = verifyKzgProof(commitment, inputPoint, claimedValue, proof);
let expectedOk = getBoolean(path.join(testPath, "ok.txt"));
expect(ok).toEqual(expectedOk);
} catch (err) {
expect(fs.existsSync(path.join(testPath, "ok.txt"))).toBe(false);
}
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for verifyBlobKzgProof should pass", () => {
2023-02-18 21:49:52 +00:00
let tests = fs.readdirSync(VERIFY_BLOB_KZG_PROOF_TESTS);
tests.forEach((test) => {
let testPath = path.join(VERIFY_BLOB_KZG_PROOF_TESTS, test);
let blob = getBytes(path.join(testPath, "blob.txt"));
let commitment = getBytes(path.join(testPath, "commitment.txt"));
let proof = getBytes(path.join(testPath, "proof.txt"));
try {
let ok = verifyBlobKzgProof(blob, commitment, proof);
let expectedOk = getBoolean(path.join(testPath, "ok.txt"));
expect(ok).toEqual(expectedOk);
} catch (err) {
expect(fs.existsSync(path.join(testPath, "ok.txt"))).toBe(false);
}
});
});
2023-02-21 20:22:44 +00:00
it("reference tests for verifyBlobKzgProofBatch should pass", () => {
2023-02-18 21:49:52 +00:00
let tests = fs.readdirSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
tests.forEach((test) => {
let testPath = path.join(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS, test);
let blobs = fs
.readdirSync(path.join(testPath, "blobs"))
.sort()
.map((filename) => {
return path.join(testPath, "blobs", filename);
})
.map(getBytes);
let commitments = fs
.readdirSync(path.join(testPath, "commitments"))
.sort()
.map((filename) => {
return path.join(testPath, "commitments", filename);
})
.map(getBytes);
let proofs = fs
.readdirSync(path.join(testPath, "proofs"))
.sort()
.map((filename) => {
return path.join(testPath, "proofs", filename);
})
.map(getBytes);
try {
let ok = verifyBlobKzgProofBatch(blobs, commitments, proofs);
let expectedOk = getBoolean(path.join(testPath, "ok.txt"));
expect(ok).toEqual(expectedOk);
} catch (err) {
expect(fs.existsSync(path.join(testPath, "ok.txt"))).toBe(false);
}
});
});
});
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
});