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");
|
2023-03-09 14:00:15 +00:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2023-01-24 18:23:42 +00:00
|
|
|
export type Bytes32 = Uint8Array; // 32 bytes
|
2023-01-26 14:53:30 +00:00
|
|
|
export type Bytes48 = Uint8Array; // 48 bytes
|
2023-03-07 01:28:30 +00:00
|
|
|
export type KZGProof = Buffer; // 48 bytes
|
|
|
|
export type KZGCommitment = Buffer; // 48 bytes
|
2022-11-03 23:20:33 +00:00
|
|
|
export type Blob = Uint8Array; // 4096 * 32 bytes
|
2023-03-09 14:40:57 +00:00
|
|
|
export type ProofResult = [KZGProof, Bytes32];
|
|
|
|
|
2023-03-09 14:00:15 +00:00
|
|
|
export interface TrustedSetupJson {
|
|
|
|
setup_G1: string[];
|
|
|
|
setup_G2: string[];
|
|
|
|
setup_G1_lagrange: string[];
|
|
|
|
roots_of_unity: string[];
|
|
|
|
}
|
2023-03-09 14:40:57 +00:00
|
|
|
|
2022-11-03 23:20:33 +00:00
|
|
|
// The C++ native addon interface
|
2023-03-09 14:00:15 +00:00
|
|
|
interface KZG {
|
2023-02-14 21:11:10 +00:00
|
|
|
BYTES_PER_BLOB: number;
|
|
|
|
BYTES_PER_COMMITMENT: number;
|
2022-11-04 18:44:57 +00:00
|
|
|
BYTES_PER_FIELD_ELEMENT: number;
|
2023-02-14 21:11:10 +00:00
|
|
|
BYTES_PER_PROOF: number;
|
|
|
|
FIELD_ELEMENTS_PER_BLOB: number;
|
2022-11-03 23:20:33 +00:00
|
|
|
|
2023-03-09 14:00:15 +00:00
|
|
|
loadTrustedSetup: (filePath: string) => void;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2023-03-09 14:00:15 +00:00
|
|
|
blobToKzgCommitment: (blob: Blob) => KZGCommitment;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2023-03-09 14:40:57 +00:00
|
|
|
computeKzgProof: (blob: Blob, zBytes: Bytes32) => ProofResult;
|
2022-11-03 19:57:46 +00:00
|
|
|
|
2023-03-09 14:40:57 +00:00
|
|
|
computeBlobKzgProof: (blob: Blob, commitmentBytes: Bytes48) => KZGProof;
|
2022-11-03 21:39:02 +00:00
|
|
|
|
2022-11-02 22:50:04 +00:00
|
|
|
verifyKzgProof: (
|
2023-01-26 14:53:30 +00:00
|
|
|
commitmentBytes: Bytes48,
|
|
|
|
zBytes: Bytes32,
|
|
|
|
yBytes: Bytes32,
|
|
|
|
proofBytes: Bytes48,
|
2022-11-03 21:39:02 +00:00
|
|
|
) => boolean;
|
2023-02-18 21:49:52 +00:00
|
|
|
|
|
|
|
verifyBlobKzgProof: (
|
|
|
|
blob: Blob,
|
|
|
|
commitmentBytes: Bytes48,
|
|
|
|
proofBytes: Bytes48,
|
|
|
|
) => boolean;
|
|
|
|
|
|
|
|
verifyBlobKzgProofBatch: (
|
|
|
|
blobs: Blob[],
|
|
|
|
commitmentsBytes: Bytes48[],
|
|
|
|
proofsBytes: Bytes48[],
|
|
|
|
) => boolean;
|
2023-03-09 14:00:15 +00:00
|
|
|
}
|
2022-11-17 02:31:30 +00:00
|
|
|
|
2023-02-14 21:11:10 +00:00
|
|
|
export const BYTES_PER_BLOB = kzg.BYTES_PER_BLOB;
|
|
|
|
export const BYTES_PER_COMMITMENT = kzg.BYTES_PER_COMMITMENT;
|
2022-11-04 18:44:57 +00:00
|
|
|
export const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT;
|
2023-02-14 21:11:10 +00:00
|
|
|
export const BYTES_PER_PROOF = kzg.BYTES_PER_PROOF;
|
|
|
|
export const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB;
|
2022-11-03 23:20:33 +00:00
|
|
|
|
2023-03-09 14:00:15 +00:00
|
|
|
/**
|
|
|
|
* Converts JSON formatted trusted setup into the native format that
|
|
|
|
* the native library requires. Returns the absolute file path to the
|
|
|
|
* the formatted file. The path will be the same as the origin
|
|
|
|
* file but with a ".txt" extension.
|
|
|
|
*
|
|
|
|
* @param {string} filePath - The absolute path of JSON formatted trusted setup
|
|
|
|
*
|
|
|
|
* @return {string} - The absolute path of the re-formatted trusted setup
|
|
|
|
*
|
|
|
|
* @throws {Error} - For invalid file operations
|
|
|
|
*/
|
|
|
|
function transformTrustedSetupJson(filePath: string): string {
|
|
|
|
const data: TrustedSetupJson = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
|
|
const textFilePath = filePath.replace(".json", ".txt");
|
|
|
|
const setupText =
|
|
|
|
kzg.FIELD_ELEMENTS_PER_BLOB +
|
|
|
|
"\n65\n" +
|
|
|
|
data.setup_G1.map((p) => p.substring(2)).join("\n") +
|
|
|
|
"\n" +
|
|
|
|
data.setup_G2.map((p) => p.substring(2)).join("\n");
|
|
|
|
fs.writeFileSync(textFilePath, setupText);
|
2022-11-17 02:31:30 +00:00
|
|
|
return textFilePath;
|
|
|
|
}
|
|
|
|
|
2023-03-09 14:00:15 +00:00
|
|
|
/**
|
|
|
|
* Sets up the c-kzg library. Pass in a properly formatted trusted setup file
|
|
|
|
* to configure the library. File must be in json format, see or {@link TrustedSetupJson}
|
|
|
|
* interface for more details, or as a properly formatted utf-8 encoded file.
|
|
|
|
*
|
|
|
|
* @remark This function must be run before any other functions in this
|
|
|
|
* library can be run.
|
|
|
|
*
|
|
|
|
* @param {string} filePath - The absolute path of the trusted setup
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*
|
|
|
|
* @throws {Error} - For invalid file operations
|
|
|
|
*/
|
2022-11-04 04:54:59 +00:00
|
|
|
export function loadTrustedSetup(filePath: string): void {
|
2023-03-09 14:00:15 +00:00
|
|
|
if (!(filePath && typeof filePath === "string")) {
|
|
|
|
throw new TypeError(
|
|
|
|
"must initialize kzg with the filePath to a txt/json trusted setup",
|
2022-11-03 21:39:02 +00:00
|
|
|
);
|
|
|
|
}
|
2023-03-09 14:00:15 +00:00
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
throw new Error(`no trusted setup found: ${filePath}`);
|
|
|
|
}
|
|
|
|
if (path.parse(filePath).ext === ".json") {
|
|
|
|
filePath = transformTrustedSetupJson(filePath);
|
|
|
|
}
|
|
|
|
return kzg.loadTrustedSetup(filePath);
|
2022-11-03 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
/**
|
|
|
|
* Convert a blob to a KZG commitment.
|
|
|
|
*
|
|
|
|
* @param {Blob} blob - The blob representing the polynomial to be committed to
|
|
|
|
*
|
|
|
|
* @return {KZGCommitment} - The resulting commitment
|
|
|
|
*
|
|
|
|
* @throws {TypeError} - For invalid arguments or failure of the native library
|
|
|
|
*/
|
2022-11-04 04:54:59 +00:00
|
|
|
export function blobToKzgCommitment(blob: Blob): KZGCommitment {
|
2023-03-09 14:00:15 +00:00
|
|
|
return kzg.blobToKzgCommitment(blob);
|
2022-11-03 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
/**
|
|
|
|
* Compute KZG proof for polynomial in Lagrange form at position z.
|
|
|
|
*
|
|
|
|
* @param {Blob} blob - The blob (polynomial) to generate a proof for
|
|
|
|
* @param {Bytes32} zBytes - The generator z-value for the evaluation points
|
|
|
|
*
|
2023-03-09 14:40:57 +00:00
|
|
|
* @return {ProofResult} - Tuple containing the resulting proof and evaluation
|
|
|
|
* of the polynomial at the evaluation point z
|
2023-03-07 01:28:30 +00:00
|
|
|
*
|
|
|
|
* @throws {TypeError} - For invalid arguments or failure of the native library
|
|
|
|
*/
|
2023-03-09 14:40:57 +00:00
|
|
|
export function computeKzgProof(blob: Blob, zBytes: Bytes32): ProofResult {
|
2023-03-09 14:00:15 +00:00
|
|
|
return kzg.computeKzgProof(blob, zBytes);
|
2023-01-27 15:15:17 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
/**
|
|
|
|
* Given a blob, return the KZG proof that is used to verify it against the
|
|
|
|
* commitment.
|
|
|
|
*
|
2023-03-09 14:40:57 +00:00
|
|
|
* @param {Blob} blob - The blob (polynomial) to generate a proof for
|
|
|
|
* @param {Bytes48} commitmentBytes - Commitment to verify
|
2023-03-07 01:28:30 +00:00
|
|
|
*
|
|
|
|
* @return {KZGProof} - The resulting proof
|
|
|
|
*
|
|
|
|
* @throws {TypeError} - For invalid arguments or failure of the native library
|
|
|
|
*/
|
2023-03-09 14:40:57 +00:00
|
|
|
export function computeBlobKzgProof(
|
|
|
|
blob: Blob,
|
|
|
|
commitmentBytes: Bytes48,
|
|
|
|
): KZGProof {
|
|
|
|
return kzg.computeBlobKzgProof(blob, commitmentBytes);
|
2022-11-03 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
/**
|
|
|
|
* Verify a KZG poof claiming that `p(z) == y`.
|
|
|
|
*
|
|
|
|
* @param {Bytes48} commitmentBytes - The serialized commitment corresponding to polynomial p(x)
|
|
|
|
* @param {Bytes32} zBytes - The serialized evaluation point
|
|
|
|
* @param {Bytes32} yBytes - The serialized claimed evaluation result
|
|
|
|
* @param {Bytes48} proofBytes - The serialized KZG proof
|
|
|
|
*
|
|
|
|
* @return {boolean} - true/false depending on proof validity
|
|
|
|
*
|
|
|
|
* @throws {TypeError} - For invalid arguments or failure of the native library
|
|
|
|
*/
|
2022-12-13 19:21:43 +00:00
|
|
|
export function verifyKzgProof(
|
2023-01-26 14:53:30 +00:00
|
|
|
commitmentBytes: Bytes48,
|
|
|
|
zBytes: Bytes32,
|
|
|
|
yBytes: Bytes32,
|
|
|
|
proofBytes: Bytes48,
|
2022-12-13 19:21:43 +00:00
|
|
|
): boolean {
|
2023-03-09 14:00:15 +00:00
|
|
|
return kzg.verifyKzgProof(commitmentBytes, zBytes, yBytes, proofBytes);
|
2022-12-13 19:21:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
/**
|
|
|
|
* Given a blob and its proof, verify that it corresponds to the provided
|
|
|
|
* commitment.
|
|
|
|
*
|
|
|
|
* @param {Blob} blob - The serialized blob to verify
|
|
|
|
* @param {Bytes48} commitmentBytes - The serialized commitment to verify
|
|
|
|
* @param {Bytes48} proofBytes - The serialized KZG proof for verification
|
|
|
|
*
|
|
|
|
* @return {boolean} - true/false depending on proof validity
|
|
|
|
*
|
|
|
|
* @throws {TypeError} - For invalid arguments or failure of the native library
|
|
|
|
*/
|
2023-02-18 21:49:52 +00:00
|
|
|
export function verifyBlobKzgProof(
|
|
|
|
blob: Blob,
|
|
|
|
commitmentBytes: Bytes48,
|
|
|
|
proofBytes: Bytes48,
|
|
|
|
): boolean {
|
2023-03-09 14:00:15 +00:00
|
|
|
return kzg.verifyBlobKzgProof(blob, commitmentBytes, proofBytes);
|
2023-02-18 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 01:28:30 +00:00
|
|
|
/**
|
|
|
|
* Given an array of blobs and their proofs, verify that they corresponds to their
|
|
|
|
* provided commitment.
|
|
|
|
*
|
|
|
|
* Note: blobs[0] relates to commitmentBytes[0] and proofBytes[0]
|
|
|
|
*
|
|
|
|
* @param {Blob} blobs - An array of serialized blobs to verify
|
|
|
|
* @param {Bytes48} commitmentBytes - An array of serialized commitments to verify
|
|
|
|
* @param {Bytes48} proofBytes - An array of serialized KZG proofs for verification
|
|
|
|
*
|
|
|
|
* @return {boolean} - true/false depending on batch validity
|
|
|
|
*
|
|
|
|
* @throws {TypeError} - For invalid arguments or failure of the native library
|
|
|
|
*/
|
2023-02-18 21:49:52 +00:00
|
|
|
export function verifyBlobKzgProofBatch(
|
2022-11-03 21:39:02 +00:00
|
|
|
blobs: Blob[],
|
2023-01-26 14:53:30 +00:00
|
|
|
commitmentsBytes: Bytes48[],
|
2023-02-18 21:49:52 +00:00
|
|
|
proofsBytes: Bytes48[],
|
2022-11-04 04:54:59 +00:00
|
|
|
): boolean {
|
2023-03-09 14:00:15 +00:00
|
|
|
return kzg.verifyBlobKzgProofBatch(blobs, commitmentsBytes, proofsBytes);
|
2022-11-03 21:39:02 +00:00
|
|
|
}
|