2022-11-03 19:57:46 +00:00
|
|
|
import { randomBytes } from 'crypto';
|
2022-11-02 22:50:04 +00:00
|
|
|
import {
|
|
|
|
loadTrustedSetup,
|
|
|
|
freeTrustedSetup,
|
|
|
|
verifyKzgProof,
|
2022-11-03 00:17:17 +00:00
|
|
|
blobToKzgCommitment,
|
|
|
|
Blob,
|
|
|
|
BLOB_SIZE,
|
|
|
|
NUMBER_OF_FIELDS,
|
2022-11-03 19:57:46 +00:00
|
|
|
computeAggregateKzgProof,
|
2022-11-03 21:39:02 +00:00
|
|
|
verifyAggregateKzgProof,
|
2022-11-02 22:50:04 +00:00
|
|
|
} from './kzg';
|
2022-11-02 20:45:29 +00:00
|
|
|
|
2022-11-03 19:57:46 +00:00
|
|
|
const SETUP_FILE_PATH = '../../src/trusted_setup.txt';
|
|
|
|
|
2022-11-03 00:17:17 +00:00
|
|
|
function generateRandomBlob(): Blob {
|
2022-11-03 21:39:02 +00:00
|
|
|
return new Uint8Array(randomBytes(BLOB_SIZE * NUMBER_OF_FIELDS));
|
2022-11-03 00:17:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 20:45:29 +00:00
|
|
|
describe('C-KZG', () => {
|
2022-11-03 21:39:02 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
loadTrustedSetup(SETUP_FILE_PATH);
|
2022-11-02 22:50:04 +00:00
|
|
|
});
|
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
afterEach(() => {
|
|
|
|
freeTrustedSetup();
|
2022-11-02 22:50:04 +00:00
|
|
|
});
|
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
it('computes and verifies an aggregate KZG proof', async () => {
|
|
|
|
const blob1 = generateRandomBlob();
|
|
|
|
const blob2 = generateRandomBlob();
|
|
|
|
const blobs = [blob1, blob2];
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
const commitments = blobs.map(blobToKzgCommitment);
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
const proof = computeAggregateKzgProof(blobs);
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
console.log({
|
|
|
|
commitments,
|
|
|
|
proof,
|
2022-11-02 22:50:04 +00:00
|
|
|
});
|
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
const result = verifyAggregateKzgProof(blobs, commitments, proof);
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
expect(result).toBe(true);
|
2022-11-02 20:45:29 +00:00
|
|
|
});
|
|
|
|
});
|