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-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-03 22:13:49 +00:00
|
|
|
} from "./kzg";
|
2022-11-02 20:45:29 +00:00
|
|
|
|
2022-11-08 16:38:07 +00:00
|
|
|
const setupFileName = "trusted_setup.txt";
|
|
|
|
|
|
|
|
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-11-03 23:20:33 +00:00
|
|
|
const generateRandomBlob = () => new Uint8Array(randomBytes(BLOB_BYTE_COUNT));
|
2022-11-03 00:17:17 +00:00
|
|
|
|
2022-11-03 22:13:49 +00:00
|
|
|
describe("C-KZG", () => {
|
2022-11-03 23:20:33 +00:00
|
|
|
beforeAll(() => {
|
2022-11-03 21:39:02 +00:00
|
|
|
loadTrustedSetup(SETUP_FILE_PATH);
|
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
|
|
|
});
|
|
|
|
|
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-08 16:38:07 +00:00
|
|
|
it("computes the aggregate proof when blobs is an empty array", () => {
|
|
|
|
const proof = computeAggregateKzgProof([]);
|
|
|
|
|
|
|
|
// Is this actually what the aggregate proof for an empty array should be?
|
|
|
|
expect(proof.toString()).toEqual(
|
|
|
|
[
|
|
|
|
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-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),
|
2022-11-04 02:54:45 +00:00
|
|
|
).toThrowError("Invalid commitment data");
|
2022-11-04 00:08:36 +00:00
|
|
|
});
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|