2022-11-03 22:30:54 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The public interface of this module exposes the functions as specified by
|
|
|
|
* https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md#kzg
|
|
|
|
*/
|
|
|
|
const kzg = require("./kzg.node");
|
|
|
|
const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB;
|
2022-11-04 12:06:06 -07:00
|
|
|
const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT;
|
2022-11-03 22:30:54 -07:00
|
|
|
// Stored as internal state
|
|
|
|
let setupHandle;
|
2022-11-04 13:29:52 -07:00
|
|
|
function requireSetupHandle() {
|
|
|
|
if (!setupHandle) {
|
|
|
|
throw new Error("You must call loadTrustedSetup to initialize KZG.");
|
|
|
|
}
|
|
|
|
return setupHandle;
|
|
|
|
}
|
2022-11-03 22:30:54 -07:00
|
|
|
function loadTrustedSetup(filePath) {
|
|
|
|
if (setupHandle) {
|
|
|
|
throw new Error("Call freeTrustedSetup before loading a new trusted setup.");
|
|
|
|
}
|
|
|
|
setupHandle = kzg.loadTrustedSetup(filePath);
|
|
|
|
}
|
|
|
|
function freeTrustedSetup() {
|
2022-11-04 13:29:52 -07:00
|
|
|
kzg.freeTrustedSetup(requireSetupHandle());
|
2022-11-03 22:30:54 -07:00
|
|
|
setupHandle = undefined;
|
|
|
|
}
|
|
|
|
function blobToKzgCommitment(blob) {
|
2022-11-04 13:29:52 -07:00
|
|
|
return kzg.blobToKzgCommitment(blob, requireSetupHandle());
|
2022-11-03 22:30:54 -07:00
|
|
|
}
|
|
|
|
function computeAggregateKzgProof(blobs) {
|
2022-11-04 13:29:52 -07:00
|
|
|
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
|
2022-11-03 22:30:54 -07:00
|
|
|
}
|
|
|
|
function verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof) {
|
2022-11-04 13:29:52 -07:00
|
|
|
return kzg.verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof, requireSetupHandle());
|
2022-11-03 22:30:54 -07:00
|
|
|
}
|
|
|
|
|
2022-11-04 12:06:06 -07:00
|
|
|
exports.BYTES_PER_FIELD_ELEMENT = BYTES_PER_FIELD_ELEMENT;
|
2022-11-03 22:30:54 -07:00
|
|
|
exports.FIELD_ELEMENTS_PER_BLOB = FIELD_ELEMENTS_PER_BLOB;
|
|
|
|
exports.blobToKzgCommitment = blobToKzgCommitment;
|
|
|
|
exports.computeAggregateKzgProof = computeAggregateKzgProof;
|
|
|
|
exports.freeTrustedSetup = freeTrustedSetup;
|
|
|
|
exports.loadTrustedSetup = loadTrustedSetup;
|
|
|
|
exports.verifyAggregateKzgProof = verifyAggregateKzgProof;
|