2022-11-03 23:20:33 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2022-11-04 05:57:03 +00:00
|
|
|
const kzg: KZG = require("./kzg.node");
|
2022-11-17 02:31:30 +00:00
|
|
|
const fs = require("fs");
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-11-03 23:20:33 +00:00
|
|
|
export type BLSFieldElement = Uint8Array; // 32 bytes
|
|
|
|
export type KZGProof = Uint8Array; // 48 bytes
|
|
|
|
export type KZGCommitment = Uint8Array; // 48 bytes
|
|
|
|
export type Blob = Uint8Array; // 4096 * 32 bytes
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 23:20:33 +00:00
|
|
|
type SetupHandle = Object;
|
2022-11-02 22:50:04 +00:00
|
|
|
|
2022-11-03 23:20:33 +00:00
|
|
|
// The C++ native addon interface
|
2022-11-02 22:50:04 +00:00
|
|
|
type KZG = {
|
2022-11-03 23:20:33 +00:00
|
|
|
FIELD_ELEMENTS_PER_BLOB: number;
|
2022-11-04 18:44:57 +00:00
|
|
|
BYTES_PER_FIELD_ELEMENT: number;
|
2022-11-03 23:20:33 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
loadTrustedSetup: (filePath: string) => SetupHandle;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-11-02 22:50:04 +00:00
|
|
|
freeTrustedSetup: (setupHandle: SetupHandle) => void;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-11-03 00:17:17 +00:00
|
|
|
blobToKzgCommitment: (blob: Blob, setupHandle: SetupHandle) => KZGCommitment;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-11-02 22:50:04 +00:00
|
|
|
computeAggregateKzgProof: (
|
2022-11-03 21:39:02 +00:00
|
|
|
blobs: Blob[],
|
2022-11-04 05:30:54 +00:00
|
|
|
setupHandle: SetupHandle,
|
2022-11-02 22:50:04 +00:00
|
|
|
) => KZGProof;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
verifyAggregateKzgProof: (
|
|
|
|
blobs: Blob[],
|
|
|
|
expectedKzgCommitments: KZGCommitment[],
|
|
|
|
kzgAggregatedProof: KZGProof,
|
2022-11-04 05:30:54 +00:00
|
|
|
setupHandle: SetupHandle,
|
2022-11-03 21:39:02 +00:00
|
|
|
) => boolean;
|
|
|
|
|
2022-11-04 02:54:45 +00:00
|
|
|
// Currently unused -- not exported
|
2022-11-02 22:50:04 +00:00
|
|
|
verifyKzgProof: (
|
2022-11-03 21:39:02 +00:00
|
|
|
polynomialKzg: KZGCommitment,
|
|
|
|
z: BLSFieldElement,
|
|
|
|
y: BLSFieldElement,
|
|
|
|
kzgProof: KZGProof,
|
2022-11-04 05:30:54 +00:00
|
|
|
setupHandle: SetupHandle,
|
2022-11-03 21:39:02 +00:00
|
|
|
) => boolean;
|
2022-11-02 22:50:04 +00:00
|
|
|
};
|
|
|
|
|
2022-11-17 02:31:30 +00:00
|
|
|
type TrustedSetupJSON = {
|
|
|
|
setup_G1: string[];
|
|
|
|
setup_G2: string[];
|
|
|
|
setup_G1_lagrange: string[];
|
|
|
|
roots_of_unity: string[];
|
|
|
|
};
|
|
|
|
|
2022-11-03 23:20:33 +00:00
|
|
|
export const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB;
|
2022-11-04 18:44:57 +00:00
|
|
|
export const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT;
|
2022-11-03 23:20:33 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
// Stored as internal state
|
|
|
|
let setupHandle: SetupHandle | undefined;
|
|
|
|
|
2022-11-04 20:29:52 +00:00
|
|
|
function requireSetupHandle(): SetupHandle {
|
|
|
|
if (!setupHandle) {
|
|
|
|
throw new Error("You must call loadTrustedSetup to initialize KZG.");
|
|
|
|
}
|
|
|
|
return setupHandle;
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:31:30 +00:00
|
|
|
export async function transformTrustedSetupJSON(
|
|
|
|
filePath: string,
|
|
|
|
): Promise<string> {
|
|
|
|
const data: TrustedSetupJSON = JSON.parse(fs.readFileSync(filePath));
|
|
|
|
|
|
|
|
const textFilePath = filePath.replace(".json", "") + ".txt";
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(textFilePath);
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
const file = fs.createWriteStream(textFilePath);
|
|
|
|
file.write(`${FIELD_ELEMENTS_PER_BLOB}\n65\n`);
|
|
|
|
file.write(data.setup_G1.map((p) => p.replace("0x", "")).join("\n"));
|
|
|
|
file.write(data.setup_G2.map((p) => p.replace("0x", "")).join("\n"));
|
|
|
|
file.end();
|
|
|
|
|
|
|
|
const p = new Promise((resolve) => {
|
|
|
|
file.close(resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
await p;
|
|
|
|
return textFilePath;
|
|
|
|
}
|
|
|
|
|
2022-11-04 04:54:59 +00:00
|
|
|
export function loadTrustedSetup(filePath: string): void {
|
2022-11-03 21:39:02 +00:00
|
|
|
if (setupHandle) {
|
|
|
|
throw new Error(
|
2022-11-04 05:30:54 +00:00
|
|
|
"Call freeTrustedSetup before loading a new trusted setup.",
|
2022-11-03 21:39:02 +00:00
|
|
|
);
|
|
|
|
}
|
2022-11-17 02:31:30 +00:00
|
|
|
|
2022-11-03 21:39:02 +00:00
|
|
|
setupHandle = kzg.loadTrustedSetup(filePath);
|
|
|
|
}
|
|
|
|
|
2022-11-04 04:54:59 +00:00
|
|
|
export function freeTrustedSetup(): void {
|
2022-11-04 20:29:52 +00:00
|
|
|
kzg.freeTrustedSetup(requireSetupHandle());
|
2022-11-03 21:39:02 +00:00
|
|
|
setupHandle = undefined;
|
|
|
|
}
|
|
|
|
|
2022-11-04 04:54:59 +00:00
|
|
|
export function blobToKzgCommitment(blob: Blob): KZGCommitment {
|
2022-11-04 20:29:52 +00:00
|
|
|
return kzg.blobToKzgCommitment(blob, requireSetupHandle());
|
2022-11-03 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 04:54:59 +00:00
|
|
|
export function computeAggregateKzgProof(blobs: Blob[]): KZGProof {
|
2022-11-04 20:29:52 +00:00
|
|
|
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
|
2022-11-03 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function verifyAggregateKzgProof(
|
|
|
|
blobs: Blob[],
|
|
|
|
expectedKzgCommitments: KZGCommitment[],
|
2022-11-04 05:30:54 +00:00
|
|
|
kzgAggregatedProof: KZGProof,
|
2022-11-04 04:54:59 +00:00
|
|
|
): boolean {
|
2022-11-03 21:39:02 +00:00
|
|
|
return kzg.verifyAggregateKzgProof(
|
|
|
|
blobs,
|
|
|
|
expectedKzgCommitments,
|
|
|
|
kzgAggregatedProof,
|
2022-11-04 20:29:52 +00:00
|
|
|
requireSetupHandle(),
|
2022-11-03 21:39:02 +00:00
|
|
|
);
|
|
|
|
}
|