2023-03-31 08:46:54 +00:00
|
|
|
import {randomBytes} from "crypto";
|
|
|
|
import {readFileSync} from "fs";
|
|
|
|
import {resolve} from "path";
|
|
|
|
import {globSync} from "glob";
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
const yaml = require("js-yaml");
|
2022-11-08 16:38:07 +00:00
|
|
|
|
2023-03-31 08:46:54 +00:00
|
|
|
interface TestMeta<I extends Record<string, any>, O extends boolean | string | string[] | Record<string, any>> {
|
2023-03-09 14:40:57 +00:00
|
|
|
input: I;
|
|
|
|
output: O;
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:50:04 +00:00
|
|
|
import {
|
|
|
|
loadTrustedSetup,
|
2022-11-03 00:17:17 +00:00
|
|
|
blobToKzgCommitment,
|
2023-01-27 15:15:17 +00:00
|
|
|
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,
|
2023-03-09 14:40:57 +00:00
|
|
|
ProofResult,
|
2023-03-09 16:21:28 +00:00
|
|
|
} from "../lib/kzg";
|
2022-11-02 20:45:29 +00:00
|
|
|
|
2023-03-31 08:46:54 +00:00
|
|
|
const SETUP_FILE_PATH = resolve(__dirname, "__fixtures__", "testing_trusted_setups.json");
|
2022-11-08 16:38:07 +00:00
|
|
|
|
2022-12-12 22:48:05 +00:00
|
|
|
const MAX_TOP_BYTE = 114;
|
|
|
|
|
2023-03-31 08:46:54 +00:00
|
|
|
const BLOB_TO_KZG_COMMITMENT_TESTS = "../../tests/blob_to_kzg_commitment/*/*/data.yaml";
|
2023-03-28 16:01:07 +00:00
|
|
|
const COMPUTE_KZG_PROOF_TESTS = "../../tests/compute_kzg_proof/*/*/data.yaml";
|
2023-03-31 08:46:54 +00:00
|
|
|
const COMPUTE_BLOB_KZG_PROOF_TESTS = "../../tests/compute_blob_kzg_proof/*/*/data.yaml";
|
2023-03-28 16:01:07 +00:00
|
|
|
const VERIFY_KZG_PROOF_TESTS = "../../tests/verify_kzg_proof/*/*/data.yaml";
|
2023-03-31 08:46:54 +00:00
|
|
|
const VERIFY_BLOB_KZG_PROOF_TESTS = "../../tests/verify_blob_kzg_proof/*/*/data.yaml";
|
|
|
|
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = "../../tests/verify_blob_kzg_proof_batch/*/*/data.yaml";
|
|
|
|
|
|
|
|
type BlobToKzgCommitmentTest = TestMeta<{blob: string}, string>;
|
|
|
|
type ComputeKzgProofTest = TestMeta<{blob: string; z: string}, string[]>;
|
|
|
|
type ComputeBlobKzgProofTest = TestMeta<{blob: string; commitment: string}, string>;
|
|
|
|
type VerifyKzgProofTest = TestMeta<{commitment: string; y: string; z: string; proof: string}, boolean>;
|
|
|
|
type VerifyBlobKzgProofTest = TestMeta<{blob: string; commitment: string; proof: string}, boolean>;
|
|
|
|
type VerifyBatchKzgProofTest = TestMeta<{blobs: string[]; commitments: string[]; proofs: string[]}, boolean>;
|
|
|
|
|
|
|
|
const generateRandomBlob = (): Uint8Array => {
|
2022-12-12 22:48:05 +00:00
|
|
|
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;
|
2023-03-31 08:46:54 +00:00
|
|
|
})
|
2022-12-12 22:48:05 +00:00
|
|
|
);
|
|
|
|
};
|
2022-11-03 00:17:17 +00:00
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
const blobValidLength = randomBytes(BYTES_PER_BLOB);
|
|
|
|
const blobBadLength = randomBytes(BYTES_PER_BLOB - 1);
|
|
|
|
const commitmentValidLength = randomBytes(BYTES_PER_COMMITMENT);
|
|
|
|
const commitmentBadLength = randomBytes(BYTES_PER_COMMITMENT - 1);
|
|
|
|
const proofValidLength = randomBytes(BYTES_PER_PROOF);
|
|
|
|
const proofBadLength = randomBytes(BYTES_PER_PROOF - 1);
|
|
|
|
const fieldElementValidLength = randomBytes(BYTES_PER_FIELD_ELEMENT);
|
|
|
|
const fieldElementBadLength = randomBytes(BYTES_PER_FIELD_ELEMENT - 1);
|
|
|
|
|
2023-04-07 13:06:13 +00:00
|
|
|
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;
|
2023-03-06 10:08:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 22:13:49 +00:00
|
|
|
describe("C-KZG", () => {
|
2022-11-17 02:31:30 +00:00
|
|
|
beforeAll(async () => {
|
2023-03-09 14:00:15 +00:00
|
|
|
loadTrustedSetup(SETUP_FILE_PATH);
|
2022-11-02 22:50:04 +00:00
|
|
|
});
|
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
describe("reference tests should pass", () => {
|
|
|
|
it("reference tests for blobToKzgCommitment should pass", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const tests = globSync(BLOB_TO_KZG_COMMITMENT_TESTS);
|
2023-03-15 14:39:19 +00:00
|
|
|
expect(tests.length).toBeGreaterThan(0);
|
|
|
|
|
2023-03-06 10:08:59 +00:00
|
|
|
tests.forEach((testFile: string) => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const test: BlobToKzgCommitmentTest = yaml.load(readFileSync(testFile, "ascii"));
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-04-07 13:06:13 +00:00
|
|
|
let commitment: Uint8Array;
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = bytesFromHex(test.input.blob);
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-02-18 21:49:52 +00:00
|
|
|
try {
|
2023-03-06 10:08:59 +00:00
|
|
|
commitment = blobToKzgCommitment(blob);
|
2023-02-18 21:49:52 +00:00
|
|
|
} catch (err) {
|
2023-03-06 10:08:59 +00:00
|
|
|
expect(test.output).toBeNull();
|
|
|
|
return;
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
expect(test.output).not.toBeNull();
|
2023-03-31 08:46:54 +00:00
|
|
|
const expectedCommitment = bytesFromHex(test.output);
|
2023-04-07 13:06:13 +00:00
|
|
|
expect(bytesEqual(commitment, expectedCommitment));
|
2023-02-18 21:49:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
it("reference tests for computeKzgProof should pass", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const tests = globSync(COMPUTE_KZG_PROOF_TESTS);
|
2023-03-15 14:39:19 +00:00
|
|
|
expect(tests.length).toBeGreaterThan(0);
|
|
|
|
|
2023-03-06 10:08:59 +00:00
|
|
|
tests.forEach((testFile: string) => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const test: ComputeKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-03-09 14:40:57 +00:00
|
|
|
let proof: ProofResult;
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = bytesFromHex(test.input.blob);
|
|
|
|
const z = bytesFromHex(test.input.z);
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-02-18 21:49:52 +00:00
|
|
|
try {
|
2023-03-06 10:08:59 +00:00
|
|
|
proof = computeKzgProof(blob, z);
|
2023-02-18 21:49:52 +00:00
|
|
|
} catch (err) {
|
2023-03-06 10:08:59 +00:00
|
|
|
expect(test.output).toBeNull();
|
|
|
|
return;
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
expect(test.output).not.toBeNull();
|
2023-04-07 13:06:13 +00:00
|
|
|
|
|
|
|
const [proofBytes, yBytes] = proof;
|
|
|
|
const [expectedProofBytes, expectedYBytes] = test.output.map((out) => bytesFromHex(out));
|
|
|
|
|
|
|
|
expect(bytesEqual(proofBytes, expectedProofBytes));
|
|
|
|
expect(bytesEqual(yBytes, expectedYBytes));
|
2023-02-18 21:49:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
it("reference tests for computeBlobKzgProof should pass", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const tests = globSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
|
2023-03-15 14:39:19 +00:00
|
|
|
expect(tests.length).toBeGreaterThan(0);
|
|
|
|
|
2023-03-06 10:08:59 +00:00
|
|
|
tests.forEach((testFile: string) => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const test: ComputeBlobKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-04-07 13:06:13 +00:00
|
|
|
let proof: Uint8Array;
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = bytesFromHex(test.input.blob);
|
|
|
|
const commitment = bytesFromHex(test.input.commitment);
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-02-18 21:49:52 +00:00
|
|
|
try {
|
2023-03-09 14:40:57 +00:00
|
|
|
proof = computeBlobKzgProof(blob, commitment);
|
2023-02-18 21:49:52 +00:00
|
|
|
} catch (err) {
|
2023-03-06 10:08:59 +00:00
|
|
|
expect(test.output).toBeNull();
|
|
|
|
return;
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
expect(test.output).not.toBeNull();
|
2023-03-31 08:46:54 +00:00
|
|
|
const expectedProof = bytesFromHex(test.output);
|
2023-04-07 13:06:13 +00:00
|
|
|
expect(bytesEqual(proof, expectedProof));
|
2023-02-18 21:49:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
it("reference tests for verifyKzgProof should pass", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const tests = globSync(VERIFY_KZG_PROOF_TESTS);
|
2023-03-15 14:39:19 +00:00
|
|
|
expect(tests.length).toBeGreaterThan(0);
|
|
|
|
|
2023-03-06 10:08:59 +00:00
|
|
|
tests.forEach((testFile: string) => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const test: VerifyKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
let valid;
|
2023-03-31 08:46:54 +00:00
|
|
|
const commitment = bytesFromHex(test.input.commitment);
|
|
|
|
const z = bytesFromHex(test.input.z);
|
|
|
|
const y = bytesFromHex(test.input.y);
|
|
|
|
const proof = bytesFromHex(test.input.proof);
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-02-18 21:49:52 +00:00
|
|
|
try {
|
2023-03-06 10:08:59 +00:00
|
|
|
valid = verifyKzgProof(commitment, z, y, proof);
|
2023-02-18 21:49:52 +00:00
|
|
|
} catch (err) {
|
2023-03-06 10:08:59 +00:00
|
|
|
expect(test.output).toBeNull();
|
|
|
|
return;
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
2023-03-06 10:08:59 +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", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const tests = globSync(VERIFY_BLOB_KZG_PROOF_TESTS);
|
2023-03-15 14:39:19 +00:00
|
|
|
expect(tests.length).toBeGreaterThan(0);
|
|
|
|
|
2023-03-06 10:08:59 +00:00
|
|
|
tests.forEach((testFile: string) => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const test: VerifyBlobKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
let valid;
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = bytesFromHex(test.input.blob);
|
|
|
|
const commitment = bytesFromHex(test.input.commitment);
|
|
|
|
const proof = bytesFromHex(test.input.proof);
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-02-18 21:49:52 +00:00
|
|
|
try {
|
2023-03-06 10:08:59 +00:00
|
|
|
valid = verifyBlobKzgProof(blob, commitment, proof);
|
2023-02-18 21:49:52 +00:00
|
|
|
} catch (err) {
|
2023-03-06 10:08:59 +00:00
|
|
|
expect(test.output).toBeNull();
|
|
|
|
return;
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
2023-03-06 10:08:59 +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", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const tests = globSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
|
2023-03-15 14:39:19 +00:00
|
|
|
expect(tests.length).toBeGreaterThan(0);
|
|
|
|
|
2023-03-06 10:08:59 +00:00
|
|
|
tests.forEach((testFile: string) => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const test: VerifyBatchKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
let valid;
|
2023-03-31 08:46:54 +00:00
|
|
|
const blobs = test.input.blobs.map(bytesFromHex);
|
|
|
|
const commitments = test.input.commitments.map(bytesFromHex);
|
|
|
|
const proofs = test.input.proofs.map(bytesFromHex);
|
2023-03-06 10:08:59 +00:00
|
|
|
|
2023-02-18 21:49:52 +00:00
|
|
|
try {
|
2023-03-06 10:08:59 +00:00
|
|
|
valid = verifyBlobKzgProofBatch(blobs, commitments, proofs);
|
2023-02-18 21:49:52 +00:00
|
|
|
} catch (err) {
|
2023-03-06 10:08:59 +00:00
|
|
|
expect(test.output).toBeNull();
|
|
|
|
return;
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
2023-03-06 10:08:59 +00:00
|
|
|
|
|
|
|
expect(valid).toEqual(test.output);
|
2023-02-18 21:49:52 +00:00
|
|
|
});
|
|
|
|
});
|
2023-01-27 15:15:17 +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", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2023-02-18 21:49:52 +00:00
|
|
|
// @ts-expect-error
|
2023-03-31 08:46:54 +00:00
|
|
|
expect(() => blobToKzgCommitment("wrong type")).toThrowError("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", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
expect(() => blobToKzgCommitment(blobBadLength)).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", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = generateRandomBlob();
|
2023-02-18 21:49:52 +00:00
|
|
|
const zBytes = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(0);
|
|
|
|
computeKzgProof(blob, zBytes);
|
|
|
|
});
|
2023-03-07 01:28:30 +00:00
|
|
|
it("throws as expected when given an argument of invalid length", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
expect(() => computeKzgProof(blobBadLength, fieldElementValidLength)).toThrowError(
|
|
|
|
"Expected blob to be 131072 bytes"
|
|
|
|
);
|
|
|
|
expect(() => computeKzgProof(blobValidLength, fieldElementBadLength)).toThrowError(
|
|
|
|
"Expected zBytes to be 32 bytes"
|
|
|
|
);
|
2023-03-07 01:28:30 +00:00
|
|
|
});
|
2022-11-08 16:38:07 +00:00
|
|
|
});
|
|
|
|
|
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", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = generateRandomBlob();
|
|
|
|
const commitment = blobToKzgCommitment(blob);
|
2023-03-09 14:40:57 +00:00
|
|
|
computeBlobKzgProof(blob, commitment);
|
2023-02-18 21:49:52 +00:00
|
|
|
});
|
2023-03-07 01:28:30 +00:00
|
|
|
it("throws as expected when given an argument of invalid length", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
expect(() => computeBlobKzgProof(blobBadLength, blobToKzgCommitment(generateRandomBlob()))).toThrowError(
|
|
|
|
"Expected blob to be 131072 bytes"
|
|
|
|
);
|
2023-03-07 01:28:30 +00:00
|
|
|
});
|
2022-11-17 23:49:12 +00:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
2023-03-07 01:28:30 +00:00
|
|
|
it("throws as expected when given an argument of invalid length", () => {
|
|
|
|
expect(() =>
|
2023-03-31 08:46:54 +00:00
|
|
|
verifyKzgProof(commitmentBadLength, fieldElementValidLength, fieldElementValidLength, proofValidLength)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected commitmentBytes to be 48 bytes");
|
|
|
|
expect(() =>
|
2023-03-31 08:46:54 +00:00
|
|
|
verifyKzgProof(commitmentValidLength, fieldElementBadLength, fieldElementValidLength, proofValidLength)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected zBytes to be 32 bytes");
|
|
|
|
expect(() =>
|
2023-03-31 08:46:54 +00:00
|
|
|
verifyKzgProof(commitmentValidLength, fieldElementValidLength, fieldElementBadLength, proofValidLength)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected yBytes to be 32 bytes");
|
|
|
|
expect(() =>
|
2023-03-31 08:46:54 +00:00
|
|
|
verifyKzgProof(commitmentValidLength, fieldElementValidLength, fieldElementValidLength, proofBadLength)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected proofBytes to be 48 bytes");
|
|
|
|
});
|
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-03-31 08:46:54 +00:00
|
|
|
const blob = generateRandomBlob();
|
|
|
|
const commitment = blobToKzgCommitment(blob);
|
|
|
|
const proof = computeBlobKzgProof(blob, commitment);
|
2023-02-18 21:49:52 +00:00
|
|
|
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(true);
|
|
|
|
});
|
2022-11-08 20:53:36 +00:00
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
it("incorrect commitment should verify as false", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = generateRandomBlob();
|
|
|
|
const commitment = blobToKzgCommitment(generateRandomBlob());
|
|
|
|
const proof = computeBlobKzgProof(blob, commitment);
|
2023-02-18 21:49:52 +00:00
|
|
|
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
|
|
|
|
});
|
2022-11-16 23:26:05 +00:00
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
it("incorrect proof should verify as false", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
const blob = generateRandomBlob();
|
|
|
|
const commitment = blobToKzgCommitment(blob);
|
|
|
|
const randomBlob = generateRandomBlob();
|
|
|
|
const randomCommitment = blobToKzgCommitment(randomBlob);
|
|
|
|
const proof = computeBlobKzgProof(randomBlob, randomCommitment);
|
2023-02-18 21:49:52 +00:00
|
|
|
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
|
|
|
|
});
|
2023-03-07 01:28:30 +00:00
|
|
|
it("throws as expected when given an argument of invalid length", () => {
|
2023-03-31 08:46:54 +00:00
|
|
|
expect(() => verifyBlobKzgProof(blobBadLength, commitmentValidLength, proofValidLength)).toThrowError(
|
|
|
|
"Expected blob to be 131072 bytes"
|
|
|
|
);
|
|
|
|
expect(() => verifyBlobKzgProof(blobValidLength, commitmentBadLength, proofValidLength)).toThrowError(
|
|
|
|
"Expected commitmentBytes to be 48 bytes"
|
|
|
|
);
|
|
|
|
expect(() => verifyBlobKzgProof(blobValidLength, commitmentValidLength, proofBadLength)).toThrowError(
|
|
|
|
"Expected proofBytes to be 48 bytes"
|
|
|
|
);
|
2023-03-07 01:28:30 +00:00
|
|
|
});
|
2023-02-01 20:56:03 +00:00
|
|
|
});
|
|
|
|
|
2023-02-21 20:22:44 +00:00
|
|
|
describe("edge cases for verifyBlobKzgProofBatch", () => {
|
2023-03-07 01:28:30 +00:00
|
|
|
it("should reject non-array args", () => {
|
|
|
|
expect(() =>
|
|
|
|
verifyBlobKzgProofBatch(
|
|
|
|
2 as unknown as Uint8Array[],
|
|
|
|
[commitmentValidLength, commitmentValidLength],
|
2023-03-31 08:46:54 +00:00
|
|
|
[proofValidLength, proofValidLength]
|
|
|
|
)
|
2023-03-30 14:17:57 +00:00
|
|
|
).toThrowError("Blobs, commitments, and proofs must all be arrays");
|
2023-03-07 01:28:30 +00:00
|
|
|
});
|
2023-02-22 15:10:09 +00:00
|
|
|
it("should reject non-bytearray blob", () => {
|
|
|
|
expect(() =>
|
2023-03-07 01:28:30 +00:00
|
|
|
verifyBlobKzgProofBatch(
|
|
|
|
["foo", "bar"] as unknown as Uint8Array[],
|
|
|
|
[commitmentValidLength, commitmentValidLength],
|
2023-03-31 08:46:54 +00:00
|
|
|
[proofValidLength, proofValidLength]
|
|
|
|
)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected blob to be a Uint8Array");
|
|
|
|
});
|
|
|
|
it("throws as expected when given an argument of invalid length", () => {
|
|
|
|
expect(() =>
|
|
|
|
verifyBlobKzgProofBatch(
|
|
|
|
[blobBadLength, blobValidLength],
|
|
|
|
[commitmentValidLength, commitmentValidLength],
|
2023-03-31 08:46:54 +00:00
|
|
|
[proofValidLength, proofValidLength]
|
|
|
|
)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected blob to be 131072 bytes");
|
|
|
|
expect(() =>
|
|
|
|
verifyBlobKzgProofBatch(
|
|
|
|
[blobValidLength, blobValidLength],
|
|
|
|
[commitmentBadLength, commitmentValidLength],
|
2023-03-31 08:46:54 +00:00
|
|
|
[proofValidLength, proofValidLength]
|
|
|
|
)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected commitmentBytes to be 48 bytes");
|
|
|
|
expect(() =>
|
|
|
|
verifyBlobKzgProofBatch(
|
|
|
|
[blobValidLength, blobValidLength],
|
|
|
|
[commitmentValidLength, commitmentValidLength],
|
2023-03-31 08:46:54 +00:00
|
|
|
[proofValidLength, proofBadLength]
|
|
|
|
)
|
2023-03-07 01:28:30 +00:00
|
|
|
).toThrowError("Expected proofBytes to be 48 bytes");
|
2023-02-22 15:10:09 +00:00
|
|
|
});
|
|
|
|
|
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);
|
2022-11-16 23:26:05 +00:00
|
|
|
});
|
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-03-31 08:46:54 +00:00
|
|
|
const count = 3;
|
|
|
|
const blobs = new Array(count);
|
|
|
|
const commitments = new Array(count);
|
|
|
|
const proofs = new Array(count);
|
2023-02-18 21:49:52 +00:00
|
|
|
|
2023-03-31 08:46:54 +00:00
|
|
|
for (const [i] of blobs.entries()) {
|
2023-02-18 21:49:52 +00:00
|
|
|
blobs[i] = generateRandomBlob();
|
|
|
|
commitments[i] = blobToKzgCommitment(blobs[i]);
|
2023-03-09 14:40:57 +00:00
|
|
|
proofs[i] = computeBlobKzgProof(blobs[i], commitments[i]);
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(verifyBlobKzgProofBatch(blobs, commitments, proofs)).toBe(true);
|
2023-03-31 08:46:54 +00:00
|
|
|
expect(() => 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-16 23:26:05 +00:00
|
|
|
});
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|