Justin Traglia 6d21a0ea98
Speed up loading trusted setup (#299)
* Replace g1 points with g1 lagrange points

* Swap out validate_kzg_g1 with blst funcs

* Update minimal preset too

* Fix java & nodejs bindings

* Put variables in smaller scope

* Update some comments

* Remove FFTSettings (#2)

* Fix issue when expanding roots of unity

* Fix formatting

* Revert back to using intermediate array

* Add missing c_kzg_free

* Fix some nits

* Replace free_kzg_settings with free_trusted_setup

* Add parens to NUM_ELEMENTS

Co-authored-by: George Kadianakis <desnacked@riseup.net>

* Move memcpy to the end

* Revert "Move memcpy to the end"

This reverts commit 5331c7feadc92e4b5dd5d4e7512e4be563d7f386.

* Add comment about free_trusted_setup

* Move check before alloc

* Add remark

* Delete unnecessary blank line

* Fix asn's nits

* Update comment for roots_of_unity in header

Co-authored-by: Gottfried Herold <GottfriedHerold@users.noreply.github.com>

* Fix formatting

* Update comment about max_scale

Co-authored-by: Gottfried Herold <GottfriedHerold@users.noreply.github.com>

---------

Co-authored-by: Suphanat Chunhapanya <haxx.pop@gmail.com>
Co-authored-by: George Kadianakis <desnacked@riseup.net>
Co-authored-by: Gottfried Herold <GottfriedHerold@users.noreply.github.com>
2023-05-12 16:44:37 +03:00

50 lines
1.8 KiB
JavaScript

/**
* 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 fs = require("fs");
const path = require("path");
const bindings = require("bindings")("kzg");
/**
* 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) {
const trustedSetup = JSON.parse(fs.readFileSync(filePath, "utf8"));
const setupText =
bindings.FIELD_ELEMENTS_PER_BLOB +
"\n65\n" +
trustedSetup.setup_G1_lagrange.map((p) => p.substring(2)).join("\n") +
"\n" +
trustedSetup.setup_G2.map((p) => p.substring(2)).join("\n");
const outputPath = filePath.replace(".json", ".txt");
fs.writeFileSync(outputPath, setupText);
return outputPath;
}
const originalLoadTrustedSetup = bindings.loadTrustedSetup;
// docstring in ./kzg.d.ts with exported definition
bindings.loadTrustedSetup = function loadTrustedSetup(filePath) {
if (!(filePath && typeof filePath === "string")) {
throw new TypeError("must initialize kzg with the filePath to a txt/json trusted setup");
}
if (!fs.existsSync(filePath)) {
throw new Error(`no trusted setup found: ${filePath}`);
}
if (path.parse(filePath).ext === ".json") {
filePath = transformTrustedSetupJson(filePath);
}
originalLoadTrustedSetup(filePath);
};
module.exports = exports = bindings;