2022-11-03 22:13:49 +00:00
|
|
|
import { randomBytes } from "crypto";
|
2022-11-08 16:38:07 +00:00
|
|
|
import { existsSync } from "fs";
|
|
|
|
|
2022-11-02 22:50:04 +00:00
|
|
|
import {
|
|
|
|
loadTrustedSetup,
|
|
|
|
freeTrustedSetup,
|
2022-11-03 00:17:17 +00:00
|
|
|
blobToKzgCommitment,
|
2022-12-13 19:21:43 +00:00
|
|
|
verifyKzgProof,
|
2023-01-27 15:15:17 +00:00
|
|
|
computeKzgProof,
|
2022-11-03 19:57:46 +00:00
|
|
|
computeAggregateKzgProof,
|
2022-11-03 21:39:02 +00:00
|
|
|
verifyAggregateKzgProof,
|
2022-11-04 18:44:57 +00:00
|
|
|
BYTES_PER_FIELD_ELEMENT,
|
2022-11-03 23:20:33 +00:00
|
|
|
FIELD_ELEMENTS_PER_BLOB,
|
2022-11-17 02:31:30 +00:00
|
|
|
transformTrustedSetupJSON,
|
2022-11-03 22:13:49 +00:00
|
|
|
} from "./kzg";
|
2022-11-02 20:45:29 +00:00
|
|
|
|
2022-11-17 02:31:30 +00:00
|
|
|
const setupFileName = "testing_trusted_setups.json";
|
2022-11-08 16:38:07 +00:00
|
|
|
|
|
|
|
const SETUP_FILE_PATH = existsSync(setupFileName)
|
|
|
|
? setupFileName
|
|
|
|
: `../../src/${setupFileName}`;
|
|
|
|
|
2022-11-04 18:44:57 +00:00
|
|
|
const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-12-12 22:48:05 +00:00
|
|
|
const MAX_TOP_BYTE = 114;
|
|
|
|
|
|
|
|
const generateRandomBlob = () => {
|
|
|
|
return new Uint8Array(
|
|
|
|
randomBytes(BLOB_BYTE_COUNT).map((x, i) => {
|
|
|
|
// 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", () => {
|
2022-11-17 02:31:30 +00:00
|
|
|
beforeAll(async () => {
|
|
|
|
const file = await transformTrustedSetupJSON(SETUP_FILE_PATH);
|
|
|
|
loadTrustedSetup(file);
|
2022-11-02 22:50:04 +00:00
|
|
|
});
|
|
|
|
|
2022-11-03 23:20:33 +00:00
|
|
|
afterAll(() => {
|
2022-11-03 21:39:02 +00:00
|
|
|
freeTrustedSetup();
|
2022-11-02 22:50:04 +00:00
|
|
|
});
|
|
|
|
|
2023-01-27 15:15:17 +00:00
|
|
|
it("computes a proof from blob", () => {
|
|
|
|
let blob = generateRandomBlob();
|
|
|
|
const zBytes = new Uint8Array(32).fill(0);
|
|
|
|
computeKzgProof(blob, zBytes);
|
|
|
|
// No check, just make sure it doesn't crash.
|
|
|
|
});
|
|
|
|
|
2022-11-08 16:38:07 +00:00
|
|
|
it("computes the correct commitments and aggregate proof from blobs", () => {
|
|
|
|
let blobs = new Array(2).fill(0).map(generateRandomBlob);
|
|
|
|
let commitments = blobs.map(blobToKzgCommitment);
|
|
|
|
let proof = computeAggregateKzgProof(blobs);
|
2022-11-03 23:20:33 +00:00
|
|
|
expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true);
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|
2022-11-04 00:08:36 +00:00
|
|
|
|
2022-11-09 20:14:44 +00:00
|
|
|
it("returns the identity (aka zero, aka neutral) element when blobs is an empty array", () => {
|
2022-11-17 23:49:12 +00:00
|
|
|
const aggregateProofOfNothing = computeAggregateKzgProof([]);
|
|
|
|
expect(aggregateProofOfNothing.toString()).toEqual(
|
2022-11-09 18:25:48 +00:00
|
|
|
[
|
|
|
|
192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0,
|
|
|
|
].toString(),
|
2022-11-08 16:38:07 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 22:48:05 +00:00
|
|
|
it("verifies the aggregate proof of empty blobs and commitments", () => {
|
2022-11-17 23:49:12 +00:00
|
|
|
expect(verifyAggregateKzgProof([], [], computeAggregateKzgProof([]))).toBe(
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-13 19:24:55 +00:00
|
|
|
it("verifies a valid KZG proof", () => {
|
2022-12-13 19:21:43 +00:00
|
|
|
const commitment = new Uint8Array(48).fill(0);
|
|
|
|
commitment[0] = 0xc0;
|
|
|
|
const z = new Uint8Array(32).fill(0);
|
|
|
|
const y = new Uint8Array(32).fill(0);
|
|
|
|
const proof = new Uint8Array(48).fill(0);
|
|
|
|
proof[0] = 0xc0;
|
|
|
|
|
2022-12-13 19:24:55 +00:00
|
|
|
expect(verifyKzgProof(commitment, z, y, proof)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("verifies an invalid valid KZG proof", () => {
|
|
|
|
const commitment = new Uint8Array(48).fill(0);
|
|
|
|
commitment[0] = 0xc0;
|
|
|
|
const z = new Uint8Array(32).fill(1);
|
|
|
|
const y = new Uint8Array(32).fill(1);
|
|
|
|
const proof = new Uint8Array(48).fill(0);
|
|
|
|
proof[0] = 0xc0;
|
|
|
|
|
|
|
|
expect(verifyKzgProof(commitment, z, y, proof)).toBe(false);
|
2022-12-13 19:21:43 +00:00
|
|
|
});
|
|
|
|
|
2022-11-08 20:53:36 +00:00
|
|
|
it("computes the aggregate proof when for a single blob", () => {
|
|
|
|
let blobs = new Array(1).fill(0).map(generateRandomBlob);
|
|
|
|
let commitments = blobs.map(blobToKzgCommitment);
|
|
|
|
let proof = computeAggregateKzgProof(blobs);
|
|
|
|
expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-11-04 00:11:25 +00:00
|
|
|
it("fails when given incorrect commitments", () => {
|
2022-11-04 00:08:36 +00:00
|
|
|
const blobs = new Array(2).fill(0).map(generateRandomBlob);
|
|
|
|
const commitments = blobs.map(blobToKzgCommitment);
|
2022-11-04 02:54:45 +00:00
|
|
|
commitments[0][0] = commitments[0][0] === 0 ? 1 : 0; // Mutate the commitment
|
2022-11-04 00:08:36 +00:00
|
|
|
const proof = computeAggregateKzgProof(blobs);
|
2022-11-04 02:54:45 +00:00
|
|
|
expect(() =>
|
2022-11-04 05:30:54 +00:00
|
|
|
verifyAggregateKzgProof(blobs, commitments, proof),
|
2023-01-16 20:05:23 +00:00
|
|
|
).toThrowError("verify_aggregate_kzg_proof failed with error code: 1");
|
2022-11-04 00:08:36 +00:00
|
|
|
});
|
2022-11-16 23:26:05 +00:00
|
|
|
|
2023-02-01 20:56:03 +00:00
|
|
|
it("throws the expected error when given fewer commitments than blobs", () => {
|
|
|
|
let blobs = new Array(1).fill(0).map(generateRandomBlob);
|
|
|
|
let commitments = [] as Uint8Array[];
|
|
|
|
let proof = computeAggregateKzgProof(blobs);
|
|
|
|
expect(() =>
|
|
|
|
verifyAggregateKzgProof(blobs, commitments, proof),
|
|
|
|
).toThrowError(
|
|
|
|
"verifyAggregateKzgProof requires blobs count to match expectedKzgCommitments count",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-16 23:26:05 +00:00
|
|
|
describe("computing commitment from blobs", () => {
|
|
|
|
it("throws as expected when given an argument of invalid type", () => {
|
|
|
|
// @ts-expect-error
|
|
|
|
expect(() => blobToKzgCommitment("wrong type")).toThrowError(
|
|
|
|
"Invalid argument type: blob. Expected UInt8Array",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|