c-kzg-4844/bindings/node.js/kzg.ts

108 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-11-02 20:45:29 +00:00
// @ts-expect-error
2022-11-03 22:13:49 +00:00
import bindings from "bindings";
2022-11-02 20:45:29 +00:00
2022-11-03 19:57:46 +00:00
export const BLOB_SIZE = 4096;
export const NUMBER_OF_FIELDS = 32;
export type SetupHandle = Object;
2022-11-03 21:39:02 +00:00
export type BLSFieldElement = Uint8Array;
export type KZGProof = Uint8Array;
export type KZGCommitment = Uint8Array;
2022-11-03 00:17:17 +00:00
export type Blob = Uint8Array;
type KZG = {
2022-11-03 21:39:02 +00:00
loadTrustedSetup: (filePath: string) => SetupHandle;
2022-11-03 19:57:46 +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
computeAggregateKzgProof: (
2022-11-03 21:39:02 +00:00
blobs: Blob[],
2022-11-03 22:13:49 +00:00
setupHandle: SetupHandle
) => 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-03 22:13:49 +00:00
setupHandle: SetupHandle
2022-11-03 21:39:02 +00:00
) => boolean;
verifyKzgProof: (
2022-11-03 21:39:02 +00:00
polynomialKzg: KZGCommitment,
z: BLSFieldElement,
y: BLSFieldElement,
kzgProof: KZGProof,
2022-11-03 22:13:49 +00:00
setupHandle: SetupHandle
2022-11-03 21:39:02 +00:00
) => boolean;
};
2022-11-03 22:13:49 +00:00
const kzg: KZG = bindings("kzg.node");
2022-11-03 21:39:02 +00:00
// Stored as internal state
let setupHandle: SetupHandle | undefined;
export function loadTrustedSetup(filePath: string) {
if (setupHandle) {
throw new Error(
2022-11-03 22:13:49 +00:00
"Call freeTrustedSetup before loading a new trusted setup."
2022-11-03 21:39:02 +00:00
);
}
setupHandle = kzg.loadTrustedSetup(filePath);
}
export function freeTrustedSetup() {
if (!setupHandle) {
2022-11-03 22:13:49 +00:00
throw new Error("You must call loadTrustedSetup before freeTrustedSetup.");
2022-11-03 21:39:02 +00:00
}
kzg.freeTrustedSetup(setupHandle);
setupHandle = undefined;
}
export function blobToKzgCommitment(blob: Blob) {
if (!setupHandle) {
2022-11-03 22:13:49 +00:00
throw new Error("You must call loadTrustedSetup to initialize KZG.");
2022-11-03 21:39:02 +00:00
}
return kzg.blobToKzgCommitment(blob, setupHandle);
}
export function computeAggregateKzgProof(blobs: Blob[]) {
if (!setupHandle) {
2022-11-03 22:13:49 +00:00
throw new Error("You must call loadTrustedSetup to initialize KZG.");
2022-11-03 21:39:02 +00:00
}
return kzg.computeAggregateKzgProof(blobs, setupHandle);
}
/**
* Verify KZG proof that ``p(z) == y`` where ``p(z)`` is the polynomial represented by ``polynomialKzg``.
*/
export function verifyKzgProof(
polynomialKzg: KZGCommitment,
z: BLSFieldElement,
y: BLSFieldElement,
2022-11-03 22:13:49 +00:00
kzgProof: KZGProof
2022-11-03 21:39:02 +00:00
) {
if (!setupHandle) {
2022-11-03 22:13:49 +00:00
throw new Error("You must call loadTrustedSetup to initialize KZG.");
2022-11-03 21:39:02 +00:00
}
return kzg.verifyKzgProof(polynomialKzg, z, y, kzgProof, setupHandle);
}
export function verifyAggregateKzgProof(
blobs: Blob[],
expectedKzgCommitments: KZGCommitment[],
2022-11-03 22:13:49 +00:00
kzgAggregatedProof: KZGProof
2022-11-03 21:39:02 +00:00
) {
if (!setupHandle) {
2022-11-03 22:13:49 +00:00
throw new Error("You must call loadTrustedSetup to initialize KZG.");
2022-11-03 21:39:02 +00:00
}
return kzg.verifyAggregateKzgProof(
blobs,
expectedKzgCommitments,
kzgAggregatedProof,
2022-11-03 22:13:49 +00:00
setupHandle
2022-11-03 21:39:02 +00:00
);
}