2022-11-03 22:13:49 +00:00
|
|
|
import { randomBytes } from "crypto";
|
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-03 23:20:33 +00:00
|
|
|
BYTES_PER_FIELD,
|
|
|
|
FIELD_ELEMENTS_PER_BLOB,
|
|
|
|
verifyKzgProof,
|
2022-11-03 22:13:49 +00:00
|
|
|
} from "./kzg";
|
2022-11-02 20:45:29 +00:00
|
|
|
|
2022-11-03 22:13:49 +00:00
|
|
|
const SETUP_FILE_PATH = "../../src/trusted_setup.txt";
|
2022-11-03 23:20:33 +00:00
|
|
|
const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD;
|
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-03 23:27:56 +00:00
|
|
|
it("verifies a proof at a given commitment point", () => {
|
2022-11-03 23:20:33 +00:00
|
|
|
const blob = generateRandomBlob();
|
2022-11-03 23:54:52 +00:00
|
|
|
const polynomialCommitment = blobToKzgCommitment(blob);
|
2022-11-03 23:20:33 +00:00
|
|
|
const proof = computeAggregateKzgProof([blob]);
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 23:54:52 +00:00
|
|
|
// TODO: How do I test this? How do I get values of z and y?
|
2022-11-03 23:20:33 +00:00
|
|
|
const z = Uint8Array.from(new Array(32).fill(0));
|
2022-11-03 23:54:52 +00:00
|
|
|
const y = Uint8Array.from(blob.slice(0, 32));
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 23:54:52 +00:00
|
|
|
expect(verifyKzgProof(polynomialCommitment, z, y, proof)).toBe(true);
|
2022-11-03 23:20:33 +00:00
|
|
|
});
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 23:27:56 +00:00
|
|
|
it("computes the correct commitments and aggregate proofs from blobs", () => {
|
2022-11-03 23:20:33 +00:00
|
|
|
const blobs = new Array(2).fill(0).map(generateRandomBlob);
|
|
|
|
const commitments = blobs.map(blobToKzgCommitment);
|
|
|
|
const proof = computeAggregateKzgProof(blobs);
|
|
|
|
expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true);
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|
2022-11-04 00:08:36 +00:00
|
|
|
|
|
|
|
it.only("fails when given incorrect commitments", () => {
|
|
|
|
const blobs = new Array(2).fill(0).map(generateRandomBlob);
|
|
|
|
const commitments = blobs.map(blobToKzgCommitment);
|
|
|
|
commitments[0][0] += 1;
|
|
|
|
const proof = computeAggregateKzgProof(blobs);
|
|
|
|
expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(false);
|
|
|
|
});
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|